ergophobe has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on a program that uses the SNMP module to gather information, but I haven't found an abundance of information on using that module. In the below sub, I'm calling the SNMP module's get method, which returns values of various types. Sometimes those values are packed, sometimes not. Is there a way for me to determine if I need to unpack it and with what template?
sub snmpget { my $self = shift; my $target = shift; my $session; my $value; $session = new SNMP::Session ( DestHost => $self->host, Community => $self->community ); $value = $session->get($target) || warn "SNMP ERROR: $session->{E +rrorStr}"; $nom->debug (3, "SNMP: snmpget succeeded. Target: $target, Value: +$value"); return $value; }

Replies are listed 'Best First'.
Re: To unpack or not to unpack
by larryl (Monk) on Mar 14, 2001 at 22:54 UTC

    It's not clear from your code what format you're using for $target (the get() functions accepts a bunch of different input formats) - it might be easiest to use an SNMP::Varbind object as the input for get(). An SNMP::Varbind object is a blessed reference to an array with 5 elements:
    - object name (e.g. "sysdesc" or ".1.3.6.1.2.1.1.1"
    - dotted-decimal instance ID (or "0" for scalar objects)
    - value
    - value type (e.g. "IPADDR", "COUNTER", etc.)
    - timestamp

    You can set the name of the object, and get() will populate the rest of the info, which you can then test to figure out what format the return value is in. Assuming your input target is a string like "sysDescr" or ".1.3.6.1.2.1.1.1", you should be able to do something like:

    my $var = new SNMP::Varbind( [ $target ] ); my $value = $session->get($var) || warn "SNMP ERROR: $session->{ErrorStr}"; print "name: ", $var->name, "\n"; print "type: ", $var->type, "\n"; if ( $var->type eq "IPADDR" ) { ... unpack $var->val here ... } elsif ( $var->type eq "COUNTER" ) { ... unpack $var->val here ... } elsif ...

    The SNMP man page will list all the valid variable types that you might need to deal with.

Re: To unpack or not to unpack
by cleen (Pilgrim) on Mar 14, 2001 at 22:50 UTC
    I may sound really dumb here, but the stuff that you are reffering to as "packed" data may be data that is returned in hex? As in teh SYNTAX DEF of a (SNMP Specific) MIB. IE Integer32, IpAddress, Integer, Octet, Hex. There is no other way to match this type of data (besides regex) and unpack it without manually looking in the MIB def file for that specific oid.

    Or mabye Im looking at this wierd or somthing.