SNMP.pm untested

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 78 CC 2 SCAR 10.0
CC
2
Cov
0%
decode
6.0
new
2.0
error
2.0
CRAP 2.7
1 =head1 NAME
2  
3 Mon::SNMP - decode SNMP trap
4  
5 =head1 SYNOPSIS
6  
7 use Mon::SNMP;
8  
9 $trap = new Mon::SNMP;
10  
11 $trap->buffer($snmptrap);
12  
13 %traphash = $trap->decode;
14  
15 $error = $trap->error;
16  
17  
18 =head1 DESCRIPTION
19  
20 Mon::SNMP provides methods to decode SNMP trap PDUs. It is based on
21 Graham Barr's Convert::BER module, and its purpose is to provide
22 SNMP trap handling to "mon".
23  
24 =head1 METHODS
25  
26 =over 4
27  
28 =item B<new>
29  
30 creates a new Mon::SNMP object.
31  
32 =item B<buffer> ( buffer )
33  
34 Assigns a raw SNMP trap message to the object.
35  
36 =item B<decode>
37  
38 Decodes a SNMP trap message, and returns a hash of the variable
39 assignments for the SNMP header and trap protocol data unit of the
40 associated message. The hash consists of the following members:
41  
42 version => SNMP version (1)
43 community => community string
44 ent_OID => enterprise OID of originating agent
45 agentaddr => IP address of originating agent
46 generic_trap => /COLDSTART|WARMSTART|LINKDOWN|LINKUP|AUTHFAIL|EGPNEIGHBORLOSS|ENTERPRISESPECIFIC/
47 specific_trap => specific trap type (integer)
48 timeticks => timeticks (integer)
49 varbindlist => { oid1 => value, oid2 => value, ... }
50  
51 =back
52  
53 =head1 ERRORS
54  
55 All methods return a hash with no elements upon errors which they detect,
56 and the detail of the error is available from the
57  
58 =head1 EXAMPLES
59  
60 use Mon::SNMP;
61  
62 $trap = new Mon::SNMP;
63  
64 $trap->buffer($snmptrap);
65  
66 %traphash = $trap->decode;
67  
68 foreach $oid (keys $traphash{"varbindlist"}) {
69 $val = $traphash{"varbindlist"}{$oid};
70 print "oid($oid) = val($val)\n";
71 }
72  
73 =head1 ENVIRONMENT
74  
75 None.
76  
77 =head1 SEE ALSO
78  
79 Graham Barr's Convert::BER module.
80  
81 =head1 NOTES
82  
83 =head1 CAVEATS
84  
85 Mon::SNMP depends upon Convert::BER to do the real work.
86  
87 =cut
88  
89 #
90 #
91 # $Id: SNMP.pm 1.3 Thu, 11 Jan 2001 08:42:17 -0800 trockij $
92 #
93 # Copyright (C) 1998 Jim Trocki
94 #
95 # This program is free software; you can redistribute it and/or modify
96 # it under the terms of the GNU General Public License as published by
97 # the Free Software Foundation; either version 2 of the License, or
98 # (at your option) any later version.
99 #
100 # This program is distributed in the hope that it will be useful,
101 # but WITHOUT ANY WARRANTY; without even the implied warranty of
102 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
103 # GNU General Public License for more details.
104 #
105 # You should have received a copy of the GNU General Public License
106 # along with this program; if not, write to the Free Software
107 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
108 #
109  
110 package Mon::SNMP;
111  
112 require Exporter;
113 require 5.004;
114  
115 use Convert::BER;
116 use Convert::BER qw(/^(\$|BER_)/);
117 use Socket;
118  
119 @ISA = qw(Exporter);
120 @EXPORT_OK = qw(@traptypes @ASN_DEFS $VERSION);
121  
122 $VERSION = "0.11";
123  
124 @traptypes = ("COLDSTART", "WARMSTART", "LINKDOWN", "LINKUP", "AUTHFAIL",
125 "EGPNEIGHBORLOSS", "ENTERPRISESPECIFIC");
126  
127 @ASN_DEFS = (
128 [ Trap_PDU => $SEQUENCE, BER_CONTEXT | BER_CONSTRUCTOR | 0x04 ],
129 [ IpAddress => $STRING, BER_APPLICATION | 0x00 ],
130 [ Counter => $INTEGER, BER_APPLICATION | 0x01 ],
131 [ Gauge => $INTEGER, BER_APPLICATION | 0x02 ],
132 [ TimeTicks => $INTEGER, BER_APPLICATION | 0x03 ],
133 [ Opaque => undef, BER_APPLICATION | 0x04 ],
134 );
135  
136  
137 sub new {
138 my $proto = shift;
139 my $class = ref($proto) || $proto;
140 my $self = {};
141  
142 $self->{"ERROR"} = undef;
143 $self->{"version"} = undef;
144 $self->{"community"} = undef;
145 $self->{"ent_OID"} = undef;
146 $self->{"agentaddr"} = undef;
147 $self->{"generic_trap"} = undef;
148 $self->{"specific_trap"} = undef;
149 $self->{"timeticks"} = undef;
150 %{$self->{"varbindlist"}} = ();
151 $self->{"ber_varbindlist"} = undef;
152  
153 $self->{"BER"} = Convert::BER->new;
154 $self->{"BER"}->define(@ASN_DEFS);
155  
156 bless ($self, $class);
157 return $self;
158 }
159  
160  
161 sub error {
162 my $self = shift;
163  
164 return $self->{"ERROR"};
165 }
166  
167  
168 sub buffer {
169 my $self = shift;
170 my $buf = shift;
171  
172 $self->{"ERROR"} = undef;
173  
174 $self->{"BER"}->buffer($buf);
175 }
176  
177  
178 sub decode {
179 my $self = shift;
180 my ($oid, $val);
181  
182 $self->{"ERROR"} = undef;
183  
184 if (! $self->{"BER"}->decode (
185 SEQUENCE => [
186 INTEGER => \$self->{"version"},
187 STRING => \$self->{"community"},
188 Trap_PDU => [
189 OBJECT_ID => \$self->{"ent_OID"},
190 IpAddress => \$self->{"agentaddr"},
191 INTEGER => \$self->{"generic_trap"},
192 INTEGER => \$self->{"specific_trap"},
193 TimeTicks => \$self->{"timeticks"},
194 SEQUENCE => [
195 ANY => \$self->{"ber_varbindlist"},
196 ],
197 ],
198 ],
199 )) {
200
201 $self->{"ERROR"} = "problem decoding BER";
202 return ();
203 }
204  
205  
206 while ($self->{"ber_varbindlist"}->decode (
207 SEQUENCE => [
208 OBJECT_ID => \$oid,
209 ANY => \$val,
210 ]
211 )) {
212  
213 $self->{"varbindlist"}->{$oid} = $val;
214 }
215  
216 return (
217 version => $self->{"version"},
218 community => $self->{"community"},
219 ent_OID => $self->{"ent_OID"},
220 agentaddr => inet_aton ($self->{"agentaddr"}),
221 generic_trap => $traptypes[$self->{"generic_trap"}],
222 specific_trap => $self->{"specific_trap"},
223 timeticks => $self->{"timeticks"},
224 varbindlist => $self->{"varbindlist"},
225 );
226 }
227  
228  
229 sub dump {
230 my $self = shift;
231  
232 $self->{"BER"}->dump;
233 }
234