#!/usr/bin/perl
use Getopt::Long;

#
# README FIRST
# 
# This script was tested with success agains following printers:
#  - OKI OkiLAN 8300e Rev.04.50 10/100BASE Ethernet PrintServer: Attached to C6100 Rev.D1.08 : (C)2005 Oki Data Corporation
#
#
# WARNING
# For printers like Lexmark try to use check_printer.pl
#

$snmpget = "/usr/bin/snmpget";
$snmpwalk = "/usr/bin/snmpwalk";

sub init {
	$status = GetOptions(
                "p=s" => \$community,"community=s" => \$community,
                "h=s" => \$hostname, "hostname=s" => \$hostname,
                "i=s" => \$item, "item" => \$item,
                "w=i" => \$w, "warning" => \$w,
                "c=i" => \$c, "critical" => \$c,
                );

	usage() if (!defined($community) || !defined($hostname) || !defined($item));
	main();
}

sub usage {
	print "Usage:\n";
	print "\t$0 -h <hostname> -p <community>  -i <item> [-w X] [-c Y]\n";
	print "\tItem can be: ";
	print "'uptime', 'miscalerts', 'status', 'toner', 'storage', 'lcdmessage', 'tray'\n";
	print "\t-w Warning \n";
	print "\t-c Critical \n\n";
	exit(3);
}


sub uptime {
	$run = `$snmpwalk -On -c $community -v1 $hostname .1.3.6.1.2.1.1.3.0 2>/dev/null`;
	$run =~ /^\.1\.3\.6\.1\.2\.1\.1\.3\.0\s(.+)$/;
	$uptime = $1;

	print "OK - Uptime $uptime\n";
	exit(0);

}

sub printStatus {

	$run = `$snmpget -Oq -On -v 1 -c $community $hostname .1.3.6.1.2.1.25.3.5.1.1.1 2>/dev/null`;
	$run =~ /^\.1\.3\.6\.1\.2\.1\.25\.3\.5\.1\.1\.1\s(.+)$/;
	$status = $1;

	if ($status ne "printing" && $status ne "idle") {
		print "CRITICAL - Print status $status\n";
		exit(2);
	}

	print "OK - Print status $status\n";
	exit(0);

}

sub toner {
	&usage() if (!defined($c) || !defined($w));
	@run = `$snmpwalk -On -Oq -c $community -v1 $hostname .1.3.6.1.4.1.2001.1.1.1.1.100.3.1.1.3.1 2>/dev/null`;

	$perfdata = "";
	$cntr = 1;
	$critical = 0;
	$msg = "";
	foreach (@run) {
		$_ =~ m/^\.1\.3\.6\.1\.4\.1\.2001\.1\.1\.1\.1\.100\.3\.1\.1\.3.\d\s\"*(\d+)\"*$/;
		
		$msg .= "Toner$cntr $1% ";
		$perfdata .= "Toner$cntr=$1%;$w;$c;0;100 ";

		if ($1 < $c) {
			$critical = 1;
		} elsif ($1 < $w) {
			$warning = 1;
		}
		$cntr++;
	}
	
	if ($critical) {
		print "CRITICAL - $msg | $perfdata\n";
		exit(2); 
	}

	if ($warning) {
                print "WARNING - $msg | $perfdata\n";
                exit(1);
	}

        print "OK - $msg | $perfdata\n";
        exit(0);

}

sub tray {

	# .1.3.6.1.4.1.2001.1.1.1.1.100.2.1.1.7.
	@run = `$snmpwalk -On -Oq -c $community -v1 $hostname .1.3.6.1.4.1.2001.1.1.1.1.100.2.1.1.7 2>/dev/null`;
	$counter = 1;
	foreach (@run) {
		m/.1.3.6.1.4.1.2001.1.1.1.1.100.2.1.1.7.\d\s\"*(\d+)\"*$/;
		$msg .= "Tray$counter=$1 ";
		$perfdata .= "Tray$counter=$1;;;; ";
	}

	print "OK - Tray counters $msg |$perfdata\n";
	exit(0);

}

sub lcdmessage {
	$run = `$snmpget -On -Oq -c $community -v1 $hostname .1.3.6.1.4.1.2001.1.1.1.1.2.20.0 2>/dev/null`;
	$run =~ m/\.1\.3\.6\.1\.4\.1\.2001\.1\.1\.1\.1\.2\.20\.0\s(.*)$/;
	$msg = $1;

	$ex = 0;
	$ex = 2 if ($msg !~ "ONLINE" );

	print "LCD Message Status $msg\n";
	exit($ex);
}

sub miscalerts {
	$run = `$snmpget -On -Oq -c $community -v1 $hostname .1.3.6.1.2.1.43.16.5.1.2.1.2 2>/dev/null`;
        $run =~ m/\.1\.3\.6\.1\.2\.1\.43\.16\.5\.1\.2\.1\.2\s\"*([\w\s]*)\"*$/;
        $msg = $1;
	if ($msg != "POWER SAVE") {
		print "CRITICAL - Mensagem do sistema $msg\n";
		exit(2);
	}

	print "OK - Mensagem do sistema $msg\n";
	exit(0);

}

sub storage {

	&usage() if (!defined($c) || !defined($w));
	# hrStorageSize
        $run = `$snmpget -On -Oq -c $community -v1 $hostname .1.3.6.1.2.1.25.2.3.1.5.1 2>/dev/null`;
	$run =~ m/\.1\.3\.6\.1\.2\.1\.25\.2\.3\.1\.5\.1\s(\d+)$/;
	$total = $1;

	# hrStorageUsed
        $run = `$snmpget -On -Oq -c $community -v1 $hostname .1.3.6.1.2.1.25.2.3.1.6.1 2>/dev/null`;
	$run =~ m/\.1\.3\.6\.1\.2\.1\.25\.2\.3\.1\.6\.1\s(\d+)$/;
	$used = $1;

	$pct = (100 * $used)/$total;

	$pct = sprintf("%.2f",$pct);

	if ($pct > $c) {
		print "CRITICAL - Ocupacao espaco de armazenamento interno $pct% |usage=$pct%;$w;$c;0;100\n";
		exit(2);
	}
	if ($pct > $w) {
		print "WARNING - Ocupacao espaco de armazenamento interno $pct% |usage=$pct%;$w;$c;0;100\n";
		exit(1);
	}

	print "OK - Ocupacao espaco de armazenamento interno $pct% |usage=$pct%;$w;$c;0;100\n";
	exit(0);
}

sub main {

	# veriry printer snmp
	system("$snmpget -Oq -v 1 -c $community $hostname .1.3.6.1.2.1.1.1.0 2>/dev/null 1>&2");
	if ($? != 0) {
		print "CRITICAL - Erro conectando snmp impressora remota $hostname\n";
		exit(2);
	}


	&uptime() if ($item eq "uptime");
	&printStatus() if ($item eq "status");
	&toner() if ($item eq "toner");
	&storage() if ($item eq "storage");
	&lcdmessage() if ($item eq "lcdmessage");
	&tray() if ($item eq "tray");
	&miscalerts() if ($item eq "miscalerts");
	&usage();
	exit;
}

init();
