<?php

$host=$argv[1];
$community=$argv[2];
$warn=$argv[3];
$critical=$argv[4];

//If the printer can't be reached, there is no sense in continuing past this point.
if(!@snmpget($host, $community, 'iso.3.6.1.2.1.1.1.0')){
	echo "Cannot reach the printer. Possibly offline.";
        exit(2);
}

$snmp_objects = @snmprealwalk($host, $community, '');
$screen = @array_keys($snmp_objects);
$screen_text = array();

$toner_low = explode(' ',snmpget($host,$community,'iso.3.6.1.4.1.11.2.3.9.1.1.2.10.0'));
$toner_low = $toner_low[1];

//Add to these as you see fit.
//Messages not accounted for will exit 0 and will show up as a green light in Nagios no matter what their real severity.
//Keep this list of status messages up to date.

//Good messages
$the_good = array('Sleep mode on','Calibrating', 'Ready','Processing', 'Checking', 'Printing');
//Warning level messages
$the_bad = array('Cartridge','Toner','Order');
//Critical messages
$the_ugly = array('Load','Tray','Jam','Intervention','Fuser', 'Replace', 'Install');

//
//Screen Text
//
$counter = 0;
if($screen){
	foreach ($screen as &$array_key){
		if (strstr($array_key,'iso.3.6.1.2.1.43.18.') && strstr($snmp_objects[$array_key],'STRING')){
	        	$screen_text[$counter] =  str_replace('"','', str_replace('STRING: "','',$snmp_objects[$array_key])).' - ';
			$counter++;
		}
	}
}
//
//Toner
//
$return_string = '';

if ($toner_low == 1){
	//These added for RFC 1514 compliance and better compatibility (V.3)
        $black_max = explode(' ',snmpget($host,$community,'iso.3.6.1.2.1.43.11.1.1.8.1.1'));
        $cyan_max = explode(' ',snmpget($host,$community,'iso.3.6.1.2.1.43.11.1.1.8.1.2'));
        $magenta_max = explode(' ',snmpget($host,$community,'iso.3.6.1.2.1.43.11.1.1.8.1.3'));
        $yellow_max = explode(' ',snmpget($host,$community,'iso.3.6.1.2.1.43.11.1.1.8.1.4'));
	$black_current = explode(' ',snmpget($host,$community,'iso.3.6.1.2.1.43.11.1.1.9.1.1'));
	$cyan_current = explode(' ',snmpget($host,$community,'iso.3.6.1.2.1.43.11.1.1.9.1.2'));
	$magenta_current = explode(' ',snmpget($host,$community,'iso.3.6.1.2.1.43.11.1.1.9.1.3'));
	$yellow_current = explode(' ',snmpget($host,$community,'iso.3.6.1.2.1.43.11.1.1.9.1.4'));

	$i=0;
	cartridges($black_current[1], $black_max[1], 1);
	cartridges($cyan_current[1], $cyan_max[1], 2);
	cartridges($magenta_current[1], $magenta_max[1], 3);
	cartridges($yellow_current[1], $yellow_max[1], 4);

	//If the JetDirect SNMP isn't advertising cartrige information...
	if($return_string == ''){
		$return_string .= "Toner Low See Printer for Details <a href='http://$host'>link</a>   ";
	}
}

//
//Other Errors
//
$i=0;
while($i <= sizeof($screen_text)-1){
	if($screen_text[$i]){
		$return_string .=  $screen_text[$i]." - ";
	}
	$i++;
}

//
//Output
//
if($return_string){

	//Some garbage cleanup (Something that annoyed me about version 1)
	if(strstr($return_string, '-  -')){
		$return_string = str_replace('-  -','',$return_string);
	}

	//Parse for the critical stuff first
	messages($the_ugly, 2);
	//...if that passes, parse for warnings
	messages($the_bad, 1);
	//...if thats all good, parse for the good statuses
	messages($the_good, 0);

	//For things not accounted for in the good, the bad or the ugly...
	echo rtrim($return_string,' - ');
	exit(0);

}
//If there is no status message, everything must be OK
else{
        echo "Printer OK";
	exit(0);
}

//(V.3)
function cartridges($current, $max, $iso_num){
	global $warn;
	global $critical;
	global $host;
	global $community;
	global $return_string;

	if(($current/$max)*100 <= $critical){
                $cartridge = explode('"',snmpget($host,$community,"iso.3.6.1.2.1.43.11.1.1.6.1.$iso_num"));
                $cartridge = $cartridge[1];

                if (strstr(snmpget($host,$community,"iso.3.6.1.2.1.43.11.1.1.6.1.$iso_num"), 'Hex-STRING: ')){
                        $hex_string .= str_replace("\n", '', str_replace('Hex-STRING: ','',snmpget($host,$community,"iso.3.6.1.2.1.43.11.1.1.6.1.$iso_num")));
                        $return_string .= 'Replace '.hex2ascii($hex_string);
                }else{
                        $return_string .= 'Replace '.$cartridge.' - '. ($current/$max)*100 .'% - ';
                }
        }else if(($current/$max)*100 > $critical && ($current/$max)*100 <= $warn){
		$cartridge = explode('"',snmpget($host,$community,"iso.3.6.1.2.1.43.11.1.1.6.1.$iso_num"));
                $cartridge = $cartridge[1];

                if (strstr(snmpget($host,$community,"iso.3.6.1.2.1.43.11.1.1.6.1.$iso_num"), 'Hex-STRING: ')){
                        $hex_string .= str_replace("\n", '', str_replace('Hex-STRING: ','',snmpget($host,$community,"iso.3.6.1.2.1.43.11.1.1.6.1.$iso_num")));
                        $return_string .= 'Order '.hex2ascii($hex_string);
                }else{
                        $return_string .= 'Order '.$cartridge.' - '. ($current/$max)*100 .'% - ';
                }

	}

}

//(V.2)
function messages($array_name, $exit_code){
	global $return_string;
	for($i=0;$i<=sizeof($array_name)-1;$i++){
        	        if (stristr($return_string,$array_name[$i])){
				if($exit_code == 0){
					$return_string = "Printer OK - ".$return_string;
				}
                	        echo rtrim($return_string,' - ');
                        	exit($exit_code);
	                }
        	}
	}
//(V.2)
function hex2ascii($hexadecimal){
	$ascii='';
	$hexadecimal=str_replace(" ", "", $hexadecimal);
	for($i=0; $i<strlen($hexadecimal); $i=$i+2) {
		$ascii.=chr(hexdec(substr($hexadecimal, $i, 2)));
	}
	return($ascii);
}

?>
