summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: e0be2e6)
raw | patch | inline | side by side (parent: e0be2e6)
author | Thomas Guyot-Sionnest <dermoth@aei.ca> | |
Fri, 31 Jul 2009 06:45:30 +0000 (02:45 -0400) | ||
committer | Thomas Guyot-Sionnest <dermoth@aei.ca> | |
Fri, 31 Jul 2009 07:07:38 +0000 (03:07 -0400) |
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
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
plugins/tests/check_snmp.t | [new file with mode: 0755] | patch | blob |
plugins/tests/check_snmp_agent.pl | [new file with mode: 0644] | patch | blob |
plugins/tests/conf/snmpd.conf | [new file with mode: 0644] | patch | blob |
diff --git a/plugins/tests/check_snmp.t b/plugins/tests/check_snmp.t
--- /dev/null
@@ -0,0 +1,56 @@
+#! /usr/bin/perl -w -I ..
+#
+# Test check_snmp by having an actual SNMP agent running
+#
+
+use strict;
+use Test::More;
+use NPTest;
+use FindBin qw($Bin);
+
+my $port_snmp = 16100 + int(rand(100));
+my $running = 1;
+
+
+# Start up server
+my @pids;
+my $pid = fork();
+if ($pid) {
+ # Parent
+ push @pids, $pid;
+ # give our agent some time to startup
+ sleep(1);
+} else {
+ # Child
+ #print "child\n";
+
+ print "Please contact SNMP at: $port_snmp\n";
+ close(STDERR); # Coment out to debug snmpd problems (most errors sent there are OK)
+ exec("snmpd -c tests/conf/snmpd.conf -C -f -r udp:$port_snmp");
+}
+
+END {
+ foreach my $pid (@pids) {
+ if ($pid) { print "Killing $pid\n"; kill "INT", $pid }
+ }
+};
+
+if ($ARGV[0] && $ARGV[0] eq "-d") {
+ while (1) {
+ sleep 100;
+ }
+}
+
+my $tests = 2;
+if (-x "./check_snmp") {
+ plan tests => $tests;
+} else {
+ plan skip_all => "No check_snmp compiled";
+}
+
+my $res;
+
+$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");
+cmp_ok( $res->return_code, '==', 0, "Exit OK when querying a multi-line string" );
+like($res->output, '/^SNMP OK - /', "String contains SNMP OK");
+
diff --git a/plugins/tests/check_snmp_agent.pl b/plugins/tests/check_snmp_agent.pl
--- /dev/null
@@ -0,0 +1,101 @@
+#! /usr/bin/perl -w -I ..
+#
+# Subagent for testing check_snmp
+#
+
+#use strict; # Doesn't work
+use NetSNMP::OID qw(:all);
+use NetSNMP::agent;
+use NetSNMP::ASN qw(ASN_OCTET_STR ASN_COUNTER ASN_COUNTER64 ASN_INTEGER ASN_INTEGER64 ASN_UNSIGNED ASN_UNSIGNED64);
+#use Math::Int64 qw(uint64); # Skip that module whie we don't need it
+sub uint64 { return $_ }
+
+if (!$agent) {
+ print "This program must run as an embedded NetSNMP agent\n";
+ exit 1;
+}
+
+my $baseoid = '.1.3.6.1.4.1.8072.3.2.67';
+# Next are arrays of indexes (Type, initial value and increments)
+# Undef miltipliers are randomized
+my $multiline = "Cisco Internetwork Operating System Software
+IOS (tm) Catalyst 4000 L3 Switch Software (cat4000-I9K91S-M), Version
+12.2(20)EWA, RELEASE SOFTWARE (fc1)
+Technical Support: http://www.cisco.com/techsupport
+Copyright (c) 1986-2004 by cisco Systems, Inc.
+";
+my @fields = (ASN_OCTET_STR, ASN_UNSIGNED, ASN_UNSIGNED, ASN_COUNTER, ASN_COUNTER64, ASN_UNSIGNED);
+my @values = ($multiline, 4294965296, 1000, 4294965296, uint64("18446744073709351616"), int(rand(2**32)));
+my @incrts = (undef, 1000, -500, 1000, 100000, undef);
+
+# Number of elements in our OID
+my $oidelts;
+{
+ my @oid = split(/\./, $baseoid);
+ $oidelts = scalar(@oid);
+}
+
+my $regoid = new NetSNMP::OID($baseoid);
+$agent->register('check_snmp_agent', $regoid, \&my_snmp_handler);
+
+sub my_snmp_handler {
+ my ($handler, $registration_info, $request_info, $requests) = @_;
+
+ for (my $request = $requests; $request; $request = $request->next) {
+ my $oid = $request->getOID();
+ my $index;
+ my $next_index;
+
+ # Validate the OID
+ my @numarray = $oid->to_array();
+ if (@numarray != $oidelts) {
+ if ($request_info->getMode() == MODE_GETNEXT && @numarray == ($oidelts - 1)) {
+ # GETNEXT for the base oid; set it to the first index
+ push(@numarray, 0);
+ $next_index = 0;
+ } else {
+ # We got a request for the base OID or with extra elements
+ $request->setError($request_info, SNMP_ERR_NOERROR);
+ next;
+ }
+ }
+
+ $index = pop(@numarray);
+ if ($index >= scalar(@fields)) {
+ # Index is out of bounds
+ $request->setError($request_info, SNMP_ERR_NOERROR);
+ next;
+ }
+
+ # Handle the request
+ if ($request_info->getMode() == MODE_GETNEXT) {
+ if (++$index >= scalar(@fields)) {
+ # Index will grow out of bounds
+ $request->setError($request_info, SNMP_ERR_NOERROR);
+ next;
+ }
+ $index = (defined($next_index) ? $next_index : $index);
+ $request->setOID("$baseoid.$index");
+ } elsif ($request_info->getMode() != MODE_GET) {
+ # Everything else is write-related modes
+ $request->setError($request_info, SNMP_ERR_READONLY);
+ next;
+ }
+
+ # Set the response... setValue is a bit touchy about the data type, but accepts plain strings.
+ my $value = sprintf("%s", $values[$index]);
+ $request->setValue($fields[$index], $value);
+
+ # And update the value
+ if (defined($incrts[$index])) {
+ $values[$index] += $incrts[$index];
+ } elsif ($fields[$index] != ASN_OCTET_STR) {
+ my $minus = int(rand(2))*-1;
+ $minus = 1 unless ($minus);
+ my $exp = 32;
+ $exp = 64 if ($fields[$index] == ASN_COUNTER64 || $fields[$index] == ASN_INTEGER64 || $fields[$index] == ASN_UNSIGNED64);
+ $values[$index] = int(rand(2**$exp));
+ }
+ }
+}
+
diff --git a/plugins/tests/conf/snmpd.conf b/plugins/tests/conf/snmpd.conf
--- /dev/null
@@ -0,0 +1,23 @@
+###############################################################################
+# Access Control
+###############################################################################
+
+com2sec readonly localhost public
+
+group MyROGroup v1 readonly
+group MyROGroup v2c readonly
+
+view all included .1 80
+
+access MyROGroup "" any noauth exact all none none
+
+syslocation Wonderland
+syscontact Alice
+
+
+###############################################################################
+# Embedded Subagents
+###############################################################################
+
+perl do "tests/check_snmp_agent.pl";
+