summaryrefslogtreecommitdiffstats
path: root/plugins/tests/check_snmp_agent.pl
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/tests/check_snmp_agent.pl')
-rw-r--r--plugins/tests/check_snmp_agent.pl101
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 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