in reply to Re: SNMP from script conflicts with snmpwalk
in thread SNMP from script conflicts with snmpwalk

------> ($q, $num_jobs) = $session->getnext($vars);
Duh, I knew it was something trivial. Yeah, I updated the line with

($q, $num_jobs, $share_comment) = $session->getnext($vars);

and it works,i.e. it shows the values I expect. I think I had changed it earlier in the code, but forgot to change this line, too.

Replies are listed 'Best First'.
Re^3: SNMP from script conflicts with snmpwalk
by monarch (Priest) on Nov 15, 2005 at 00:35 UTC
    If your IP addresses are getting jumbled up you now have to ask yourself: "are all my values that are returned on the same row?"

    If it is at all possible that particular rows have a field missing then your individual column getnexts are going to get out of sync.. you may be retrieving share name for row 12 while retrieving comment for row 15.

    To guard against this you are going to have to harvest the last integer from the OID of each column and ensure they are all the same. (The last OID of each column is the row number, or more accurately, the index of the row.. actually the index might be an IP address and consist of many integers.. The only right way to obtain the row number is to strip off the leading digits that match the OID of the variable you are getting - confused yet?).

    Whilst SNMP is a simple protocol to implement, it's not so simple to use.. you have my empathy.

      I see. So, even though the output from snmpwalk turns out OK, I still get erroneous results when autointerpreting MIB values. So for that reason, it would be safe to use OIDs instead names? If so, it seems odd that snmpwalk returns what I expect, but perl returns another.

      For example, when I run the script, I get:

      q - "A6_1640_cn", comment - q - "D1_319_CN3", comment - "" q - "130_1016_4+", comment - "61.134" q - "130_1035_4+", comment - "61.116"
      So, it seems that the script is seeing blank values for the first two queues, and is getting messed up. However, when I check those values using snmpwalk, things are what I expect:

      [burvil@localhost printer-list]$ foreach p ( A6_1640_cn D1_319_CN3 ) foreach? snmpwalk server-ip-address public .1.3.6.1.4.1.77.1.2.27.1.1 +| egrep $p foreach? end enterprises.77.1.2.27.1.1.10.65.54.95.49.54.52.48.95.99.110 = "A6_1640 +_cn" enterprises.77.1.2.27.1.1.10.68.49.95.51.49.57.95.67.78.51 = "D1_319_C +N3" [burvil@localhost printer-list]$ foreach p ( 10.65.54.95.49.54.52.48.9 +5.99.110 10.68.49.95.51.49.57.95.67.78.51) foreach? snmpwalk server-ip-address public .1.3.6.1.4.1.77.1.2.27.1.3 +| egrep $p foreach? end enterprises.77.1.2.27.1.3.10.65.54.95.49.54.52.48.95.99.110 = "57.13" enterprises.77.1.2.27.1.3.10.68.49.95.51.49.57.95.67.78.51 = "61.88" [burvil@localhost printer-list]$
      Somewhat annoying, since one of the main reasons I chose SNMP over Net::SNMP or SNMP_Session was because it could parse MIBs, allowing me to write (in theory at least) more maintainable code.
        I haven't used SNMP, I've only ever used Net::SNMP myself..

        But it appears that you must be calling getnext in an invalid manner. Try breaking down the problem a little.

        Firstly, use 'getnext' on 'svPrintQName' only. Then identify the row number that is returned to you (in the above examples you were returned 10.65.54.95.49.54.52.48.95.99.110 and 10.68.49.95.51.49.57.95.67.78.51). Then perform a 'get' on the 'svShareComment' object with the rowID that you just extracted from the result of 'getnext svPrintQName'..

        The snmpwalk command line tool is showing you that the SNMP Agent you're querying has the results you want, so it's a matter of digging through the documentation you have for the module you're using and learning how to use it correctly.

Re^3: SNMP from script conflicts with snmpwalk
by bowei_99 (Friar) on Nov 15, 2005 at 00:23 UTC
    Actually, the comments show up as IP addresses expected, but they are jumbled up, i.e. different values from what I see in Windows.... Hmm... the values from snmpwalk match with what's seen through the Windows share - perhaps something with this module?

    This is what the code looks like now. Only made a couple changes - since I only need the queue name and comments, I only collect that ...

    sub QuerySNMP { use SNMP; #$SNMP::debugging = 2; $ENV{'MIBFILES'} = "LanMgr-Mib-II-MIB.my"; my ($hostname) = shift @_; my ($session) = new SNMP::Session( DestHost => $hostname, Community => "public", UseSprintValue => 1, ); if (!(defined($session))) { die "Session creation error: $SNMP::Session::ErrorStr + - $!\n" ; } my ($vars) = new SNMP::VarList(['svPrintQName'], ['s +vShareComment '], ); my ($q, $num_jobs, $share_comment) = $session->getnext($vars) +; if ($session->{ErrorStr}) {; die "Getnext failed - error: " . $session->{ErrorStr} +. " - $!\n" } while (!$session->{ErrorStr} ) { print "q - $q, comment - $share_comment\n"; my ($q, $share_comment) = $session->getnext($vars); } }