check_ifoperstatus.pl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/perl -w
  2. #
  3. # check_ifoperstatus.pl - nagios plugin
  4. #
  5. #
  6. #
  7. #
  8. # Copyright (C) 2000 Christoph Kron
  9. #
  10. # This program is free software; you can redistribute it and/or
  11. # modify it under the terms of the GNU General Public License
  12. # as published by the Free Software Foundation; either version 2
  13. # of the License, or (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License
  21. # along with this program; if not, write to the Free Software
  22. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. #
  24. #
  25. # Report bugs to: ck@zet.net
  26. #
  27. # 11.01.2000 Version 1.0
  28. use strict;
  29. use Net::SNMP;
  30. use Getopt::Long;
  31. &Getopt::Long::config('auto_abbrev');
  32. my $status;
  33. my $TIMEOUT = 15;
  34. my %ERRORS = ('UNKNOWN' , '-1',
  35. 'OK' , '0',
  36. 'WARNING', '1',
  37. 'CRITICAL', '2');
  38. my %ifOperStatus = ('1','up',
  39. '2','down',
  40. '3','testing',
  41. '4','unknown',
  42. '5','dormant',
  43. '6','notPresent');
  44. my $state = "UNKNOWN";
  45. my $answer = "";
  46. my $snmpkey = 1;
  47. my $community = "public";
  48. my $port = 161;
  49. my @snmpoids;
  50. my $snmpIfOperStatus;
  51. my $snmpLocIfDescr;
  52. my $hostname;
  53. my $session;
  54. my $error;
  55. my $response;
  56. sub usage {
  57. printf "\nMissing arguments!\n";
  58. printf "\n";
  59. printf "Perl Check IfOperStatus plugin for Nagios\n";
  60. printf "checks operational status of specified interface\n";
  61. printf "usage: \n";
  62. printf "ifoperstatus.pl -k <IF_KEY> -c <READCOMMUNITY> -p <PORT> <HOSTNAME>";
  63. printf "\nCopyright (C) 2000 Christoph Kron\n";
  64. printf "check_ifoperstatus.pl comes with ABSOLUTELY NO WARRANTY\n";
  65. printf "This programm is licensed under the terms of the ";
  66. printf "GNU General Public License\n(check source code for details)\n";
  67. printf "\n\n";
  68. exit $ERRORS{"UNKNOWN"};
  69. }
  70. # Just in case of problems, let's not hang Nagios
  71. $SIG{'ALRM'} = sub {
  72. print ("ERROR: No snmp response from $hostname (alarm)\n");
  73. exit $ERRORS{"UNKNOWN"};
  74. };
  75. alarm($TIMEOUT);
  76. $status = GetOptions("key=i",\$snmpkey,
  77. "community=s",\$community,
  78. "port=i",\$port);
  79. if ($status == 0)
  80. {
  81. &usage;
  82. }
  83. #shift;
  84. $hostname = shift || &usage;
  85. ($session, $error) = Net::SNMP->session(
  86. -hostname => $hostname,
  87. -community => $community,
  88. -port => $port
  89. );
  90. if (!defined($session)) {
  91. $state='UNKNOWN';
  92. $answer=$error;
  93. print ("$state: $answer");
  94. exit $ERRORS{$state};
  95. }
  96. $snmpIfOperStatus = '1.3.6.1.2.1.2.2.1.8' . "." . $snmpkey;
  97. $snmpLocIfDescr = '1.3.6.1.4.1.9.2.2.1.1.28' . "." . $snmpkey;
  98. push(@snmpoids,$snmpIfOperStatus);
  99. push(@snmpoids,$snmpLocIfDescr);
  100. if (!defined($response = $session->get_request(@snmpoids))) {
  101. $answer=$session->error;
  102. $session->close;
  103. $state = 'CRITICAL';
  104. print ("$state: $answer,$community,$snmpkey");
  105. exit $ERRORS{$state};
  106. }
  107. $answer = sprintf("host '%s',%s(%s) is %s\n",
  108. $hostname,
  109. $response->{$snmpLocIfDescr},
  110. $snmpkey,
  111. $ifOperStatus{$response->{$snmpIfOperStatus}}
  112. );
  113. $session->close;
  114. if ( $response->{$snmpIfOperStatus} == 1 ) {
  115. $state = 'OK';
  116. }
  117. else {
  118. $state = 'CRITICAL';
  119. }
  120. print ("$state: $answer");
  121. exit $ERRORS{$state};