ping RTT and packet loss script

Templates, scripts for templates, scripts and requests for templates.

Moderators: Developers, Moderators

phriday613
Posts: 32
Joined: Thu Aug 15, 2002 10:29 am
Location: ct
Contact:

ping RTT and packet loss script

Post by phriday613 »

Ive been looking all over for an rtt/packet loss script for cacti. I know nothing about perl, but i got it to work..

-----eping.pl-----------

Code: Select all

#!/usr/bin/perl

$host = $ARGV[1];
$pingcount = $ARGV[0];
my @array = ();

@array = qx{/bin/ping -n -c $pingcount $host};

$array[$pingcount + 4] =~ /\/(\d*\.\d*)\//;
print "$1 ";

$array[$pingcount + 3] =~ /(\d*)%/;

print "$1\n";
the data input, input string would be:

perl <path_cacti>/scripts/eping.pl <number> <host>
output would be:

<rtt> <loss>

hope that helps! :)
Guest

Post by Guest »

My graph is displaying -1 all the time...

I executed rrdtool fetch ..... > test.txt
All the values are -1 so the problem is not my graph

I know nothing from Perl, but I see /bin/ping
So I created c:\bin\ and copied ping.exe to it
Also tried putting ping.exe in ...\cacti\bin

Maybe you know the problem? Is there a way to test the perl script on its own without using rrdtool or cacti. Just in command prompt or something to see if this is working

Thx!

(using IIS 5.0, windows XP and cacti 0.8.5)
Guest

Post by Guest »

Maybe I have to integrate rrdtool in perl?

something like ppm install rrds.ppd ?
Guest

Post by Guest »

Can somebody please help me? :cry:
pbulteel
Cacti User
Posts: 150
Joined: Fri Sep 05, 2003 9:20 am
Location: London
Contact:

Post by pbulteel »

The problem is that the linux ping command and the windows ping command use differrent arguements.

ping -n -c 1 www.yahoo.com would work on linux but will return an error in windows. That error gets captured as -1 (exit code) and that's probably what's being entered.

You could replace the -n option in linux ping tells it to show the ip address of the host instead of the name. The -n in windows does the same as -c in linux it send that many packets.

If you want to still use that script, you could change it by removing the -c option (maybe even just make it ping instead of /bin/ping) and that should work.

I'll submit a script so that windows AND unix systems can use this.
uname -a
pbulteel
Cacti User
Posts: 150
Joined: Fri Sep 05, 2003 9:20 am
Location: London
Contact:

Post by pbulteel »

Here's the one I would use...

Code: Select all

#!/usr/bin/perl

$host = $ARGV[1];
$pingcount = $ARGV[0];

# lets start with the unix ping
my $ping = `ping -c $pingcount $host`;
# starts some variables
my $rtt;
my $loss;

# check if the windows ping was run and therefore returned a Bad Value
# error - if it did we're going to use the windows ping arguements
if ( $ping =~ /Bad/ ) {

        $ping = `ping -n $pingcount $host`;

# check for this word and grab it's value
        $ping =~ /Average = (.*)ms/;
        $rtt = $1;

# check for the % sign inside parens and grab it's value
        $ping =~ /(\d*)%/;
        $loss = $1;

} else {

# unix ping has it's values separated by /s
        $ping =~ /\/(\d*\.\d*)\//;
        $rtt = $1;
# same as windows ping... value is in parens
        $ping =~ /(\d*)%/;
        $loss = $1;
}

#used for debugging
#print $ping;

#print the values
print $rtt . " " . $loss;
uname -a
ThyReX
Posts: 6
Joined: Wed Feb 04, 2004 8:33 pm

Post by ThyReX »

Here is the one i use:

Code: Select all

#!/usr/bin/perl

$count = $ARGV[0];
$host = $ARGV[1];
open (PING, "ping -nqc$count $host|");
while (<PING>) {
/(\d+)% packet loss/ && ($loss = $1);
/= (.+)\/(.+)\/(.+)\/(.+) ms/ && (($min,$avg,$max,$dev) = ($1,$2,$3,$4));
};
close (PING);
print "min:$min avg:$avg max:$max dev:$dev loss:$loss";
use it like your script: latence.pl <count> <host>
and it return somethink like:

Code: Select all

min:10.766 avg:17.067 max:60.479 dev:14.497 loss:0
thx to my friend TiChou for helping :)
Attachments
This is a graph made with the script
This is a graph made with the script
latency.jpg (132.35 KiB) Viewed 26064 times
pbulteel
Cacti User
Posts: 150
Joined: Fri Sep 05, 2003 9:20 am
Location: London
Contact:

Post by pbulteel »

Unfortunately it doesn't work on windows ping. I'll modify it so it does and repost it!
uname -a
eyechart
Posts: 49
Joined: Thu Apr 25, 2002 11:48 pm

Post by eyechart »

maybe we could just incorporate smokeping data into cacti. There is nothing better than smokeping as far as graphing ping rtt and latency.


-ec
Guest

Post by Guest »

Thank you a thousand times folks!

I'm working in a company for school without earning money (don't know the english word) and got a little bit stressed because it didn't work.

pbulteel, your script doesn't work because you have to return it in the format var1:10 var2:66 when you return more than 1 var. But just changing the print line in your code worked. Also had to make some other changes. Left out the linux part which didn't work and the my $ping stuff
what does "my $var" means? Declaring the var? I can use variables without declaring. Know nothing about perl :cry:

Anyway, it is now working fine!
Now I can try the second script to also receive average and min values

Code: Select all

#!/usr/bin/perl 

$host = $ARGV[1]; 
$pingcount = $ARGV[0];  


$ping = `ping -n $pingcount $host`; 
# starts some variables 
$rtt; 
$loss; 

# check for this word and grab it's value 
        $ping =~ /Average = (.*)ms/; 
        $rtt = $1; 

# check for the % sign inside parens and grab it's value 
        $ping =~ /(\d*)%/; 
        $loss = $1; 

#print the values 
print "rtt:$rtt loss:$loss"; 
Last question: I am using a dutch windows, so he doesn't return 'average: 1 ms', but 'gemiddelde: 1 ms'
how can I use the OR (||) operator in the regular expression to check both values?
pbulteel
Cacti User
Posts: 150
Joined: Fri Sep 05, 2003 9:20 am
Location: London
Contact:

Post by pbulteel »

I'm glad you figured it out. The my $var is to declare the variable. I'm use to using the -w which is "strict" usage.

You could always replace the line $ping =~ /Average = (.*)ms/; with

Code: Select all

$ping =~ /e = (.*)ms/;
I'm not sure if any other word will show up with that pattern, but it's worth a try. You could always use |

Code: Select all

$ping =~ /(Averag|Gemiddeld)e = (.*)ms/;
uname -a
Guest

Post by Guest »

ok, that works. I only have to change $1 in $2
when you do another search and also use brackets, is the first value then available through $1 or do you have to continue counting: $3

now I'm trying to get also the minimum value:

Code: Select all

$ping =~ /Minimum = (.*)ms/; 
        $min = $1; 
I added these lines right under the average, but it gives me a whole line and not just the value. I don't see what's different. Maybe there's a new line character before Minumum or something?
Guest

Post by Guest »

ok, this script works for me: 8)

Code: Select all

#!/usr/bin/perl 

$host = $ARGV[1]; 
$pingcount = $ARGV[0]; 

$ping = `ping -n $pingcount $host`; 

# starts some variables 

$min; 
$max; 
$avg;
$loss; 

# check for the minimum value 

	$ping =~ /Minimum = (\d*)/; 
        $min = $1; 

# check for the maximum value 
	
	$ping =~ /Maximum = (.*)ms,/; 
        $max = $1; 

# check for the average value 

        $ping =~ /(Averag|Gemiddeld)e = (.*)ms/; 
        $avg = $2; 	

# check for the % sign inside parens and grab it's value 
        $ping =~ /(\d*)%/; 
        $loss = $1; 

#print the values 
print "min:$min avg:$avg max:$max loss:$loss"; 
Only the graph doesn't appear (using my own written graph template)
I'm going to investigate that... The script itself should be working.

Now, that it is working I want more :P
Can it be replaced by a simple DOS batch script? Then I don't need to have Perl installed because I use it for nothing else but just that ping script :(
I've tried with grep (grep from borland, windows has not a standard grep function) but I don't think dos has powerfull commands like perl to work with strings and search for a specific value...
kurowsky
Cacti User
Posts: 52
Joined: Wed Feb 04, 2004 4:43 pm
Location: Richmond, VA.

RRD not updating ??

Post by kurowsky »

I'm attempting to use the rtt scripts below for windows.
This is the first script I've tried to use

Actually the script works fine, I can run it manually and get good output.
Graph actually gets created in cacti (not from running it manually).

The rrd is just not getting updated.

I created the data input method, data template and graph template for the script.

I don't know what I may be missing...

Any ideas?

Anonymous wrote:ok, this script works for me: 8)

Code: Select all

#!/usr/bin/perl 

$host = $ARGV[1]; 
$pingcount = $ARGV[0]; 

$ping = `ping -n $pingcount $host`; 

# starts some variables 

$min; 
$max; 
$avg;
$loss; 

# check for the minimum value 

	$ping =~ /Minimum = (\d*)/; 
        $min = $1; 

# check for the maximum value 
	
	$ping =~ /Maximum = (.*)ms,/; 
        $max = $1; 

# check for the average value 

        $ping =~ /(Averag|Gemiddeld)e = (.*)ms/; 
        $avg = $2; 	

# check for the % sign inside parens and grab it's value 
        $ping =~ /(\d*)%/; 
        $loss = $1; 

#print the values 
print "min:$min avg:$avg max:$max loss:$loss"; 
Only the graph doesn't appear (using my own written graph template)
I'm going to investigate that... The script itself should be working.

Now, that it is working I want more :P
Can it be replaced by a simple DOS batch script? Then I don't need to have Perl installed because I use it for nothing else but just that ping script :(
I've tried with grep (grep from borland, windows has not a standard grep function) but I don't think dos has powerfull commands like perl to work with strings and search for a specific value...
User avatar
dieter
Cacti User
Posts: 164
Joined: Fri Feb 20, 2004 3:08 pm

Post by dieter »

Got my graph template also working now...

Seems like you need to draw an invisible line (no color) for every source you want to put on your GPRINT (except for the one that you want to be drawed offcourse)

I don't understand your problem kurowsky. How can the graph being displayed correctly without the values being updated? Do you mean you get al -#1 or something? Just wait 5 minutes and the values will appear. or manually run cmd.php

Maybe you can try to run cmd.php in a dos prompt to see the output of the script. There may be something wrong in that step.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests