Code

Fix for regex input of '|', being output causing problems with Nagios' parsing of
[nagiosplug.git] / contrib / check_traceroute-pure_perl.pl
1 #!/usr/bin/perl -Wall
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 #
17 #
18 # check_traceroute is designed to generate and alarm if a traceroute exceeds
19 # a certain number of hops. This is useful in cases where a multi-homed 
20 # network looses a connection and can still ping the remote end because
21 # traffic is being re-routed out through the redundent connection.
22 #
23 # check_traceroute v0.1
24 # By mp@xmission.com
25 #
26 # Thanks to Sebastian Hetze, Linux Information Systems AG who wrote the
27 # excellent check_apache plugin, which this plugin was modeled after.
28 #
29 #############################################################################
31 use strict;
32 use Net::Traceroute;
33 use Getopt::Long;
35 my $version = "v0.1";
36 my $opt_v;
37 my $opt_t=undef; #for usage () unless ($opt_t) to work right
38 my $opt_help;
39 my $opt_w;
40 my $opt_c;
42 my %ERRORS = ('UNKNOWN' , '-1',
43               'OK' , '0',
44               'WARNING', '1',
45               'CRITICAL', '2');
46 # Set this to whatever you like, but make sure you don't hang Nagios
47 # for too long.
48 my $timeout = "30";
50 GetOptions 
51         ("v"   => \$opt_v, "t=s"        => \$opt_t, 
52         "help"  => \$opt_help, "h"      => \$opt_help,
53         "w=i"   => \$opt_w, "c=i"       => \$opt_c
54         );
56 if ($opt_v) {
57         print "\nThis is check_traceroute version $version\n";
58         print "\n";
59         print "Please report errors to mp\@xmission\.com";
60         print "\n\n";
61         }
63 #subs   
64 sub print_help () {
65         print "\n\nThis is check_traceroute.pl. It is designed to send an alert\n";
66         print "to Nagios if a route to a particular destination exceeds a\n";
67         print "certain number of hops.\n\n";
68         print "Usage:\n";
69         print "\n";
70         print "--help Display this help.\n";
71         print "-v Display the version number of check_traceroute.\n";
72         print "-t Host that you wish to traceroute to.\n";
73         print "-w Number of hops before Nagios generates a WARNING.\n";
74         print "-c Number of hops before Nagios generates a CRITICAL.\n";
75         }
77 sub usage() {
78         print "check_traceroute -t <host> [-w <warning>] [-c <critical>]\n";
79         }
81 sub do_check() {
82         if ($opt_t) {
83                 unless ($opt_w && $opt_c) {die "You must specify thresholds using -c and -w\n";}
84                 my $tr = Net::Traceroute->new(host=>"$opt_t") || die "Can't init traceroute!\n";
85                 if($tr->found) {
86                         my $hops = $tr->hops;
87                         if($hops > $opt_w) {
88                                 print "Warning: $opt_t is $hops hops away!\n";
89                                 exit $ERRORS{'WARNING'};
90                         }
91                         elsif($hops > $opt_c) {
92                                 print "Critical: $opt_t is $hops hops away!\n";
93                                 exit $ERRORS{'CRITICAL'};
94                         }
95                         else {
96                                 print "OK: $opt_t is $hops hops away\n";
97                                 exit $ERRORS{'OK'};
98                                 }
99                 }               
100                 else {
101                         print "Couldn't locate host $opt_t!\n";
102                         exit $ERRORS{'UNKNOWN'};
103                 }
104         }
105         }
107 # Must be placed at the end for -Wall to compile cleanly, blech
108 if ($opt_help) {
109         print_help();
110         }
111 usage() unless ($opt_t);
112 #timeoutes
113 $SIG{'ALRM'} = sub {
114      print ("ERROR: No response from $opt_t (timeout) in $timeout seconds\n");
115           exit $ERRORS{"UNKNOWN"};
116           };
117           alarm($timeout);
118 do_check();