#!/usr/bin/perl -w
use strict;
use Data::Dumper;
use English;
use Getopt::Long;
use vars qw($PROGNAME);
use lib "/usr/local/nagios/libexec";
use utils qw(%ERRORS &print_revision &support);
sub print_help ();
sub print_usage ();

#Enter here your Admin or Adminequivalent credentials
my $USER='enteryouradminuserhere'; 
my $CONTEXT='enteryouradmincontexthere';
my $PASSWORD='enter pw here';


#Normally you do not need to modify this lines.
my $CURLPATH='/usr/bin/curl';
my ($opt_c, $opt_w, $opt_h, $opt_V, $opt_H, $opt_u, $version, $author, $timestamp);
my ($opt_Q, $opt_N, $IPPQUEUE);
$timestamp="18.07.2006 - 19:51";
$version="0.0.2";
$author="Christian Mies - cmies\@nagios-wiki.de";
$PROGNAME="$0";
$opt_w = 3;
$opt_c = 9;
$opt_Q = 0; 	#enter Q-Number here

Getopt::Long::Configure('bundling');
GetOptions(
        "V"   => \$opt_V, "version"     => \$opt_V,
        "h"   => \$opt_h, "help"        => \$opt_h,
        "w=f" => \$opt_w, "warning=f" => \$opt_w,
        "c=f" => \$opt_c, "critical=f" => \$opt_c,
	"H=s" => \$opt_H, "hostname=s" => \$opt_H,
	"q=f" => \$opt_Q, "queue=f" => \$opt_Q,
	"u"   => \$opt_u, "usage" => \$opt_u,
	"n" => \$opt_N, "nw5" => \$opt_N,
);

if ($opt_h){
	print_help ();
	exit $ERRORS{'OK'};
}
if ($opt_u){
	print_usage();
	exit $ERRORS{'OK'};
}
if ($opt_V) {
#       print_revision($PROGNAME,'$Revision: 0.0.1 $ ');
	print "Version: $version by $author \n\n";
	print_revision($PROGNAME, '$Revision: Version see above $');
        exit $ERRORS{'OK'};
}

if (!$opt_H) {
        print "No Hostname specified\n\n";
	print_usage();
        exit $ERRORS{'UNKNOWN'};
}

#Check if Hostname is a NDPS Server"
open(QUEUECOUNT, "$CURLPATH -k -m 2 -s -u $USER.$CONTEXT:$PASSWORD https://$opt_H:8009/PsmStatus/JobList/$opt_Q | wc -l |") || die "Curl nicht ausfuehrbar";
	my $curlcount=<QUEUECOUNT>;
	if ($curlcount <= 100) {
		print "CRITICAL: $opt_H Is not a NDPS / IPP Server or Queue not existing \n";
		exit $ERRORS{'UNKNOWN'}; 
		}
	close(QUEUECOUNT);

#Print Queue <Nummer> auf NDPS Server auslesen
#if (!$opt_N) {
#   open(QUEUE, "$CURLPATH -k -m 2 -s -u $USER.$CONTEXT:$PASSWORD https://$opt_H:8009/PsmStatus/JobList/$opt_Q | cat -n | grep 122 | cut -f4 -d';' | cut -c1 - 2>/dev/null |") || die "Curl nicht ausfuehrbar";
#chomp($IPPQUEUE=<QUEUE>);
#   $IPPQUEUE=<QUEUE>;
#   print "**********TEST****** $IPPQUEUE ********";
#   close(QUEUE);
   #print "**********TEST****** $IPPQUEUE ********";
#   if ( $IPPQUEUE !=~/^[0-9]/ ) {
#   if ( $IPPQUEUE !=~ /^[0-9].*/ ) {
#
# if ( defined $IPPQUEUE && $IPPQUEUE !=~ /^[0-9]/ ) {   	
#	print "CRITICAL: $opt_H in not a Netware 5 Server";
#	exit $ERRORS{'UNKNOWN'};
#   }
if ($opt_N) {
#   print "nw5 is set \n";
#   exit;
#}
   open(QUEUE, "$CURLPATH -k -m 2 -s -u $USER.$CONTEXT:$PASSWORD https://$opt_H:8009/PsmStatus/JobList/$opt_Q | cat -n | grep 122 | cut -f4 -d';' | cut -c1 - 2>/dev/null |") || die "Curl nicht ausfuehrbar";
   chomp($IPPQUEUE=<QUEUE>);
   close(QUEUE);
#   print "Debug: ***",Dumper($IPPQUEUE),"***\n";
#   print "$IPPQUEUE \n";
#   exit;
   if ($IPPQUEUE eq ' ') {
       print "CRITICAL: $opt_H is not a Netware 5 Server \n";
    exit $ERRORS{'UNKNOWN'};
   }
}





if (not $opt_N) {
   open(QUEUE, "$CURLPATH -k -m 2 -s -u $USER.$CONTEXT:$PASSWORD https://$opt_H:8009/PsmStatus/JobList/$opt_Q | cat -n | grep 123 | cut -f4 -d';' | cut -c1 - 2>/dev/null |") || die "Curl nicht ausfuehrbar";
   $IPPQUEUE=<QUEUE>;
   close(QUEUE);
}

if ($IPPQUEUE < $opt_w) {		
		print "OK: On Server $opt_H the Queue $opt_Q has Queuesize: $IPPQUEUE"; 
		exit $ERRORS{'OK'};
		}

if ($IPPQUEUE > $opt_w && $IPPQUEUE < $opt_c) {
		print "WARNING: On Server $opt_H the Queue $opt_Q has Queuesize: $IPPQUEUE"; 
		exit $ERRORS{'WARNING'};
		}

if ($IPPQUEUE > $opt_w && $IPPQUEUE > $opt_c) {
		print "CRITICAL: On Server $opt_H the Queue $opt_Q has Queuesize: $IPPQUEUE"; 
		exit $ERRORS{'CRITICAL'};
		}

#####subs
sub print_usage() {
	print "Usage: $PROGNAME -H <host> [-q <queuenumber>] [-w <warn>] [-c <crit>]\n\n";
	}

sub print_help (){
	print "----------------------------------------\n";
	print "$PROGNAME \n";
	print "Version $version / Author: $author \n";
	print "Licenseing: GNU Public License \n";
	print "Created on: 05.07.2006 / Last Modified: $timestamp \n";
	print "-----------------------------------------\n";
	print "$PROGNAME give the actually status of the specified Print Queue	\n";
	print "For find out the Print Queue Number go to http://<yourNDPS/IPPServer>:8009/PsmStatus \n";
	print "click on the printer you want monitor, and click on \"Job List\" \n";
	print "The Queue Number is behind https://..Joblist/ \n\n";
	print "Please modify the values \$USER, \$CONTEXT, \$PASSWORD in the head of the script \n";
	print "for your environment.\n\n";
	print "  -H, --hostname\n";
	print "     Host name or IP Address\n";
	print "  -q, --queue\n";
        print "     Queue Number you want to Monitor (default is 0)\n";
	print "  -w, --warning\n";
        print "     Warning value for Job List Numbers (default is 3)\n";
        print "  -c, --critical\n";
        print "     Critical value for Job List Numbers (default is 9)\n";
        print "  -h, --help\n";
        print "     This help screen \n";
        print "  -V, --version\n";
        print "     Print version information\n";
        print "  -u, --usage\n";
        print "     Print short usage information\n\n";
	print_usage();
	print "\n";
	support ();
	}


