diff options
author | Thomas Guyot-Sionnest <dermoth@aei.ca> | 2009-07-31 02:45:30 -0400 |
---|---|---|
committer | Thomas Guyot-Sionnest <dermoth@aei.ca> | 2009-07-31 03:07:38 -0400 |
commit | 8a96ee4741633cf8e832903f7ce0f542a77dbed8 (patch) | |
tree | 5d237bbcca8f3c4007ead8d71170b8cd1c895408 /plugins | |
parent | e0be2e6094a54771a7a310eea5853d2e05edf480 (diff) | |
download | monitoring-plugins-8a96ee4741633cf8e832903f7ce0f542a77dbed8.tar.gz |
Add tests using custom snmp agent
Only multi-line string test for now (regression test), counter rollover
tests planed with my snmp_counters_new branch.
NB: 64bit counters are broken in NetSNMP::agent from NetSNMP version 5.4.1
and lower, but might come in handy one day
Diffstat (limited to 'plugins')
-rwxr-xr-x | plugins/tests/check_snmp.t | 56 | ||||
-rw-r--r-- | plugins/tests/check_snmp_agent.pl | 101 | ||||
-rw-r--r-- | plugins/tests/conf/snmpd.conf | 23 |
3 files changed, 180 insertions, 0 deletions
diff --git a/plugins/tests/check_snmp.t b/plugins/tests/check_snmp.t new file mode 100755 index 00000000..fcd15ea4 --- /dev/null +++ b/plugins/tests/check_snmp.t | |||
@@ -0,0 +1,56 @@ | |||
1 | #! /usr/bin/perl -w -I .. | ||
2 | # | ||
3 | # Test check_snmp by having an actual SNMP agent running | ||
4 | # | ||
5 | |||
6 | use strict; | ||
7 | use Test::More; | ||
8 | use NPTest; | ||
9 | use FindBin qw($Bin); | ||
10 | |||
11 | my $port_snmp = 16100 + int(rand(100)); | ||
12 | my $running = 1; | ||
13 | |||
14 | |||
15 | # Start up server | ||
16 | my @pids; | ||
17 | my $pid = fork(); | ||
18 | if ($pid) { | ||
19 | # Parent | ||
20 | push @pids, $pid; | ||
21 | # give our agent some time to startup | ||
22 | sleep(1); | ||
23 | } else { | ||
24 | # Child | ||
25 | #print "child\n"; | ||
26 | |||
27 | print "Please contact SNMP at: $port_snmp\n"; | ||
28 | close(STDERR); # Coment out to debug snmpd problems (most errors sent there are OK) | ||
29 | exec("snmpd -c tests/conf/snmpd.conf -C -f -r udp:$port_snmp"); | ||
30 | } | ||
31 | |||
32 | END { | ||
33 | foreach my $pid (@pids) { | ||
34 | if ($pid) { print "Killing $pid\n"; kill "INT", $pid } | ||
35 | } | ||
36 | }; | ||
37 | |||
38 | if ($ARGV[0] && $ARGV[0] eq "-d") { | ||
39 | while (1) { | ||
40 | sleep 100; | ||
41 | } | ||
42 | } | ||
43 | |||
44 | my $tests = 2; | ||
45 | if (-x "./check_snmp") { | ||
46 | plan tests => $tests; | ||
47 | } else { | ||
48 | plan skip_all => "No check_snmp compiled"; | ||
49 | } | ||
50 | |||
51 | my $res; | ||
52 | |||
53 | $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"); | ||
54 | cmp_ok( $res->return_code, '==', 0, "Exit OK when querying a multi-line string" ); | ||
55 | like($res->output, '/^SNMP OK - /', "String contains SNMP OK"); | ||
56 | |||
diff --git a/plugins/tests/check_snmp_agent.pl b/plugins/tests/check_snmp_agent.pl new file mode 100644 index 00000000..d1ff6d00 --- /dev/null +++ b/plugins/tests/check_snmp_agent.pl | |||
@@ -0,0 +1,101 @@ | |||
1 | #! /usr/bin/perl -w -I .. | ||
2 | # | ||
3 | # Subagent for testing check_snmp | ||
4 | # | ||
5 | |||
6 | #use strict; # Doesn't work | ||
7 | use NetSNMP::OID qw(:all); | ||
8 | use NetSNMP::agent; | ||
9 | use NetSNMP::ASN qw(ASN_OCTET_STR ASN_COUNTER ASN_COUNTER64 ASN_INTEGER ASN_INTEGER64 ASN_UNSIGNED ASN_UNSIGNED64); | ||
10 | #use Math::Int64 qw(uint64); # Skip that module whie we don't need it | ||
11 | sub uint64 { return $_ } | ||
12 | |||
13 | if (!$agent) { | ||
14 | print "This program must run as an embedded NetSNMP agent\n"; | ||
15 | exit 1; | ||
16 | } | ||
17 | |||
18 | my $baseoid = '.1.3.6.1.4.1.8072.3.2.67'; | ||
19 | # Next are arrays of indexes (Type, initial value and increments) | ||
20 | # Undef miltipliers are randomized | ||
21 | my $multiline = "Cisco Internetwork Operating System Software | ||
22 | IOS (tm) Catalyst 4000 L3 Switch Software (cat4000-I9K91S-M), Version | ||
23 | 12.2(20)EWA, RELEASE SOFTWARE (fc1) | ||
24 | Technical Support: http://www.cisco.com/techsupport | ||
25 | Copyright (c) 1986-2004 by cisco Systems, Inc. | ||
26 | "; | ||
27 | my @fields = (ASN_OCTET_STR, ASN_UNSIGNED, ASN_UNSIGNED, ASN_COUNTER, ASN_COUNTER64, ASN_UNSIGNED); | ||
28 | my @values = ($multiline, 4294965296, 1000, 4294965296, uint64("18446744073709351616"), int(rand(2**32))); | ||
29 | my @incrts = (undef, 1000, -500, 1000, 100000, undef); | ||
30 | |||
31 | # Number of elements in our OID | ||
32 | my $oidelts; | ||
33 | { | ||
34 | my @oid = split(/\./, $baseoid); | ||
35 | $oidelts = scalar(@oid); | ||
36 | } | ||
37 | |||
38 | my $regoid = new NetSNMP::OID($baseoid); | ||
39 | $agent->register('check_snmp_agent', $regoid, \&my_snmp_handler); | ||
40 | |||
41 | sub my_snmp_handler { | ||
42 | my ($handler, $registration_info, $request_info, $requests) = @_; | ||
43 | |||
44 | for (my $request = $requests; $request; $request = $request->next) { | ||
45 | my $oid = $request->getOID(); | ||
46 | my $index; | ||
47 | my $next_index; | ||
48 | |||
49 | # Validate the OID | ||
50 | my @numarray = $oid->to_array(); | ||
51 | if (@numarray != $oidelts) { | ||
52 | if ($request_info->getMode() == MODE_GETNEXT && @numarray == ($oidelts - 1)) { | ||
53 | # GETNEXT for the base oid; set it to the first index | ||
54 | push(@numarray, 0); | ||
55 | $next_index = 0; | ||
56 | } else { | ||
57 | # We got a request for the base OID or with extra elements | ||
58 | $request->setError($request_info, SNMP_ERR_NOERROR); | ||
59 | next; | ||
60 | } | ||
61 | } | ||
62 | |||
63 | $index = pop(@numarray); | ||
64 | if ($index >= scalar(@fields)) { | ||
65 | # Index is out of bounds | ||
66 | $request->setError($request_info, SNMP_ERR_NOERROR); | ||
67 | next; | ||
68 | } | ||
69 | |||
70 | # Handle the request | ||
71 | if ($request_info->getMode() == MODE_GETNEXT) { | ||
72 | if (++$index >= scalar(@fields)) { | ||
73 | # Index will grow out of bounds | ||
74 | $request->setError($request_info, SNMP_ERR_NOERROR); | ||
75 | next; | ||
76 | } | ||
77 | $index = (defined($next_index) ? $next_index : $index); | ||
78 | $request->setOID("$baseoid.$index"); | ||
79 | } elsif ($request_info->getMode() != MODE_GET) { | ||
80 | # Everything else is write-related modes | ||
81 | $request->setError($request_info, SNMP_ERR_READONLY); | ||
82 | next; | ||
83 | } | ||
84 | |||
85 | # Set the response... setValue is a bit touchy about the data type, but accepts plain strings. | ||
86 | my $value = sprintf("%s", $values[$index]); | ||
87 | $request->setValue($fields[$index], $value); | ||
88 | |||
89 | # And update the value | ||
90 | if (defined($incrts[$index])) { | ||
91 | $values[$index] += $incrts[$index]; | ||
92 | } elsif ($fields[$index] != ASN_OCTET_STR) { | ||
93 | my $minus = int(rand(2))*-1; | ||
94 | $minus = 1 unless ($minus); | ||
95 | my $exp = 32; | ||
96 | $exp = 64 if ($fields[$index] == ASN_COUNTER64 || $fields[$index] == ASN_INTEGER64 || $fields[$index] == ASN_UNSIGNED64); | ||
97 | $values[$index] = int(rand(2**$exp)); | ||
98 | } | ||
99 | } | ||
100 | } | ||
101 | |||
diff --git a/plugins/tests/conf/snmpd.conf b/plugins/tests/conf/snmpd.conf new file mode 100644 index 00000000..eff5b0b3 --- /dev/null +++ b/plugins/tests/conf/snmpd.conf | |||
@@ -0,0 +1,23 @@ | |||
1 | ############################################################################### | ||
2 | # Access Control | ||
3 | ############################################################################### | ||
4 | |||
5 | com2sec readonly localhost public | ||
6 | |||
7 | group MyROGroup v1 readonly | ||
8 | group MyROGroup v2c readonly | ||
9 | |||
10 | view all included .1 80 | ||
11 | |||
12 | access MyROGroup "" any noauth exact all none none | ||
13 | |||
14 | syslocation Wonderland | ||
15 | syscontact Alice | ||
16 | |||
17 | |||
18 | ############################################################################### | ||
19 | # Embedded Subagents | ||
20 | ############################################################################### | ||
21 | |||
22 | perl do "tests/check_snmp_agent.pl"; | ||
23 | |||