check_ifstatus.pl 12 KB

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