Sorry for the pun- it's late. Anyways, I'm new to using Fork(), and I dont think I'm following it too well. Here is an app that I am hammering out. The issue is that I dont see it forking. Here is the code.
#!/usr/bin/perl -w strict
use strict;
use DBI;
my $dbh = DBI->connect('DBI:mysql:noc', 'root', '') or die "Couldn't o
+pen database: $DBI::errstr; stopped";
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 @a;
while ( my ($aid, $aip, $asnmp) = $sth->fetchrow_array() ) {
my @a = ($aid, $aip, $asnmp);
forkin($aid, $aip, $asnmp);
}
sleep(5);
print "repeating\n";
}
sub forkin {
my $aid = shift;
my $aip = shift;
my $asnmp = shift;
my @ret;
my $pid=fork();
die "Cannot fork: $!" if (! defined $pid);
if (! $pid) {
my @ret = ping_remote($aip,$asnmp);
commitDB($aip,$aid,$asnmp,@ret);
exit 0;
}
waitpid($pid,0);
}
sub commitDB {
my $ip = shift;
my $assetid = shift;
my $snmpname = shift;
my @ret = @_;
if (@ret) {
my $sth2 = $dbh->prepare("insert into history set assetid='$asse
+tid', trafficload='$ret[1]'") or die "Couldn't prepare statement: $DB
+I::errstr; stopped";
$sth2->execute() or die "Couldnt execute statement: $DBI::errsrt
+; stopped";
$sth2 = $dbh->prepare("update assets set firmware='$ret[2]', upt
+ime='$ret[1]', status='u', trafficload='$ret[0]' where assetid='$asse
+tid'") or die "Couldn't prepare statement: $DBI::
errstr; stopped";
$sth2->execute() or die "Couldnt execute statement: $DBI::errsrt
+; stopped";
} else {
my $sth2 = $dbh->prepare("update assets set status='d' where ass
+etid='$assetid'") or die "Couldn't prepare statement: $DBI::errstr; s
+topped";
$sth2->execute() or die "Couldnt execute statement: $DBI::errsrt
+; stopped";
}
}
sub ping_remote {
my $ip = 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);
}
return @ret;
}
You may be wondering what the php files are for- well, I'm going to play with SNMP next with Perl :P
To summarize, when executed, I want the program to pull data from a database and fork subprocesses that will do the polling and then store their values into a database. I already have this functionality in another perl app, but it only does it sequentially- I dont want that. Imagine polling 16,000 devices, taking 1 second per device. That's about 4.4 hours until getting back to the first device.
Someone mind steering me into the right direction again? Thanks all!