5

This is the code that I used to walk through the table in net:snmp using perl:

      #! /usr/local/bin/perl

      use strict;
      use warnings;

      use Net::SNMP qw(:snmp);

      my $OID_hrSystem = '1.3.6.1.2.1.25.1';
      my $OID_ifPhysAddress = '1.3.6.1.2.1.2.2.1.6';        

      my ($session, $error) = Net::SNMP->session(
         -hostname    => shift || 'localhost',
         -community   => shift || 'public',
         -nonblocking => 1,
         -translate   => [-octetstring => 0],
         -version     => 'snmpv2c',
      );

      if (!defined $session) {
         printf "ERROR: %s.\n", $error;
         exit 1;
      }

      my %table; # Hash to store the results

      my $result = $session->get_bulk_request(
         -varbindlist    => [ $OID_hrSystem ],
         -callback       => [ \&table_callback, \%table ],
         -maxrepetitions => 10,
      );

      if (!defined $result) {
         printf "ERROR: %s\n", $session->error();
         $session->close();
         exit 1;
      }

      # Now initiate the SNMP message exchange.

      snmp_dispatcher();

      $session->close();

      # Print the results, specifically formatting ifPhysAddress.

      for my $oid (oid_lex_sort(keys %table)) {
         if (!oid_base_match($OID_ifPhysAddress, $oid)) {
            printf "%s = %s\n", $oid, $table{$oid};                 
         } else {                                   
            printf "%s = %s\n", $oid, unpack 'H*', $table{$oid};
         }                                      
      }

      exit 0;

      sub table_callback
      {
         my ($session, $table) = @_;

         my $list = $session->var_bind_list();

         if (!defined $list) {
            printf "ERROR: %s\n", $session->error();
            return;
         }

         # Loop through each of the OIDs in the response and assign
         # the key/value pairs to the reference that was passed with
         # the callback.  Make sure that we are still in the table
         # before assigning the key/values.

         my @names = $session->var_bind_names();
         my $next  = undef;

         while (@names) {
            $next = shift @names;
            if (!oid_base_match($OID_hrSystem, $next)) {
               return; # Table is done. chakri
            }
            $table->{$next} = $list->{$next};
         }

         # Table is not done, send another request, starting at the last
         # OBJECT IDENTIFIER in the response.  No need to include the
         # calback argument, the same callback that was specified for the
         # original request will be used.

         my $result = $session->get_bulk_request(
            -varbindlist    => [ $next ],
            -maxrepetitions => 10,
         );

         if (!defined $result) {
            printf "ERROR: %s.\n", $session->error();
         }

         return;
      }

Output is:

1.3.6.1.2.1.25.1.1.0 = 1 hour, 12:00.77

1.3.6.1.2.1.25.1.2.0 = �
                        +

1.3.6.1.2.1.25.1.3.0 = 1536

1.3.6.1.2.1.25.1.4.0 = BOOT_IMAGE=/boot/vmlinuz-3.0.0-14-generic root=UUID=5c4c8d22-3cea-4410-aaad-f297c75d217e ro quiet splash vt.handoff=7

1.3.6.1.2.1.25.1.5.0 = 1

1.3.6.1.2.1.25.1.6.0 = 133

1.3.6.1.2.1.25.1.7.0 = 0

But the required output for me is as follows:

hrSystemUptime.0 = 1:08:54.36

hrSystemDate.0 = 2011-12-14,16:0:2.0,+1:0

hrSystemInitialLoadDevice.0 = 1536

hrSystemInitialLoadParameters.0 = "BOOT_IMAGE=/boot/vmlinuz-3.0.0-14-generic root=UUID=5c4c8d22-3cea-4410-aaad-f297c75d217e ro quiet splash vt.handoff=7"

hrSystemNumUsers.0 = 1

hrSystemProcesses.0 = 133

hrSystemMaxProcesses.0 = 0

The main thing in the output is I want mib names to be printed in the output instead of the mib values

Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153
Cherry Chakri
  • 51
  • 1
  • 2

2 Answers2

1

You could use the SNMP module (available on Ubuntu as libsnmp-perl) which offers a tied hash to loaded MIBs, %SNMP::MIB. Here's some example code:

use SNMP;
SNMP::initMib();
print "$SNMP::MIB{'1.3.6.1.2.1.25.1.1.0'}{label} = \n";
#Should print "hrSystemUptime = "

Because %SNMP::MIB is a tied hash, you can't just do a lookup and assign to a lexical variable, i.e. my $oid = $SNMP::MIB{$oidstr}. You have to access it directly every time.

There is lots of other information that it loads from the MIB, including data type, which could help with the issue it looks like you have with hrSystemDate. Also, see the man page for mib_api if you need to load specific MIBs. The ones you used in your example loaded by default on my system, though.

bonsaiviking
  • 5,825
  • 1
  • 20
  • 35
0

have you tried the snmpget command on your server? When I run snmpget direcly on CLI, the result cames with the name:

Ex: /usr/local/bin/snmpget -O Q -v 2c -c Community x.x.x.x .1.3.6.1.2.1.31.1.1.1.6.100663301 IF-MIB::ifHCInOctets.100663301 = 152528664859348

If it works for you, you might want to exectute the command in the PERL code, instead of using the LIB. Then you just have to handle with the output.

Also, tou can use snmptranslate to tranlate your OIDs:

Ex: /usr/local/bin/snmptranslate 1.3.6.1.2.1.25.1.1

HOST-RESOURCES-MIB::hrSystemUptime

More Info -> http://www.net-snmp.org/wiki/index.php/TUT:snmptranslate

EDIT

Why don't you:

my $pathSnmpTranslate = '/your/path/to/snmptranslate';  

for my $oid (oid_lex_sort(keys %table)) {  
    my $oidTrans = `$pathSnmpTranslate $oid`;  
    if (!oid_base_match($OID_ifPhysAddress, $oid)) {  
        printf "%s = %s\n", $oidTrans, $table{$oid};  
    } else {    
        printf "%s = %s\n", $oidTrans, unpack 'H*',$table{$oid};  
    }  
 }  

On my machine it worked:

> /xxx % /usr/local/bin/snmptranslate 1.3.6.1.2.1.25.1.1
HOST-RESOURCES-MIB::hrSystemUptime

> /xxx % /usr/local/bin/snmptranslate 1.3.6.1.2.1.25.1.1.0
HOST-RESOURCES-MIB::hrSystemUptime.0

Fred
  • 31
  • 6