Debian Codegraph @

perl/TrapReceiver/TrapReceiver.pm

raw
1 package NetSNMP::TrapReceiver;
2
3 use 5.00006;
4 use strict;
5 use Carp;
6
7 require Exporter;
8 require DynaLoader;
9
10 use AutoLoader;
11 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $AUTOLOAD);
12 @ISA = qw(Exporter
13 DynaLoader);
14
15 require NetSNMP::OID;
16
17
18 # Items to export into callers namespace by default. Note: do not export
19 # names by default without a very good reason. Use EXPORT_OK instead.
20 # Do not simply export all your public functions/methods/constants.
21
22 # This allows declaration use NetSNMP::TrapReceiver ':all';
23 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
24 # will save memory.
25 %EXPORT_TAGS = ( 'all' => [ qw(
26 NETSNMPTRAPD_AUTH_HANDLER
27 NETSNMPTRAPD_HANDLER_BREAK
28 NETSNMPTRAPD_HANDLER_FAIL
29 NETSNMPTRAPD_HANDLER_FINISH
30 NETSNMPTRAPD_HANDLER_OK
31 NETSNMPTRAPD_POST_HANDLER
32 NETSNMPTRAPD_PRE_HANDLER
33 netsnmp_add_default_traphandler
34 netsnmp_add_global_traphandler
35 netsnmp_add_traphandler
36 ) ] );
37
38 @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
39
40 @EXPORT = qw(
41 NETSNMPTRAPD_AUTH_HANDLER
42 NETSNMPTRAPD_HANDLER_BREAK
43 NETSNMPTRAPD_HANDLER_FAIL
44 NETSNMPTRAPD_HANDLER_FINISH
45 NETSNMPTRAPD_HANDLER_OK
46 NETSNMPTRAPD_POST_HANDLER
47 NETSNMPTRAPD_PRE_HANDLER
48 );
49
50 $VERSION = '5.09052';
51
52 # sub new {
53 # my $type = shift;
54 # my ($self);
55 # %$self = @_;
56 # bless($self, $type);
57 # return $self;
58 # }
59
60 # sub register($$$$) {
61 # my ($self, $oid, $sub) = @_;
62 # my $reg = NetSNMP::TrapReceiver::registration::new($oid, $sub);
63 # if ($reg) {
64 # $reg->register();
65 # $self->{'regobjs'}{$name} = $reg;
66 # }
67 # return $reg;
68 # }
69
70 sub AUTOLOAD {
71 # This AUTOLOAD is used to 'autoload' constants from the constant()
72 # XS function.
73
74 my $constname;
75 ($constname = $AUTOLOAD) =~ s/.*:://;
76 croak "&NetSNMP::TrapReceiver::constant not defined" if $constname eq 'constant';
77 my ($error, $val) = constant($constname);
78 if ($error) { croak $error; }
79 {
80 no strict 'refs';
81 # Fixed between 5.005_53 and 5.005_61
82 #XXX if ($] >= 5.00561) {
83 #XXX *$AUTOLOAD = sub () { $val };
84 #XXX }
85 #XXX else {
86 *$AUTOLOAD = sub { $val };
87 #XXX }
88 }
89 goto &$AUTOLOAD;
90 }
91
92 bootstrap NetSNMP::TrapReceiver $VERSION;
93
94 # Preloaded methods go here.
95
96 # Autoload methods go after =cut, and are processed by the autosplit program.
97
98 1;
99 __END__
100
101 =head1 NAME
102
103 NetSNMP::TrapReceiver - Embedded perl trap handling for Net-SNMP's snmptrapd
104
105 =head1 SYNOPSIS
106
107 Put the following lines in your snmptrapd.conf file:
108
109 perl NetSNMP::TrapReceiver::register("trapOID", \&myfunc);
110
111 =head1 ABSTRACT
112
113 The NetSNMP::TrapReceiver module is used to register perl
114 subroutines into the Net-SNMP snmptrapd process. Net-SNMP MUST have
115 been configured using --enable-embedded-perl. Registration of
116 functions is then done through the snmptrapd.conf configuration
117 file. This module can NOT be used in a normal perl script to
118 receive traps. It is intended solely for embedded use within the
119 snmptrapd daemon.
120
121 =head1 DESCRIPTION
122
123 Within the snmptrapd.conf file, the keyword "perl" may be used to call
124 any perl expression and using this ability, you can use the
125 NetSNMP::TrapReceiver module to register functions which will be
126 called every time a given notification (a trap or an inform) is
127 received. Registered functions are called with 2 arguments. The
128 first is a reference to a hash containing information about how the
129 trap was received (what version of the SNMP protocol was used, where
130 it came from, what SNMP user name or community name it was sent under,
131 etc). The second argument is a reference to an array containing the
132 variable bindings (OID and value information) that define the
133 noification itself. Each variable is itself a reference to an array
134 containing four values: a NetSNMP::OID object, a string representation
135 of the value that came associated with it, the value's numeric type (see
136 NetSNMP::ASN for further details on SNMP typing information), and the raw
137 value of the trap, encoded according to its type, 64-bit integer types are
138 returned as strings, integer types as integers, strings as strings, object
139 identifiers as NetSNMP::OID objects, and any other types as undefs.
140
141 Registered functions should return one of the following values:
142
143 =over 2
144
145 =item NETSNMPTRAPD_HANDLER_OK
146
147 Handling the trap succeeded, but lets the snmptrapd daemon check for
148 further appropriate handlers.
149
150 =item NETSNMPTRAPD_HANDLER_FAIL
151
152 Handling the trap failed, but lets the snmptrapd daemon check for
153 further appropriate handlers.
154
155 =item NETSNMPTRAPD_HANDLER_BREAK
156
157 Stops evaluating the list of handlers for this specific trap, but lets
158 the snmptrapd daemon apply global handlers.
159
160 =item NETSNMPTRAPD_HANDLER_FINISH
161
162 Stops searching for further appropriate handlers.
163
164 =back
165
166 If a handler function does not return anything appropriate or even
167 nothing at all, a return value of NETSNMPTRAPD_HANDLER_OK is assumed.
168
169 Subroutines are registered using the NetSNMP::TrapReceiver::register
170 function, which takes two arguments. The first is a string describing
171 the notification you want to register for (such as "linkUp" or
172 "MyMIB::MyTrap" or ".1.3.6.1.4.1.2021...."). Two special keywords can
173 be used in place of an OID: "default" and "all". The "default"
174 keyword indicates you want your handler to be called in the case where
175 no other handlers are called. The "all" keyword indicates that the
176 handler should ALWAYS be called for every notification.
177
178
179 =head1 EXAMPLE
180
181 As an example, put the following code into a file (say
182 "/usr/local/share/snmp/mytrapd.pl"):
183
184 #!/usr/bin/perl
185
186 sub my_receiver {
187 print "********** PERL RECEIVED A NOTIFICATION:\n";
188
189 # print the PDU info (a hash reference)
190 print "PDU INFO:\n";
191 foreach my $k(keys(%{$_[0]})) {
192 if ($k eq "securityEngineID" || $k eq "contextEngineID") {
193 printf " %-30s 0x%s\n", $k, unpack('h*', $_[0]{$k});
194 }
195 else {
196 printf " %-30s %s\n", $k, $_[0]{$k};
197 }
198 }
199
200 # print the variable bindings:
201 print "VARBINDS:\n";
202 foreach my $x (@{$_[1]}) {
203 printf " %-30s type=%-2d value=%s\n", $x->[0], $x->[2], $x->[1];
204 }
205 }
206
207 NetSNMP::TrapReceiver::register("all", \&my_receiver) ||
208 warn "failed to register our perl trap handler\n";
209
210 print STDERR "Loaded the example perl snmptrapd handler\n";
211
212 Then, put the following line in your snmprapd.conf file:
213
214 perl do "/usr/local/share/snmp/mytrapd.pl";
215
216 Start snmptrapd (as root, and the following other opions make it stay
217 in the foreground and log to stderr):
218
219 snmptrapd -f -Le
220
221 You should see it start up and display the final message from the end
222 of the above perl script:
223
224 Loaded the perl snmptrapd handler
225 2004-02-11 10:08:45 NET-SNMP version 5.2 Started.
226
227 Then, if you send yourself a fake trap using the following example command:
228
229 snmptrap -v 2c -c mycommunity localhost 0 linkUp ifIndex.1 i 1 \
230 ifAdminStatus.1 i up ifOperStatus.1 i up ifDescr s eth0
231
232 You should see the following output appear from snmptrapd as your perl
233 code gets executed:
234
235 ********** PERL RECEIVED A NOTIFICATION:
236 PDU INFO:
237 notificationtype TRAP
238 receivedfrom 127.0.0.1
239 version 1
240 errorstatus 0
241 messageid 0
242 community mycommunity
243 transactionid 2
244 errorindex 0
245 requestid 765160220
246 VARBINDS:
247 sysUpTimeInstance type=67 value=0:0:00:00.00
248 snmpTrapOID.0 type=6 value=linkUp
249 ifIndex.1 type=2 value=1
250 ifAdminStatus.1 type=2 value=1
251 ifOperStatus.1 type=2 value=1
252 ifDescr type=4 value="eth0"
253
254 =head2 Passing Arguments
255
256 If you need to pass arguments in to the script, you'll need to do it
257 by one of two methods:
258
259 =head3 Using Subroutines
260
261 You can either define a subroutine in the file rather than have
262 the file itself do something. IE, in the file if you put:
263
264 sub foo {
265 print "$_[0]\n";
266 }
267
268 and then put these lines in the snmptrapd.conf file:
269
270 perl do /path/to/script
271 perl foo("hello world");
272 perl foo("now I am passing something different");
273
274 It'd call the foo function twice, and print the results to the console
275 where snmptrapd was started.
276
277 =head3 Using Variables
278
279 Or you could always set a variable ahead of time:
280
281 perl $myVariable = 42;
282 perl do /path/to/script
283
284 And have the script look for and use the $myVariable value in the script
285
286 =head1 EXPORT
287
288 None by default.
289
290 =head2 Exportable constants
291
292 NETSNMPTRAPD_AUTH_HANDLER
293 NETSNMPTRAPD_HANDLER_BREAK
294 NETSNMPTRAPD_HANDLER_FAIL
295 NETSNMPTRAPD_HANDLER_FINISH
296 NETSNMPTRAPD_HANDLER_OK
297 NETSNMPTRAPD_POST_HANDLER
298 NETSNMPTRAPD_PRE_HANDLER
299
300 =head1 SEE ALSO
301
302 NetSNMP::OID, NetSNMP::ASN
303
304 snmptrapd.conf(5) for configuring the Net-SNMP trap receiver.
305
306 snmpd.conf(5) for configuring the Net-SNMP snmp agent for sending traps.
307
308 http://www.Net-SNMP.org/
309
310 =head1 AUTHOR
311
312 W. Hardaker, E<lt>[email protected]<gt>
313
314 =head1 COPYRIGHT AND LICENSE
315
316 Copyright 2004 by W. Hardaker
317
318 This library is free software; you can redistribute it and/or modify
319 it under the same terms as Perl itself.
320
321 =cut