Replace HP OV SNMP call to Perl SNMP API

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • moon1
    New Member
    • Sep 2008
    • 4

    Replace HP OV SNMP call to Perl SNMP API

    how will I change the code the 1st code according to the 2nd one?
    1st code
    Code:
    sub snmpGet(@)
    {
        # Assign arguments
        my ($deviceName)=$_[0];     # Device name to read from
        my ($community)=$_[1];      # SNMP Community String
        my ($timeout)=$_[2];        # Timeout value for SNMP-GET request
        my ($type)=$_[3];           # SNMP Object Type
        my ($oid)=$_[4];            # SNMP Object ID
        my ($version)=$_[5];        # SNMP Version
        my ($port)=$_[6];           # Port
    
        # Local variables
        my ($result);               # Output from SNMP-GET
    
    
        # Set default version and port
        if ($version) { $version='-v ' . $version; } else { $version=''; }
        if ($port) { $port='-p ' . $port; } else { $port=''; }
    
        # Check parameters
        unless ($deviceName && $community && $timeout && $type && $oid)
        {
            # Missing parameter
            rxnnmsLogError("${snmpErrorPrefix}snmpGet:missing parameter:snmpGet($deviceName,$community,$timeout,$type,$oid");
            return;
        }
    
        # Check for valid type
        unless ($type eq $snmpIntegerType || $type eq $snmpIPAddressType ||
                $type eq $snmpOctetStringType || $type eq $snmpGauge32Type ||
                $type eq $snmpCounter64Type || $type eq $snmpTimeTicksType)
        {
            rxnnmsLogError("${snmpErrorPrefix}snmpGet:invalid type:$type");
            return;
        }
    
        # Generate the SNMP-GET using syntax of underlying SNMP implementation.
        rxnnmsLogTrace("$snmpget $version $port -c $community -t $timeout $deviceName $oid");
        $result=`$snmpget $version $port -c $community -t $timeout $deviceName $oid 2>&1`;
    
        # Trace output of snmpget command
        chomp($result);
        rxnnmsLogTrace("$snmpget $oid to $deviceName returned: $result");
    
       # If $type is 'integer' and $result contains 'INTEGER' or 'Unsigned' then SNMP-GET ran
        # successfully, so return $result, etc. Otherwise return '$snmpErrorPrefix$result'
        # (bad status).
    
        if (($type=$snmpIntegerType && $result=~/INTEGER/) ||
            ($type=$snmpIntegerType && $result=~/Unsigned/) ||
            ($type=$snmpIPAddressType && $result=~/IpAddress/) ||
            ($type=$snmpOctetStringType && $result=~/DISPLAY STRING/) ||
            ($type=$snmpOctetStringType && $result=~/OCTET STRING/) ||
            ($type=$snmpGauge32Type && $result=~/Gauge32/) ||
            ($type=$snmpCounter64Type && $result=~/Counter64/) ||
            ($type=$snmpTimeTicksType && $result=~/Timeticks/))
        {
            # SNMP-GET successful. Return output.
            rxnnmsLogTrace("$snmpget $oid from $deviceName was successful");
            return($result);
        }
        else
        {
            # SNMP-GET failed. Return error.
            rxnnmsLogTrace("$snmpget $oid from $deviceName failed: $result");
            return("$snmpErrorPrefix$result");
        }
    
    } # End of snmpGet
    2nd code
    Code:
    sub snmpget ($@) {
      my($host, @vars) = @_;
      my(@enoid, $var, $response, $bindings, $binding, $value, $oid, @retvals);
      my $session;
    
      $session = &snmpopen($host, 0, \@vars);
      if (!defined($session)) {
        carp "SNMPGET Problem for $host\n"
          unless ($SNMP_Session::suppress_warnings > 1);
        return undef;
      }
    
      @enoid = &toOID(@vars);
      return undef unless defined $enoid[0];
    
      if ($session->get_request_response(@enoid)) {
        $response = $session->pdu_buffer;
        ($bindings) = $session->decode_get_response($response);
        while ($bindings) {
          ($binding, $bindings) = decode_sequence($bindings);
          ($oid, $value) = decode_by_template($binding, "%O%@");
          my $tempo = pretty_print($value);
          push @retvals, $tempo;
        }
        return(@retvals);
      }
      $var = join(' ', @vars);
      carp "SNMPGET Problem for $var on $host\n"
        unless ($SNMP_Session::suppress_warnings > 1);
      return undef;
    }
    Last edited by numberwhun; Sep 19 '08, 12:12 PM. Reason: Please use code tags
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    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.
    Last edited by numberwhun; Sep 19 '08, 12:17 PM. Reason: Add moderation note

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      I don't even understand what the question/problem is, so there is no way to begin to answer it.

      Comment

      • moon1
        New Member
        • Sep 2008
        • 4

        #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.T he 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.

        Comment

        • moon1
          New Member
          • Sep 2008
          • 4

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

          Code:
          [B]sub snmpopen [/B] ($$$) {
            my($host, $type, $vars) = @_;
            my($nhost, $port, $community, $lhost, $lport, $nlhost);
            my($timeout, $retries, $backoff, $version);
            my $v4onlystr;
          
            $type = 0 if (!defined($type));
            $community = "public";
            $nlhost = "";
          
            ($community, $host) = ($1, $2) if ($host =~ /^(.*)@([^@]+)$/);
          
            # We can't split on the : character because a numeric IPv6
            # address contains a variable number of :'s
            my $opts;
           if( ($host =~ /^(\[.*\]):(.*)$/) || ($host =~ /^(\[.*\])$/) ) {
              # Numeric IPv6 address between []
              ($host, $opts) = ($1, $2);
            } else {
              # Hostname or numeric IPv4 address
              ($host, $opts) = split(':', $host, 2);
            }
            ($port, $timeout, $retries, $backoff, $version, $v4onlystr) = split(':', $opts, 6)
              if(defined($opts) && (length $opts > 0) );
          
            undef($version) if (defined($version) && length($version) <= 0);
            $v4onlystr = "" unless defined $v4onlystr;
            $version = '1' unless defined $version;
            if (defined($port) && ($port =~ /^([^!]*)!(.*)$/)) {
              ($port, $lhost) = ($1, $2);
              $nlhost = $lhost;
              ($lhost, $lport) = ($1, $2) if ($lhost =~ /^(.*)!(.*)$/);
              undef($lhost) if (defined($lhost) && (length($lhost) <= 0));
              undef($lport) if (defined($lport) && (length($lport) <= 0));
            }
            undef($port) if (defined($port) && length($port) <= 0);
            $port = 162 if ($type == 1 && !defined($port));
            $nhost = "$community\@$host";
            $nhost .= ":" . $port if (defined($port));
          
            if ((!defined($SNMP_util::Session))
              || ($SNMP_util::Host ne $nhost)
              || ($SNMP_util::Version ne $version)
              || ($SNMP_util::LHost ne $nlhost)
              || ($SNMP_util::IPv4only ne $v4onlystr)) {
              if (defined($SNMP_util::Session)) {
                $SNMP_util::Session->close();
                undef $SNMP_util::Session;
                undef $SNMP_util::Host;
                undef $SNMP_util::Version;
                undef $SNMP_util::LHost;
                undef $SNMP_util::IPv4only;
              }
              $SNMP_util::Session = ($version =~ /^2c?$/i)
                ? SNMPv2c_Session->open($host, $community, $port, undef,
          				$lport, undef, $lhost, ($v4onlystr eq 'v4only') ? 1:0 )
                : SNMP_Session->open($host, $community, $port, undef,
          				$lport, undef, $lhost, ($v4onlystr eq 'v4only') ? 1:0 );
              ($SNMP_util::Host = $nhost, $SNMP_util::Version = $version,
                $SNMP_util::LHost = $nlhost, $SNMP_util::IPv4only = $v4onlystr) if defined($SNMP_util::Session);
            }
          
            if (defined($SNMP_util::Session)) {
              if (ref $vars->[0] eq 'HASH') {
                my $opts = shift @$vars;
                foreach $type (keys %$opts) {
          	if ($type eq 'return_array_refs') {
          	  $SNMP_util::Return_array_refs = $opts->{$type};
          	}
          	else {
          	  if (exists $SNMP_util::Session->{$type}) {
          	    if ($type eq 'timeout') {
          	      $SNMP_util::Session->set_timeout($opts->{$type});
          	    } elsif ($type eq 'retries') {
          	      $SNMP_util::Session->set_retries($opts->{$type});
          	    } elsif ($type eq 'backoff') {
          	      $SNMP_util::Session->set_backoff($opts->{$type});
          	    } else {
          	      $SNMP_util::Session->{$type} = $opts->{$type};
          	    }
          	  } else {
          	    carp "SNMPopen Unknown SNMP Option Key '$type'\n"
          	      unless ($SNMP_Session::suppress_warnings > 1);
          	  }
          	}
                }
              }
              $SNMP_util::Session->set_timeout($timeout)
                if (defined($timeout) && (length($timeout) > 0));
              $SNMP_util::Session->set_retries($retries)
                if (defined($retries) && (length($retries) > 0));
              $SNMP_util::Session->set_backoff($backoff)
                if (defined($backoff) && (length($backoff) > 0));
            }
            return $SNMP_util::Session;
          }
          
          [B]sub toOID[/B](@) {
            my(@vars) = @_;
            my($oid, $var, $tmp, $tmpv, @retvar);
          
            undef @retvar;
            foreach $var (@vars) {
              ($oid, $tmp) = &Check_OID($var);
              if (!$oid && $SNMP_util::CacheLoaded == 0) {
                $tmp = $SNMP_Session::suppress_warnings;
                $SNMP_Session::suppress_warnings = 1000;
          
                &snmpLoad_OID_Cache($SNMP_util::CacheFile);
          
                $SNMP_util::CacheLoaded = 1;
                $SNMP_Session::suppress_warnings = $tmp;
          
                ($oid, $tmp) = &Check_OID($var);
              }
              while (!$oid && $#SNMP_util::MIB_Files >= 0) {
                $tmp = $SNMP_Session::suppress_warnings;
                $SNMP_Session::suppress_warnings = 1000;
          
                snmpMIB_to_OID(shift(@SNMP_util::MIB_Files));
          
                $SNMP_Session::suppress_warnings = $tmp;
          
                ($oid, $tmp) = &Check_OID($var);
                if ($oid) {
          	open(CACHE, ">>$SNMP_util::CacheFile");
          	print CACHE "$tmp\t$oid\n";
          	close(CACHE);
                }
              }
              if ($oid) {
                $var =~ s/^$tmp/$oid/;
              } else {
                carp "Unknown SNMP var $var\n"
                unless ($SNMP_Session::suppress_warnings > 1);
                next;
              }
              while ($var =~ /\"([^\"]*)\"/) {
                $tmp = sprintf("%d.%s", length($1), join(".", map(ord, split(//, $1))));
                $var =~ s/\"$1\"/$tmp/;
              }
              print "toOID: $var\n" if $SNMP_util::Debug;
              $tmp = encode_oid_with_errmsg($var);
              return undef unless defined $tmp;
              push(@retvar, $tmp);
            }
            return @retvar;
          }
          Last edited by numberwhun; Sep 22 '08, 01:21 PM. Reason: Please use code tags

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            Sorry mate, I have no idea.

            Comment

            • moon1
              New Member
              • Sep 2008
              • 4

              #7
              toOID subroutine has depemdencies on the following subroutines Check_OID :
              Code:
              [B]sub Check_OID [/B] ($) {
                my($var) = @_;
                my($tmp, $tmpv, $oid);
              
                if ($var =~ /^(([a-zA-Z][a-zA-Z\d\-]*\.)*([a-zA-Z][a-zA-Z\d\-]*))/)
                {
                  $tmp = $&;
                  $tmpv = $tmp;
                  for (;;) {
                    last if defined($SNMP_util::OIDS{$tmpv});
                    last if !($tmpv =~ s/^[^\.]*\.//);
                  }
                  $oid = $SNMP_util::OIDS{$tmpv};
                  if ($oid) {
                    return ($oid, $tmp);
                  } else {
                    return undef;
                  }
                }
                return ($var, $var);
              }

              Comment

              • numberwhun
                Recognized Expert Moderator Specialist
                • May 2007
                • 3467

                #8
                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

                Comment

                Working...