<?php

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

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

$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];

//
//Screen Text
//
$counter = 0;
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++;
	}
}

//print_r($screen_text);

//
//Toner
//
$return_string = '';
if ($toner_low == 1){
                $black = str_replace('"','', str_replace('STRING: "','',snmpget($host,$community,'iso.3.6.1.2.1.43.11.1.1.6.1.1')));
                $cyan = str_replace('"','', str_replace('STRING: "','',snmpget($host,$community,'iso.3.6.1.2.1.43.11.1.1.6.1.2')));
		$magenta = str_replace('"','', str_replace('STRING: "','',snmpget($host,$community,'iso.3.6.1.2.1.43.11.1.1.6.1.3')));
                $yellow = str_replace('"','', str_replace('STRING: "','',snmpget($host,$community,'iso.3.6.1.2.1.43.11.1.1.6.1.4')));
		$i=0;
                while($i <= sizeof($screen_text)){
                        if (strstr($screen_text[$i], 'ORDER BLACK')){
                                $return_string .= 'Order '.$black.' - ';
				unset($screen_text[$i]);
                        }else if (strstr($screen_text[$i], 'ORDER CYAN')){
                                $return_string .= 'Order '.$cyan.' - ';
                                unset($screen_text[$i]);
                        }else if (strstr($screen_text[$i], 'ORDER MAGENTA')){
                                $return_string .= 'Order '.$magenta.' - ';
                                unset($screen_text[$i]);
                        }else if (strstr($screen_text[$i], 'ORDER YELLOW')){
                                $return_string .= 'Order '.$yellow.' - ';
                                unset($screen_text[$i]);
                        }
                        $i++;
                }

		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)){
	if($screen_text[$i]){
		$return_string .=  $screen_text[$i]." - ";
	}
	$i++;
}



//
//Output
//
if($return_string){
	if (stristr($return_string,'Cartridge') || stristr($return_string,'Toner')){
		$return_string = substr($return_string, 0, -3);
	        echo $return_string;
        	exit(1);
	}else if (stristr($return_string,'LOAD') || stristr($return_string,'TRAY')){
                $return_string = substr($return_string, 0, -3);
                echo $return_string;
                exit(2);
        }else if(stristr($return_string,'Sleep mode on ')){
		$this_return =  "Printer OK - ".$return_string;
		echo $this_return;
        	exit(0);
	}else if(stristr($return_string,'Calibrating')){
                echo $return_string;
                exit(0);
        }else{
		$return_string = substr($return_string, 0, -3);
		echo $return_string;
		exit(2);
	}
}else{
	if(snmpget($host, $community, 'iso.3.6.1.2.1.1.1.0')){
        	echo "Printer OK";
                exit(0);
	}else{
        	echo "Cannot reach the printer. Possibly offline.";
                exit(2);
        }
}

?>

