http://www.perlmonks.org?node_id=1160466

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


This is the first time I need to do fork , so Im reading and trying to learn how to do it.
Im reading and processing the log lines of my dhcp sever wich are coming really fast.
For each line I receive I need to perform a snmpget querie to the ips Im grabing.
The problem is that each querie takes more than a second to return a value so it is delaying all the script so I had to do fork.
I have this first version working but I would like have your opinion in order to improve it or make it run in a more secure way.
#!/usr/bin/perl -w use strict; use File::Tail; use Cwd qw(); my $path = Cwd::cwd(); $SIG{'CHLD'} = "wait_for_child_to_die"; my $community='public'; my $snmp_fver = '.1.3.6.1.4.1.2700.1.1.8.0'; my $file = File::Tail->new( name =>'/var/log/messages', interval => 1, maxinterval => 1, # resetafter=> 5, ); while (defined(my $line=$file->read)) { #print $line,"\n"; if ($line =~ /^(.*) dns02cor dhcpd: DHCPACK on ([0-9\. +]+) to ([[:xdigit:]:]+).* ([0-9\.]+)/) { unless (my $pid = fork) { die "Couldn't fork " unless defined $pid; write_register($1,$2,$3); exit; } } } sub write_register { my ($date,$client_ip,$client_imsi) = @_; $output=qx(snmpwalk -v2c -t1 -c $community $client_ip $snmp_fver 2>&1 +); chomp($output); if( $output eq "Timeout: No Response from $client_ip" ) { return; } else{ my @result=split(/:/,$output); if ($result[3]){ $fver=$result[3]; $fver=~s/ //g; $fver=~s/\n//g; }else{ exit; } } if($bsid){ system "sed -i '/$client_imsi/d' $path/leases_list.txt +"; system 'echo "'.$date.','.$client_ip.','.$client_imsi.','. $fv +er.'" >> '.$path.'/leases_list.txt'; } } sub wait_for_child_to_die { wait; } 1;

I must admint this is a mix of multiple scripts I founded on the web.
I hope to hear your opinion in case is there is something I should be aware or change.
I promess to read and learn about forking , wait , and related topics.
So far , it is not creating more than 8 process and no zombies also so Im very happy.
Regards,
Leandro.