1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
#!/usr/bin/perl
# $Id: check_traceroute.pl,v 1.1 2005/01/27 10:34:16 stanleyhopcroft Exp $
# $Log: check_traceroute.pl,v $
# Revision 1.1 2005/01/27 10:34:16 stanleyhopcroft
# Jon Meek's check_traceroute for Mon hacked by YT for Nagios. Prob pretty weak
#
use strict ;
use vars qw(%ERRORS $TIMEOUT) ;
use utils qw(%ERRORS $TIMEOUT &print_revision &support &usage) ;
sub print_help ();
sub print_usage ();
$ENV{'PATH'}='/bin:/usr/bin:/usr/sbin';
my $PROGNAME = 'check_traceroute' ;
# delay units are millisecs.
my $MAX_INTERHOP_DELAY = 200 ;
my $MAX_HOPS = 30 ;
use Getopt::Std;
use vars qw($opt_H $opt_N $opt_r $opt_R $opt_T $opt_d $opt_h $opt_i $opt_v $opt_V) ;
getopts('i:H:N:R:T:dhrvV');
# H, N, R, T, and i take parms, others are flags
do { print_help ; exit $ERRORS{OK}; }
if $opt_h ;
do { print_revision($PROGNAME, '$Revision: 1.1 $'); exit $ERRORS{OK}; }
if $opt_V ;
do { print_help; exit $ERRORS{OK}; }
unless $opt_R || $opt_r ;
do { print_help; exit $ERRORS{OK}; }
unless $opt_R =~ m|
(?:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}-)+
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
|x
|| $opt_r ;
my $should_be = $opt_R ;
# Set default timeout in seconds
my $TimeOut = $opt_T || $TIMEOUT;
my $max_interhop_delay = $opt_i || $MAX_INTERHOP_DELAY ;
my $max_hops = $opt_N || $MAX_HOPS ;
my $TRACEROUTE = '/usr/sbin/traceroute';
my $TargetHost = $opt_H ;
print_help
unless $TargetHost ;
my ($route, $pid, $rta_list) = ( '', '', '' );
my %ResultString = () ;
$SIG{ALRM} = sub { die "timeout" };
eval {
alarm($TimeOut);
# XXXX Discarding STDERR _should_ reduce the risk
# of unexpected output but consequently, results for
# non existent hosts are stupid. However, why would you
# specify a route to a NX host, other than a typo ...
$pid = open(TR, "$TRACEROUTE -n $TargetHost 2>/dev/null |")
or do {
"Failed. Cannot fork \"$TRACEROUTE\": $!" ;
$ERRORS{UNKNOWN} ;
} ;
my $hops = 0 ;
while (<TR>) {
print $_
if $opt_d;
if ( m|#\*\s+\*\s+\*| ) {
# Get * * * then give up
$route .= '*';
# 13 = PIPE, prevents Broken Pipe Error, at least on Solaris
kill 13, $pid;
last;
}
# We will only pick up the first IP address listed on a line for now
# traceroute to csg.citec.com.au (203.9.184.12), 64 hops max, 44 byte packets
# 1 10.254.254.254 0.868 ms 0.728 ms 0.705 ms
# 2 192.168.9.1 1.240 ms 1.165 ms 1.191 ms
my ($ThisHopIP) = m|\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+|;
my ($max_rta) = m|\d{1,3}\.\d{1,3}\.\d{1,3}\s+ (\d+\.\d+) ms| ;
$route .= $ThisHopIP . '-';
$rta_list .= sprintf("%.1f", $max_rta) . '-' ;
if ( $opt_v ) {
chomp $_ ;
print $_, ' ' x (58 - length), $route, "\n";
}
$hops++ ;
if ( ($hops >= $max_hops) && ! $opt_r ) {
kill 13, $pid ;
print qq(Failed. Max hops ($max_hops) exceeeded: incomplete after $hops hops, "$route".\n) ;
exit $ERRORS{CRITICAL} ;
}
if ( ($hops %2 == 0) && ($hops >= 4) && ! $opt_r ) {
# Check for 2 cycles at end of path ie -(a-b)-(a-b)$
# where a and b are IP v4 addresses of IS (routers).
my ($last_2_is) = $route =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}-\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})-$/ ;
if ( $route =~ /$last_2_is-$last_2_is-$/ ) {
kill 13, $pid ;
print qq(Failed. Last 2 routers ($last_2_is) repeated, "$route".\n) ;
exit $ERRORS{CRITICAL} ;
}
}
}
};
alarm(0);
if ( $@ and $@ =~ /timeout/ ) {
$route .= '*';
# It was a traceroute timeout
kill 13, $pid;
} elsif ( $@ and $@ !~ /timeout/ ) {
close TR ;
print "Failed. Somethings gone wrong with \"$TRACEROUTE\": $!" ;
exit $ERRORS{UNKNOWN} ;
}
close TR;
# Remove trailing '-'s
# $route =~ s/\-$//;
chop($route) ;
chop($rta_list) ;
print "$route\n"
if $opt_d;
if ( $opt_r ) {
print qq(Ok. Traceroute to host "$TargetHost" via route "$route".\n) ;
exit $ERRORS{OK};
}
if ( &RouteEqual($should_be, $route) ) {
print qq(Ok. Traceroute to "$TargetHost" via expected route "$route" ($rta_list).\n) ;
exit $ERRORS{OK};
} else {
print qq(Failed. Route "$route" ne expected "$should_be".\n) ;
exit $ERRORS{CRITICAL};
}
sub RouteEqual {
my ($current_route, $prev_route) = @_;
return $current_route eq $prev_route ;
}
sub print_usage () {
print "Usage: $PROGNAME [ -R <route_string>|-r ] [ -d -T timeout -v -h -i ] -H <host>\n";
}
sub print_help () {
print_revision($PROGNAME, '$Revision: 1.1 $') ;
print "Copyright (c) 2004 J Meek/Karl DeBisschop
This plugin checks whether traceroute to the destination succeeds and if so that the route string option (-R) matches the list of routers
returned by traceroute.
";
print_usage();
print "
-d
Debug
-h
Help
-i
_TODO_
Max inter-hop delay (msec).
-H
Host.
-N
Max number of hops.
-r
Record current route (and output to STDOUT). Useful for getting the value of -R option ...
-v
Greater verbosity.
-R
Mandatory route string ie r1-r2-... where ri is the ip address of the ith router.
-T
Maximum time (seconds) to wait for the traceroute command to complete. Defaults to $TIMEOUT seconds.
";
support();
}
|