summaryrefslogtreecommitdiffstats
path: root/contrib/check_traceroute-pure_perl.pl
blob: 96d1ff82560dff8341a5965030134e97f9f8c72a (plain)
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
#!/usr/bin/perl -Wall
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
#
# check_traceroute is designed to generate and alarm if a traceroute exceeds
# a certain number of hops. This is useful in cases where a multi-homed 
# network looses a connection and can still ping the remote end because
# traffic is being re-routed out through the redundent connection.
#
# check_traceroute v0.1
# By mp@xmission.com
#
# Thanks to Sebastian Hetze, Linux Information Systems AG who wrote the
# excellent check_apache plugin, which this plugin was modeled after.
#
#############################################################################

use strict;
use Net::Traceroute;
use Getopt::Long;

my $version = "v0.1";
my $opt_v;
my $opt_t=undef; #for usage () unless ($opt_t) to work right
my $opt_help;
my $opt_w;
my $opt_c;

my %ERRORS = ('UNKNOWN' , '-1',
              'OK' , '0',
              'WARNING', '1',
              'CRITICAL', '2');
# Set this to whatever you like, but make sure you don't hang Nagios
# for too long.
my $timeout = "30";

GetOptions 
	("v"   => \$opt_v, "t=s"	=> \$opt_t, 
	"help"	=> \$opt_help, "h"	=> \$opt_help,
	"w=i"	=> \$opt_w, "c=i"	=> \$opt_c
	);

if ($opt_v) {
	print "\nThis is check_traceroute version $version\n";
	print "\n";
	print "Please report errors to mp\@xmission\.com";
	print "\n\n";
	}

#subs	
sub print_help () {
	print "\n\nThis is check_traceroute.pl. It is designed to send an alert\n";
	print "to Nagios if a route to a particular destination exceeds a\n";
	print "certain number of hops.\n\n";
	print "Usage:\n";
	print "\n";
	print "--help Display this help.\n";
	print "-v Display the version number of check_traceroute.\n";
	print "-t Host that you wish to traceroute to.\n";
	print "-w Number of hops before Nagios generates a WARNING.\n";
	print "-c Number of hops before Nagios generates a CRITICAL.\n";
	}

sub usage() {
	print "check_traceroute -t <host> [-w <warning>] [-c <critical>]\n";
	}

sub do_check() {
	if ($opt_t) {
		unless ($opt_w && $opt_c) {die "You must specify thresholds using -c and -w\n";}
		my $tr = Net::Traceroute->new(host=>"$opt_t") || die "Can't init traceroute!\n";
		if($tr->found) {
			my $hops = $tr->hops;
			if($hops > $opt_w) {
				print "Warning: $opt_t is $hops hops away!\n";
				exit $ERRORS{'WARNING'};
			}
			elsif($hops > $opt_c) {
				print "Critical: $opt_t is $hops hops away!\n";
				exit $ERRORS{'CRITICAL'};
			}
			else {
				print "OK: $opt_t is $hops hops away\n";
				exit $ERRORS{'OK'};
				}
		}		
		else {
			print "Couldn't locate host $opt_t!\n";
			exit $ERRORS{'UNKNOWN'};
		}
	}
	}

# Must be placed at the end for -Wall to compile cleanly, blech
if ($opt_help) {
	print_help();
	}
usage() unless ($opt_t);
#timeoutes
$SIG{'ALRM'} = sub {
     print ("ERROR: No response from $opt_t (timeout) in $timeout seconds\n");
          exit $ERRORS{"UNKNOWN"};
	  };
	  alarm($timeout);
do_check();