Previous section   Next section

Recipe 17.4 Extracting Inventory Information from a List of Routers with SNMP

17.4.1 Problem

You want to build a report of important router information for all of your managed routers.

17.4.2 Solution

The following Perl script extracts important router information such as router name, physical locations, contact names, and serial numbers from a list of routers, and creates a report of this information. The script is intended to be run manually and no arguments are required or expected.

Here's some example output:

Freebsd% ./inventory.pl
  Router             Location                     Contact              Serial
Router     999 Queen St. W., Toronto, Ont  Ian Brown 416-555-2943     JAX123456
Boston     1273 Main Street, Boston, MA    Bob Irwin 800-555-1221     JAX231567
Denver     24 Sussex Drive, Denver, CO     Helpdesk  800-555-2992     JAX928362
Frame      999 Queen St. W., Toronto, Ont  Ian Brown 416-555-2943     JAX212321
Toronto    999 Queen St. W., Toronto, Ont  Ian Brown 416-555-2943     JAX283291
Boston2    1273 Main Street, Boston, MA    Bob Irwin 800-555-1221     JAX292228
Denver2    24 Sussex Drive, Denver, CO     Helpdesk  800-555-2992     JAX219115
Freebsd%

Example 17-1 contains the Perl code.

Example 17-1. inventory.pl
#!/usr/bin/perl
#
#    inventory.pl -- a script to extract valuable information
#                    from a Router.  (Name, Location, Contact, S/N)
#
#
# Set behaviour
$workingdir="/home/nms"; 
$snmpro="ORARO"; 
$rtrlist="$workingdir/RTR_LIST";
#
#
open (RTR, "$rtrlist") || die "Can't open $rtrlist file";
open (LOG, ">$workingdir/RESULT") || die "Can't open $workingdir/RESULT file";
printf "  Router\t\t Location\t\t\tContact\t\t  Serial\n";
printf LOG  "  Router\t\t; Location\t\t\t;Contact\t\t  ;Serial\n";
while (<RTR>) {
  chomp($rtr="$_");
  $snmpget="/usr/local/bin/snmpget -v1 -c $snmpro $rtr ";
  $rtr=`$snmpget .1.3.6.1.4.1.9.2.1.3.0`;
  $loc=`$snmpget .1.3.6.1.2.1.1.6.0`;
  $con=`$snmpget .1.3.6.1.2.1.1.4.0`;
  $sin=`$snmpget .1.3.6.1.4.1.9.3.6.3.0`;
  chomp(($foo, $RTR) = split (/"/, $rtr));
  chomp(($foo, $LOC) = split (/= /, $loc));
  chomp(($foo, $CON) = split (/= /, $con));
  chomp(($foo, $SIN) = split (/"/, $sin));
  printf ("%-12.12s  %-30.30s  %-25.25s  %-12.12s\n", $RTR, $LOC, $CON, $SIN);
  printf LOG ("%-12.12s; %-30.30s; %-25.25s; %-12.12s\n", $RTR, $LOC, $CON, $SIN);
}

17.4.3 Discussion

This script extracts important router information from a list of routers and presents that list in a semicolon-delimited file, as well as displaying it on the screen. It is a very simple script that just extracts MIB variables from a list of routers using snmpget, illustrating the value and versatility of SNMP.

This Perl script works by cycling through a list of routers. For each router, it uses SNMP get requests to extract the values of four variables: router name, location, contact, and serial number, as we did manually in Recipe 17.3. The core of this script is NET-SNMP's snmpget utility. NET-SNMP must be present on the server and the script expects to find snmpget in the /usr/local/bin directory.

This script contains three important variables that you have to set correctly before you can run this script. First, the variable $workingdir contains the directory in which the script resides. Next is the read-only SNMP community string, which is stored in the script's $snmpro variable. Substitute your organization's SNMP read-only community string for the example value shown (ORARO). Note that this script assumes that you use the same SNMP read-only community string on all routers.

The third variable, $rtrlist, contains the location of the router list. The example script uses a file called RTR_LIST, which is located in the working directory. You will need to change this variable to point to a file containing a list of routers on your network. The script expects this file to have a single router name per line.

The script produces two types of output. It displays the results on your screen while the script is executing. And the script also logs all results to a file called RESULT that resides in the working directory. The script automatically creates this file the first time you run it, and it overwrites the contents on each subsequent execution.

17.4.4 See Also

Recipe 17.3


  Previous section   Next section
Top