check_traceroute-pure_perl.pl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. use strict;
  31. use Net::Traceroute;
  32. use Getopt::Long;
  33. my $version = "v0.1";
  34. my $opt_v;
  35. my $opt_t=undef; #for usage () unless ($opt_t) to work right
  36. my $opt_help;
  37. my $opt_w;
  38. my $opt_c;
  39. my %ERRORS = ('UNKNOWN' , '-1',
  40. 'OK' , '0',
  41. 'WARNING', '1',
  42. 'CRITICAL', '2');
  43. # Set this to whatever you like, but make sure you don't hang Nagios
  44. # for too long.
  45. my $timeout = "30";
  46. GetOptions
  47. ("v" => \$opt_v, "t=s" => \$opt_t,
  48. "help" => \$opt_help, "h" => \$opt_help,
  49. "w=i" => \$opt_w, "c=i" => \$opt_c
  50. );
  51. if ($opt_v) {
  52. print "\nThis is check_traceroute version $version\n";
  53. print "\n";
  54. print "Please report errors to mp\@xmission\.com";
  55. print "\n\n";
  56. }
  57. #subs
  58. sub print_help () {
  59. print "\n\nThis is check_traceroute.pl. It is designed to send an alert\n";
  60. print "to Nagios if a route to a particular destination exceeds a\n";
  61. print "certain number of hops.\n\n";
  62. print "Usage:\n";
  63. print "\n";
  64. print "--help Display this help.\n";
  65. print "-v Display the version number of check_traceroute.\n";
  66. print "-t Host that you wish to traceroute to.\n";
  67. print "-w Number of hops before Nagios generates a WARNING.\n";
  68. print "-c Number of hops before Nagios generates a CRITICAL.\n";
  69. }
  70. sub usage() {
  71. print "check_traceroute -t <host> [-w <warning>] [-c <critical>]\n";
  72. }
  73. sub do_check() {
  74. if ($opt_t) {
  75. unless ($opt_w && $opt_c) {die "You must specify thresholds using -c and -w\n";}
  76. my $tr = Net::Traceroute->new(host=>"$opt_t") || die "Can't init traceroute!\n";
  77. if($tr->found) {
  78. my $hops = $tr->hops;
  79. if($hops > $opt_w) {
  80. print "Warning: $opt_t is $hops hops away!\n";
  81. exit $ERRORS{'WARNING'};
  82. }
  83. elsif($hops > $opt_c) {
  84. print "Critical: $opt_t is $hops hops away!\n";
  85. exit $ERRORS{'CRITICAL'};
  86. }
  87. else {
  88. print "OK: $opt_t is $hops hops away\n";
  89. exit $ERRORS{'OK'};
  90. }
  91. }
  92. else {
  93. print "Couldn't locate host $opt_t!\n";
  94. exit $ERRORS{'UNKNOWN'};
  95. }
  96. }
  97. }
  98. # Must be placed at the end for -Wall to compile cleanly, blech
  99. if ($opt_help) {
  100. print_help();
  101. }
  102. usage() unless ($opt_t);
  103. #timeoutes
  104. $SIG{'ALRM'} = sub {
  105. print ("ERROR: No response from $opt_t (timeout) in $timeout seconds\n");
  106. exit $ERRORS{"UNKNOWN"};
  107. };
  108. alarm($timeout);
  109. do_check();