check_ifstatus.pl 12 KB

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