Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I am a network admin looking to store some info about our switches/routers in a postgres db. We have several hundred devices, so I am looking to poll data via snmp, and store it in my db. Since we have so many devices it takes quite a while to poll them all sequentially. For this reason I am attempting to fork off child processes in order to speed things up a bit.

I am having a problem with my database handles being closed before they should be. I get these errors when I run my script:

Database handle destroyed without explicit disconnect. Database handle destroyed without explicit disconnect. DBD::Pg::db do failed: server closed the connection unexpectedly at ./ line 203.

Here is the main part of the program. I stripped out the sql for the sake of shortening it down a bit.

# DESCRIPTION # This script gets a list of non-access layer, failover switches from +a database.It will # then poll those switches for all active vlans and add them to the da +tabase. # The script will automatically delete any vlans from the db that have + been # removed from our switches, and are not marked with the 'dnd' flag. #=================================================================== # MAIN #------------------- # open db my $parent_dbh = DBI->connect($DATASOURCE, $USER); if (!$parent_dbh) { exit(0); } #--------- #------------------- # get list of switches from db my $switch_ref = $parent_dbh->selectall_arrayref($get_switch); #--------- #------------------- # fork off child processes, poll snmp, update database for (my $count = 0; $count < @$switch_ref; ++$count) { my $switch = $switch_ref->[$count]; my $pid; #------------------- # parent process if ($pid = fork) { push @pids, $pid; } #--------- #------------------- # children processes elsif (defined $pid) { #------------------- # set some variables my $ip = $switch->[0]; my $stp_domain_id = $switch->[1]; my $ro_community = $switch->[2]; my $params = {'community' => $ro_community, 'default_max_repetitions' => '48' }; #--------- #------------------- # walk mib for vlan info from this switch. my @ret = &snmpwalk($ip, $params, "ifalias"); #--------- #------------------- # open db my $child_dbh = DBI->connect($DATASOURCE, $USER); if (!$child_dbh) { exit(0); } #--------- #------------------- # set 'updated' field of the 'vlan' table to false for all # vlans in current domain $child_dbh->do($set_updated); #--------- #------------------- # insert data into vlan table foreach my $val (@ret){ my ($vlan, $name) = split(':', $val, 2); # if entry for this vlan exists, do # SQL UPDATE. else insert new data. my $result_h = $child_dbh->selectrow_array($exists_vlan); if ($result_h > 0){ $child_dbh->do($update_data); } else { $child_dbh->do($insert_data); } } #--------- $child_dbh->disconnect; exit; } #--------- #------------------- # error, the parent is saying this... else { die "Fork failed: $!\n"; } #--------- } #--------- #------------------- # wait for children to finish before proceeding foreach my $pid (@pids){ waitpid($pid,0); } #--------- #------------------- # delete all vlans that have 'dnd'='f' and where 'updated'='f' # this is query that fails because of a closed db handle $parent_dbh->do($delete_vlan); #--------- $parent_dbh->disconnect; exit(0); #=======================================

In reply to forking and dbi by dlspinhir

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    Domain Nodelet?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others surveying the Monastery: (4)
    As of 2024-07-23 18:42 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found

      erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.