Perl5 SNMP Extension Module for the UCD SNMP Library G.S. Marzot (gmarzot@baynetworks.com) Contents: Introduction: Availability: Supported Platforms: Installation: patch Operational Description: Trouble Shooting: Acknowledments: History: Introduction: This is a Perl5 extension module which provides an interface to the UCD SNMP client toolkit. The UCD SNMP Library is based on version 2.1.2.1 of the CMU SNMP Library and has incorporated many bugfixes and enhancements. For information on the UCD SNMP Library see the documentation provided with the UCD SNMP distribution. (NOTE: In previous releases this module was compatible with both the UCD and CMU SNMP libraries. Starting with Perl5/SNMP-1.7 this module will only work with the UCD SNMP Library 3.3 or later due to dependence on new features) Availability: The most recent release of the UCD SNMP Libray can be found at: ftp://ftp.ece.ucdavis.edu/pub/snmp/ucd-snmp.tar.gz The most recent release of the Perl5/SNMP module can be found at: ftp:/ftp.wellfleet.com/netman/snmp/perl5/SNMP.tar.gz (as well as the usual perl archives, CPAN) Supported Platforms: SunOS 4.1.x Solaris 2.x Linux 1.2.x, 2.0.x Many other UNIX variants Win95/NT ***NEW*** Installation: patch*/build/install ucd-snmp-3.3 libsnmp.a (e.g., /usr/local/lib) make sure header files are available someplace (e.g., /usr/local/include) after that it is basically perl Makefile.PL make make test make install *may not need patch in latest release of ucd-snmp ----ucd.patch---- diff -r -p ucd-snmp-3.3/snmplib/mib.c ucd-snmp-3.3.new/snmplib/mib.c *** ucd-snmp-3.3/snmplib/mib.c Wed Nov 5 17:00:09 1997 --- ucd-snmp-3.3.new/snmplib/mib.c Sat Nov 22 21:36:24 1997 *************** static void sprint_counter64 __P((char * *** 91,98 **** static void sprint_unknowntype __P((char *, struct variable_list *, struct enum_list *, char *, char *)); static void sprint_badtype __P((char *, struct variable_list *, struct enum_list *, char *, char *)); - static struct tree *get_symbol __P((oid *, int, struct tree *, char *)); - static struct tree *get_tree __P((oid *, int, struct tree *)); static char *get_description __P((oid *, int)); static int quick_print = 0; --- 91,96 ---- *************** extern void init_mib_internals __P((v *** 778,783 **** --- 776,786 ---- extern int which_module __P((char *)); /* from parse.c */ extern struct tree *find_tree_node __P((char *, int)); /* from parse.c */ + struct tree *get_tree_head __P((void)) + { + return(tree_head); + } + void init_mib __P((void)) { *************** fprint_value(f, objid, objidlen, variabl *** 1251,1257 **** fprintf(f, "%s\n", tempbuf); } ! static struct tree * get_symbol(objid, objidlen, subtree, buf) oid *objid; int objidlen; --- 1254,1260 ---- fprintf(f, "%s\n", tempbuf); } ! struct tree * get_symbol(objid, objidlen, subtree, buf) oid *objid; int objidlen; *************** lc_cmp(s1, s2) *** 1326,1332 **** /* * Clone of get_symbol that doesn't take a buffer argument */ ! static struct tree * get_tree(objid, objidlen, subtree) oid *objid; int objidlen; --- 1329,1335 ---- /* * Clone of get_symbol that doesn't take a buffer argument */ ! struct tree * get_tree(objid, objidlen, subtree) oid *objid; int objidlen; diff -r -p ucd-snmp-3.3/snmplib/mib.h ucd-snmp-3.3.new/snmplib/mib.h *** ucd-snmp-3.3/snmplib/mib.h Wed Oct 29 15:43:38 1997 --- ucd-snmp-3.3.new/snmplib/mib.h Sat Nov 22 21:36:24 1997 *************** void print_description __P((oid *, int)) *** 272,274 **** --- 272,277 ---- void fprint_description __P((FILE *, oid *, int)); int get_module_node __P((char *, char *, oid *, int *)); int get_node __P((char *, oid *, int *)); + struct tree *get_symbol __P((oid *, int, struct tree *, char *)); + struct tree *get_tree __P((oid *, int, struct tree *)); + struct tree *get_tree_head __P((void)); diff -r -p ucd-snmp-3.3/snmplib/snmp.h ucd-snmp-3.3.new/snmplib/snmp.h *** ucd-snmp-3.3/snmplib/snmp.h Wed Oct 29 15:43:38 1997 --- ucd-snmp-3.3.new/snmplib/snmp.h Sat Nov 22 21:36:24 1997 *************** SOFTWARE. *** 145,150 **** --- 145,152 ---- /* in SNMPv2c, SNMPv2u, SNMPv2*, and SNMPv3 PDUs */ #define SNMP_ERR_INCONSISTENTNAME (18) + #define MAX_SNMP_ERR 18 + /* values of the generic-trap field in trap PDUs */ #define SNMP_TRAP_COLDSTART (0) #define SNMP_TRAP_WARMSTART (1) diff -r -p ucd-snmp-3.3/snmplib/snmp_api.c ucd-snmp-3.3.new/snmplib/snmp_api.c *** ucd-snmp-3.3/snmplib/snmp_api.c Wed Nov 12 13:38:36 1997 --- ucd-snmp-3.3.new/snmplib/snmp_api.c Sat Nov 22 21:36:24 1997 *************** static char *api_errors[-SNMPERR_MAX+1] *** 224,229 **** --- 224,230 ---- "Bad Party definition", "Session abort failure", "Unknown PDU type", + "Timeout", }; struct timeval Now; *************** void snmp_set_dump_packet(val) *** 259,264 **** --- 260,270 ---- int snmp_get_dump_packet __P((void)) { return snmp_dump_packet; + } + + int snmp_get_errno __P((void)) + { + return snmp_errno; } void diff -r -p ucd-snmp-3.3/snmplib/snmp_api.h ucd-snmp-3.3.new/snmplib/snmp_api.h *** ucd-snmp-3.3/snmplib/snmp_api.h Wed Oct 29 15:43:38 1997 --- ucd-snmp-3.3.new/snmplib/snmp_api.h Sat Nov 22 21:36:24 1997 *************** extern void snmp_set_detail __P((char *) *** 129,135 **** #define SNMPERR_BAD_PARTY (-21) #define SNMPERR_ABORT (-22) #define SNMPERR_UNKNOWN_PDU (-23) ! #define SNMPERR_MAX (-23) #define non_repeaters errstat #define max_repetitions errindex --- 129,136 ---- #define SNMPERR_BAD_PARTY (-21) #define SNMPERR_ABORT (-22) #define SNMPERR_UNKNOWN_PDU (-23) ! #define SNMPERR_TIMEOUT (-24) ! #define SNMPERR_MAX (-24) #define non_repeaters errstat #define max_repetitions errindex *************** void snmp_set_dump_packet __P((int)); *** 295,297 **** --- 296,299 ---- int snmp_get_dump_packet __P((void)); void snmp_set_quick_print __P((int)); int snmp_get_quick_print __P((void)); + int snmp_get_errno __P((void)); diff -r -p ucd-snmp-3.3/snmplib/snmp_client.c ucd-snmp-3.3.new/snmplib/snmp_client.c *** ucd-snmp-3.3/snmplib/snmp_client.c Wed Nov 12 13:38:36 1997 --- ucd-snmp-3.3.new/snmplib/snmp_client.c Sat Nov 22 21:36:24 1997 *************** snmp_synch_input(op, session, reqid, pdu *** 178,183 **** --- 178,184 ---- } else if (op == TIMED_OUT){ state->pdu = NULL; state->status = STAT_TIMEOUT; + snmp_errno = SNMPERR_TIMEOUT; } return 1; } *************** snmp_synch_setup(session) *** 403,414 **** } char *error_string[18] = { ! "No Error", ! "Response message would have been too large.", ! "There is no such variable name in this MIB.", ! "The value given has the wrong type or length.", ! "The two parties used do not have access to use the specified SNMP PDU.", ! "A general failure occured", "noAccess", "wrongType", "wrongLength", --- 404,415 ---- } char *error_string[18] = { ! "noError", ! "tooBig", /* Response message would have been too large */ ! "noSuchName", /* There is no such variable name in this MIB */ ! "badValue", /* The value given has the wrong type or length */ ! "readOnly", /* The two parties do not have access to the specified PDU ?", ! "genError", /* A general failure occured */ "noAccess", "wrongType", "wrongLength", *************** char *error_string[18] = { *** 420,433 **** "commitFailed", "undoFailed", "authorizationError", ! "notWritable" }; char * snmp_errstring(errstat) int errstat; { ! if (errstat <= SNMP_ERR_NOTWRITABLE && errstat >= SNMP_ERR_NOERROR){ return error_string[errstat]; } else { return "Unknown Error"; --- 421,435 ---- "commitFailed", "undoFailed", "authorizationError", ! "notWritable", ! "inconsistentName" }; char * snmp_errstring(errstat) int errstat; { ! if (errstat <= MAX_SNMP_ERR && errstat >= SNMP_ERR_NOERROR){ return error_string[errstat]; } else { return "Unknown Error"; diff -r -p ucd-snmp-3.3/win32/libsnmp_dll/libsnmp.def ucd-snmp-3.3.new/win32/libsnmp_dll/libsnmp.def *** ucd-snmp-3.3/win32/libsnmp_dll/libsnmp.def Wed Nov 12 23:27:11 1997 --- ucd-snmp-3.3.new/win32/libsnmp_dll/libsnmp.def Sun Nov 23 14:19:28 1997 *************** EXPORTS *** 41,46 **** --- 41,47 ---- print_mib print_ascii_dump read_objid + get_tree_head init_mib init_mib_internals print_variable *************** EXPORTS *** 54,59 **** --- 55,62 ---- get_module_node get_node find_node + get_symbol + get_tree read_module read_mib read_all_mibs *************** EXPORTS *** 88,93 **** --- 91,97 ---- snmp_free_var snmp_select_info snmp_timeout + snmp_get_errno snmp_set_dump_packet snmp_get_dump_packet snmp_set_quick_print *************** EXPORTS *** 97,102 **** --- 101,107 ---- snmp_pdu_create snmp_fix_pdu snmp_clone_pdu + snmp_api_errstring snmp_errstring snmp_synch_setup snmp_synch_response ------ Operational Description: The basic operations offered by SNMP are provided through an object oriented interface for modularity and ease of use. The primary class is SNMP::Session which is implemented as a blessed hash reference. This class supplies 'get', 'getnext', 'set', 'fget', and 'fgetnext' method calls. All calls are currently blocking/synchronous (i.e., they must receive a response or timeout before control is returned to the caller - an asyncronous implementation is very high on the todo list). The methods take a variety of input argument formats (see below). A description of the fields which can be initialized/overriden when a SNMP::Session object is created follows: SNMP::Session public: DestHost - default 'localhost', hostname or ip addr of SNMP agent Community - default 'public', SNMP community string (used for both R/W) Version - default '1', [2 (same as 2c), 2c, 3 (not implemented)] RemotePort - default '161', allow remote UDP port to be overriden Timeout - default '1000000', micro-seconds before retry Retries - default '5', retries before failure RetryNoSuch - default '1', 'get' request NOSUCH errors in the pdu will be repaired, removing the varbind in error, and resent - undef will be returned for all NOSUCH varbinds, setting to '0' disables this feature and the entire get request will fail on any NOSUCH error (applies to v1 only) VarFormats - default 'undef', used by 'fget[next]', holds an hash reference of output value formatters, (e.g., { => , ... }, must match the and format used in the get operation. A special , '*', may be used to apply all s, the supplied sub is called to translate the value to a new format. The sub is called passing the Varbind as the arg TypeFormats - default 'undef', used by 'fget[next]', holds an hash reference of output value formatters, (e.g., { => , ... }, the supplied sub is called to translate the value to a new format, unless a VarFormat mathces first (e.g., $session->{TypeFormats}{INTEGER} = \&mapEnum();) UseLongNames - defaults to the value of SNMP::use_long_names at time of session creation. set to non-zero to have for 'getnext' methods generated preferring longer Mib name convention (e.g., system.sysDescr vs just sysDescr) UseSprintValue - defaults to the value of SNMP::use_sprint_value at time of session creation. set to non-zero to have return values for 'get' and 'getnext' methods formatted with the libraries sprint_value function. This will result in certain data types being returned in non-canonical format NOTE: values returned with this option set may not be appropriate for 'set' operations (see discussion value formats in description section) UseEnums - defaults to the value of SNMP::use_enums at time of session creation. set to non-zero to have integer return values converted to enumeration identifiers if possible ErrorStr - read-only, holds the error message assoc. w/ last request ErrorNum - read-only, holds the snmp_err or staus of last request ErrorInd - read-only, holds the snmp_err_index when appropriate private: DestAddr - internal field used to hold the translated DestHost field SessPtr - internal field used to cache a created session structure methods: new() - Constructs a new SNMP::Session object. The fields are passed to the contructor as a hash list (e.g., $session = new SNMP::Session(DestHost => 'foo', Community => 'private');), returns an object reference or undef in case of error. get() - do SNMP GET, multiple formats accepted, updates with value(s) and type(s), returns retrieved value(s) fget() - do SNMP GET like 'get' and format the values according the handlers specified in $sess->{VarFormats} and $sess->{TypeFormats} getnext() - do SNMP GETNEXT, multiple formats accepted, returns retrieved value(s), passed as arguments are updated to indicate next lexicographical ,,, and NOTE: simple scalar form is not updated fgetnext()- do SNMP GETNEXT like getnext and format the values according the handlers specified in $sess->{VarFormats} and $sess->{TypeFormats} set() - do SNMP SET, multiple formats accepted, the value field in all formats must be in a canonical format (i.e., well known format) to ensure unambiguous translation to SNMP MIB data value (see discussion of canonical value format description section), returns snmp_errno getbulk(, , ) - * Not Implemented * may be one of the following forms: SNMP::VarList: - represents an array of MIB objects to get/set, implemented as a blessed reference to an array of SNMP::Varbinds, (e.g., [, , ...]) SNMP::Varbind: - represents a single MIB object to get or set, implemented as a blessed reference to a 4 element array; [, , , ]. - one of the following forms: 1) leaf identifier (e.g., 'sysDescr') assumed to be unique for practical purposes 2) fully qualified identifier (e.g., '.iso.org.dod.internet.mgmt.mib-2.system.sysDescr') 3) fully qualified, dotted-decimal, numeric OID (e.g., '.1.3.6.1.2.1.1.1') - the dotted-decimal, instance identifier. for scalar MIB objects use '0' - the SNMP data value retrieved from or being set to the agents MIB. for (f)get(next) operations may have a variety of formats as determined by session and package settings. However for set operations the format must be canonical to ensure unambiguous translation. The canonical forms are as follows: 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, - SNMP data type (see list above), this field is populated by 'get' and 'getnext' operations. In some cases the programmer should populate this field when passing to a 'set' operation. this field need not be supplied when attribute indicated by is already described by loaded Mib modules. for 'set's, if a numeric OID is used and the object is not currently in the loaded Mib, the fileds must be supplied simple scalar - light weight form of used to 'set' or 'get' a single attribute without constructing an SNMP::Varbind. stored in a perl scalar, has the form '.', (e.g., 'sysDescr.0'). for 'set' operations the value is passed as a second arg. NOTE: This argument form is not updated in get[next] operations as are the other forms. SNMP package variables and functions: $SNMP::VERSION - the current version specifier (e.g., 1.7) $SNMP::auto_init_mib - default '1', set to 0 to disable automatic reading of the MIB upon session creation. set to non-zero to call initMib at session creation which will result in MIB loading according to UCD env. variables (see man mib_api) $SNMP::verbose - default '0', verbosity level, 0 => no output $SNMP::use_long_names - default '0', set to non-zero to enable the use of longer Mib identifiers. see translateObj. will also influence the formatting of in varbinds returned from 'getnext' operations. Can be set on a per session basis (UseLongNames) $SNMP::use_sprint_value - default '0', set to non-zero to enable formatting of response values using the snmp libraries sprint_value function. can also be set on a per session basis (see UseSprintValue) NOTE: returned values may not be suitable for 'set' operations $SNMP::use_enums - default '0',set non-zero to return values as enums and allow sets using enums where appropriate. integer data will still be accepted for set operations. can also be set on a per session basis (see UseEnums) &SNMP::setMib() - allows dynamic (re)parsing of the mib and explicit specification of mib file independent of enviroment variables. called with no args acts like initMib, loading MIBs indicated by environment variables (see ucd mib_api docs). passing non-zero second arg forces previous mib to be freed and replaced (second arg not working since freeing previous Mib is more involved than before). &SNMP::initMib() - calls library init_mib function if Mib not already loaded - does nothing if Mib already loaded. will pasre directories and load modules according to environment variables described in UCD documentations. (see man mib_api, MIBDIRS, MIBS, MIBFILE(S), etc.) &SNMP::addMibDirs(,...) - calls library add_mibdir for each directory supplied. will cause directory(s) to be added to internal list and made available for searching in subsequent loadModules calls &SNMP::addMibFiles(,...) - calls library read_mib function. The file(s) supplied will be read and all Mib module definitions contained therein will be added to internal mib tree structure &SNMP::loadModules(,...) - calls library read_module function. The module(s) supplied will be searched for in the current mibdirs and and added to internal mib tree structure. Passing special , 'ALL', will cause all known modules to be loaded. &SNMP::unloadModules(,...) - *Not Implemented* &SNMP::translateObj() - will convert a text object tag to an OID and vice-versa. iid suffix is retained numerically. default behaviour when converting a numeric OID to text form is to return leaf indentifier only (e.g.,'sysDescr') but when $SNMP::use_long_names is non-zero the whole identifier will be returned &SNMP::getType() - return SNMP data type for given textual identifier OBJECTID, OCTETSTR, INTEGER, NETADDR, IPADDR, COUNTER GAUGE, TIMETICKS, OPAQUE, or undef &SNMP::mapEnum() - converts integer value to enumertion tag defined in Mib Trouble Shooting: If problems occur there are number areas to look at to narrow down the possibilities. The first step should be to test the UCD installation independent from the Perl5/SNMP interface. Try running the apps from the UCD distribution. Ensure that your MIBs are installed and enviroment variables are set appropriately (see man mib_api) If the problem occurs during compilation/linking check that the snmp library being linked is actually the UCD library (there have been name conflicts with existing snmp libs). Also check that the header files are correct and up to date. Sometimes compiling the UCD snmp library with 'position-independent-code' enabled is required. if you cannot resolve the problem you can email me at gmarzot@baynetworks.com and/or ucd-snmp-coders@ece.ucdavis.edu. and maybe I can help. please give sufficient information to analyze the problem (OS type, versions for OS/Perl/UCD/compiler, complete error output, etc.) Acknowledments: Many thanks to all those who supplied patches, suggestions and feedback. Wes Hardaker and the ucd-coders Dave Perkins Marcel Wiget David Blackburn John Stofell Gary Hayward Claire Harrison Achim Bohnet Doug Kingston Jacques Vidrine Perl5 Porters Apologies to any/all who's patch/feature/request was not mentioned or included - most likely it was lost when paying work intruded on my fun. Please try again if you do not see a desired feature. This may actually turn out to be a decent package with such excellent help and the fact that I have more time to work on it than in the past. bugs, comments, questions to gmarzot@baynetworks.com ------ History: Bugs fixed and changes in 1.7 1) fixed seg fault on use of unknown/unparsed attribute in Varbind or passed to translateObj 2) fixed truncation of last char of attribute name in translateObj 3) handles variable args to setMib without complaint 4) added SNMP::getType to query data type of a given attribute and extended Varbind structure to return type wherever possible 5) added RemotePort to SNMP::Session initialation list to allow override of port 161 6) removed noisy announcement of mib parse success unless verbose is set *WARNING* *this may break existing scripts* 7) changed return format for IpAddress and ObjID data types - these are now always returned as dotted decimal strings as opposed to the packed binary forms in 1.6 *WARNING* *this may break existing scripts* 8) Session now sets ErrorInd (e.g., $session->{ErrorInd}) where appropriate 9) Support for ucd-snmp-3.2 (and greater) style of Mib loading 10) Fully qualified attriubute names and numeric OIDs are now valid defintions. 11) Numeric OIDs can be used even if they have not been parsed in the current Mib - Mib loading is now optional 12) Support for Win32 perl 13) Updated docs and examples 14) Reworked/extended the test harness to use the perl t/* facility (thanks to jfs@fluent.com) 15) fixed up error handling to be more consistent with library and more useful in general. Now returns both library API errors and snmp protocol error numbers and strings. 16) added per object and per type formatting of returned values - more control of value formatting with UseEnums and UseSprintValue