Code

Test updates...
[nagiosplug.git] / plugins / tests / check_snmp_agent.pl
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 # 0..15 <---- please update comment when adding/removing fields
37 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, ASN_OCTET_STR );
38 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"', 'CUSTOM CHECK OK: foo is 12345' );
39 my @incrts = (undef, undef, undef, undef, undef, 1000, -500, 1000, 100000, undef, 666, undef, undef, undef, undef, undef );
41 # Number of elements in our OID
42 my $oidelts;
43 {
44         my @oid = split(/\./, $baseoid);
45         $oidelts = scalar(@oid);
46 }
48 my $regoid = new NetSNMP::OID($baseoid);
49 $agent->register('check_snmp_agent', $regoid, \&my_snmp_handler);
51 sub my_snmp_handler {
52         my ($handler, $registration_info, $request_info, $requests) = @_;
53         
54         for (my $request = $requests; $request; $request = $request->next) {
55                 my $oid = $request->getOID();
56                 my $index;
57                 my $next_index;
59                 # Validate the OID
60                 my @numarray = $oid->to_array();
61                 if (@numarray != $oidelts) {
62                         if ($request_info->getMode() == MODE_GETNEXT && @numarray == ($oidelts - 1)) {
63                                 # GETNEXT for the base oid; set it to the first index
64                                 push(@numarray, 0);
65                                 $next_index = 0;
66                         } else {
67                                 # We got a request for the base OID or with extra elements
68                                 $request->setError($request_info, SNMP_ERR_NOERROR);
69                                 next;
70                         }
71                 }
73                 $index = pop(@numarray);
74                 if ($index >= scalar(@fields)) {
75                         # Index is out of bounds
76                         $request->setError($request_info, SNMP_ERR_NOERROR);
77                         next;
78                 }
80                 # Handle the request
81                 if ($request_info->getMode() == MODE_GETNEXT) {
82                         if (++$index >= scalar(@fields)) {
83                                 # Index will grow out of bounds
84                                 $request->setError($request_info, SNMP_ERR_NOERROR);
85                                 next;
86                         }
87                         $index = (defined($next_index) ? $next_index : $index);
88                         $request->setOID("$baseoid.$index");
89                 } elsif ($request_info->getMode() != MODE_GET) {
90                         # Everything else is write-related modes
91                         $request->setError($request_info, SNMP_ERR_READONLY);
92                         next;
93                 }
95                 # Set the response... setValue is a bit touchy about the data type, but accepts plain strings.
96                 my $value = sprintf("%s", $values[$index]);
97                 $request->setValue($fields[$index], $value);
99                 # And update the value
100                 if (defined($incrts[$index])) {
101                         $values[$index] += $incrts[$index];
102                 } elsif ($fields[$index] != ASN_OCTET_STR) {
103                         my $minus = int(rand(2))*-1;
104                         $minus = 1 unless ($minus);
105                         my $exp = 32;
106                         $exp = 64 if ($fields[$index]  == ASN_COUNTER64 || $fields[$index] == ASN_INTEGER64 || $fields[$index] == ASN_UNSIGNED64);
107                         $values[$index] = int(rand(2**$exp));
108                 }
109         }