Latency Graphs with MRTG

By Tim Pozar

I got this spiffy 384Kb/s SDSL connection via a local ISP and found that during the day my lantency to the ISPs router that my router at home talks to, is getting pretty high. I suspected that they are overloading their DSL provider and wanted to log this via MRTG to document for a complaint. A quick check with some search engines only showed one ping script that I thought was far to complicated and I wanted a new challenge.

So here it is. The external latency script and the mrtg.cfg fragment that you would add. This script is also a good example of how MRTG wants the output of its external programs.

You can check out the live graphs and see how my provider is doing.

mrtgping.pl...

#!/usr/local/bin/perl

$ipaddr = "NULL";
$ipaddr = $ARGV[0];
$numpings = 5;
$mrtgpinglog = ">> /usr/local/etc/mrtg/mrtgping.log";

if ($ipaddr eq "NULL"){
        print "Usage mrtgping.pl [ipaddress]\n";
        exit;
}

open(MRTGPINGLOG, $mrtgpinglog);

$date = `date +%Y%m%d%H%M%S`;
chop ($date);

$result = `/sbin/ping -c $numpings -n -q $ipaddr | /usr/bin/tail -1`;
chop($result);

# Ping result will look like... 
# round-trip min/avg/max/stddev = 18.707/91.234/325.268/118.491 ms

($descp1,$descp2,$equal,$data) = split(/ /,$result);

($min,$avg,$max,$stddev) = split(/\//,$data);

($avgint,$avgdec) = split(/\./,$avg);
($stddevint,$stddevdec) = split(/\./,$stddev);

print MRTGPINGLOG "$date: ";
print MRTGPINGLOG "$avgint ";
print MRTGPINGLOG "$stddevint ";
print MRTGPINGLOG "0 ";
print MRTGPINGLOG "$ipaddr\n";

print "$avgint\n";
print "$stddevint\n";
print "0\n";
print "$ipaddr\n";

mrtg.cfg snippet...

Target[sdsllat1]: `/usr/local/etc/mrtg/mrtgping.pl 209.152.183.1`
MaxBytes[sdsllat1]: 1250000
Title[sdsllat1]: SDSL Latency
# remember not to define "bits" globally or here as everything will be multiplied by 8...
Options[sdsllat1]: gauge noinfo, nopercent
YLegend[sdsllat1]: mS
ShortLegend[sdsllat1]: mS
LegendI[sdsllat1]:  Avg.:
LegendO[sdsllat1]:  Stddev:
Legend1[sdsllat1]: Average Round Trip Time in ms
Legend2[sdsllat1]: StdDev Round Trip Time in ms