check_ifstatus.pl 13 KB

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