#!/usr/bin/perl -w # # check ping reply and packetloss on interlinks # # syntax: # ./check_interlink --host= [--warning=,%] [--critical=,%] --interlink= # # jorg@wirelessleiden.nl - 12 september 2005 use strict; use Getopt::Long; use Net::SNMP; #use Data::Dumper; # # settings # $ENV{PATH} = "/usr/bin"; my $ping = "/bin/ping"; my $hostname2 = ""; my $interlink = ""; my $warning = "500.0,10%"; my $critical = "750.0,50%"; my $community = "public"; GetOptions("hostname:s" => \$hostname2, "warning:s" => \$warning, "critical:s" => \$critical, "interlink:s" => \$interlink); 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) or (! defined $interlink)) { print "usage: ./check_interlink --host= [--warning=,%] [--critical=,%] --interlink=\n"; exit 3; # unknown } # # get data # my ($session, $error) = Net::SNMP->session(-hostname => $hostname, -community => $community, -timeout => 60, -retries => 1); if (! defined $session) { printf("ERROR: %s.\n", $error); exit 3; # unknown } my @oids = (".1.3.6.1.4.1.2021.50.2"); my $result = $session->get_request(-varbindlist => \@oids); if (! defined $result) { print "No snmpd running or bad connection\n"; exit 0; # ok } my $counters; my @fields = split(/ /, $result->{$oids[0]}); foreach my $field (@fields) { my ($il, $data) = split(',', $field); if ($il eq $interlink) { $counters = $data; last; }; } if (! defined $counters) { print "Interlink not found\n"; exit 2; # error } # # parse data # my ($loss, $min, $avg, $max) = split(/:/, $counters); if (! defined $loss) { $loss = 100; } if (! defined $min) { $min = 'U'; } if (! defined $avg) { $avg = 'U'; } if (! defined $max) { $max = 'U'; } if ("$loss" ne "0") { print("$loss% packetloss. "); } if (($min eq 'U') or ($avg eq 'U') or ($max eq 'U')) { print "\n"; exit 2; # error } #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 }