#!/usr/bin/perl # # $Id$ # # latence.pl # TiChou # # Require iputils. # my $version = "1.0"; my $cmdname = (split('/', $0))[-1]; my $rrdlib = "/var/lib/rrd"; my $ping4 = "/bin/ping"; my $ping6 = "/bin/ping6"; use Getopt::Long; use RRDs; sub usage($) { my $usage = "Usage: $cmdname [options] host[,host...] Options: -4 use IPv4 ping (default) -6 use IPv6 ping -f, --file=FILE[,FILE...] specify RRD files (default $rrdlib/latence_HOST) -c, --count=NUM specify probe NUM icmp echo pings (default 10) -h, --help display this help and exit -v, --version display version Report bugs to .\n"; if (shift) { print STDOUT $usage; exit 0; } else { print STDERR $usage; exit 1; } } sub version { print "$cmdname $version\n"; exit 0; } my $ping = $ping4; my @rrdfiles = (); my $count= 10; Getopt::Long::Configure ("bundling"); GetOptions("4" => sub { $ping = $ping4}, "6" => sub { $ping = $ping6}, "f|file=s@" => \@rrdfiles, "c|count=i" => \$count, "h|help" => \&usage, "v|version" => \&version) or &usage(0); my @hosts = @ARGV or &usage(0); @rrdfiles = split(/,/,join(',',@rrdfiles)); @hosts = split(/,/,join(',',@hosts)); for (my $i = 0; $i < @hosts; $i++) { my $host = $hosts[$i]; my $file = $rrdfiles[$i] ? $rrdfiles[$i] : "$rrdlib/latence_$host.rrd"; if (not -f $file) { RRDs::create $file, qw(DS:min:GAUGE:600:0:U DS:avg:GAUGE:600:0:U DS:max:GAUGE:600:0:U DS:mdev:GAUGE:600:0:U DS:loss:GAUGE:600:0:100 RRA:AVERAGE:0.5:1:288 RRA:AVERAGE:0.5:6:336 RRA:AVERAGE:0.5:24:360 RRA:AVERAGE:0.5:288:365 RRA:MIN:0.5:1:288 RRA:MIN:0.5:6:336 RRA:MIN:0.5:24:360 RRA:MIN:0.5:288:365 RRA:MAX:0.5:1:288 RRA:MAX:0.5:6:336 RRA:MAX:0.5:24:360 RRA:MAX:0.5:288:365); my $error = RRDs::error; die "$0: $error\n" if $error; } my ($min,$avg,$max,$mdev,$loss) = ("U") x 5; open(PING, "$ping -nqc$count $host|") or die "$0: unable to run $ping: $!\n"; while () { /(\d+)% packet loss/ && ($loss = $1); /= (.+)\/(.+)\/(.+)\/(.+) ms/ && (($min,$avg,$max,$mdev) = ($1,$2,$3,$4)); }; close(PING); RRDs::update $file, "N:$min:$avg:$max:$mdev:$loss"; my $error = RRDs::error; die "$0: $error\n" if $error; }