check_ifstatus.pl 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #!/usr/local/bin/perl -w
  2. #
  3. # check_ifstatus.pl - nagios plugin
  4. #
  5. #
  6. # Copyright (C) 2000 Christoph Kron
  7. # Modified 5/2002 to conform to updated Nagios Plugin Guidelines (S. Ghosh)
  8. # Added -x option (4/2003)
  9. # Added -u option (4/2003)
  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: ck@zet.net, nagiosplug-help@lists.sf.net
  27. #
  28. # 11.01.2000 Version 1.0
  29. #
  30. # $Id$
  31. use POSIX;
  32. use strict;
  33. use lib utils.pm ;
  34. use utils qw($TIMEOUT %ERRORS &print_revision &support);
  35. use Net::SNMP;
  36. use Getopt::Long;
  37. Getopt::Long::Configure('bundling');
  38. my $PROGNAME = "check_ifstatus";
  39. my $status;
  40. my %ifOperStatus = ('1','up',
  41. '2','down',
  42. '3','testing',
  43. '4','unknown',
  44. '5','dormant',
  45. '6','notPresent');
  46. my $state = "UNKNOWN";
  47. my $answer = "";
  48. my $snmpkey=0;
  49. my $snmpoid=0;
  50. my $key=0;
  51. my $community = "public";
  52. my $port = 161;
  53. my @snmpoids;
  54. my $snmpIfAdminStatus = '1.3.6.1.2.1.2.2.1.7';
  55. my $snmpIfDescr = '1.3.6.1.2.1.2.2.1.2';
  56. my $snmpIfOperStatus = '1.3.6.1.2.1.2.2.1.8';
  57. my $snmpIfName = '1.3.6.1.2.1.31.1.1.1.1';
  58. my $snmpIfAlias = '1.3.6.1.2.1.31.1.1.1.18';
  59. my $snmpLocIfDescr = '1.3.6.1.4.1.9.2.2.1.1.28';
  60. my $snmpIfType = '1.3.6.1.2.1.2.2.1.3';
  61. my $hostname;
  62. my $session;
  63. my $error;
  64. my $response;
  65. my %ifStatus;
  66. my $ifup =0 ;
  67. my $ifdown =0;
  68. my $ifdormant = 0;
  69. my $ifexclude = 0 ;
  70. my $ifunused = 0;
  71. my $ifmessage = "";
  72. my $snmp_version = 1;
  73. my $ifXTable;
  74. my $opt_h ;
  75. my $opt_V ;
  76. my $opt_u;
  77. my $opt_x ;
  78. my %excluded ;
  79. my @unused_ports ;
  80. # Just in case of problems, let's not hang Nagios
  81. $SIG{'ALRM'} = sub {
  82. print ("ERROR: No snmp response from $hostname (alarm timeout)\n");
  83. exit $ERRORS{"UNKNOWN"};
  84. };
  85. alarm($TIMEOUT);
  86. #Option checking
  87. $status = GetOptions(
  88. "V" => \$opt_V, "version" => \$opt_V,
  89. "h" => \$opt_h, "help" => \$opt_h,
  90. "v=i" => \$snmp_version, "snmp_version=i" => \$snmp_version,
  91. "C=s" =>\$community,"community=s" => \$community,
  92. "p=i" =>\$port, "port=i" => \$port,
  93. "H=s" => \$hostname, "hostname=s" => \$hostname,
  94. "I" => \$ifXTable, "ifmib" => \$ifXTable,
  95. "x:s" => \$opt_x, "exclude:s" => \$opt_x,
  96. "u=s" => \$opt_u, "unused_ports=s" => \$opt_u);
  97. if ($status == 0)
  98. {
  99. print_help() ;
  100. exit $ERRORS{'OK'};
  101. }
  102. if ($opt_V) {
  103. print_revision($PROGNAME,'$Revision$ ');
  104. exit $ERRORS{'OK'};
  105. }
  106. if ($opt_h) {
  107. print_help();
  108. exit $ERRORS{'OK'};
  109. }
  110. if (defined $opt_x) {
  111. my @x = split(/,/, $opt_x);
  112. if ( @x) {
  113. foreach $key (@x){
  114. $excluded{$key} = 1;
  115. }
  116. }else{
  117. $excluded{23} = 1; # default PPP(23) if empty list - note (AIX seems to think PPP is 22 according to a post)
  118. }
  119. #debugging
  120. #foreach $x (keys %excluded)
  121. # { print "key = $x val = $excluded{$x}\n";}
  122. }
  123. if ($opt_u) {
  124. @unused_ports = split(/,/,$opt_u);
  125. foreach $key (@unused_ports) {
  126. $ifStatus{$key}{'notInUse'}++ ;
  127. }
  128. }
  129. if (! utils::is_hostname($hostname)){
  130. usage();
  131. exit $ERRORS{"UNKNOWN"};
  132. }
  133. if ( ! $snmp_version ) {
  134. $snmp_version =1 ;
  135. }else{
  136. if ( $snmp_version =~ /[12]/ ) {
  137. ($session, $error) = Net::SNMP->session(
  138. -hostname => $hostname,
  139. -community => $community,
  140. -port => $port,
  141. -version => $snmp_version
  142. );
  143. if (!defined($session)) {
  144. $state='UNKNOWN';
  145. $answer=$error;
  146. print ("$state: $answer");
  147. exit $ERRORS{$state};
  148. }
  149. }elsif ( $snmp_version =~ /3/ ) {
  150. $state='UNKNOWN';
  151. print ("$state: No support for SNMP v3 yet\n");
  152. exit $ERRORS{$state};
  153. }else{
  154. $state='UNKNOWN';
  155. print ("$state: No support for SNMP v$snmp_version yet\n");
  156. exit $ERRORS{$state};
  157. }
  158. }
  159. push(@snmpoids,$snmpIfOperStatus);
  160. push(@snmpoids,$snmpIfAdminStatus);
  161. push(@snmpoids,$snmpIfDescr);
  162. push(@snmpoids,$snmpIfType);
  163. push(@snmpoids,$snmpIfName) if ( defined $ifXTable);
  164. push(@snmpoids,$snmpIfAlias) if ( defined $ifXTable);
  165. foreach $snmpoid (@snmpoids) {
  166. if (!defined($response = $session->get_table($snmpoid))) {
  167. $answer=$session->error;
  168. $session->close;
  169. $state = 'CRITICAL';
  170. if ( ( $snmpoid =~ $snmpIfName ) && defined $ifXTable ) {
  171. print ("$state: Device does not support ifTable - try without -I option\n");
  172. }else{
  173. print ("$state: $answer for $snmpoid with snmp version $snmp_version\n");
  174. }
  175. exit $ERRORS{$state};
  176. }
  177. foreach $snmpkey (keys %{$response}) {
  178. $snmpkey =~ /.*\.(\d+)$/;
  179. $key = $1;
  180. $ifStatus{$key}{$snmpoid} = $response->{$snmpkey};
  181. }
  182. }
  183. $session->close;
  184. foreach $key (keys %ifStatus) {
  185. # skip unused interfaces
  186. if (!defined($ifStatus{$key}{'notInUse'})) {
  187. # check only if interface is administratively up
  188. if ($ifStatus{$key}{$snmpIfAdminStatus} == 1 ) {
  189. # check only if interface type is not listed in %excluded
  190. if (!defined $excluded{$ifStatus{$key}{$snmpIfType}} ) {
  191. if ($ifStatus{$key}{$snmpIfOperStatus} == 1 ) { $ifup++ ;}
  192. if ($ifStatus{$key}{$snmpIfOperStatus} == 2 ) {
  193. $ifdown++ ;
  194. if (defined $ifXTable) {
  195. $ifmessage .= sprintf("%s: down -> %s<BR>",
  196. $ifStatus{$key}{$snmpIfName},
  197. $ifStatus{$key}{$snmpIfAlias});
  198. }else{
  199. $ifmessage .= sprintf("%s: down <BR>",
  200. $ifStatus{$key}{$snmpIfDescr});
  201. }
  202. }
  203. if ($ifStatus{$key}{$snmpIfOperStatus} == 5 ) { $ifdormant++ ;}
  204. }else{
  205. $ifexclude++;
  206. }
  207. }
  208. }else{
  209. $ifunused++;
  210. }
  211. }
  212. if ($ifdown > 0) {
  213. $state = 'CRITICAL';
  214. $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d, excluded: %d, unused: %d<BR>",
  215. $hostname,
  216. $ifup,
  217. $ifdown,
  218. $ifdormant,
  219. $ifexclude,
  220. $ifunused);
  221. $answer = $answer . $ifmessage . "\n";
  222. }
  223. else {
  224. $state = 'OK';
  225. $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d, excluded: %d, unused: %d",
  226. $hostname,
  227. $ifup,
  228. $ifdown,
  229. $ifdormant,
  230. $ifexclude,
  231. $ifunused);
  232. }
  233. my $perfdata = sprintf("up:%d,down:%d,dormant:%d,excluded:%d,unused:%d",$ifup,$ifdown,$ifdormant,$ifexclude,$ifunused);
  234. print ("$state: $answer |$perfdata\n");
  235. exit $ERRORS{$state};
  236. sub usage {
  237. printf "\nMissing arguments!\n";
  238. printf "\n";
  239. printf "check_ifstatus -C <READCOMMUNITY> -p <PORT> -H <HOSTNAME>\n";
  240. printf "Copyright (C) 2000 Christoph Kron\n";
  241. printf "Updates 5/2002 Subhendu Ghosh\n";
  242. printf "\n\n";
  243. support();
  244. exit $ERRORS{"UNKNOWN"};
  245. }
  246. sub print_help {
  247. printf "check_ifstatus plugin for Nagios monitors operational \n";
  248. printf "status of each network interface on the target host\n";
  249. printf "\nUsage:\n";
  250. printf " -H (--hostname) Hostname to query - (required)\n";
  251. printf " -C (--community) SNMP read community (defaults to public,\n";
  252. printf " used with SNMP v1 and v2c\n";
  253. printf " -v (--snmp_version) 1 for SNMP v1 (default)\n";
  254. printf " 2 for SNMP v2c\n";
  255. printf " SNMP v2c will use get_bulk for less overhead\n";
  256. printf " -p (--port) SNMP port (default 161)\n";
  257. printf " -I (--ifmib) Agent supports IFMIB ifXTable. For Cisco - this will provide\n";
  258. printf " the descriptive name. Do not use if you don't know what this is. \n";
  259. printf " -x (--exclude) A comma separated list of ifType values that should be excluded \n";
  260. printf " from the report (default for an empty list is PPP(23).\n";
  261. printf " See the IANAifType-MIB for a list of interface types.\n";
  262. printf " -V (--version) Plugin version\n";
  263. printf " -h (--help) usage help \n\n";
  264. print_revision($PROGNAME, '$Revision$');
  265. }