check_snmp.t 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. my $tests = 29;
  45. if (-x "./check_snmp") {
  46. plan tests => $tests;
  47. } else {
  48. plan skip_all => "No check_snmp compiled";
  49. }
  50. my $res;
  51. $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");
  52. cmp_ok( $res->return_code, '==', 0, "Exit OK when querying a multi-line string" );
  53. like($res->output, '/^SNMP OK - /', "String contains SNMP OK");
  54. like($res->output, '/'.quotemeta('SNMP OK - Cisco Internetwork Operating System Software |
  55. .1.3.6.1.4.1.8072.3.2.67.0:
  56. "Cisco Internetwork Operating System Software
  57. IOS (tm) Catalyst 4000 \"L3\" Switch Software (cat4000-I9K91S-M), Version
  58. 12.2(20)EWA, RELEASE SOFTWARE (fc1)
  59. Technical Support: http://www.cisco.com/techsupport
  60. Copyright (c) 1986-2004 by cisco Systems, Inc.
  61. "').'/m', "String contains all lines");
  62. # sysContact.0 is "Alice" (from our snmpd.conf)
  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 -o sysContact.0 -o .1.3.6.1.4.1.8072.3.2.67.1");
  64. cmp_ok( $res->return_code, '==', 0, "Exit OK when querying multi-line OIDs" );
  65. like($res->output, '/^SNMP OK - /', "String contains SNMP OK");
  66. like($res->output, '/'.quotemeta('SNMP OK - Cisco Internetwork Operating System Software Alice Kisco Outernetwork Oserating Gystem Totware |
  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. "
  74. .1.3.6.1.4.1.8072.3.2.67.1:
  75. "Kisco Outernetwork Oserating Gystem Totware
  76. Copyleft (c) 2400-2689 by kisco Systrems, Inc."').'/m', "String contains all lines with multiple OIDs");
  77. $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");
  78. like($res->output, '/'.quotemeta('SNMP OK - This should not confuse check_snmp \"parser\" |
  79. .1.3.6.1.4.1.8072.3.2.67.2:
  80. "This should not confuse check_snmp \"parser\"
  81. into thinking there is no 2nd line"').'/m', "Attempt to confuse parser No.1");
  82. $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");
  83. like($res->output, '/'.quotemeta('SNMP OK - It\'s getting even harder if the line |
  84. .1.3.6.1.4.1.8072.3.2.67.3:
  85. "It\'s getting even harder if the line
  86. ends with with this: C:\\\\"').'/m', "Attempt to confuse parser No.2");
  87. $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");
  88. like($res->output, '/'.quotemeta('SNMP OK - And now have fun with with this: \"C:\\\\\" |
  89. .1.3.6.1.4.1.8072.3.2.67.4:
  90. "And now have fun with with this: \"C:\\\\\"
  91. because we\'re not done yet!"').'/m', "Attempt to confuse parser No.3");
  92. system("rm /usr/local/nagios/var/check_snmp/*");
  93. $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" );
  94. is($res->return_code, 0, "Returns OK");
  95. is($res->output, "No previous data to calculate rate - assume okay");
  96. # Need to sleep, otherwise duration=0
  97. sleep 1;
  98. $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" );
  99. is($res->return_code, 1, "WARNING - due to going above rate calculation" );
  100. is($res->output, "SNMP RATE WARNING - *666* | iso.3.6.1.4.1.8072.3.2.67.10-rate=666 ");
  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, 3, "UNKNOWN - basically the divide by zero error" );
  103. is($res->output, "Time duration between plugin calls is invalid");
  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 -l inoctets" );
  105. is($res->return_code, 0, "OK for first call" );
  106. is($res->output, "No previous data to calculate rate - assume okay" );
  107. # Need to sleep, otherwise duration=0
  108. sleep 1;
  109. $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" );
  110. is($res->return_code, 0, "OK as no thresholds" );
  111. is($res->output, "SNMP RATE OK - inoctets 666 | inoctets-rate=666 ", "Check label");
  112. sleep 2;
  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 -l inoctets" );
  114. is($res->return_code, 0, "OK as no thresholds" );
  115. is($res->output, "SNMP RATE OK - inoctets 333 | inoctets-rate=333 ", "Check rate decreases due to longer interval");
  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.11 -s '\"stringtests\"'" );
  117. is($res->return_code, 0, "OK as string matches" );
  118. is($res->output, 'SNMP OK - "stringtests" | ', "Good string match" );
  119. $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" );
  120. is($res->return_code, 2, "CRITICAL as string doesn't match (though is a substring)" );
  121. is($res->output, 'SNMP CRITICAL - *"stringtests"* | ', "Failed string match" );
  122. $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\"'" );
  123. is($res->return_code, 2, "CRITICAL as string matches but inverted" );
  124. is($res->output, 'SNMP CRITICAL - *"stringtests"* | ', "Inverted string match" );
  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.11 --invert-search -s ring" );
  126. is($res->return_code, 0, "OK as string doesn't match but inverted" );
  127. is($res->output, 'SNMP OK - "stringtests" | ', "OK as inverted string no match" );