The whole code:
Declare the needed modules to import
use IO::Socket::INET;
use threads;
use constant TIMEOUT => 5;
# Bind arguments passed to the code to variables
my $tag = shift;
my $cfgFilesDir = shift;
my $peerPort = shift;
# Open the configuration files directory and create a thread for each
+configuration file
opendir (CDIR, $cfgFilesDir) or die;
my $file;
my $thr;
while ($file = readdir(CDIR)) {
if ($file =~ /.+?\.properties/) {
$thr = threads->create(\&sendRequest, $file, 'localhost', $pee
+rPort );
}
}
closedir (CDIR);
# Wait till all the threads will finish their work. In the threads sub
+ I implemented a timeout
# mechanism to prevent a infinity loop
my @threads;
my $thread;
my $rc;
while (@threads = threads->list()) {
foreach $thread (@threads) {
if ($thread->is_joinable()) {
$rc = $thread->join();
# Send the opcmon
# $rc->[0] is the return code.
# $rc->[1] is the nodeName the thread worked on.
&sendOpcmon($tag, $rc->[0], $rc->[1]);
}
}
# To prevent the hard CPU lookups
sleep(1);
}
#
sub sendRequest {
my ($msg, $peerAddr, $peerPort) = @_;
my $eTime = time() + TIMEOUT;
my $host = (split/\./, $msg)[0];
# configure the socket.
$MySocket = new IO::Socket::INET->new( PeerPort => $peerPort,
Proto => 'udp',
PeerAddr => $peerAddr,
);
ioctl($MySocket, 0x8004667e, pack("I", 1));
# Send the request.
$MySocket->send($msg);
# Now we will wait till the server will respond to our request.
while(time() < $eTime)
{
$MySocket->recv($text,128);
if($text =~ /OK/) {
return [1, $host];
}
# Sleep for 2 secs just to free up a %CPU usage
sleep 2;
}
# We reached the timeout, probably no response will arrive.
return [0, $host];
}
#
sub sendOpcmon
{
my ($tag, $val, $object) = @_;
my $cmd = "opcmon $tag=$val -object $object";
#qx{$cmd};
print $cmd."\n";
}
Module versions you requested:
perl: v5.8.8 built for MSWin32-x86-multi-thread
threads: 1.63
IO:Socket:INET: 1.31
(all the above modules where installed with the standard activePerl distribution)
Hope that information will be enough. Thank you!