in reply to Re: Forking Issues
in thread Forking Issues

Take a look at Parallel::ForkManager. It'll do most of this work for you, keeping track of N available process slots.

Replies are listed 'Best First'.
Re: Re: Re: Forking Issues
by satanklawz (Beadle) on Aug 08, 2003 at 01:58 UTC
    You are right- it is far easier.
    #!/usr/bin/perl -w strict use strict; use DBI; use Parallel::ForkManager; my $dbh = DBI->connect('DBI:mysql:noc', 'root', '') or die "Couldn't o +pen database: $DBI::errstr; stopped"; my $fm = new Parallel::ForkManager(10); while (1) { my $sth = $dbh->prepare("select assetid,ip,snmpname from assets") or d +ie "Couldn't prepare statement: $DBI::errstr; stopped"; $sth->execute() or die "Couldnt execute statement: $DBI::errsrt; stopp +ed"; my @ret; while ( my ($aid, $aip, $asnmp) = $sth->fetchrow_array() ) { $fm->start and next; ping_remote($aip,$aid,$asnmp); $fm->finish; } $fm->wait_all_children; sleep(5); print "repeating\n"; } sub ping_remote { my $ip = shift; my $aid = shift; my $snmpname = shift; my @ret = undef; my @p = split(/(,)/,`ping -c 1 $ip | grep "0% loss"`); if ($p[4] eq " 0% loss") { my $a=`/etc/poller/snmptraffic.php "$ip" "$snmpname"`; my $u=`/etc/poller/snmpuptime.php "$ip" "$snmpname"`; my $f=`/etc/poller/snmpfirmware.php "$ip" "$snmpname"`; @ret = ($a,$u,$f); } if (@ret) { print "$ip\n"; my $sth2 = $dbh->prepare("insert into history set assetid='$aid', +trafficload='$ret[1]'") or die "Couldn't prepare statement: $DBI::err +str; stopped"; $sth2->execute() or die "Couldnt execute statement: $DBI::errsrt; +stopped"; $sth2 = $dbh->prepare("update assets set firmware='$ret[2]', uptim +e='$ret[1]', status='u', trafficload='$ret[0]' where assetid='$aid'") + or die "Couldn't prepare statement: $DBI::errstr; stopped"; $sth2->execute() or die "Couldnt execute statement: $DBI::errsrt; +stopped"; } else { print "down - $ip\n"; my $sth2 = $dbh->prepare("update assets set status='d' where asset +id='$aid'") or die "Couldn't prepare statement: $DBI::errstr; stopped +"; $sth2->execute() or die "Couldnt execute statement: $DBI::errsrt; +stopped"; } }

    THANKS!
      well, now to clean it up :P that that should give other people an idea as to how to do it... roughly :)