PHP printer tracking script

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

do you get the message “name_of_printer” is offline? if so then the following if statement is returning true
[php]
if(!$current_toner)
{
if($current_printer < 4)
{
echo $printer_name[$current_printer]. " is offline.". “
”;
}
else
{
die($printer_name[$current_printer]. " is offline.". “
”);
}
}
[/php]
and the inner if else block may be deffering to the else block which terminates your script. I have no clue what printcall does, or what its return values could be but that is where I would start.

the if block actually works correctly, when the printer is turned on it returns results and when the printer does not return anything it states the printer as offline.

The only problem I am having is with the snmp walk within the printcall function not returning any other results other than that of the first printer.

SNMP Walk is a call to a network activated device, to get the status of the device; in my case network printers.

I found a way around it, I created another function and set all the snmp calls and print outs in there. In the main foreach loop call the function each time with the ip address passed through

Can you share this solution?

This is the code in my index, in the index I store the array of printer names and IP addesses passing one at a time
[php]
for($current_printer = 0;$current_printer <$num_results;$current_printer++)
{
printer($printer_ip[$current_printer],$printer_name[$current_printer],$printer_num[$current_printer],$current_printer,$num_results);
}[/php]

A snippet of the printer function:
[php]function printer($printer_ip,$printer_name,$printer_num,$current_printer,$printers_total)
{
$printer_ip = trim($printer_ip);

$current_toner = printcall($printer_ip,".1.3.6.1.2.1.43.11.1.1.9", ":"); //SNMP Walk for current toner values

if(!$current_toner)//check if printers are turned on
{
    if($current_printer  < ($printers_total-1))
    {
        echo $printer_name . " is offline.". "<br />". "<br />";
        return;
    }
    else
    {
        die($printer_name . " is offline.". "<br />");
    }
}
else
{
    //SNMP Walk for max toner values
    $current_max = printcall($printer_ip,".1.3.6.1.2.1.43.11.1.1.8", ":");

[/php]

and the modified printcall function:
[php]
//Calls printer through SNMP, then seperates values depending on delimiter
//returns array of values
function printcall($ip, $oid,$delimiter)
{
//actual SNMP walk call to printer
$result = snmpwalk($ip, “public”, $oid ,100000,2);

foreach ($result as $val)
{
    $pieces = explode($delimiter,$val); //removes Integer string before value
    $array_results[] = $pieces[1];
}
return $array_results;

}
[/php]

hope that helps

Sponsor our Newsletter | Privacy Policy | Terms of Service