Author: Colin Beckingham
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.
Category:
- System Administration