summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorThomas Guyot-Sionnest <dermoth@aei.ca>2009-07-31 06:45:30 (GMT)
committerThomas Guyot-Sionnest <dermoth@aei.ca>2009-07-31 07:07:38 (GMT)
commit8a96ee4741633cf8e832903f7ce0f542a77dbed8 (patch)
tree5d237bbcca8f3c4007ead8d71170b8cd1c895408 /plugins
parente0be2e6094a54771a7a310eea5853d2e05edf480 (diff)
downloadmonitoring-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-xplugins/tests/check_snmp.t56
-rw-r--r--plugins/tests/check_snmp_agent.pl101
-rw-r--r--plugins/tests/conf/snmpd.conf23
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 0000000..fcd15ea
--- /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
6use strict;
7use Test::More;
8use NPTest;
9use FindBin qw($Bin);
10
11my $port_snmp = 16100 + int(rand(100));
12my $running = 1;
13
14
15# Start up server
16my @pids;
17my $pid = fork();
18if ($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
32END {
33 foreach my $pid (@pids) {
34 if ($pid) { print "Killing $pid\n"; kill "INT", $pid }
35 }
36};
37
38if ($ARGV[0] && $ARGV[0] eq "-d") {
39 while (1) {
40 sleep 100;
41 }
42}
43
44my $tests = 2;
45if (-x "./check_snmp") {
46 plan tests => $tests;
47} else {
48 plan skip_all => "No check_snmp compiled";
49}
50
51my $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");
54cmp_ok( $res->return_code, '==', 0, "Exit OK when querying a multi-line string" );
55like($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 0000000..d1ff6d0
--- /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
7use NetSNMP::OID qw(:all);
8use NetSNMP::agent;
9use 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
11sub uint64 { return $_ }
12
13if (!$agent) {
14 print "This program must run as an embedded NetSNMP agent\n";
15 exit 1;
16}
17
18my $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
21my $multiline = "Cisco Internetwork Operating System Software
22IOS (tm) Catalyst 4000 L3 Switch Software (cat4000-I9K91S-M), Version
2312.2(20)EWA, RELEASE SOFTWARE (fc1)
24Technical Support: http://www.cisco.com/techsupport
25Copyright (c) 1986-2004 by cisco Systems, Inc.
26";
27my @fields = (ASN_OCTET_STR, ASN_UNSIGNED, ASN_UNSIGNED, ASN_COUNTER, ASN_COUNTER64, ASN_UNSIGNED);
28my @values = ($multiline, 4294965296, 1000, 4294965296, uint64("18446744073709351616"), int(rand(2**32)));
29my @incrts = (undef, 1000, -500, 1000, 100000, undef);
30
31# Number of elements in our OID
32my $oidelts;
33{
34 my @oid = split(/\./, $baseoid);
35 $oidelts = scalar(@oid);
36}
37
38my $regoid = new NetSNMP::OID($baseoid);
39$agent->register('check_snmp_agent', $regoid, \&my_snmp_handler);
40
41sub 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 0000000..eff5b0b
--- /dev/null
+++ b/plugins/tests/conf/snmpd.conf
@@ -0,0 +1,23 @@
1###############################################################################
2# Access Control
3###############################################################################
4
5com2sec readonly localhost public
6
7group MyROGroup v1 readonly
8group MyROGroup v2c readonly
9
10view all included .1 80
11
12access MyROGroup "" any noauth exact all none none
13
14syslocation Wonderland
15syscontact Alice
16
17
18###############################################################################
19# Embedded Subagents
20###############################################################################
21
22perl do "tests/check_snmp_agent.pl";
23