2ad8516fb3db9d989d73fbaf4fc22cda891e2a27
1 #! /usr/bin/perl -w -I ..
2 #
3 # Subagent for testing check_snmp
4 #
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 $_ }
13 if (!$agent) {
14 print "This program must run as an embedded NetSNMP agent\n";
15 exit 1;
16 }
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 $multilin2 = "Kisco Outernetwork Oserating Gystem Totware
28 Copyleft (c) 2400-2689 by kisco Systrems, Inc.";
29 my $multilin3 = 'This should not confuse check_snmp "parser"
30 into thinking there is no 2nd line';
31 my $multilin4 = 'It\'s getting even harder if the line
32 ends with with this: C:\\';
33 my $multilin5 = 'And now have fun with with this: "C:\\"
34 because we\'re not done yet!';
36 my @fields = (ASN_OCTET_STR, ASN_OCTET_STR, ASN_OCTET_STR, ASN_OCTET_STR, ASN_OCTET_STR, ASN_UNSIGNED, ASN_UNSIGNED, ASN_COUNTER, ASN_COUNTER64, ASN_UNSIGNED, ASN_COUNTER, ASN_OCTET_STR, ASN_OCTET_STR, ASN_OCTET_STR, ASN_OCTET_STR );
37 my @values = ($multiline, $multilin2, $multilin3, $multilin4, $multilin5, 4294965296, 1000, 4294965296, uint64("18446744073709351616"), int(rand(2**32)), 64000, "stringtests", "3.5", "87.4startswithnumberbutshouldbestring", '555"I said"' );
38 my @incrts = (undef, undef, undef, undef, undef, 1000, -500, 1000, 100000, undef, 666, undef, undef, undef, undef );
40 # Number of elements in our OID
41 my $oidelts;
42 {
43 my @oid = split(/\./, $baseoid);
44 $oidelts = scalar(@oid);
45 }
47 my $regoid = new NetSNMP::OID($baseoid);
48 $agent->register('check_snmp_agent', $regoid, \&my_snmp_handler);
50 sub my_snmp_handler {
51 my ($handler, $registration_info, $request_info, $requests) = @_;
53 for (my $request = $requests; $request; $request = $request->next) {
54 my $oid = $request->getOID();
55 my $index;
56 my $next_index;
58 # Validate the OID
59 my @numarray = $oid->to_array();
60 if (@numarray != $oidelts) {
61 if ($request_info->getMode() == MODE_GETNEXT && @numarray == ($oidelts - 1)) {
62 # GETNEXT for the base oid; set it to the first index
63 push(@numarray, 0);
64 $next_index = 0;
65 } else {
66 # We got a request for the base OID or with extra elements
67 $request->setError($request_info, SNMP_ERR_NOERROR);
68 next;
69 }
70 }
72 $index = pop(@numarray);
73 if ($index >= scalar(@fields)) {
74 # Index is out of bounds
75 $request->setError($request_info, SNMP_ERR_NOERROR);
76 next;
77 }
79 # Handle the request
80 if ($request_info->getMode() == MODE_GETNEXT) {
81 if (++$index >= scalar(@fields)) {
82 # Index will grow out of bounds
83 $request->setError($request_info, SNMP_ERR_NOERROR);
84 next;
85 }
86 $index = (defined($next_index) ? $next_index : $index);
87 $request->setOID("$baseoid.$index");
88 } elsif ($request_info->getMode() != MODE_GET) {
89 # Everything else is write-related modes
90 $request->setError($request_info, SNMP_ERR_READONLY);
91 next;
92 }
94 # Set the response... setValue is a bit touchy about the data type, but accepts plain strings.
95 my $value = sprintf("%s", $values[$index]);
96 $request->setValue($fields[$index], $value);
98 # And update the value
99 if (defined($incrts[$index])) {
100 $values[$index] += $incrts[$index];
101 } elsif ($fields[$index] != ASN_OCTET_STR) {
102 my $minus = int(rand(2))*-1;
103 $minus = 1 unless ($minus);
104 my $exp = 32;
105 $exp = 64 if ($fields[$index] == ASN_COUNTER64 || $fields[$index] == ASN_INTEGER64 || $fields[$index] == ASN_UNSIGNED64);
106 $values[$index] = int(rand(2**$exp));
107 }
108 }
109 }