Linux.com

Feature: System Administration

Small-scale SNMP reporting

By Colin Beckingham on February 12, 2008 (7:00:00 PM)

Share    Print    Comments   

The Simple Network Management Protocol (SNMP) is a useful tool for examining the state of devices on a network. The open source world offers a number of consoles designed to manage the information from these devices and produce reports. However, there are circumstances in which access to the devices at a more customizable level is more beneficial. Given that the programming involved is minimal, it is worth considering custom applications for SNMP reporting.

Some examples might be:

  • Keeping a history of values stored to MySQL back end
  • Alarm systems
  • Cron jobs
  • Password-protected access to special reports
  • Applications to be dovetailed into other custom systems

Consider an alarm system. You might have a sensor delivering temperature measurements to an SNMP device. Normal values for temperature range from 20°C to 30°C. Anything outside that range might prompt an action to alert an operator.

One approach would be to fire off an alert as soon as an improper value is detected. In this case no logging is necessary; as soon as the value is seen, the script takes action. A different case might come into play when some values are transitory and should be ignored. Suppose you encountered this set of values, taken every 30 seconds: 21, 22, 23, 22, 0, 24, 25. Was that fifth value really zero, or was there some transitory collision on the network that hid the real value of the temperature?

To see how such a system could work, suppose that we have a Room Alert 7E monitor. This monitor is designed for use in a computer room, and comes with bundled software which gives clues to the addresses or 'object id' numbers of the internal variables we will need. Our monitor has, for simplicity's sake, just one temperature sensor attached, though it can support four.

We can read the temperature values and store them in a database back end for ongoing analysis by another application that will be responsible for generating an alarm. This SNMP query will need to be easily available to the operating system and require the least resources, so it can run in a bash script as a cron job.

I downloaded and installed the latest version of Net-SNMP, a software suite that can retrieve and manipulate SNMP information. SNMP is programmable in a number of contexts including bash, Perl, PHP, C, and Java. The Net-SNMP Web site provides some interesting examples in C.

Bash for cron

Here is the code for the cron job:

#!/bin/bash temp1=`/usr/bin/snmpget -v 1 -c public 192.168.0.xxx .1.3.6.1.4.1.20916.1.2.1.1.5.0` t1=${temp1##S* } t1p=${#t1}-2 t1=${t1:0:$t1p}.${t1:$t1p:2} dd=`date +%Y-%m-%d\ %T` mysql -u user -ppassword -h host -D database -e "insert table1 values('${dd}',${t1})" exit 0

The IP address 192.168.0.xxx is the network address of the SNMP monitor, and the long string '.1.3.6.1.4.1.20916.1.2.1.1.5.0' is the address of the variable on the device that we wish to read, which you can obtain from examining the bundled software. Example actual output, line by line, of the above:

SNMPv2-SMI::enterprises.20916.1.2.1.1.5.0 = INTEGER: 5561 5561 4-2 55.61 2008-01-24 11:06:41

The first value gives us more than we need, so we extract the actual value (5561), find out what length of string we have, and subtract 2 from the length, since we are expecting a decimal value with two decimal places. We then format the value to two decimal places and store it in our database.

Say we call the script 'temp1reader' and we want it to run every minute. Assuming we are using the superuser cron, add the line * * * * * /path/to/script/temp1reader to /etc/crontab, and restart cron with the command crontab /etc/crontab. If you want to use a specific user's cron, ensure that the script has the user's permissions with the chmod command, and follow your installation's instructions for modifying a user's crontab list.

We are now logging the temperatures every minute and making the values available in the MySQL back end. A separate alarm application can now query the database and apply rules regarding whether to generate an alarm or not. Admittedly, the bash job contains no error checking; we may need to add some before it hits production.

PHP for an immediate view

Now suppose that from time to time we need to check the immediate temperature reading. A different application will allow us to see the current values only and investigate the state of the device in general. We want to view this information on a Web page across the Internet, so we can code it in PHP.

The standard compile of PHP does not include the necessary links to Net-SNMP libraries. Net-SNMP needs to be available on the server as a library so that PHP can link to it and allow the SNMP functions to work. The PHP help file contains instructions on adding the instruction --with-snmp to the ./configure command.

This example code for the PHP application reports the immediate value of the temperature sensor:

<?php $output = "Start OK<br />"; $snmp_host = "192.168.0.xxx"; $snmp_community = "public"; $snmp_object = ".1.3.6.1.4.1.20916.1.2.1.1.5.0"; $output .= "<hr />Temperatures<hr />"; $temperature1 = snmpget($snmp_host, $snmp_community, $snmp_object); $output .= "Temp 1 = ".$temperature1."<hr />"; $output .= "End OK"; echo $output; ?>

The output from the PHP variable $output resembles:

Start OK ---------------- Temperatures ---------------- Temp 1 = INTEGER: 5786 ---------------- End OK

To show the temperature, we can then take the contents of the variable and place it between relevant !DOCTYPE ... <html>... <body> and </body></html> tags, then place the HTML file in the document root of an Apache server.

These examples illustrate that for some special applications, little coding is necessary to create a functional logger and quick check of a device that offers SNMP facilities. You can probably imagine equally simple yet useful ways to put the information in SNMP variables to work in your own utilities, without turning to a network management console to monitor them.

Colin Beckingham is a freelance programmer and writer in Eastern Ontario. He is currently working on a project that logs and charts the operation of a biomass burner using open source resources.

Share    Print    Comments   

Comments

on Small-scale SNMP reporting

Note: Comments are owned by the poster. We are not responsible for their content.

Very neat.

Posted by: Anonymous [ip: 169.233.25.159] on February 12, 2008 09:48 PM
I always thought SNMP was a useless feature, but I now see that for small business (and possibly large business [I haven't got the chance to peak in just yet]) it could be useful

#

Small-scale SNMP reporting

Posted by: Anonymous [ip: 190.58.168.139] on February 13, 2008 12:01 AM
Zenoss uses Net-SNMP and a few other packages very nicely to give you a truly stellar package. You should give that a look.
And to #1, snmp is one of those things that's there and you don't have to use it, but once you do you get so much visibility into the state of your devices that it really does allow only a handful of administrators to stay on top of a vast number of devices.

#

Very powerful and flexible

Posted by: Anonymous [ip: 24.209.228.183] on February 13, 2008 02:49 AM
I use a combination of SNMP + MRTG + PHP + shell scripting + MYSQL to manage and display historical data on all the servers at my work. Very flexible and great for when the boss is looking for answers on historical usage patterns.

#

Small-scale SNMP reporting

Posted by: Anonymous [ip: 194.237.142.6] on February 13, 2008 01:29 PM
I have used Cacti (http://cacti.net) for monitoring servers and printers both at work and at home. It can monitor SNMP-devices and there are plugins that allow alarms to be definied with different thresholds.

#

Small-scale SNMP reporting

Posted by: Anonymous [ip: 203.91.190.117] on February 14, 2008 05:56 AM
Another option is INM (http://www.intellipool.se) who also can monitor SNMP devices as well as other stuff.

#

Small-scale SNMP reporting

Posted by: Anonymous [ip: 217.20.22.66] on February 14, 2008 05:20 PM
<quote>the long string '.1.3.6.1.4.1.20916.1.2.1.1.5.0' is the address of the variable on the device that we wish to read</quote>

That's not an address, it's an ASN.1 OID. They're assigned hierarchically but they aren't really addresses - stripping off the last few components wouldn't be a meaningful request, for example.

#

Small-scale SNMP reporting

Posted by: Anonymous [ip: 192.168.3.46] on March 05, 2008 01:50 AM
my question is how do linux identify the decimal number? im just only beginner. thank you

#

This story has been archived. Comments can no longer be posted.


 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya