Book HomeEssential SNMPSearch this book

Appendix E. SNMP Support for Perl

Contents:

MIB Management Routines
SNMP Operations

This appendix summarizes Mike Mitchell's SNMP_util module, which we have used in our Perl scripts throughout this book. This module is distributed with Simon Leinen's SNMP Perl module; Mike's module, together with Simon's, can make SNMP programming a snap. You can get these modules from http://www.switch.ch/misc/leinen/snmp/perl/ or http://www.cpan.org.

Perl scripts need two use statements to take advantage of the SNMP Perl module:

use BER;
use SNMP_Session;
The BER and SNMP_Session modules make up the core of Simon's package. The SNMP_util module discussed in this appendix makes using this package a little easier. It requires only one use statement:

use SNMP_util;
Mike's package uses the other two modules, so it's not necessary to include all three in your scripts.

E.1. MIB Management Routines

The following sections describe a set of routines for working with MIBs.

E.1.1. snmpmapOID( )

The MIB objects in RFC 1213 (MIB-II) and RFC 2955 (Frame Relay) are preloaded by the routines in this package. This means that you can refer to a symbolic name like sysLocation.0 rather than to its numeric OID (.1.3.6.1.2.1.1.6 ). The snmpmapOID() routine allows you to add name-OID pairs to this map. The routine is used as follows:

snmpmapOID(text, OID, [text, OID...])
All the parameters are strings. text is the textual (or symbolic) name that you want to use and OID is the numeric object ID of the object to which the name refers. A single call to this routine may specify any number of name-OID pairs.

If snmpmapOID() fails it returns undef, so you can test for errors like this:

@return = snmpmapOID(..);
if(!@return) {
   # error
}

E.1.2. snmpMIB_to_OID( )

This routine takes the filename of a MIB as an argument. It reads and parses the MIB file and associates the object IDs defined by the MIB with their textual names. It returns the number of mappings it created. A return value of zero means that no mappings were created; -1 means an error occurred (i.e., it was unable to open the file). The routine is used as follows:

snmpMIB_to_OID(filename)

E.1.3. snmpLoad_OID_Cache( )

This routine allows you to map textual names to object IDs using a file. The file should consist of a number of lines in the form:

textual_name OID
This is much faster than calling snmpMIB_to_OID() because it doesn't require parsing a MIB file. The only argument to this routine is the name of the file that contains the preparsed data:

snmpLoad_OID_Cache(filename)
snmpLoad_OID_Cache() returns -1 if it can't open the file; a return value of 0 indicates success.

E.1.4. snmpQueue_MIB_File( )

This routine specifies a list of MIB files that will be used for mapping textual names to object IDs. If a name or OID can't be found in the internal map, each MIB file is parsed in turn until a match is found. The routine is used as follows:

snmpQueue_MIB_File(filename, [filename])


Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.