#!/usr/bin/perl -w # # check ping reply and packetloss # # syntax: # ./check_ping --host= [--warning=,%] [--critical=,%] [--count=] # # jorg@wirelessleiden.nl - 16 april 2004 use strict; use Getopt::Long; #use Data::Dumper; # # settings # $ENV{PATH} = "/usr/bin"; my $ping = "/sbin/ping"; my $hostname2 = ""; my $warning = "500.0,10%"; my $critical = "750.0,50%"; my $count = 10; GetOptions("hostname:s" => \$hostname2, "warning:s" => \$warning, "critical:s" => \$critical, "count:i" => \$count); my $errorcode = 0; my ($hostname) = $hostname2 =~ /([\/\w\.\-]+)/s; my ($reply_warning, $loss_warning) = parse_csv($warning); my ($reply_critical, $loss_critical) = parse_csv($critical); $loss_warning =~ s/\%//; $loss_critical =~ s/\%//; if (! defined $hostname) { print "usage: ./check_ping --host= [--warning=,%] [--critical=,%] [--count=]\n"; exit 3; # unknown } if (! -e "$ping") { print "$ping not found\n"; exit 3; # unknown } # # get data # my @result = `$ping -qc $count $hostname 2>/dev/null | tail -2`; # # parse data # if ((! defined $result[0]) && (! defined $result[1])) { print("100% packetloss|100:U:U:U\n"); exit 2; # alert } chop($result[0]); chop($result[1]); if (($result[0] =~ '---') || ($result[1] eq '')) { # bsd- or linux-type result print("100% packetloss|100:U:U:U\n"); exit 2; # alert } $result[0] =~ s/.*received, //; # remove first part $result[0] =~ s/%.*//; # remove last part $result[0] =~ s/.*, //; # sometimes there's something about duplicates. my $loss = $result[0]; if ("$loss" ne "0") { print("$loss% packetloss, "); } $result[1] =~ s/.* = //; $result[1] =~ s/ .*//; my $roundtrip = $result[1]; my ($min, $avg, $max, $stddev) = split(/\//, $roundtrip); #print "min/avg/max: $min / $avg / $max ms|$loss:$min:$avg:$max\n"; print "Response: $avg ms|$loss:$min:$avg:$max\n"; if (($loss > $loss_warning) || ($avg > $reply_warning)) { $errorcode = 1; } if (($loss > $loss_critical) || ($avg > $reply_critical)) { $errorcode = 2; } # # exit # (0=ok 1=warning 2=critical 3=unknown 4=dependent) # exit $errorcode; # ---------------------------------- # parse csv # sub parse_csv { my $text = shift; # record containing comma-separated values my @new = (); push(@new, $+) while $text =~ m{ # the first part groups the phrase inside the quotes. "([^\"\\]*(?:\\.[^\"\\]*)*)",? | ([^,]+),? | , }gx; push(@new, undef) if substr($text, -1,1) eq ','; return @new; # list of values that were comma-separated }