Greeting Monks,

I am new to PERL so forgive me if this is a trival question. I am building a script to be run via Openview to query an SNMP MIB for get status of an indexed interface. Based on the description of the interface and status, I want to formulate a custom trap.

At this point, I am using Net::SNMP to gain the information from the network device using SNMPv1 (only support by device). Where I am running into trouble is exacting the variables from the FOREACH loop so I can utilize them in a if/else loop. I started from a well know bit of code and modified it.

Here is the code so far:
#!/usr/local/bin/perl # use strict; use Net::SNMP '5.0' || die("Cannot Load SNMP Module\n"); ############################## CONFIGURATION ######################### +######### $snmpnotify = "nnmsnmpnotify.ovpl"; $trap3 = ".1.3.6.1.4.1.13169.9999.0.5"; #Context trap with informatio +n(used by the Pairwise correlation) #$snmp_target = '10.180.89.81'; $snmp_target = ''; $snmp_community = '$@g3on'; ########### Check to see that the number of args is correct ########## +######### if (!($#ARGV == 0)){ exit 1; } $snmp_target = $ARGV[0]; ################################### OIDs ############################# +######### my $oid_ifIndex = '.1.3.6.1.4.1.13169.1.2.10.4.1.1'; my $oid_ifDescr = '.1.3.6.1.4.1.13169.1.2.11.5.1.3'; my $oid_ifOperStatus = '.1.3.6.1.4.1.13169.1.2.10.4.1.2'; my @interfaceList = [$oid_ifDescr, $oid_ifOperStatus]; ################################### CODE ############################# +######### my ($session, $error, $result, @ifindex, $key); my %alarmpoint_status = ( 1 => 'off', 2 => 'on', 3 => 'unknown'); ###################################################################### +######### ## Creation Session ($session, $error) = Net::SNMP->session( -hostname => shift || $snmp_target, -community => shift || $snmp_community, -port => shift || 161, -version => 'snmpv1' ); if (!defined($session)) { printf("ERROR: %s.\n", $error); exit 1; } ## Get AlarmPoint Indexes $result = $session->get_table( -baseoid => $oid_ifIndex ); printf("%s\n", $result ); if (!defined($result)) { printf("ERROR: %s.\n", $session->error); $session->close; exit 1; } foreach $key ($session->var_bind_names) { push(@ifindex, $result->{$key}); } ## Get AlarmPoint Details $result = $session->get_entries( -columns => @interfaceList); printf("%s\n", $result ); if (!defined($result)) { printf("ERROR: %s.\n", $session->error); $session->close; exit 1; } ## Loop through indexes and Output Interface Description and Status foreach $key (@ifindex) { my $alarmd = $result->{$oid_ifDescr.'.'.$key}; my $alarms = $alarmpoint_status{ $result->{$oid_ifOperStatus.'.'. +$key} }; printf("%s : %s\n", $alarmd, $alarms ); } ### Here is where I want to take the value $alarmd and $alarms and uti +lize them in an if state like below: if ($alarms = "off") { $cmd = `$snmpnotify -v 2c -a $snmp_target 10.180.52.58 $trap3 1.3. +6.1.4.1.13169.9999.5.1 OCTETSTRINGASCII $alarmd "Alarm" 1.3.6.1.4.1.1 +3169.9999.5.2 OCTETSTRINGASCII $snmp_target 1.3.6.1.4.1.13169.9999.5. +3 OCTETSTRINGASCII $alarms`; } else {} $session->close;
Any thoughts would be greatly appreciated as I cannot seem to figure this out. Since it is SNMPv1 the getbulk and other options do not work.

Thanks

Rick

In reply to FOREACH Variables by tx_sailor

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.