Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing system calls in HP-UX

Status
Not open for further replies.

neotrexx

MIS
Nov 21, 2000
2
US
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,&quot;>>$data&quot;) || die &quot;Cant open output file $data ($!)\n&quot;;


my %status; #tmp storage for misc crap
print INFO &quot;starting ping sweep : $time\n&quot;;


foreach my $host (@hosts){
print &quot;pinging $host...&quot;;


open(PING, &quot;ping $host -n 10|&quot;);
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(&quot;\/&quot;,$1);
}
}
close PING;



print &quot;done\n&quot;;
print INFO &quot;host $host\n&quot;;
if($status{'loss'} != 100){
print INFO $host . &quot;,&quot; . $date . &quot;,&quot; . $status{'time'} -> {'avg'} . &quot;\n&quot;;
} else {
print INFO $host . &quot;,&quot; . $date . &quot;,&quot; . &quot;TO&quot; &quot;\n&quot;;
}
}


close INFO;

Thanks for the help.
Steve-0

 
try

my ($min, $avg, $max);

($min, $avg, $max) = split(&quot;\/&quot;,$1);
$status{'time'}{'min'} = $min;
$status{'time'}{'avg'} = $avg;
$status{'time'}{'max'} = $max;

to be honest I'm not sure what your intention is with

@{$status{'time'}}{'min','avg','max'} = split(&quot;\/&quot;,$1);

it *looks* as if you're trying to assign to a hash of hashes, is that what you meant to do?
Mike
michael.j.lacey@ntlworld.com
 
That is what i did the problem i have it that the $1 var is not getting assigned a value from the (.+?) so there is nothing there to split.

Steve-0
 
Sorry Steve, my post wasn't clear at all. I meant to say that you should try saving the value of $1 straightaway. Like this:

if(/^min\/avg\/max = (.+?) $/){
[tab]($min, $avg, $max) = split(&quot;\/&quot;,$1);

I'm not sure what the exact output of your version of ping is but you are checking for a line that begins with the string 'min/avg/max' -- is that what you intended?

And (probably a stupid question) wouldn't the regular expression (/^min\/avg\/max = (.*) $/ work better? I'm not sure what effect using + and ? next to each other has.

Mike
michael.j.lacey@ntlworld.com
 
oic -- the ? changes the greediness of the re. My suggestion still stands though.
Mike
michael.j.lacey@ntlworld.com
 
try this

if(/^min\/avg\/max\s+=\s+/){
($min, $avg, $max) = split(&quot;\/&quot;,$');

this will match 'min/avg/max = ' and what was *after* that match will get put into $'

i think you'll find that the output hpux ping is ascii, it will be something incorrect in the script. (from an offline discussion in case anyone's wondering)
Mike
michael.j.lacey@ntlworld.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top