check_arping.pl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #! /usr/bin/perl -w
  2. #
  3. # check_arping.pl - Nagios plugin to check host status via ARP ping
  4. #
  5. # usage:
  6. # check_arping -H hostname -I interface -T timeout
  7. #
  8. #
  9. # Copyright (C) 2003 Kenny Root
  10. #
  11. # This program is free software; you can redistribute it and/or
  12. # modify it under the terms of the GNU General Public License
  13. # as published by the Free Software Foundation; either version 2
  14. # of the License, or (at your option) any later version.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU General Public License
  22. # along with this program; if not, write to the Free Software
  23. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24. #
  25. #
  26. # Report bugs to: kenny@the-b.org, nagiosplug-help@lists.sf.net
  27. use POSIX;
  28. use strict;
  29. use lib "/usr/lib/nagios/plugins" ;
  30. use utils qw($TIMEOUT %ERRORS &print_revision &support);
  31. use Net::Arping;
  32. use Getopt::Long;
  33. my $PROGNAME = "check_arping";
  34. my($status, $state, $answer);
  35. my($opt_V, $opt_h, $opt_t, $opt_I, $opt_H);
  36. #Option checking
  37. $status = GetOptions(
  38. "V|version" => \$opt_V,
  39. "help" => \$opt_h,
  40. "I|interface=s" => \$opt_I,
  41. "H|host=s" => \$opt_H,
  42. "t|timeout=i" => \$opt_t);
  43. if ($status == 0)
  44. {
  45. print_help() ;
  46. exit $ERRORS{'OK'};
  47. }
  48. if ($opt_V) {
  49. print_revision($PROGNAME,'$Revision$ ');
  50. exit $ERRORS{'OK'};
  51. }
  52. if ($opt_h) {
  53. print_help();
  54. exit $ERRORS{'OK'};
  55. }
  56. if ($opt_t) {
  57. if ($opt_t ne int($opt_t)) {
  58. print "Timeout not in seconds!\n";
  59. print_help();
  60. exit $ERRORS{'OK'};
  61. }
  62. $opt_t = int($opt_t);
  63. } else {
  64. $opt_t = 3;
  65. }
  66. if (! utils::is_hostname($opt_H)){
  67. usage();
  68. exit $ERRORS{"UNKNOWN"};
  69. }
  70. my $ping = Net::Arping->new();
  71. my $reply = $ping->arping(Host => $opt_H, Interface => $opt_I, Timeout => $opt_t);
  72. if ($reply eq "0") {
  73. $state = "CRITICAL";
  74. print "$state: no reply from $opt_H on interface $opt_I in $opt_t seconds.\n";
  75. exit $ERRORS{$state};
  76. } else {
  77. $state = "OK";
  78. $answer = "replied with MAC address $reply";
  79. }
  80. print "ARPING $state - $answer\n";
  81. exit $ERRORS{$state};
  82. sub usage {
  83. print "\nMissing arguments!\n";
  84. print "\n";
  85. print "check_arping -I <interface> -H <host IP> [-t <timeout>]\n";
  86. print "\n\n";
  87. support();
  88. exit $ERRORS{"UNKNOWN"};
  89. }
  90. sub print_help {
  91. print "check_arping pings hosts that normally wouldn't allow\n";
  92. print "ICMP packets but are still on the local network.\n";
  93. print "\nUsage:\n";
  94. print " -H (--host) IP to query - (required)\n";
  95. print " -I (--interface) Interface to use.\n";
  96. print " -t (--timeout) Timeout in seconds.\n";
  97. print " -V (--version) Plugin version\n";
  98. print " -h (--help) usage help \n\n";
  99. print_revision($PROGNAME, '$Revision$');
  100. }