check_ifstatus.pl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. # Added -M option (10/2003)
  11. # Added SNMPv3 support (10/2003)
  12. #
  13. # This program is free software; you can redistribute it and/or
  14. # modify it under the terms of the GNU General Public License
  15. # as published by the Free Software Foundation; either version 2
  16. # of the License, or (at your option) any later version.
  17. #
  18. # This program is distributed in the hope that it will be useful,
  19. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. # GNU General Public License for more details.
  22. #
  23. # You should have received a copy of the GNU General Public License
  24. # along with this program; if not, write to the Free Software
  25. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  26. #
  27. #
  28. # Report bugs to: ck@zet.net, nagiosplug-help@lists.sf.net
  29. #
  30. # 11.01.2000 Version 1.0
  31. #
  32. use POSIX;
  33. use strict;
  34. use lib utils.pm ;
  35. use utils qw($TIMEOUT %ERRORS &print_revision &support);
  36. use Net::SNMP;
  37. use Getopt::Long;
  38. Getopt::Long::Configure('bundling');
  39. my $PROGNAME = "check_ifstatus";
  40. sub print_help ();
  41. sub usage ($);
  42. sub print_usage ();
  43. sub process_arguments ();
  44. my $status;
  45. my %ifOperStatus = ('1','up',
  46. '2','down',
  47. '3','testing',
  48. '4','unknown',
  49. '5','dormant',
  50. '6','notPresent',
  51. '7','lowerLayerDown'); # down due to the state of lower layer interface(s));
  52. my $timeout ;
  53. my $state = "UNKNOWN";
  54. my $answer = "";
  55. my $snmpkey=0;
  56. my $snmpoid=0;
  57. my $key=0;
  58. my $community = "public";
  59. my $maxmsgsize = 1472 ; # Net::SNMP default is 1472
  60. my ($seclevel, $authproto, $secname, $authpass, $privpass, $privproto, $auth, $priv, $context);
  61. my $port = 161;
  62. my @snmpoids;
  63. my $snmpIfAdminStatus = '1.3.6.1.2.1.2.2.1.7';
  64. my $snmpIfDescr = '1.3.6.1.2.1.2.2.1.2';
  65. my $snmpIfOperStatus = '1.3.6.1.2.1.2.2.1.8';
  66. my $snmpIfName = '1.3.6.1.2.1.31.1.1.1.1';
  67. my $snmpIfAlias = '1.3.6.1.2.1.31.1.1.1.18';
  68. my $snmpLocIfDescr = '1.3.6.1.4.1.9.2.2.1.1.28';
  69. my $snmpIfType = '1.3.6.1.2.1.2.2.1.3';
  70. my $hostname;
  71. my $session;
  72. my $error;
  73. my $response;
  74. my %ifStatus;
  75. my $ifup =0 ;
  76. my $ifdown =0;
  77. my $ifdormant = 0;
  78. my $ifexclude = 0 ;
  79. my $ifunused = 0;
  80. my $ifmessage = "";
  81. my $snmp_version = 1;
  82. my $ifXTable;
  83. my $opt_h ;
  84. my $opt_V ;
  85. my $opt_u;
  86. my $opt_x ;
  87. my %excluded ;
  88. my @unused_ports ;
  89. my %session_opts;
  90. # Just in case of problems, let's not hang Nagios
  91. $SIG{'ALRM'} = sub {
  92. print ("ERROR: No snmp response from $hostname (alarm timeout)\n");
  93. exit $ERRORS{"UNKNOWN"};
  94. };
  95. #Option checking
  96. $status = process_arguments();
  97. if ($status != 0)
  98. {
  99. print_help() ;
  100. exit $ERRORS{'OK'};
  101. }
  102. alarm($timeout);
  103. ($session, $error) = Net::SNMP->session(%session_opts);
  104. if (!defined($session)) {
  105. $state='UNKNOWN';
  106. $answer=$error;
  107. print ("$state: $answer\n");
  108. exit $ERRORS{$state};
  109. }
  110. push(@snmpoids,$snmpIfOperStatus);
  111. push(@snmpoids,$snmpIfAdminStatus);
  112. push(@snmpoids,$snmpIfDescr);
  113. push(@snmpoids,$snmpIfType);
  114. push(@snmpoids,$snmpIfName) if ( defined $ifXTable);
  115. push(@snmpoids,$snmpIfAlias) if ( defined $ifXTable);
  116. foreach $snmpoid (@snmpoids) {
  117. if (!defined($response = $session->get_table($snmpoid))) {
  118. $answer=$session->error;
  119. $session->close;
  120. $state = 'CRITICAL';
  121. if ( ( $snmpoid =~ $snmpIfName ) && defined $ifXTable ) {
  122. print ("$state: Device does not support ifTable - try without -I option\n");
  123. }else{
  124. print ("$state: $answer for $snmpoid with snmp version $snmp_version\n");
  125. }
  126. exit $ERRORS{$state};
  127. }
  128. foreach $snmpkey (keys %{$response}) {
  129. $snmpkey =~ /.*\.(\d+)$/;
  130. $key = $1;
  131. $ifStatus{$key}{$snmpoid} = $response->{$snmpkey};
  132. }
  133. }
  134. $session->close;
  135. alarm(0);
  136. foreach $key (keys %ifStatus) {
  137. # skip unused interfaces
  138. if (!defined($ifStatus{$key}{'notInUse'})) {
  139. # check only if interface is administratively up
  140. if ($ifStatus{$key}{$snmpIfAdminStatus} == 1 ) {
  141. # check only if interface type is not listed in %excluded
  142. if (!defined $excluded{$ifStatus{$key}{$snmpIfType}} ) {
  143. if ($ifStatus{$key}{$snmpIfOperStatus} == 1 ) { $ifup++ ;}
  144. if ($ifStatus{$key}{$snmpIfOperStatus} == 2 ) {
  145. $ifdown++ ;
  146. if (defined $ifXTable) {
  147. $ifmessage .= sprintf("%s: down -> %s<BR>",
  148. $ifStatus{$key}{$snmpIfName},
  149. $ifStatus{$key}{$snmpIfAlias});
  150. }else{
  151. $ifmessage .= sprintf("%s: down <BR>",
  152. $ifStatus{$key}{$snmpIfDescr});
  153. }
  154. }
  155. if ($ifStatus{$key}{$snmpIfOperStatus} == 5 ) { $ifdormant++ ;}
  156. }else{
  157. $ifexclude++;
  158. }
  159. }
  160. }else{
  161. $ifunused++;
  162. }
  163. }
  164. if ($ifdown > 0) {
  165. $state = 'CRITICAL';
  166. $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d, excluded: %d, unused: %d<BR>",
  167. $hostname,
  168. $ifup,
  169. $ifdown,
  170. $ifdormant,
  171. $ifexclude,
  172. $ifunused);
  173. $answer = $answer . $ifmessage . "\n";
  174. }
  175. else {
  176. $state = 'OK';
  177. $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d, excluded: %d, unused: %d",
  178. $hostname,
  179. $ifup,
  180. $ifdown,
  181. $ifdormant,
  182. $ifexclude,
  183. $ifunused);
  184. }
  185. my $perfdata = sprintf("up=%d,down=%d,dormant=%d,excluded=%d,unused=%d",$ifup,$ifdown,$ifdormant,$ifexclude,$ifunused);
  186. print ("$state: $answer |$perfdata\n");
  187. exit $ERRORS{$state};
  188. sub usage($) {
  189. print "$_[0]\n";
  190. print_usage();
  191. exit $ERRORS{"UNKNOWN"};
  192. }
  193. sub print_usage() {
  194. printf "\n";
  195. printf "usage: \n";
  196. printf "check_ifstatus -C <READCOMMUNITY> -p <PORT> -H <HOSTNAME>\n";
  197. printf "Copyright (C) 2000 Christoph Kron\n";
  198. printf "Updates 5/2002 Subhendu Ghosh\n";
  199. support();
  200. printf "\n\n";
  201. }
  202. sub print_help() {
  203. print_revision($PROGNAME, '@NP_VERSION@');
  204. print_usage();
  205. printf "check_ifstatus plugin for Nagios monitors operational \n";
  206. printf "status of each network interface on the target host\n";
  207. printf "\nUsage:\n";
  208. printf " -H (--hostname) Hostname to query - (required)\n";
  209. printf " -C (--community) SNMP read community (defaults to public,\n";
  210. printf " used with SNMP v1 and v2c\n";
  211. printf " -v (--snmp_version) 1 for SNMP v1 (default)\n";
  212. printf " 2 for SNMP v2c\n";
  213. printf " SNMP v2c will use get_bulk for less overhead\n";
  214. printf " 3 for SNMPv3 (requires -U option)";
  215. printf " -p (--port) SNMP port (default 161)\n";
  216. printf " -I (--ifmib) Agent supports IFMIB ifXTable. For Cisco - this will provide\n";
  217. printf " the descriptive name. Do not use if you don't know what this is. \n";
  218. printf " -x (--exclude) A comma separated list of ifType values that should be excluded \n";
  219. printf " from the report (default for an empty list is PPP(23).\n";
  220. printf " -u (--unused_ports) A comma separated list of ifIndex values that should be excluded \n";
  221. printf " from the report (default is an empty exclusion list).\n";
  222. printf " See the IANAifType-MIB for a list of interface types.\n";
  223. printf " -L (--seclevel) choice of \"noAuthNoPriv\", \"authNoPriv\", or \"authPriv\"\n";
  224. printf " -U (--secname) username for SNMPv3 context\n";
  225. printf " -c (--context) SNMPv3 context name (default is empty string)\n";
  226. printf " -A (--authpass) authentication password (cleartext ascii or localized key\n";
  227. printf " in hex with 0x prefix generated by using \"snmpkey\" utility\n";
  228. printf " auth password and authEngineID\n";
  229. printf " -a (--authproto) Authentication protocol (MD5 or SHA1)\n";
  230. printf " -X (--privpass) privacy password (cleartext ascii or localized key\n";
  231. printf " in hex with 0x prefix generated by using \"snmpkey\" utility\n";
  232. printf " privacy password and authEngineID\n";
  233. printf " -P (--privproto) privacy protocol (DES or AES; default: DES)\n";
  234. printf " -M (--maxmsgsize) Max message size - usefull only for v1 or v2c\n";
  235. printf " -t (--timeout) seconds before the plugin times out (default=$TIMEOUT)\n";
  236. printf " -V (--version) Plugin version\n";
  237. printf " -h (--help) usage help \n\n";
  238. print_revision($PROGNAME, '@NP_VERSION@');
  239. }
  240. sub process_arguments() {
  241. $status = GetOptions(
  242. "V" => \$opt_V, "version" => \$opt_V,
  243. "h" => \$opt_h, "help" => \$opt_h,
  244. "v=i" => \$snmp_version, "snmp_version=i" => \$snmp_version,
  245. "C=s" => \$community,"community=s" => \$community,
  246. "L=s" => \$seclevel, "seclevel=s" => \$seclevel,
  247. "a=s" => \$authproto, "authproto=s" => \$authproto,
  248. "U=s" => \$secname, "secname=s" => \$secname,
  249. "A=s" => \$authpass, "authpass=s" => \$authpass,
  250. "X=s" => \$privpass, "privpass=s" => \$privpass,
  251. "P=s" => \$privproto, "privproto=s" => \$privproto,
  252. "c=s" => \$context, "context=s" => \$context,
  253. "p=i" =>\$port, "port=i" => \$port,
  254. "H=s" => \$hostname, "hostname=s" => \$hostname,
  255. "I" => \$ifXTable, "ifmib" => \$ifXTable,
  256. "x:s" => \$opt_x, "exclude:s" => \$opt_x,
  257. "u=s" => \$opt_u, "unused_ports=s" => \$opt_u,
  258. "M=i" => \$maxmsgsize, "maxmsgsize=i" => \$maxmsgsize,
  259. "t=i" => \$timeout, "timeout=i" => \$timeout,
  260. );
  261. if ($status == 0){
  262. print_help();
  263. exit $ERRORS{'OK'};
  264. }
  265. if ($opt_V) {
  266. print_revision($PROGNAME,'@NP_VERSION@');
  267. exit $ERRORS{'OK'};
  268. }
  269. if ($opt_h) {
  270. print_help();
  271. exit $ERRORS{'OK'};
  272. }
  273. unless (defined $timeout) {
  274. $timeout = $TIMEOUT;
  275. }
  276. if ($snmp_version !~ /[123]/){
  277. $state='UNKNOWN';
  278. print ("$state: No support for SNMP v$snmp_version yet\n");
  279. exit $ERRORS{$state};
  280. }
  281. %session_opts = (
  282. -hostname => $hostname,
  283. -port => $port,
  284. -version => $snmp_version,
  285. -maxmsgsize => $maxmsgsize
  286. );
  287. $session_opts{'-community'} = $community if (defined $community && $snmp_version =~ /[12]/);
  288. if ($snmp_version =~ /3/ ) {
  289. # Must define a security level even though default is noAuthNoPriv
  290. # v3 requires a security username
  291. if (defined $seclevel && defined $secname) {
  292. $session_opts{'-username'} = $secname;
  293. # Must define a security level even though defualt is noAuthNoPriv
  294. unless ( grep /^$seclevel$/, qw(noAuthNoPriv authNoPriv authPriv) ) {
  295. usage("Must define a valid security level even though default is noAuthNoPriv");
  296. }
  297. # Authentication wanted
  298. if ( $seclevel eq 'authNoPriv' || $seclevel eq 'authPriv' ) {
  299. if (defined $authproto && $authproto ne 'MD5' && $authproto ne 'SHA1') {
  300. usage("Auth protocol can be either MD5 or SHA1");
  301. }
  302. $session_opts{'-authprotocol'} = $authproto if(defined $authproto);
  303. if ( !defined $authpass) {
  304. usage("Auth password/key is not defined");
  305. }else{
  306. if ($authpass =~ /^0x/ ) {
  307. $session_opts{'-authkey'} = $authpass ;
  308. }else{
  309. $session_opts{'-authpassword'} = $authpass ;
  310. }
  311. }
  312. }
  313. # Privacy (DES encryption) wanted
  314. if ($seclevel eq 'authPriv' ) {
  315. if (! defined $privpass) {
  316. usage("Privacy passphrase/key is not defined");
  317. }else{
  318. if ($privpass =~ /^0x/){
  319. $session_opts{'-privkey'} = $privpass;
  320. }else{
  321. $session_opts{'-privpassword'} = $privpass;
  322. }
  323. }
  324. $session_opts{'-privprotocol'} = $privproto if(defined $privproto);
  325. }
  326. # Context name defined or default
  327. unless ( defined $context) {
  328. $context = "";
  329. }
  330. }else {
  331. usage("Security level or name is not defined");
  332. }
  333. } # end snmpv3
  334. # Excluded interfaces types (ifType) (backup interfaces, dial-on demand interfaces, PPP interfaces
  335. if (defined $opt_x) {
  336. my @x = split(/,/, $opt_x);
  337. if ( @x) {
  338. foreach $key (@x){
  339. $excluded{$key} = 1;
  340. }
  341. }else{
  342. $excluded{23} = 1; # default PPP(23) if empty list - note (AIX seems to think PPP is 22 according to a post)
  343. }
  344. }
  345. # Excluded interface ports (ifIndex) - management reasons
  346. if ($opt_u) {
  347. @unused_ports = split(/,/,$opt_u);
  348. foreach $key (@unused_ports) {
  349. $ifStatus{$key}{'notInUse'}++ ;
  350. }
  351. }
  352. if (! utils::is_hostname($hostname)){
  353. usage("Hostname invalid or not given");
  354. exit $ERRORS{"UNKNOWN"};
  355. }
  356. if ($snmp_version !~ /[123]/) {
  357. $state='UNKNOWN';
  358. print ("$state: No support for SNMP v$snmp_version yet\n");
  359. exit $ERRORS{$state};
  360. }
  361. return $ERRORS{"OK"};
  362. }