PERL + Threads ping multiple host


#!/usr/local/bin/perl
use Net::Ping;
use threads;
use Time::HiRes qw(sleep usleep);
use Log::Log4perl;
use threads::shared;

my $shr_var : shared = 0;

my $log_conf = “/etc/log4perl.conf”;
Log::Log4perl::init($log_conf);
my $logger = Log::Log4perl->get_logger();

my @hosts = (‘192.168.0.1’, ‘192.168.0.2’, ‘192.168.0.3’, ‘192.168.0.4’, ‘192.168.0.5’, ‘192.168.0.6’, ‘192.168.0.7’);

for ($ip=0; $ip<=$#hosts; $ip++){
threads->create(\&pingHost,$hosts[$ip]);
}

while(1) {
#print “Main is running\n”;
sleep(1);
}

exit 0;

sub pingHost{
my $host = $_[0];
my $timeout = 1;
my $cnt = 0;
my $pinger = Net::Ping->new(‘icmp’, $timeout);

while ($shr_var < 1) {
$cnt++;

if (!$pinger->ping($host)) {
$logger->error(“$host IS DOWN”);
}
sleep(1);
}

$pinger->close();
}

—————————————————–

[root@alfamart ping-perl]# more log4perl.conf
###############################################################################
## Log::Log4perl Conf #
################################################################################
log4perl.rootLogger = DEBUG, LOG1
log4perl.appender.LOG1 = Log::Log4perl::Appender::File
log4perl.appender.LOG1.filename = /home/moses/log/ping.log
log4perl.appender.LOG1.mode = append
log4perl.appender.LOG1.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.LOG1.layout.ConversionPattern = %d %p %m %n

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

shisdew

Listens until think alike

moses.spaceku@yahoo.com / voip ipbx

Hosted PBX, IP-PBX SOHO/ CALL CENTER, VOICE GATEWAY, VOICE CARD, COST EFECTIVE SOLUTIONS (LCR), GSM/CDMA GATEWAY

%d bloggers like this: