check_snmp.t 8.4 KB

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