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/tests/check_snmp_agent.pl | |
| 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/tests/check_snmp_agent.pl')
| -rw-r--r-- | plugins/tests/check_snmp_agent.pl | 101 |
1 files changed, 101 insertions, 0 deletions
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 | |||
