Table of Contents

Overview

Gather information about paper and toner usage from the computer lab printers to help keep a better quality of service.

Implementation

This is a simple example of how to pull information using snmp and do something useful with it. I have always wanted a simple status page for our lab printers. So, by pulling snmp data I can then produce a very simple percentage bar indicating current levels. I'm a sucker for ASCII art, so these are produced by altering the background color using css tags.

When I wrote this, I didn't have the net-snmp module loaded into php, so I used the php function exec to wrap the actual command line calls. If you do have the module loaded, I highly reccomend using it. The exec calls can easily be replaced with the folling: substr(snmpget(#ip address#, “public”, #snmp keys#), 11); Just FYI, this code is a bit sloppy, but it gets the job done. :)

<meta http-equiv="refresh" content="600">
<head>
   <style type="text/css">
   body {
       background: white;
   }
   full {
       background: green;
   }
   warn {
       background: yellow;
   }
   low {
       background: red;
    }
   empty {
       background: grey;
   }
   pre {
       color: black;
   }
   </style>
 </head>

<?php

exec("snmpget -v1 -c public -O qv #ip address# \
	mib-2.43.11.1.1.8.1.1 \
	mib-2.43.11.1.1.9.1.1 \
	mib-2.43.16.5.1.2.1.1", $nash);

exec("snmpget -v1 -c public -O qv #ip address# \
	mib-2.43.11.1.1.8.1.1 \
	mib-2.43.11.1.1.9.1.1 \
	mib-2.43.16.5.1.2.1.1", $mathes);

if( isset($nash) && $nash[0] != 0 )
    $tonerlevel["Nash"] = (($nash[1] / $nash[0]) * 100);
else
    $tonerlevel["Nash"] = -1;

 if( isset($mathes) && $mathes[0] != 0 )
    $tonerlevel["Mathes"] = (($mathes[1] / $mathes[0]) * 100);
else
    $tonerlevel["Mathes"] = -1;

$status["Nash"] = $nash[2];
$status["Mathes"] = $mathes[2];

foreach($tonerlevel as $printer => $value)
{
   if( $value < 0 )
	$value = "error";

    echo($printer ." toner left: ". $value . "%\n");

    if( $value < 10 )
	$level = "low";
    else if( $value < 40 )
	$level = "warn";
    else
	$level = "full";

   for($i=0;$i<50;$i++)
   {
	if( $i < ($value / 2) )
	    echo("<". $level ."> </". $level .">");
	else
	    echo("<empty> </empty>");
   }
   echo("\n");
}

echo("\n");

foreach($status as $printer => $value)
{
   echo($printer ." status: ". $value ."\n");
}
?>

Throw it all together, and you get something like this:

Resources

 
snmp_printer_monitoring.txt · Last modified: 2006/10/08 12:56 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki