Hi all,
I am writing a script to collect data from printers through snmp walk calls, the code works fine with one printer, but when I set the hostname to an array call, the function repeats only the first array element and does not run through the whole array. Here is my full current code:
[php]
<?php //full mib list can be found @ http://www.oidview.com/mibs/0/Printer-MIB.html // MID descriptions @ http://www.telecomm.uh.edu/stats/rfc/Printer-MIB_.html#prtGeneralReset // snmpwalk( hostname , community , OID , timeout( microsec), retries); function printcall($ip, $oid) { $add = $ip; $result = snmpwalk($add, "public", $oid ,100000,2); return $result; } $address = array("128.193.74.47","128.193.74.55","128.193.74.48","128.193.74.56","128.193.75.177"); $printer_name = array("BX112","BX112-BW","BX120","BX120-BW","MBA Lounge"); $current_printer = 0; foreach($printer_name as $printer) { $p = $address[$current_printer]; echo $p . ""; //Does SNMP Walk for toner values $current_toner = printcall($p,".1.3.6.1.2.1.43.11.1.1.9"); //SNMP Walk for current values if(!$current_toner) { if($current_printer < 4) { echo $printer_name[$current_printer]. " is offline.". "
"; } else { die($printer_name[$current_printer]. " is offline.". "
"); } } else { $values = 0; // records how many data fields there are for current printer foreach ($current_toner as $val) { $pieces = explode(":",$val); //removes Integer string before value $current[] = $pieces[1]; $values++; } $current_max = printcall($p,".1.3.6.1.2.1.43.11.1.1.8"); //SNMP Walk for max values foreach ($current_max as $val) { $pieces = explode(":",$val); //removes Integer string before value $max[] = $pieces[1]; } $status_lines = 0; $printer_status = printcall($p,".1.3.6.1.2.1.43.16.5.1.2"); //SNMP Walk for current status foreach ($printer_status as $val) { $pieces = explode("\"",$val); //removes Integer string before value $status[] = $pieces[1]; if(strlen($pieces[1]) != NULL) { $status_lines++; } } //Using the amount of data fields from SNMP Walk, decides which type of field display to choose if($values < 4) { $toners = array("Black","Image Transfer Kit"); //All printers except HP 4700 } else { $toners = array("Black","Cyan","Magenta","Yellow","Image Transfer Kit","Fuser Kit"); //HP 4700 } //Outputs to screen: printer name and percentage values for each toner echo "These are the current percentages for printer ". "".$printer_name[$current_printer].":
"; for($i = 0;$i < $values;$i++) { $percent = round(($current[$i]/$max[$i])*100,1); echo $toners[$i].": ".$percent."%
"; } for($i = 0;$i < $status_lines;$i++) { echo "Current Status: " . $status[$i] . "
"; } echo "
"; } $current_printer++; } [/php] If anyone can help, that would be much appreciated. ~Albert