Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Return from a subroutine generates a negative value

by c (Hermit)
on Aug 07, 2003 at 22:04 UTC ( #282048=perlquestion: print w/replies, xml ) Need Help??

c has asked for the wisdom of the Perl Monks concerning the following question:

I have the following subroutine:

sub get_latest_avg { my $host = shift; my $rrd = shift; my $type = shift; + my $snmp = Net::SNMP->session( -hostname => $host, -version => '1', -community => $nodes{$host}->{community}, ); + my $num = $snmp->get_request( -varbindlist => [$rrd{$rrd}{$type}] +) + ; + my $avg = $num->{$rrd{$rrd}{$type}}; + $snmp->close; + return $avg; + }

I call this subroutine from within another for loop:

for my $host(keys %nodes) { for my $rrd(keys %rrd) { my $avg = &get_latest_avg($host,$rrd,'free'); print $avg. "\n"; } }

I've confirmed each of the variables are assigned correctly through various print statements in key places. What is unusual is that no matter how i order the keys of %rrd, only the first pass returns a valid value. The remaining passes over the other keys return '-1' as the value.

Your input is very appreciated. -c

Replies are listed 'Best First'.
Re: Return from a subroutine generates a negative value
by TomDLux (Vicar) on Aug 07, 2003 at 23:01 UTC

    You cannot order hash keys, by definition, their order is arbitrary.

    What I understand by first pass means that for one key $host from %nodes, $rrd takes on the values from %rrd in thje way you expect, but it does not for the other keys in %nodes. I doubt that is what you mean ... or if it is, you are not showing the relevant coded, since %rrd is not modified in the code you present.

    How about putting a print statement in the inner loop, to see what is happening?

    for my $host(keys %nodes) { for my $rrd(keys %rrd) { printf "host: %s\trrd: %s\n", $host, $rrd; # my $avg = &get_latest_avg($host,$rrd,'free'); # print $avg. "\n"; } }

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

      I understand what you're saying about the random order of hash keys. However, in my situation, its always the first one that succeeds, regardless of which keys its using. The rest always return -1 as the result of the return. I've tried adding hosts to the %nodes hash and always, the first host works, and the rest fail, no matter which host is selected in the first iteration of the for loop. Hosts that failed in a previous run, run properly if they are the first in line.

      To plug in some values for the hashes I use (both are declared outside of the for loops), I'm adding this:

      my %nodes = ( 'host1' => { 'hardware' => 'servers', 'community' => "$serverCommunity", }, 'host2' => { 'hardware' => 'servers', 'community' => "$serverCommunity", }, ); my %rrd = ( 'real_trend' => { 'free' => $memAvailReal, 'desc' => 'Physical Memory', }, 'swap_trend' => { 'free' => $memAvailSwap, 'desc' => 'Swap Memory', }, );

      I just don't think these values matter that much since their order seems unimportant.

      I somehow think the issue may be in the snmp session somehow however I have no clue as to why.

      Thanks very much for your time on this -c

Re: Return from a subroutine generates a negative value
by LazerRed (Pilgrim) on Aug 08, 2003 at 00:28 UTC
    I'm still rather new to the module, but how about putting in some error checking?

    my ( $session, $error ) = Net::SNMP->session( -hostname => $host, -version => '1', -community => $nodes{$host}->{community}, ); if ( !defined($session) ) { printf("ERROR: sub snmp session undef\n"); } my $num = $snmp->get_request( -varbindlist => [$rrd{$rrd}{$type}] ) if ( !defined($num) ) { printf( "ERROR: %s.\n", $session->error ); }

    Oh, and the default version for Net::SNMP is v1, so you can probably remove ( -version => '1', ) from your session.

    Whip me, Beat me, Make me use Y-ModemG.
Re: Return from a subroutine generates a negative value
by eric256 (Parson) on Aug 07, 2003 at 23:26 UTC

    For clarity i would suggest not using a global %rrd and a local $rrd in the same sub. You could rename one or the other to make it clear. At first i thought you where using a hash as a key in a hash, seemed strange. :-)

    ___________
    Eric Hodges

      I take completely the opposite view. I see it as quite natural to use the same name for a hash or array and for individual elements. Generally, I try to make the collective object be plural, while the individial are sometimes localized with 'the_rrd' or 'a_rrd', but often the sigils are sufficient differentiation, especially if the scope is small.

      --
      TTTATCGGTCGTTATATAGATGTTTGCA

        I'm in between you two (is ambivalence a problem for me? Maybe...). I'll end up using the plural and singular so I can say for my $host ( @hosts ) { code }, but I think using the exact same name could be confusing. Admittedly anyone who knows perl will know that $host and $host{key} are compeltely different variables, but why court confusion? Also, if you keep them separate, 'strict' and 'warning' can save you from typos or brainfarts.

        --Bob Niederman, http://bob-n.com

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2021-06-20 22:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (96 votes). Check out past polls.

    Notices?