#!/usr/bin/perl -w


################################################################################
# programm: 		check_lexprn
# version:		0.1
# autor: 		Bernd Krumbck
# email: 		b.krumboeck@billa.co.at or office@universalnet.at
# last change: 		2004-02-10
# license:		GPL (Please see the file LICENSE.TXT)
# description: 		Checks the status of lexmark printers
#
# 2004-02-10: Version 0.1
#  Created from check_disk_smb script provided with nagios
#  (cause I was too lazy to write it complete from scratch)
#
################################################################################


# Please change the path of lib to your nagios plugin dir


require 5.004;
use POSIX;
use strict;
use Getopt::Long;
use SNMP;
use vars qw($opt_V $opt_h $opt_H $opt_C $opt_u $opt_p $opt_w $opt_c $verbose);
use vars qw($PROGNAME);
use lib "/usr/lib/nagios/plugins"; # this value must be set to your nagios plugin directory
use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);


# Please don't change the code below this line if you don't know what you do!


sub print_help ();
sub print_usage ();

my $prog_version = "0.1";
my $prog_autor = "Bernd Krumbck";



$PROGNAME = "check_lexprn";

$ENV{'PATH'}='';
$ENV{'BASH_ENV'}='';
$ENV{'ENV'}='';

Getopt::Long::Configure('bundling');
GetOptions
	("v"   => \$verbose, "verbose"    => \$verbose,
	 "V"   => \$opt_V, "version"    => \$opt_V,
	 "h"   => \$opt_h, "help"       => \$opt_h,
	 "w=s" => \$opt_w, "warning=s"  => \$opt_w,
	 "c=s" => \$opt_c, "critical=s" => \$opt_c,
	 "p=s" => \$opt_p, "password=s" => \$opt_p,
	 "u=s" => \$opt_u, "username=s" => \$opt_u,
	 "C=s" => \$opt_C, "community=s"    => \$opt_C,
	 "H=s" => \$opt_H, "hostname=s" => \$opt_H);

if ($opt_V) {
	print_revision($PROGNAME,'$Revision: 0.1 $'); #'
	exit $ERRORS{'OK'};
}
if ($opt_h) {print_help(); exit $ERRORS{'OK'};}

# Options checking

($opt_H) || ($opt_H = shift) || usage("Host name not specified\n");
my $host = $1 if ($opt_H =~ /([-_.A-Za-z0-9]+\$?)/);
($host) || usage("Invalid host: $opt_H\n");

($opt_C) || ($opt_C = shift) || usage("SNMP Community not specified\n");
my $community = $1 if ($opt_C =~ /([-_.A-Za-z0-9]+)/);
($community) || usage("Invalid community: $opt_C\n");

($opt_u) || ($opt_u = shift) || ($opt_u = "guest");
my $user = $1 if ($opt_u =~ /([-_.A-Za-z0-9]+)/);
($user) || usage("Invalid user: $opt_u\n");

($opt_p) || ($opt_p = shift) || ($opt_p = "guest");
my $pass = $1 if ($opt_p =~ /(.*)/);

#($opt_w) || ($opt_w = shift) || ($opt_w = 25);
#my $warn = $1 if ($opt_w =~ /([0-9]{1,2}\%?|100\%?|[0-9]+[kMG])/);
#($warn) || usage("Invalid warning threshold: $opt_w\n");

#($opt_c) || ($opt_c = shift) || ($opt_c = 10);
#my $crit = $1 if ($opt_c =~ /([0-9]{1,2}\%?|100\%?|[0-9]+[kMG])/);
#($crit) || usage("Invalid critical threshold: $opt_c\n");

# check if both warning and critical are percentage or size
#unless( ( ($opt_w =~ /([0-9]){1,2}$/ ) && ($opt_c =~ /([0-9]){1,2}$/ )  )|| (( $opt_w =~ /[kMG]/ ) && ($opt_c =~ /[kMG]/) )  ){
#	usage("Both warning and critical should be same type- warning: $opt_w critical: $opt_c \n");
#}


my $state = "OK";
my $answer = '';
my $res = undef;
my @lines = undef;

my $printer_model;
my %printer_status;

# Just in case of problems, let's not hang Nagios
$SIG{'ALRM'} = sub {
	print "No Answer from Client\n";
	exit $ERRORS{"UNKNOWN"};
};
alarm($TIMEOUT);



SNMP::initMib();
if (my $sess = new SNMP::Session(DestHost => "$host", Community => "$community", Version => '1', UseSprintValue => '0')) {
	$printer_model = $sess->get('.1.3.6.1.4.1.641.2.1.2.1.2.1');
	if ($printer_model) {
		$printer_status{'IRC'} = $sess->get('.1.3.6.1.4.1.641.2.1.3.1.2.1');
		$printer_status{'OutHopFull'} = $sess->get('.1.3.6.1.4.1.641.2.1.3.1.3.1');
		$printer_status{'InputEmpty'} = $sess->get('.1.3.6.1.4.1.641.2.1.3.1.4.1');
		$printer_status{'PaperJam'} = $sess->get('.1.3.6.1.4.1.641.2.1.3.1.5.1');
		$printer_status{'TonerError'} = $sess->get('.1.3.6.1.4.1.641.2.1.3.1.6.1');
		$printer_status{'SrvcReqd'} = $sess->get('.1.3.6.1.4.1.641.2.1.3.1.7.1');
		$printer_status{'DiskError'} = $sess->get('.1.3.6.1.4.1.641.2.1.3.1.8.1');
		$printer_status{'CoverOpen'} = $sess->get('.1.3.6.1.4.1.641.2.1.3.1.9.1');
		$printer_status{'PageComplex'} = $sess->get('.1.3.6.1.4.1.641.2.1.3.1.10.1');
		$printer_status{'LineStatus'} = $sess->get('.1.3.6.1.4.1.641.2.1.3.1.11.1');
		$printer_status{'Busy'} = $sess->get('.1.3.6.1.4.1.641.2.1.3.1.12.1');
		$printer_status{'Waiting'} = $sess->get('.1.3.6.1.4.1.641.2.1.3.1.13.1');
		$printer_status{'Warming'} = $sess->get('.1.3.6.1.4.1.641.2.1.3.1.14.1');
		$printer_status{'Printing'} = $sess->get('.1.3.6.1.4.1.641.2.1.3.1.15.1');
	} else {
		$answer .= "Couldn\'t connect to  printer.\n";
		$answer .= "Couldn\'t get model name of this printer. It seems that Lexmark is not the printer vendor or the printer is not compatible.\n";
		$state = "CRITICAL";
	}
} else {
	$answer .= "Couldn\'t connect to  printer.\n";
	$state = "CRITICAL";
}


#Turn off alarm
alarm(0);

if ($state eq "OK") {
if ($printer_status{'IRC'} > 0) {
	$answer .= "Intervention required ($printer_status{'IRC'}); ";
	$state = "WARNING";
}
if ($printer_status{'LineStatus'} eq 2) {
	$answer .= "Printer is off-line; ";
	$state = "WARNING";
}
if ($printer_status{'Busy'} eq 2) {
	$answer .= "Printer is busy; ";
}
if ($printer_status{'Waiting'} eq 2) {
	$answer .= "Printer is waiting; ";
}
if ($printer_status{'Warming'} eq 2) {
	$answer .= "Printer is warming up; ";
}
if ($printer_status{'Printing'} eq 2) {
	$answer .= "printing; ";
}
if ($printer_status{'OutHopFull'} eq 2) {
	$answer .= "Output hopper is full; ";
	$state = "WARNING" if ($state eq "OK");
}
if ($printer_status{'InputEmpty'} eq 2) {
	$answer .= "Input tray is empty; ";
	$state = "WARNING" if ($state eq "OK");
}
if ($printer_status{'PaperJam'} eq 2) {
	$answer .= "Paper jam; ";
	$state = "WARNING" if ($state eq "OK");
}
if ($printer_status{'TonerError'} eq 2) {
	$answer .= "Toner error; ";
	$state = "WARNING" if ($state eq "OK");
}
if ($printer_status{'SrvcReqd'} eq 2) {
	$answer .= "Service Required; ";
	$state = "WARNING" if ($state eq "OK");
}
if ($printer_status{'DiskError'} eq 2) {
	$answer .= "Disk error; ";
	$state = "WARNING" if ($state eq "OK");
}
if ($printer_status{'CoverOpen'} eq 2) {
	$answer .= "Cover open; ";
	$state = "WARNING" if ($state eq "OK");
}
if ($printer_status{'PageComplex'} eq 2) {
	$answer .= "Page Complex; ";
	$state = "WARNING" if ($state eq "OK");
}
}


print $answer;
print "Model: $printer_model\n" if ($verbose);
print "$state\n" if ($verbose);
print "Printer $state; Model: $printer_model\n" if ($state eq "OK");
exit $ERRORS{$state};



sub print_usage () {
#	print "Usage: $PROGNAME -H <host> -C <community> [-u <user> -p <password>]
#      [-w <warn> -c <crit>]\n";
	print "Usage: $PROGNAME -H <host> -C <community> [-v] [-V] [-h]\n";
}

sub print_help () {
	print_revision($PROGNAME,'$Revision: 0.1 $');
	print "Created by $prog_autor

Perl Check Lexmark Printer plugin for Nagios

";
	print_usage();
#	print "
#-H, --hostname=HOST
#   Name\/IP of the Printer
#-C, --community=STRING
#   SNMP Community
#-u, --user=STRING
#   Username to log in to printer. (Defaults to \"guest\", but currently not used)
#-p, --password=STRING
#   Password to log in to printer. (Defaults to \"guest\", but currently not used)
#-w, --warning=INTEGER
#   Percent of available marker supply level at which a warning will be generated (Default: 25%)
#
#-c, --critical=INTEGER
#   Percent of available marker supply level at which a critical will be generated (Defaults: 10%)
#
#   Warning percentage should be greater than critical
#
#";

	print "
-H, --hostname=HOST
   Name\/IP of the Printer
-C, --community=STRING
   SNMP Community
-v, --verbose
   Set Verbose mode on
-V, --version
   Print some informations
-h, --help
   Print this help text

";

#	support();
}
