~ubuntu-branches/ubuntu/trusty/nagios-plugins/trusty-proposed

« back to all changes in this revision

Viewing changes to contrib/check_traceroute-pure_perl.pl

  • Committer: Package Import Robot
  • Author(s): Yolanda Robla
  • Date: 2013-12-11 10:09:01 UTC
  • mfrom: (12.2.22 sid)
  • Revision ID: package-import@ubuntu.com-20131211100901-g0kqwx300l24be53
Tags: 1.5-1ubuntu1
* Merge from Debian unstable (LP: #1259863).  Remaining changes:
  - debian/control, debian/rules, debian/nagios-plugins-extra.dirs
     + add package nagios-plugins-extra
     + Suggest nagios-plugins-contrib in the universe package (-extras).
  - debian/control
     - replaces on nagios-plugins-basic (<< 1.4.16-1ubuntu1)
     - conflicts and replaces on nagios-plugins-extra

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
#############################################################################
30
 
 
31
 
use strict;
32
 
use Net::Traceroute;
33
 
use Getopt::Long;
34
 
 
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;
41
 
 
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";
49
 
 
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
 
        );
55
 
 
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
 
        }
62
 
 
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
 
        }
76
 
 
77
 
sub usage() {
78
 
        print "check_traceroute -t <host> [-w <warning>] [-c <critical>]\n";
79
 
        }
80
 
 
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
 
        }
106
 
 
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();
119