Section E.2. Net-SNMP


E.2. Net-SNMP

Note that when you download and build the source distribution for Net-SNMP, you must explicitly tell the configure command to install the Perl modules as well. You do so like this:

     $ ./configure --with-perl-modules 

The use statement for this module looks like the following:

     use SNMP; 

Beyond this, the actual usage of the Net-SNMP Perl module is broken into two categories, which are discussed next.

E.2.1. MIB Management Routines

By default, when you use this Perl module it will load all the MIB files located in the default location, /usr/local/share/snmp/mibs. This means you can use names like sysDescr and not have to remember the OIDs for these objects. But if you want to use some private MIB, there are routines that can help you with this. We'll discuss the two more commonly used ones.

E.2.1.1. &SNMP::loadModules(<mod>,...)

The &SNMP::loadModules( ) method is used to load a particular MIB file. If you simply copy your MIB file to the normal location (/usr/local/share/snmp/mibs), the Net-SNMP library will not automatically load it. It has an internal list of MIB files it knows about (these are installed when you build the Net-SNMP package). This routine can be used to load a specific MIB, list of MIBs, or all MIBs. For example, this line of code will load all MIB files, including any you copied to the default location:

     &SNMP::loadModules('ALL'); 

Alternatively, you could have invoked this routine like so:

     &SNMP::loadModules('IEEE802dot11-MIB'); 

This loads the 802.11 MIB we installed. How did we know to use IEEE802dot11-MIB? If you look at the top of any MIB file, you will see the BEGIN clause. For example, this line is at the top of the 802.11 MIB file:

     IEEE802dot11-MIB DEFINITIONS ::= BEGIN 

You just use the name of the MIB definition as the argument to the loadModules routine.

E.2.1.2. &SNMP::addMibDirs(<dir>,...)

The &SNMP::addMibDirs( ) routine allows you to add directories to be searched where other MIB files may belong. This is advantageous if you have private MIBs and either you want to store them in the default location, or you don't have write permission for the directory.

E.2.2. SNMP Operations

Unlike SNMP_Util, the Net-SNMP library requires a little more work to achieve the same goal. However, it is a flexible package that allows you a full range of control over your application. We'll present several SNMP applications that will highlight the basic usage of this package. To learn more about this module, you can install the Net-SNMP package and read the manual page for the module by running man SNMP.

E.2.2.1. snmpwalk

The following is a simple implementation of the snmpwalk command:

     #!/usr/bin/perl     use SNMP;     $SNMP::use_sprint_value = 1;     my $host = "localhost";     $sess = new SNMP::Session(  DestHost => $host,                                 Version => 3,                                 SecName => "kjs",                                 AuthProto => "MD5",                                 AuthPass => "mypassword",                                 PrivProto => "DES",                                 PrivPass => "myotherpassword",                                 SecLevel => "authPriv");     my $var = new SNMP::Varbind([]);     do {       my $val = $sess->getnext($var);       print "$var->[$SNMP::Varbind::tag_f].$var->[$SNMP::Varbind::iid_f] = ",             "$var->[$SNMP::Varbind::val_f]\n";     } until ($sess->{ErrorNum}); 

First off, we set $SNMP::use_sprint_value to 1. This forces the module to use Net-SNMP 's snprint_value library function, which helps make output a little more user friendly. Next we create a new SNMP session. There are many options you can pass to the constructor. In this example, we're creating an SNMPv3 session. If you wanted to use just SNMPv2, you could simply do the following:

     $sess = new SNMP::Session(  DestHost => $host,                                 Community => $comm,                                 Version => 2); 

The line $var = new SNMP::Varbind([]); creates an empty variable binding. This means we want to walk the entire MIB tree on the host. Next we go into a loop where we call getnext( ) on the newly created variable binding. We print the tag name for the OID we retrieved, its instance identifier (always 0 for scalar objects), and the value itself. We check to see if $sess->{ErrorNum} is set (which means we have reached the end of the MIB or some other failure has occurred). If no error has occurred, we do the getnext( ) again.

Here is a sample run of this program:

     $ ./mibwalk.pl     sysDescr.0 = Linux snort 2.4.7-10 #1 Thu Sep 6 17:27:27 EDT 2001 i686     sysObjectID.0 = linux     sysUpTimeInstance. = 0:0:51:06.71     sysContact.0 = Root <root@localhost>     sysName.0 = machine     sysLocation.0 = Kevin J. Schmidt     .... 

Note that the output has been cut short.

E.2.2.2. snmpget

Now let's look at how the simple SNMP get operation is implemented:

     #!/usr/bin/perl     use SNMP;     $SNMP::use_sprint_value = 1;     my $host = "localhost";     $sess = new SNMP::Session(  DestHost => $host,                                 Version => 3,                                 SecName => "kjs",                                 AuthProto => "MD5",                                 AuthPass => "mypassword",                                 PrivProto => "DES",                                 PrivPass => "myotherpassword",                                 SecLevel => "authPriv");     $var = new SNMP::VarList(['sysDescr',0],['sysUpTime',0]);     my @vars = $sess->get($var);     foreach (@vars) {         print "$_\n";     } 

The main difference here is the use of the SNMP::VarList command:

     $var = new SNMP::VarList(['sysDescr',0],['sysUpTime',0]); 

This allows us to specify one or more OIDs we wish to get. The format of each OID you pass to this routine is as follows:

     [object, iid] 

The value for iid depends on what the object is. If it's a simple scalar, use 0. Otherwise, you have a columnar object and iid will need to be the identifier or index for the object.

The call $sess->get($var); returns an array with each respective bucket set to the return value for each object in the order you specified them with the call to VarList.

E.2.2.3. snmpset

Here's a script that sets the sysName OID:

     #!/usr/bin/perl     use SNMP;     $SNMP::use_sprint_value = 1;     my $host = "localhost";     $sess = new SNMP::Session(  DestHost => $host,                                 Version => 3,                                 SecName => "kjs",                                 AuthProto => "MD5",                                 AuthPass => "mypassword",                                 PrivProto => "DES",                                 PrivPass => "myotherpassword",                                 SecLevel => "authPriv");     $var = new SNMP::Varbind(['sysName',0]);     my ($sysDescr) = $sess->get($var);     print "Old name: $sysDescr\n";     $sess->set(['sysName',0,"New Name","OCTETSTR"]);     my ($newSysDescr) = $sess->get($var);     print "New name: $newSysDescr\n";     my $setter = new SNMP::Varbind(['sysName',0,$sysDescr,"OCTETSTR"]);     $sess->set($setter);     my ($newSysDescr) = $sess->get($var);     print "Back to old name: $newSysDescr\n"; 

Note the line:

     $sess->set(['sysName',0,"New Name","OCTETSTR"]); 

Here we change sysName to the value "New Name". The set routine takes an extended form of the format we pass to the Varbind routine:

     [oid, iid, value, type] 

The oid and iid we already know about. value is whatever you want the oid to be changed to. type must be one of the following:


OBJECTID

Dotted-decimal (e.g., .1.3.6.1.2.1.1.1)


OCTETSTR

Perl scalar containing octets


INTEGER

Decimal signed integer (or enum)


NETADDR

Dotted-decimal


IPADDR

Dotted-decimal


COUNTER

Decimal unsigned integer


COUNTER64

Decimal unsigned integer


GAUGE

Decimal unsigned integer


UINTEGER

Decimal unsigned integer


TICKS

Decimal unsigned integer


OPAQUE

Perl scalar containing octets


NULL

Perl scalar containing nothing

Also note the lines:

     my $setter = new SNMP::Varbind(['sysName',0,$sysDescr,"OCTETSTR"]);     $sess->set($setter); 

This just shows that a Varbind object can be used as an argument to the set routine. Finally, here is the output from this script:

     $ ./set.pl     Old name: machine     New name: New Name     Back to old name: machine 




Essential SNMP
Essential SNMP, Second Edition
ISBN: 0596008406
EAN: 2147483647
Year: 2003
Pages: 165

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