By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,692 Members | 1,998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,692 IT Pros & Developers. It's quick & easy.

Replace HP OV SNMP call to Perl SNMP API

P: 4
how will I change the code the 1st code according to the 2nd one?
1st code
Expand|Select|Wrap|Line Numbers
  1. sub snmpGet(@)
  2. {
  3.     # Assign arguments
  4.     my ($deviceName)=$_[0];     # Device name to read from
  5.     my ($community)=$_[1];      # SNMP Community String
  6.     my ($timeout)=$_[2];        # Timeout value for SNMP-GET request
  7.     my ($type)=$_[3];           # SNMP Object Type
  8.     my ($oid)=$_[4];            # SNMP Object ID
  9.     my ($version)=$_[5];        # SNMP Version
  10.     my ($port)=$_[6];           # Port
  11.  
  12.     # Local variables
  13.     my ($result);               # Output from SNMP-GET
  14.  
  15.  
  16.     # Set default version and port
  17.     if ($version) { $version='-v ' . $version; } else { $version=''; }
  18.     if ($port) { $port='-p ' . $port; } else { $port=''; }
  19.  
  20.     # Check parameters
  21.     unless ($deviceName && $community && $timeout && $type && $oid)
  22.     {
  23.         # Missing parameter
  24.         rxnnmsLogError("${snmpErrorPrefix}snmpGet:missing parameter:snmpGet($deviceName,$community,$timeout,$type,$oid");
  25.         return;
  26.     }
  27.  
  28.     # Check for valid type
  29.     unless ($type eq $snmpIntegerType || $type eq $snmpIPAddressType ||
  30.             $type eq $snmpOctetStringType || $type eq $snmpGauge32Type ||
  31.             $type eq $snmpCounter64Type || $type eq $snmpTimeTicksType)
  32.     {
  33.         rxnnmsLogError("${snmpErrorPrefix}snmpGet:invalid type:$type");
  34.         return;
  35.     }
  36.  
  37.     # Generate the SNMP-GET using syntax of underlying SNMP implementation.
  38.     rxnnmsLogTrace("$snmpget $version $port -c $community -t $timeout $deviceName $oid");
  39.     $result=`$snmpget $version $port -c $community -t $timeout $deviceName $oid 2>&1`;
  40.  
  41.     # Trace output of snmpget command
  42.     chomp($result);
  43.     rxnnmsLogTrace("$snmpget $oid to $deviceName returned: $result");
  44.  
  45.    # If $type is 'integer' and $result contains 'INTEGER' or 'Unsigned' then SNMP-GET ran
  46.     # successfully, so return $result, etc. Otherwise return '$snmpErrorPrefix$result'
  47.     # (bad status).
  48.  
  49.     if (($type=$snmpIntegerType && $result=~/INTEGER/) ||
  50.         ($type=$snmpIntegerType && $result=~/Unsigned/) ||
  51.         ($type=$snmpIPAddressType && $result=~/IpAddress/) ||
  52.         ($type=$snmpOctetStringType && $result=~/DISPLAY STRING/) ||
  53.         ($type=$snmpOctetStringType && $result=~/OCTET STRING/) ||
  54.         ($type=$snmpGauge32Type && $result=~/Gauge32/) ||
  55.         ($type=$snmpCounter64Type && $result=~/Counter64/) ||
  56.         ($type=$snmpTimeTicksType && $result=~/Timeticks/))
  57.     {
  58.         # SNMP-GET successful. Return output.
  59.         rxnnmsLogTrace("$snmpget $oid from $deviceName was successful");
  60.         return($result);
  61.     }
  62.     else
  63.     {
  64.         # SNMP-GET failed. Return error.
  65.         rxnnmsLogTrace("$snmpget $oid from $deviceName failed: $result");
  66.         return("$snmpErrorPrefix$result");
  67.     }
  68.  
  69. } # End of snmpGet
  70.  
2nd code
Expand|Select|Wrap|Line Numbers
  1. sub snmpget ($@) {
  2.   my($host, @vars) = @_;
  3.   my(@enoid, $var, $response, $bindings, $binding, $value, $oid, @retvals);
  4.   my $session;
  5.  
  6.   $session = &snmpopen($host, 0, \@vars);
  7.   if (!defined($session)) {
  8.     carp "SNMPGET Problem for $host\n"
  9.       unless ($SNMP_Session::suppress_warnings > 1);
  10.     return undef;
  11.   }
  12.  
  13.   @enoid = &toOID(@vars);
  14.   return undef unless defined $enoid[0];
  15.  
  16.   if ($session->get_request_response(@enoid)) {
  17.     $response = $session->pdu_buffer;
  18.     ($bindings) = $session->decode_get_response($response);
  19.     while ($bindings) {
  20.       ($binding, $bindings) = decode_sequence($bindings);
  21.       ($oid, $value) = decode_by_template($binding, "%O%@");
  22.       my $tempo = pretty_print($value);
  23.       push @retvals, $tempo;
  24.     }
  25.     return(@retvals);
  26.   }
  27.   $var = join(' ', @vars);
  28.   carp "SNMPGET Problem for $var on $host\n"
  29.     unless ($SNMP_Session::suppress_warnings > 1);
  30.   return undef;
  31. }
  32.  
Sep 19 '08 #1
Share this Question
Share on Google+
7 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
First, you need to please use code tags around any code you post into the forums. There is a sample in the "Reply Guidelines" next to your reply window.

Also, you need to please better explain what it is you are looking for or to do as I don't know what you want.

Regards,

Jeff (Moderator)

NOTE:: I have deleted your second AND third posts as they are duplicates of this one. Just because you have a short timeline and what you are working on is urgent, please know that that urgency does not translate into an urgency for us. We are all volunteers here and are not under your same time crunch. Please DO NOT double post your questions just because you do not get an answer as quick as you expect.
Sep 19 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
I don't even understand what the question/problem is, so there is no way to begin to answer it.
Sep 19 '08 #3

P: 4
hi,
First of all the 2nd and third posts were different codes from the 1st post.They were not the same codes.I value your and mine time.
The 1st code is from a HP OV CommonSnmp.pm.The 2nd code is from snmp_util.pm.
I just wanted the 1st code to be transparent to the second code. i.e. the parameters that are passed by the 1st code must be same as the 2nd code. Also the operations of the 1st code to be same as the 2nd code. So that when I use CommonSnmp.pm it works alike like snmp_util.pm.
Sep 22 '08 #4

P: 4
Also the 2nd code has called the subroutines like snmpopen & toOID.the definations are as follows:

Expand|Select|Wrap|Line Numbers
  1. sub snmpopen  ($$$) {
  2.   my($host, $type, $vars) = @_;
  3.   my($nhost, $port, $community, $lhost, $lport, $nlhost);
  4.   my($timeout, $retries, $backoff, $version);
  5.   my $v4onlystr;
  6.  
  7.   $type = 0 if (!defined($type));
  8.   $community = "public";
  9.   $nlhost = "";
  10.  
  11.   ($community, $host) = ($1, $2) if ($host =~ /^(.*)@([^@]+)$/);
  12.  
  13.   # We can't split on the : character because a numeric IPv6
  14.   # address contains a variable number of :'s
  15.   my $opts;
  16.  if( ($host =~ /^(\[.*\]):(.*)$/) || ($host =~ /^(\[.*\])$/) ) {
  17.     # Numeric IPv6 address between []
  18.     ($host, $opts) = ($1, $2);
  19.   } else {
  20.     # Hostname or numeric IPv4 address
  21.     ($host, $opts) = split(':', $host, 2);
  22.   }
  23.   ($port, $timeout, $retries, $backoff, $version, $v4onlystr) = split(':', $opts, 6)
  24.     if(defined($opts) && (length $opts > 0) );
  25.  
  26.   undef($version) if (defined($version) && length($version) <= 0);
  27.   $v4onlystr = "" unless defined $v4onlystr;
  28.   $version = '1' unless defined $version;
  29.   if (defined($port) && ($port =~ /^([^!]*)!(.*)$/)) {
  30.     ($port, $lhost) = ($1, $2);
  31.     $nlhost = $lhost;
  32.     ($lhost, $lport) = ($1, $2) if ($lhost =~ /^(.*)!(.*)$/);
  33.     undef($lhost) if (defined($lhost) && (length($lhost) <= 0));
  34.     undef($lport) if (defined($lport) && (length($lport) <= 0));
  35.   }
  36.   undef($port) if (defined($port) && length($port) <= 0);
  37.   $port = 162 if ($type == 1 && !defined($port));
  38.   $nhost = "$community\@$host";
  39.   $nhost .= ":" . $port if (defined($port));
  40.  
  41.   if ((!defined($SNMP_util::Session))
  42.     || ($SNMP_util::Host ne $nhost)
  43.     || ($SNMP_util::Version ne $version)
  44.     || ($SNMP_util::LHost ne $nlhost)
  45.     || ($SNMP_util::IPv4only ne $v4onlystr)) {
  46.     if (defined($SNMP_util::Session)) {
  47.       $SNMP_util::Session->close();
  48.       undef $SNMP_util::Session;
  49.       undef $SNMP_util::Host;
  50.       undef $SNMP_util::Version;
  51.       undef $SNMP_util::LHost;
  52.       undef $SNMP_util::IPv4only;
  53.     }
  54.     $SNMP_util::Session = ($version =~ /^2c?$/i)
  55.       ? SNMPv2c_Session->open($host, $community, $port, undef,
  56.                 $lport, undef, $lhost, ($v4onlystr eq 'v4only') ? 1:0 )
  57.       : SNMP_Session->open($host, $community, $port, undef,
  58.                 $lport, undef, $lhost, ($v4onlystr eq 'v4only') ? 1:0 );
  59.     ($SNMP_util::Host = $nhost, $SNMP_util::Version = $version,
  60.       $SNMP_util::LHost = $nlhost, $SNMP_util::IPv4only = $v4onlystr) if defined($SNMP_util::Session);
  61.   }
  62.  
  63.   if (defined($SNMP_util::Session)) {
  64.     if (ref $vars->[0] eq 'HASH') {
  65.       my $opts = shift @$vars;
  66.       foreach $type (keys %$opts) {
  67.     if ($type eq 'return_array_refs') {
  68.       $SNMP_util::Return_array_refs = $opts->{$type};
  69.     }
  70.     else {
  71.       if (exists $SNMP_util::Session->{$type}) {
  72.         if ($type eq 'timeout') {
  73.           $SNMP_util::Session->set_timeout($opts->{$type});
  74.         } elsif ($type eq 'retries') {
  75.           $SNMP_util::Session->set_retries($opts->{$type});
  76.         } elsif ($type eq 'backoff') {
  77.           $SNMP_util::Session->set_backoff($opts->{$type});
  78.         } else {
  79.           $SNMP_util::Session->{$type} = $opts->{$type};
  80.         }
  81.       } else {
  82.         carp "SNMPopen Unknown SNMP Option Key '$type'\n"
  83.           unless ($SNMP_Session::suppress_warnings > 1);
  84.       }
  85.     }
  86.       }
  87.     }
  88.     $SNMP_util::Session->set_timeout($timeout)
  89.       if (defined($timeout) && (length($timeout) > 0));
  90.     $SNMP_util::Session->set_retries($retries)
  91.       if (defined($retries) && (length($retries) > 0));
  92.     $SNMP_util::Session->set_backoff($backoff)
  93.       if (defined($backoff) && (length($backoff) > 0));
  94.   }
  95.   return $SNMP_util::Session;
  96. }
  97.  
  98. sub toOID(@) {
  99.   my(@vars) = @_;
  100.   my($oid, $var, $tmp, $tmpv, @retvar);
  101.  
  102.   undef @retvar;
  103.   foreach $var (@vars) {
  104.     ($oid, $tmp) = &Check_OID($var);
  105.     if (!$oid && $SNMP_util::CacheLoaded == 0) {
  106.       $tmp = $SNMP_Session::suppress_warnings;
  107.       $SNMP_Session::suppress_warnings = 1000;
  108.  
  109.       &snmpLoad_OID_Cache($SNMP_util::CacheFile);
  110.  
  111.       $SNMP_util::CacheLoaded = 1;
  112.       $SNMP_Session::suppress_warnings = $tmp;
  113.  
  114.       ($oid, $tmp) = &Check_OID($var);
  115.     }
  116.     while (!$oid && $#SNMP_util::MIB_Files >= 0) {
  117.       $tmp = $SNMP_Session::suppress_warnings;
  118.       $SNMP_Session::suppress_warnings = 1000;
  119.  
  120.       snmpMIB_to_OID(shift(@SNMP_util::MIB_Files));
  121.  
  122.       $SNMP_Session::suppress_warnings = $tmp;
  123.  
  124.       ($oid, $tmp) = &Check_OID($var);
  125.       if ($oid) {
  126.     open(CACHE, ">>$SNMP_util::CacheFile");
  127.     print CACHE "$tmp\t$oid\n";
  128.     close(CACHE);
  129.       }
  130.     }
  131.     if ($oid) {
  132.       $var =~ s/^$tmp/$oid/;
  133.     } else {
  134.       carp "Unknown SNMP var $var\n"
  135.       unless ($SNMP_Session::suppress_warnings > 1);
  136.       next;
  137.     }
  138.     while ($var =~ /\"([^\"]*)\"/) {
  139.       $tmp = sprintf("%d.%s", length($1), join(".", map(ord, split(//, $1))));
  140.       $var =~ s/\"$1\"/$tmp/;
  141.     }
  142.     print "toOID: $var\n" if $SNMP_util::Debug;
  143.     $tmp = encode_oid_with_errmsg($var);
  144.     return undef unless defined $tmp;
  145.     push(@retvar, $tmp);
  146.   }
  147.   return @retvar;
  148. }
  149.  
Sep 22 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
Sorry mate, I have no idea.
Sep 22 '08 #6

P: 4
toOID subroutine has depemdencies on the following subroutines Check_OID :
Expand|Select|Wrap|Line Numbers
  1. sub Check_OID  ($) {
  2.   my($var) = @_;
  3.   my($tmp, $tmpv, $oid);
  4.  
  5.   if ($var =~ /^(([a-zA-Z][a-zA-Z\d\-]*\.)*([a-zA-Z][a-zA-Z\d\-]*))/)
  6.   {
  7.     $tmp = $&;
  8.     $tmpv = $tmp;
  9.     for (;;) {
  10.       last if defined($SNMP_util::OIDS{$tmpv});
  11.       last if !($tmpv =~ s/^[^\.]*\.//);
  12.     }
  13.     $oid = $SNMP_util::OIDS{$tmpv};
  14.     if ($oid) {
  15.       return ($oid, $tmp);
  16.     } else {
  17.       return undef;
  18.     }
  19.   }
  20.   return ($var, $var);
  21. }
  22.  
Sep 22 '08 #7

numberwhun
Expert Mod 2.5K+
P: 3,503
Moon1:

I have asked you nicely to please use code tags in a previous post AND I have even sent you a PM (private message) relating to your lack of code tags use. This is your last warning, use code tags or I will be forced to ban you for 1 week for failure to comply with site guidelines AND not listening to the suggestions of Moderator(s).

Regards,

Moderator
Sep 22 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.