check_snmp.t 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #! /usr/bin/perl -w -I ..
  2. #
  3. # Simple Network Management Protocol (SNMP) Test via check_snmp
  4. #
  5. #
  6. use strict;
  7. use Test::More;
  8. use NPTest;
  9. BEGIN {
  10. plan skip_all => 'check_snmp is not compiled' unless -x "./check_snmp";
  11. plan tests => 60;
  12. }
  13. my $res;
  14. my $host_snmp = getTestParameter( "host_snmp", "NP_HOST_SNMP", "localhost",
  15. "A host providing an SNMP Service");
  16. my $snmp_community = getTestParameter( "snmp_community", "NP_SNMP_COMMUNITY", "public",
  17. "The SNMP Community string for SNMP Testing (assumes snmp v1)" );
  18. my $host_nonresponsive = getTestParameter( "host_nonresponsive", "NP_HOST_NONRESPONSIVE", "10.0.0.1",
  19. "The hostname of system not responsive to network requests" );
  20. my $hostname_invalid = getTestParameter( "hostname_invalid", "NP_HOSTNAME_INVALID", "nosuchhost",
  21. "An invalid (not known to DNS) hostname" );
  22. $res = NPTest->testCmd( "./check_snmp -t 1" );
  23. is( $res->return_code, 3, "No host name" );
  24. is( $res->output, "No host specified" );
  25. $res = NPTest->testCmd( "./check_snmp -H fakehostname" );
  26. is( $res->return_code, 3, "No OIDs specified" );
  27. is( $res->output, "No OIDs specified" );
  28. $res = NPTest->testCmd( "./check_snmp -H fakehost -o oids -P 3 --seclevel=rubbish" );
  29. is( $res->return_code, 3, "Invalid seclevel" );
  30. like( $res->output, "/check_snmp: Invalid seclevel - rubbish/" );
  31. $res = NPTest->testCmd( "./check_snmp -H fakehost -o oids -P 3c" );
  32. is( $res->return_code, 3, "Invalid protocol" );
  33. like( $res->output, "/check_snmp: Invalid SNMP version - 3c/" );
  34. SKIP: {
  35. skip "no snmp host defined", 38 if ( ! $host_snmp );
  36. $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o system.sysUpTime.0 -w 1: -c 1:");
  37. cmp_ok( $res->return_code, '==', 0, "Exit OK when querying uptime" );
  38. like($res->output, '/^SNMP OK - (\d+)/', "String contains SNMP OK");
  39. $res->output =~ /^SNMP OK - (\d+)/;
  40. my $value = $1;
  41. cmp_ok( $value, ">", 0, "Got a time value" );
  42. like($res->perf_output, "/sysUpTime.*$1/", "Got perfdata with value '$1' in it");
  43. # some more threshold tests
  44. $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o system.sysUpTime.0 -c 1");
  45. cmp_ok( $res->return_code, '==', 2, "Threshold test -c 1" );
  46. $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o system.sysUpTime.0 -c 1:");
  47. cmp_ok( $res->return_code, '==', 0, "Threshold test -c 1:" );
  48. $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o system.sysUpTime.0 -c ~:1");
  49. cmp_ok( $res->return_code, '==', 2, "Threshold test -c ~:1" );
  50. $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o system.sysUpTime.0 -c 1:10");
  51. cmp_ok( $res->return_code, '==', 2, "Threshold test -c 1:10" );
  52. $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o system.sysUpTime.0 -c \@1:10");
  53. cmp_ok( $res->return_code, '==', 0, "Threshold test -c \@1:10" );
  54. $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o system.sysUpTime.0 -c 10:1");
  55. cmp_ok( $res->return_code, '==', 0, "Threshold test -c 10:1" );
  56. $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o .1.3.6.1.2.1.1.3.0 -w 1: -c 1:");
  57. cmp_ok( $res->return_code, '==', 0, "Test with numeric OID (no mibs loaded)" );
  58. like($res->output, '/^SNMP OK - \d+/', "String contains SNMP OK");
  59. $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o system.sysDescr.0");
  60. cmp_ok( $res->return_code, '==', 0, "Exit OK when querying sysDescr" );
  61. unlike($res->perf_output, '/sysDescr/', "Perfdata doesn't contain string values");
  62. $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o system.sysDescr.0,system.sysDescr.0");
  63. cmp_ok( $res->return_code, '==', 0, "Exit OK when querying two string OIDs, comma-separated" );
  64. like($res->output, '/^SNMP OK - /', "String contains SNMP OK");
  65. $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o system.sysDescr.0 -o system.sysDescr.0");
  66. cmp_ok( $res->return_code, '==', 0, "Exit OK when querying two string OIDs, repeated option" );
  67. like($res->output, '/^SNMP OK - /', "String contains SNMP OK");
  68. $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w 1:1 -c 1:1");
  69. cmp_ok( $res->return_code, '==', 0, "Exit OK when querying hrSWRunIndex.1" );
  70. like($res->output, '/^SNMP OK - 1\s.*$/', "String fits SNMP OK and output format");
  71. $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w 0 -c 1:");
  72. cmp_ok( $res->return_code, '==', 1, "Exit WARNING when querying hrSWRunIndex.1 and warn-th doesn't apply " );
  73. like($res->output, '/^SNMP WARNING - \*1\*\s.*$/', "String matches SNMP WARNING and output format");
  74. $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w :0 -c 0");
  75. cmp_ok( $res->return_code, '==', 2, "Exit CRITICAL when querying hrSWRunIndex.1 and crit-th doesn't apply" );
  76. like($res->output, '/^SNMP CRITICAL - \*1\*\s.*$/', "String matches SNMP CRITICAL and output format");
  77. $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o ifIndex.2,ifIndex.1 -w 1:2 -c 1:2");
  78. cmp_ok( $res->return_code, '==', 0, "Checking two OIDs at once" );
  79. like($res->output, "/^SNMP OK - 2 1/", "Got two values back" );
  80. like( $res->perf_output, "/ifIndex.2=2/", "Got 1st perf data" );
  81. like( $res->perf_output, "/ifIndex.1=1/", "Got 2nd perf data" );
  82. $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o ifIndex.2,ifIndex.1 -w 1:2,1:2 -c 2:2,2:2");
  83. cmp_ok( $res->return_code, '==', 2, "Checking critical threshold is passed if any one value crosses" );
  84. like($res->output, "/^SNMP CRITICAL - 2 *1*/", "Got two values back" );
  85. like( $res->perf_output, "/ifIndex.2=2/", "Got 1st perf data" );
  86. like( $res->perf_output, "/ifIndex.1=1/", "Got 2nd perf data" );
  87. $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o host.hrStorage.hrMemorySize.0,host.hrSystem.hrSystemProcesses.0 -w 1:,1: -c 1:,1:");
  88. cmp_ok( $res->return_code, '==', 0, "Exit OK when querying hrMemorySize and hrSystemProcesses");
  89. like($res->output, '/^SNMP OK - \d+ \d+/', "String contains hrMemorySize and hrSystemProcesses");
  90. $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w \@:0 -c \@0");
  91. cmp_ok( $res->return_code, '==', 0, "Exit OK with inside-range thresholds");
  92. like($res->output, '/^SNMP OK - 1\s.*$/', "String matches SNMP OK and output format");
  93. $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o enterprises.ucdavis.laTable.laEntry.laLoad.3");
  94. $res->output =~ m/^SNMP OK - (\d+\.\d{2})\s.*$/;
  95. my $lower = $1 - 0.05;
  96. my $higher = $1 + 0.05;
  97. $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o enterprises.ucdavis.laTable.laEntry.laLoad.3 -w $lower -c $higher");
  98. cmp_ok( $res->return_code, '==', 1, "Exit WARNING with fractionnal arguments");
  99. $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o system.sysUpTime.0,host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w ,:0 -c ,:2");
  100. cmp_ok( $res->return_code, '==', 1, "Exit WARNING on 2nd threshold");
  101. like($res->output, '/^SNMP WARNING - Timeticks:\s\(\d+\)\s+(?:\d+ days?,\s+)?\d+:\d+:\d+\.\d+\s+\*1\*\s.*$/', "First OID returned as string, 2nd checked for thresholds");
  102. $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w '' -c ''");
  103. cmp_ok( $res->return_code, '==', 0, "Empty thresholds doesn't crash");
  104. $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o host.hrStorage.hrMemorySize.0,host.hrSystem.hrSystemProcesses.0 -w ,,1 -c ,,2");
  105. cmp_ok( $res->return_code, '==', 0, "Skipping first two thresholds on 2 OID check");
  106. like($res->output, '/^SNMP OK - \d+ \w+ \d+\s.*$/', "Skipping first two thresholds, result printed rather than parsed");
  107. $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o host.hrStorage.hrMemorySize.0,host.hrSystem.hrSystemProcesses.0 -w ,, -c ,,");
  108. cmp_ok( $res->return_code, '==', 0, "Skipping all thresholds");
  109. like($res->output, '/^SNMP OK - \d+ \w+ \d+\s.*$/', "Skipping all thresholds, result printed rather than parsed");
  110. $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o system.sysUpTime.0 -c 1000000000000: -u '1/100 sec'");
  111. cmp_ok( $res->return_code, '==', 2, "Timetick used as a threshold");
  112. like($res->output, '/^SNMP CRITICAL - \*\d+\* 1\/100 sec.*$/', "Timetick used as a threshold, parsed as numeric");
  113. $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o system.sysUpTime.0");
  114. cmp_ok( $res->return_code, '==', 0, "Timetick used as a string");
  115. like($res->output, '/^SNMP OK - Timeticks:\s\(\d+\)\s+(?:\d+ days?,\s+)?\d+:\d+:\d+\.\d+\s.*$/', "Timetick used as a string, result printed rather than parsed");
  116. }
  117. # These checks need a complete command line. An invalid community is used so
  118. # the tests can run on hosts w/o snmp host/community in NPTest.cache. Execution will fail anyway
  119. SKIP: {
  120. skip "no non responsive host defined", 2 if ( ! $host_nonresponsive );
  121. $res = NPTest->testCmd( "./check_snmp -H $host_nonresponsive -C np_foobar -o system.sysUpTime.0 -w 1: -c 1:");
  122. cmp_ok( $res->return_code, '==', 3, "Exit UNKNOWN with non responsive host" );
  123. like($res->output, '/External command error: Timeout: No Response from /', "String matches timeout problem");
  124. }
  125. SKIP: {
  126. skip "no non invalid host defined", 2 if ( ! $hostname_invalid );
  127. $res = NPTest->testCmd( "./check_snmp -H $hostname_invalid -C np_foobar -o system.sysUpTime.0 -w 1: -c 1:");
  128. cmp_ok( $res->return_code, '==', 3, "Exit UNKNOWN with non responsive host" );
  129. like($res->output, '/External command error: .*(nosuchhost|Name or service not known|Unknown host)/', "String matches invalid host");
  130. }