check_snmp.t 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 = 67;
  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. # check if snmpd has perl support
  21. my $test = `snmpd -c tests/conf/snmpd.conf -C -r -H 2>&1`;
  22. if(!defined $test) {
  23. plan skip_all => "snmpd required";
  24. }
  25. elsif($test =~ m/Warning: Unknown token: perl/) {
  26. plan skip_all => "snmpd has no perl support";
  27. } else {
  28. plan tests => $tests;
  29. }
  30. } else {
  31. plan skip_all => "No check_snmp compiled";
  32. }
  33. }
  34. my $port_snmp = 16100 + int(rand(100));
  35. # Start up server
  36. my @pids;
  37. my $pid = fork();
  38. if ($pid) {
  39. # Parent
  40. push @pids, $pid;
  41. # give our agent some time to startup
  42. sleep(1);
  43. } else {
  44. # Child
  45. #print "child\n";
  46. print "Please contact SNMP at: $port_snmp\n";
  47. close(STDERR); # Coment out to debug snmpd problems (most errors sent there are OK)
  48. exec("snmpd -c tests/conf/snmpd.conf -C -f -r udp:$port_snmp");
  49. }
  50. END {
  51. foreach my $pid (@pids) {
  52. if ($pid) { print "Killing $pid\n"; kill "INT", $pid }
  53. }
  54. };
  55. if ($ARGV[0] && $ARGV[0] eq "-d") {
  56. while (1) {
  57. sleep 100;
  58. }
  59. }
  60. # We should merge that with $ENV{'NPTEST_CACHE'}, use one dir for all test data
  61. $ENV{'NAGIOS_PLUGIN_STATE_DIRECTORY'} ||= "/var/tmp";
  62. my $res;
  63. $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");
  64. cmp_ok( $res->return_code, '==', 0, "Exit OK when querying a multi-line string" );
  65. like($res->output, '/^SNMP OK - /', "String contains SNMP OK");
  66. like($res->output, '/'.quotemeta('SNMP OK - Cisco Internetwork Operating System Software |
  67. .1.3.6.1.4.1.8072.3.2.67.0:
  68. "Cisco Internetwork Operating System Software
  69. IOS (tm) Catalyst 4000 \"L3\" Switch Software (cat4000-I9K91S-M), Version
  70. 12.2(20)EWA, RELEASE SOFTWARE (fc1)
  71. Technical Support: http://www.cisco.com/techsupport
  72. Copyright (c) 1986-2004 by cisco Systems, Inc.
  73. "').'/m', "String contains all lines");
  74. # sysContact.0 is "Alice" (from our snmpd.conf)
  75. $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");
  76. cmp_ok( $res->return_code, '==', 0, "Exit OK when querying multi-line OIDs" );
  77. like($res->output, '/^SNMP OK - /', "String contains SNMP OK");
  78. like($res->output, '/'.quotemeta('SNMP OK - Cisco Internetwork Operating System Software "Alice" Kisco Outernetwork Oserating Gystem Totware |
  79. .1.3.6.1.4.1.8072.3.2.67.0:
  80. "Cisco Internetwork Operating System Software
  81. IOS (tm) Catalyst 4000 \"L3\" Switch Software (cat4000-I9K91S-M), Version
  82. 12.2(20)EWA, RELEASE SOFTWARE (fc1)
  83. Technical Support: http://www.cisco.com/techsupport
  84. Copyright (c) 1986-2004 by cisco Systems, Inc.
  85. "
  86. .1.3.6.1.4.1.8072.3.2.67.1:
  87. "Kisco Outernetwork Oserating Gystem Totware
  88. Copyleft (c) 2400-2689 by kisco Systrems, Inc."').'/m', "String contains all lines with multiple OIDs");
  89. $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");
  90. like($res->output, '/'.quotemeta('SNMP OK - This should not confuse check_snmp \"parser\" |
  91. .1.3.6.1.4.1.8072.3.2.67.2:
  92. "This should not confuse check_snmp \"parser\"
  93. into thinking there is no 2nd line"').'/m', "Attempt to confuse parser No.1");
  94. $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");
  95. like($res->output, '/'.quotemeta('SNMP OK - It\'s getting even harder if the line |
  96. .1.3.6.1.4.1.8072.3.2.67.3:
  97. "It\'s getting even harder if the line
  98. ends with with this: C:\\\\"').'/m', "Attempt to confuse parser No.2");
  99. $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");
  100. like($res->output, '/'.quotemeta('SNMP OK - And now have fun with with this: \"C:\\\\\" |
  101. .1.3.6.1.4.1.8072.3.2.67.4:
  102. "And now have fun with with this: \"C:\\\\\"
  103. because we\'re not done yet!"').'/m', "Attempt to confuse parser No.3");
  104. system("rm -f ".$ENV{'NAGIOS_PLUGIN_STATE_DIRECTORY'}."/check_snmp/*");
  105. $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" );
  106. is($res->return_code, 0, "Returns OK");
  107. is($res->output, "No previous data to calculate rate - assume okay");
  108. # Need to sleep, otherwise duration=0
  109. sleep 1;
  110. $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" );
  111. is($res->return_code, 1, "WARNING - due to going above rate calculation" );
  112. is($res->output, "SNMP RATE WARNING - *666* | iso.3.6.1.4.1.8072.3.2.67.10=666;600 ");
  113. $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" );
  114. is($res->return_code, 3, "UNKNOWN - basically the divide by zero error" );
  115. is($res->output, "Time duration between plugin calls is invalid");
  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 for first call" );
  118. is($res->output, "No previous data to calculate rate - assume okay" );
  119. # Need to sleep, otherwise duration=0
  120. sleep 1;
  121. $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" );
  122. is($res->return_code, 0, "OK as no thresholds" );
  123. is($res->output, "SNMP RATE OK - inoctets 666 | inoctets=666 ", "Check label");
  124. sleep 2;
  125. $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" );
  126. is($res->return_code, 0, "OK as no thresholds" );
  127. is($res->output, "SNMP RATE OK - inoctets 333 | inoctets=333 ", "Check rate decreases due to longer interval");
  128. # label performance data check
  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" );
  130. is($res->return_code, 0, "OK as no thresholds" );
  131. is($res->output, "SNMP OK - test 67996 | test=67996c ", "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 -l \"test'test\"" );
  133. is($res->return_code, 0, "OK as no thresholds" );
  134. is($res->output, "SNMP OK - test'test 68662 | \"test'test\"=68662c ", "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 69328 | 'test\"test'=69328c ", "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 -l test -O" );
  139. is($res->return_code, 0, "OK as no thresholds" );
  140. is($res->output, "SNMP OK - test 69994 | iso.3.6.1.4.1.8072.3.2.67.10=69994c ", "Check label");
  141. $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" );
  142. is($res->return_code, 0, "OK as no thresholds" );
  143. is($res->output, "SNMP OK - 70660 | iso.3.6.1.4.1.8072.3.2.67.10=70660c ", "Check label");
  144. $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'" );
  145. is($res->return_code, 0, "OK as no thresholds" );
  146. is($res->output, "SNMP OK - test test 71326 | 'test test'=71326c ", "Check label");
  147. $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" );
  148. is($res->return_code, 0, "OK for first call" );
  149. is($res->output, "No previous data to calculate rate - assume okay" );
  150. # Need to sleep, otherwise duration=0
  151. sleep 1;
  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.10 --rate -l inoctets_per_minute --rate-multiplier=60" );
  153. is($res->return_code, 0, "OK as no thresholds" );
  154. is($res->output, "SNMP RATE OK - inoctets_per_minute 39960 | inoctets_per_minute=39960 ", "Checking multiplier");
  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 -s '\"stringtests\"'" );
  156. is($res->return_code, 0, "OK as string matches" );
  157. is($res->output, 'SNMP OK - "stringtests" | ', "Good string 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.11 -s ring" );
  159. is($res->return_code, 2, "CRITICAL as string doesn't match (though is a substring)" );
  160. is($res->output, 'SNMP CRITICAL - *"stringtests"* | ', "Failed string match" );
  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.11 --invert-search -s '\"stringtests\"'" );
  162. is($res->return_code, 2, "CRITICAL as string matches but inverted" );
  163. is($res->output, 'SNMP CRITICAL - *"stringtests"* | ', "Inverted string match" );
  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.11 --invert-search -s ring" );
  165. is($res->return_code, 0, "OK as string doesn't match but inverted" );
  166. is($res->output, 'SNMP OK - "stringtests" | ', "OK as inverted string no match" );
  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.12 -w 4:5" );
  168. is($res->return_code, 1, "Numeric in string test" );
  169. is($res->output, 'SNMP WARNING - *3.5* | iso.3.6.1.4.1.8072.3.2.67.12=3.5;4:5 ', "WARNING threshold checks for string masquerading as number" );
  170. $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" );
  171. is($res->return_code, 0, "Not really numeric test" );
  172. is($res->output, 'SNMP OK - "87.4startswithnumberbutshouldbestring" | ', "Check string with numeric start is still string" );
  173. $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" );
  174. is($res->return_code, 0, "Not really numeric test (trying best to fool it)" );
  175. 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)" );
  176. $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'" );
  177. is($res->return_code, 0, "String check should check whole string, not a parsed number" );
  178. is($res->output, 'SNMP OK - "CUSTOM CHECK OK: foo is 12345" | ', "String check witn numbers returns whole string");
  179. $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.16 -w -2: -c -3:" );
  180. is($res->return_code, 0, "Negative integer check OK" );
  181. is($res->output, 'SNMP OK - -2 | iso.3.6.1.4.1.8072.3.2.67.16=-2;-2:;-3: ', "Negative integer check OK output" );
  182. $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.16 -w -2: -c -3:" );
  183. is($res->return_code, 1, "Negative integer check WARNING" );
  184. is($res->output, 'SNMP WARNING - *-3* | iso.3.6.1.4.1.8072.3.2.67.16=-3;-2:;-3: ', "Negative integer check WARNING output" );
  185. $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.16 -w -2: -c -3:" );
  186. is($res->return_code, 2, "Negative integer check CRITICAL" );
  187. is($res->output, 'SNMP CRITICAL - *-4* | iso.3.6.1.4.1.8072.3.2.67.16=-4;-2:;-3: ', "Negative integer check CRITICAL output" );
  188. $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.17 -w -3: -c -6:" );
  189. is($res->return_code, 1, "Negative integer as string, WARNING" );
  190. is($res->output, 'SNMP WARNING - *-4* | iso.3.6.1.4.1.8072.3.2.67.17=-4;-3:;-6: ', "Negative integer as string, WARNING output" );
  191. $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.17 -w -2: -c -3:" );
  192. is($res->return_code, 2, "Negative integer as string, CRITICAL" );
  193. is($res->output, 'SNMP CRITICAL - *-4* | iso.3.6.1.4.1.8072.3.2.67.17=-4;-2:;-3: ', "Negative integer as string, CRITICAL output" );
  194. $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.18 -c '~:-6.5'" );
  195. is($res->return_code, 0, "Negative float OK" );
  196. is($res->output, 'SNMP OK - -6.6 | iso.3.6.1.4.1.8072.3.2.67.18=-6.6;;~:-6.5 ', "Negative float OK output" );
  197. $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.18 -w '~:-6.65' -c '~:-6.55'" );
  198. is($res->return_code, 1, "Negative float WARNING" );
  199. is($res->output, 'SNMP WARNING - *-6.6* | iso.3.6.1.4.1.8072.3.2.67.18=-6.6;~:-6.65;~:-6.55 ', "Negative float WARNING output" );