<?php

$host=$argv[1];
$community=$argv[2];

//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');
//Critical messages
$the_ugly = array('Load','Tray','Jam','Intervention','Fuser');

//
//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){
	$i=0;
        while($i <= sizeof($screen_text)){
        	if (strstr($screen_text[$i], 'ORDER BLACK')){
			if (strstr(snmpget($host,$community,'iso.3.6.1.2.1.43.11.1.1.6.1.1'), '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.1')));
				$return_string .= hex2ascii($hex_string);
				
			}else{
				$return_string .= 'Order '.str_replace('"','', str_replace('STRING: "','',snmpget($host,$community,'iso.3.6.1.2.1.43.11.1.1.6.1.1'))).' - ';
			}
			unset($screen_text[$i]);
               	}else if (strstr($screen_text[$i], 'ORDER CYAN')){
	                $return_string .= 'Order '.str_replace('"','', str_replace('STRING: "','',snmpget($host,$community,'iso.3.6.1.2.1.43.11.1.1.6.1.2'))).' - ';
                        unset($screen_text[$i]);
                }else if (strstr($screen_text[$i], 'ORDER MAGENTA')){
                        $return_string .= 'Order '.str_replace('"','', str_replace('STRING: "','',snmpget($host,$community,'iso.3.6.1.2.1.43.11.1.1.6.1.3'))).' - ';
                        unset($screen_text[$i]);
                }else if (strstr($screen_text[$i], 'ORDER YELLOW')){
                        $return_string .= 'Order '.str_replace('"','', str_replace('STRING: "','',snmpget($host,$community,'iso.3.6.1.2.1.43.11.1.1.6.1.4'))).' - ';
                        unset($screen_text[$i]);
                }
                $i++;
	}
	//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);
}
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);
	                }
        	}
	}

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);
}

?>
