check_ifstatus.pl 13 KB

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