Hello all,
Here is my problem. I am writing a perl script that pings a number of nodes through out my company's network for network monitoring. What is happing is i call a ping command from the system then run it through a parse. The problem is that it is not placing the parsed information into the $1 var. When i do a cat of the information it shows all the output except for the ping time. What i am parsing is the avg ping time. I need it to be in the format of "$host,`date +%x`, 'avg ping time here'" so that it can inported to a SQL DB automaticly through an ftp script. Everything works except for the parse of the avg ping time. I am running this on a HP-UX 11.00 and i have perl 5.6.0 on the box. Below I have supplied the code. Please Help me. I have asked a couple of my perl friends who are more knowlegelbe then myself and they have no idea. They say the code looks solid but they dont understand what is happening.
#/opt/perl5/bin/perl
use strict;
$| = 1; # flush buffers on end of print statements
my $data = 'testresult'; #output file
my $hosts = 'ip_address'; #ip address input file
my @hosts;
open(INFO,$hosts) || die "cant open $hosts ($!)\n";
@hosts = <INFO>;
close INFO;
chomp(@hosts); # get rid of the \ns at the end of each line
my $time = `date +%x`; #grab the time
chop ($time); #Removing \n from date
open(INFO,">>$data"
|| die "Cant open output file $data ($!)\n";
my %status; #tmp storage for misc crap
print INFO "starting ping sweep : $time\n";
foreach my $host (@hosts){
print "pinging $host...";
open(PING, "ping $host -n 10|"
;
while(<PING>){
# parse the output, we only care bout packet loss and times
if(/(\d+?)% packet loss/){
# grab the packet loss
$status{'loss'} = $1;
}
if(/^min\/avg\/max = (.+?) $/){
$status{'time'} = {}; # anon hash, man perlref
# dump data into the min, avg, and max hash indexs using a slice
@{$status{'time'}}{'min','avg','max'} = split("\/",$1);
}
}
close PING;
print "done\n";
print INFO "host $host\n";
if($status{'loss'} != 100){
print INFO $host . "," . $date . "," . $status{'time'} -> {'avg'} . "\n";
} else {
print INFO $host . "," . $date . "," . "TO" "\n";
}
}
close INFO;
Thanks for the help.
Steve-0
Here is my problem. I am writing a perl script that pings a number of nodes through out my company's network for network monitoring. What is happing is i call a ping command from the system then run it through a parse. The problem is that it is not placing the parsed information into the $1 var. When i do a cat of the information it shows all the output except for the ping time. What i am parsing is the avg ping time. I need it to be in the format of "$host,`date +%x`, 'avg ping time here'" so that it can inported to a SQL DB automaticly through an ftp script. Everything works except for the parse of the avg ping time. I am running this on a HP-UX 11.00 and i have perl 5.6.0 on the box. Below I have supplied the code. Please Help me. I have asked a couple of my perl friends who are more knowlegelbe then myself and they have no idea. They say the code looks solid but they dont understand what is happening.
#/opt/perl5/bin/perl
use strict;
$| = 1; # flush buffers on end of print statements
my $data = 'testresult'; #output file
my $hosts = 'ip_address'; #ip address input file
my @hosts;
open(INFO,$hosts) || die "cant open $hosts ($!)\n";
@hosts = <INFO>;
close INFO;
chomp(@hosts); # get rid of the \ns at the end of each line
my $time = `date +%x`; #grab the time
chop ($time); #Removing \n from date
open(INFO,">>$data"
my %status; #tmp storage for misc crap
print INFO "starting ping sweep : $time\n";
foreach my $host (@hosts){
print "pinging $host...";
open(PING, "ping $host -n 10|"
while(<PING>){
# parse the output, we only care bout packet loss and times
if(/(\d+?)% packet loss/){
# grab the packet loss
$status{'loss'} = $1;
}
if(/^min\/avg\/max = (.+?) $/){
$status{'time'} = {}; # anon hash, man perlref
# dump data into the min, avg, and max hash indexs using a slice
@{$status{'time'}}{'min','avg','max'} = split("\/",$1);
}
}
close PING;
print "done\n";
print INFO "host $host\n";
if($status{'loss'} != 100){
print INFO $host . "," . $date . "," . $status{'time'} -> {'avg'} . "\n";
} else {
print INFO $host . "," . $date . "," . "TO" "\n";
}
}
close INFO;
Thanks for the help.
Steve-0