check_snmp.t 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #! /usr/bin/perl -w -I ..
  2. #
  3. # Test check_snmp by having an actual SNMP agent running
  4. #
  5. use strict;
  6. use Test::More;
  7. use NPTest;
  8. use FindBin qw($Bin);
  9. my $tests = 53;
  10. # Check that all dependent modules are available
  11. eval {
  12. require NetSNMP::OID;
  13. require NetSNMP::agent;
  14. require NetSNMP::ASN;
  15. };
  16. if ($@) {
  17. plan skip_all => "Missing required module for test: $@";
  18. } else {
  19. if (-x "./check_snmp") {
  20. plan tests => $tests;
  21. } else {
  22. plan skip_all => "No check_snmp compiled";
  23. }
  24. }
  25. my $port_snmp = 16100 + int(rand(100));
  26. # Start up server
  27. my @pids;
  28. my $pid = fork();
  29. if ($pid) {
  30. # Parent
  31. push @pids, $pid;
  32. # give our agent some time to startup
  33. sleep(1);
  34. } else {
  35. # Child
  36. #print "child\n";
  37. print "Please contact SNMP at: $port_snmp\n";
  38. close(STDERR); # Coment out to debug snmpd problems (most errors sent there are OK)
  39. exec("snmpd -c tests/conf/snmpd.conf -C -f -r udp:$port_snmp");
  40. }
  41. END {
  42. foreach my $pid (@pids) {
  43. if ($pid) { print "Killing $pid\n"; kill "INT", $pid }
  44. }
  45. };
  46. if ($ARGV[0] && $ARGV[0] eq "-d") {
  47. while (1) {
  48. sleep 100;
  49. }
  50. }
  51. # We should merge that with $ENV{'NPTEST_CACHE'}, use one dir for all test data
  52. $ENV{'NAGIOS_PLUGIN_STATE_DIRECTORY'} ||= "/var/tmp";
  53. my $res;
  54. $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.0");
  55. cmp_ok( $res->return_code, '==', 0, "Exit OK when querying a multi-line string" );
  56. like($res->output, '/^SNMP OK - /', "String contains SNMP OK");
  57. like($res->output, '/'.quotemeta('SNMP OK - Cisco Internetwork Operating System Software |
  58. .1.3.6.1.4.1.8072.3.2.67.0:
  59. "Cisco Internetwork Operating System Software
  60. IOS (tm) Catalyst 4000 \"L3\" Switch Software (cat4000-I9K91S-M), Version
  61. 12.2(20)EWA, RELEASE SOFTWARE (fc1)
  62. Technical Support: http://www.cisco.com/techsupport
  63. Copyright (c) 1986-2004 by cisco Systems, Inc.
  64. "').'/m', "String contains all lines");
  65. # sysContact.0 is "Alice" (from our snmpd.conf)
  66. $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.0 -o sysContact.0 -o .1.3.6.1.4.1.8072.3.2.67.1");
  67. cmp_ok( $res->return_code, '==', 0, "Exit OK when querying multi-line OIDs" );
  68. like($res->output, '/^SNMP OK - /', "String contains SNMP OK");
  69. like($res->output, '/'.quotemeta('SNMP OK - Cisco Internetwork Operating System Software Alice Kisco Outernetwork Oserating Gystem Totware |
  70. .1.3.6.1.4.1.8072.3.2.67.0:
  71. "Cisco Internetwork Operating System Software
  72. IOS (tm) Catalyst 4000 \"L3\" Switch Software (cat4000-I9K91S-M), Version
  73. 12.2(20)EWA, RELEASE SOFTWARE (fc1)
  74. Technical Support: http://www.cisco.com/techsupport
  75. Copyright (c) 1986-2004 by cisco Systems, Inc.
  76. "
  77. .1.3.6.1.4.1.8072.3.2.67.1:
  78. "Kisco Outernetwork Oserating Gystem Totware
  79. Copyleft (c) 2400-2689 by kisco Systrems, Inc."').'/m', "String contains all lines with multiple OIDs");
  80. $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.2");
  81. like($res->output, '/'.quotemeta('SNMP OK - This should not confuse check_snmp \"parser\" |
  82. .1.3.6.1.4.1.8072.3.2.67.2:
  83. "This should not confuse check_snmp \"parser\"
  84. into thinking there is no 2nd line"').'/m', "Attempt to confuse parser No.1");
  85. $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.3");
  86. like($res->output, '/'.quotemeta('SNMP OK - It\'s getting even harder if the line |
  87. .1.3.6.1.4.1.8072.3.2.67.3:
  88. "It\'s getting even harder if the line
  89. ends with with this: C:\\\\"').'/m', "Attempt to confuse parser No.2");
  90. $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.4");
  91. like($res->output, '/'.quotemeta('SNMP OK - And now have fun with with this: \"C:\\\\\" |
  92. .1.3.6.1.4.1.8072.3.2.67.4:
  93. "And now have fun with with this: \"C:\\\\\"
  94. because we\'re not done yet!"').'/m', "Attempt to confuse parser No.3");
  95. system("rm -f ".$ENV{'NAGIOS_PLUGIN_STATE_DIRECTORY'}."/check_snmp/*");
  96. $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 --rate -w 600" );
  97. is($res->return_code, 0, "Returns OK");
  98. is($res->output, "No previous data to calculate rate - assume okay");
  99. # Need to sleep, otherwise duration=0
  100. sleep 1;
  101. $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 --rate -w 600" );
  102. is($res->return_code, 1, "WARNING - due to going above rate calculation" );
  103. is($res->output, "SNMP RATE WARNING - *666* | iso.3.6.1.4.1.8072.3.2.67.10=666 ");
  104. $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 --rate -w 600" );
  105. is($res->return_code, 3, "UNKNOWN - basically the divide by zero error" );
  106. is($res->output, "Time duration between plugin calls is invalid");
  107. $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 --rate -l inoctets" );
  108. is($res->return_code, 0, "OK for first call" );
  109. is($res->output, "No previous data to calculate rate - assume okay" );
  110. # Need to sleep, otherwise duration=0
  111. sleep 1;
  112. $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 --rate -l inoctets" );
  113. is($res->return_code, 0, "OK as no thresholds" );
  114. is($res->output, "SNMP RATE OK - inoctets 666 | inoctets=666 ", "Check label");
  115. sleep 2;
  116. $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 --rate -l inoctets" );
  117. is($res->return_code, 0, "OK as no thresholds" );
  118. is($res->output, "SNMP RATE OK - inoctets 333 | inoctets=333 ", "Check rate decreases due to longer interval");
  119. # label performance data check
  120. $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 -l test" );
  121. is($res->return_code, 0, "OK as no thresholds" );
  122. is($res->output, "SNMP OK - test 67996 | test=67996c ", "Check label");
  123. $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 -l \"test'test\"" );
  124. is($res->return_code, 0, "OK as no thresholds" );
  125. is($res->output, "SNMP OK - test'test 68662 | \"test'test\"=68662c ", "Check label");
  126. $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 -l 'test\"test'" );
  127. is($res->return_code, 0, "OK as no thresholds" );
  128. is($res->output, "SNMP OK - test\"test 69328 | 'test\"test'=69328c ", "Check label");
  129. $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 -l test -O" );
  130. is($res->return_code, 0, "OK as no thresholds" );
  131. is($res->output, "SNMP OK - test 69994 | iso.3.6.1.4.1.8072.3.2.67.10=69994c ", "Check label");
  132. $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10" );
  133. is($res->return_code, 0, "OK as no thresholds" );
  134. is($res->output, "SNMP OK - 70660 | iso.3.6.1.4.1.8072.3.2.67.10=70660c ", "Check label");
  135. $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 -l 'test test'" );
  136. is($res->return_code, 0, "OK as no thresholds" );
  137. is($res->output, "SNMP OK - test test 71326 | 'test test'=71326c ", "Check label");
  138. $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 --rate -l inoctets_per_minute --rate-multiplier=60" );
  139. is($res->return_code, 0, "OK for first call" );
  140. is($res->output, "No previous data to calculate rate - assume okay" );
  141. # Need to sleep, otherwise duration=0
  142. sleep 1;
  143. $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.10 --rate -l inoctets_per_minute --rate-multiplier=60" );
  144. is($res->return_code, 0, "OK as no thresholds" );
  145. is($res->output, "SNMP RATE OK - inoctets_per_minute 39960 | inoctets_per_minute=39960 ", "Checking multiplier");
  146. $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.11 -s '\"stringtests\"'" );
  147. is($res->return_code, 0, "OK as string matches" );
  148. is($res->output, 'SNMP OK - "stringtests" | ', "Good string match" );
  149. $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.11 -s ring" );
  150. is($res->return_code, 2, "CRITICAL as string doesn't match (though is a substring)" );
  151. is($res->output, 'SNMP CRITICAL - *"stringtests"* | ', "Failed string match" );
  152. $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.11 --invert-search -s '\"stringtests\"'" );
  153. is($res->return_code, 2, "CRITICAL as string matches but inverted" );
  154. is($res->output, 'SNMP CRITICAL - *"stringtests"* | ', "Inverted string match" );
  155. $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.11 --invert-search -s ring" );
  156. is($res->return_code, 0, "OK as string doesn't match but inverted" );
  157. is($res->output, 'SNMP OK - "stringtests" | ', "OK as inverted string no match" );
  158. $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.12 -w 4:5" );
  159. is($res->return_code, 1, "Numeric in string test" );
  160. is($res->output, 'SNMP WARNING - *3.5* | iso.3.6.1.4.1.8072.3.2.67.12=3.5 ', "WARNING threshold checks for string masquerading as number" );
  161. $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.13" );
  162. is($res->return_code, 0, "Not really numeric test" );
  163. is($res->output, 'SNMP OK - "87.4startswithnumberbutshouldbestring" | ', "Check string with numeric start is still string" );
  164. $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.14" );
  165. is($res->return_code, 0, "Not really numeric test (trying best to fool it)" );
  166. is($res->output, 'SNMP OK - "555\"I said\"" | ', "Check string with a double quote following is still a string (looks like the perl routine will always escape though)" );
  167. $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.15 -r 'CUSTOM CHECK OK'" );
  168. is($res->return_code, 0, "String check should check whole string, not a parsed number" );
  169. is($res->output, 'SNMP OK - "CUSTOM CHECK OK: foo is 12345" | ', "String check witn numbers returns whole string");