Using SNMP to Extract Inventory Information from a List of Routers

Problem

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

Solution

The following Perl script extracts important router informationsuch as router name, physical location, contact name, and serial number from a list of routersand 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%

The Perl code follows in Example 17-1.

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 behavior
$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		 Location			Contact		 Serial
";
printf LOG " Router		; Location			;Contact		 ;Serial
";
while () {
 chomp($rtr="$_");
 $snmpget="/usr/local/bin/snmpget -v1 c $snmpro $rtr ";
 $rtr=Q$snmpget .1.3.6.1.4.1.9.2.1.3.0Q;
 $loc=Q$snmpget .1.3.6.1.2.1.1.6.0Q;
 $con=Q$snmpget .1.3.6.1.2.1.1.4.0Q;
 $sin=Q$snmpget .1.3.6.1.4.1.9.3.6.3.0Q;
 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
", $RTR, $LOC, $CON, $SIN);
 printf LOG ("%-12.12s; %-30.30s; %-25.25s; %-12.12s
", $RTR, $LOC, $CON, $SIN);
}

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 value 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, which extracts SNMP data from the list of routers. NET-SNMP must be present on the server, and snmpget must reside in the /usr/local/bin directory before this script will function.

This script contains three important variables that you have to set correctly before it will run properly. 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, 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.

See Also

Recipe 17.3





Cisco IOS Cookbook
Cisco IOS Cookbook (Cookbooks (OReilly))
ISBN: 0596527225
EAN: 2147483647
Year: 2004
Pages: 505
Simiral book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net