Code

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