Code

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