Dear Monks,

Here goes:

We have around 3000 routers and a configuration database with entries created when the devices are commissioned. In order to ensure the integrity and accuracy of the recorded data I have a bash shell script that grabs the list of all devices and a few config variables (such as community strings, device type, line speed etc) and then double checks the validity of the config against the live device settings.

I am in the process of converting to Perl (from the original, and very slow bash script).

(Obviously, "perl -w" and "use strict" are used!)

So, I grab the list using LWP:

my $browser = LWP::UserAgent->new(); my $source_data = $browser->get( $config{source_file} ); die( "Couldn't GET $config{source_file}\n" ) unless defined $source_da +ta; print "Got source data\n"; my $results_table; my @all_results = split /\n/, $source_data->{_content}; for ( @all_results ) { next if $_ =~ m/^\#/; my ( $site_name, $line_speed, $mgmt_ip, $switch_port, $ro_community, +$group, $site_code ) = split /\|/;
I then populate a HoH:
$results_table->{$group}->{$site_name} = { line_speed => $line_spee +d, mgmt_ip => $mgmt_ip, switch_port => $switch_po +rt, ro_community => $ro_commun +ity, site_code => $site_code +, oss_device => $oss_devic +e, snmp_status => '', sysobjid => '', };
At this point I set up non-blocking Net::SNMP sessions and use callback to launch a subfunction to, in this example, add the mib2 sysobjid to the HoH:
my ($session, $error) = Net::SNMP->session( -community => $results_table->{$group}->{$site_name}->{ro_commun +ity}, -hostname => $results_table->{$group}->{$site_name}->{mgmt_ip +}, -nonblocking => 0x1, # Create non-blocking objects ); if (!defined($session)) { printf("ERROR: %s.\n", $error); exit 1; }
And:
$session->get_request( -varbindlist => [ $oids{sysobjid} ], -callback => [ \&store_sysobjid, \$group, \$site_name ] );
Now, close the for loop and get the ball rolling:
} snmp_dispatcher();
The subfunction:
sub store_sysobjid { my ($session, $group, $site_name ) = @_; if (!defined($session->var_bind_list)) { $results_table->{$$group}}->{$$site_name} = { sysobjid => "unknown", snmp_status => "NOT OK +", }; printf("%-15s ERROR: %s\n", $session->hostname, $session->error); } else { $results_table->{$$group}->{$$site_name} = { sysobjid => ${$session +->var_bind_list}{$oids{sysobjid}}, snmp_status => "OK"}; } }

Now, it has taken me a little while to get the referencing / dereferencing syntax working correctly but this seems OK now.

My questions:

1. When I use Data::Dumper to get the contents of \$results_table the sysbojid and snmp_status variables seem to have zapped the rest of the data on the HoH for that site out of existence. Is the syntax incorrect? If I remove the sysobjid => unknown code then any site that is down (i.e. has its snmp allowed managers configured incorrectly) retains the line_speed / mgmt_ip data correctly.

2. Is using a HoH an appropriate approach? My thoughts were to use a HoH so that when I have all of the data I can chuck out all of the relevant HTML pages quite easily.

FYI - Using non-blocking Net::SNMP has reduced the runtime of this from around 20-30 minutes for the shell script (which ran serially) to around 24 seconds (WoooHooo!)

Any comments / suggestions / criticisms are more than welcome.

I am here to learn.

Thanks

smullis


In reply to Hash of Hashes, referencing, dereferencing and Net::SNMP by smullis

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.