check_snmp.t 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 = 9;
  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");