Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

FOREACH Variables

by tx_sailor (Initiate)
on Aug 11, 2009 at 00:35 UTC ( #787452=perlquestion: print w/ replies, xml ) Need Help??
tx_sailor has asked for the wisdom of the Perl Monks concerning the following question:

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

Comment on FOREACH Variables
Download Code
Replies are listed 'Best First'.
Re: FOREACH Variables
by NetWallah (Abbot) on Aug 11, 2009 at 01:17 UTC
    Couldn't you just move the "if ($alarms.." inside the "foreach $key.." loop ?

    BTW - why is "use strict;" commented out ?

    You should probably change "foreach $key ..." to "foreach my $key...", and "my $cmd=..." instead of $cmd=..." .. but then you do not use $cmd, so just use the backtick without assigning it to anything.

         Potentia vobiscum ! (Si hoc legere scis nimium eruditionis habes)

Re: FOREACH Variables
by snoopy (Deacon) on Aug 11, 2009 at 04:43 UTC
    Another catch:
    use Net::SNMP '5.0' || die("Cannot Load SNMP Module\n");
    is interpreted as:
    use Net::SNMP ('5.0' || die("Cannot Load SNMP Module\n"));
    Just stick to:
    use Net::SNMP '5.0';
    Perl will die anyway, if it can't find and load the module.
Re: FOREACH Variables
by jbt (Chaplain) on Aug 11, 2009 at 03:17 UTC
Re: FOREACH Variables
by cdarke (Prior) on Aug 11, 2009 at 07:41 UTC
    To clarify, $alarms = "off" is an assignment. Use == to test for numeric equality or eq to test for textual equality.

    Always
    use strict; use warnings;
    Although not related to your question, you are over-using printf, which is (allegedly) slower than print. Perl, and several other languages, has interpolation - when a scalar or array variable is enclosed inside double-quotes it translates to its value. This is one of the advantages of the $ and @ prefixes. So:
    printf("%s : %s\n", $alarmd, $alarms );
    is better written as:
    print "$alarmd : $alarms\n";
    and error messages should really be routed to stderr:
    print STDERR "ERROR: $error.\n";
Re: FOREACH Variables
by snoopy (Deacon) on Aug 11, 2009 at 03:18 UTC
    Also,
    if ($alarms = "off") { #... }
    should be:
    if ($alarms eq "off") { # ... }

      which the OP would have caught with use warnings of course.


      True laziness is hard work

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://787452]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2015-09-01 02:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The oldest computer book still on my shelves (or on my digital media) is ...













    Results (366 votes), past polls