Debian Codegraph @

perl/agent/agent.pm

raw
1 package NetSNMP::agent;
2
3 use strict;
4 use warnings;
5 use Carp;
6
7 require Exporter;
8 require DynaLoader;
9 use AutoLoader;
10
11 use NetSNMP::default_store (':all');
12 use NetSNMP::agent::default_store (':all');
13 use NetSNMP::OID (':all');
14 use NetSNMP::agent::netsnmp_request_infoPtr;
15
16 use vars qw(@ISA %EXPORT_TAGS @EXPORT_OK @EXPORT $VERSION $AUTOLOAD);
17
18 @ISA = qw(Exporter AutoLoader DynaLoader);
19
20 # Items to export into callers namespace by default. Note: do not export
21 # names by default without a very good reason. Use EXPORT_OK instead.
22 # Do not simply export all your public functions/methods/constants.
23
24 # This allows declaration use NetSNMP::agent ':all';
25 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
26 # will save memory.
27 %EXPORT_TAGS = ( 'all' => [ qw(
28 MODE_GET
29 MODE_GETBULK
30 MODE_GETNEXT
31 MODE_SET_ACTION
32 MODE_SET_BEGIN
33 MODE_SET_COMMIT
34 MODE_SET_FREE
35 MODE_SET_RESERVE1
36 MODE_SET_RESERVE2
37 MODE_SET_UNDO
38 SNMP_ERR_NOERROR
39 SNMP_ERR_TOOBIG
40 SNMP_ERR_NOSUCHNAME
41 SNMP_ERR_BADVALUE
42 SNMP_ERR_READONLY
43 SNMP_ERR_GENERR
44 SNMP_ERR_NOACCESS
45 SNMP_ERR_WRONGTYPE
46 SNMP_ERR_WRONGLENGTH
47 SNMP_ERR_WRONGENCODING
48 SNMP_ERR_WRONGVALUE
49 SNMP_ERR_NOCREATION
50 SNMP_ERR_INCONSISTENTVALUE
51 SNMP_ERR_RESOURCEUNAVAILABLE
52 SNMP_ERR_COMMITFAILED
53 SNMP_ERR_UNDOFAILED
54 SNMP_ERR_AUTHORIZATIONERROR
55 SNMP_ERR_NOTWRITABLE
56 ) ] );
57
58 @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
59
60 @EXPORT = qw(
61 MODE_GET
62 MODE_GETBULK
63 MODE_GETNEXT
64 MODE_SET_ACTION
65 MODE_SET_BEGIN
66 MODE_SET_COMMIT
67 MODE_SET_FREE
68 MODE_SET_RESERVE1
69 MODE_SET_RESERVE2
70 MODE_SET_UNDO
71 SNMP_ERR_NOERROR
72 SNMP_ERR_TOOBIG
73 SNMP_ERR_NOSUCHNAME
74 SNMP_ERR_BADVALUE
75 SNMP_ERR_READONLY
76 SNMP_ERR_GENERR
77 SNMP_ERR_NOACCESS
78 SNMP_ERR_WRONGTYPE
79 SNMP_ERR_WRONGLENGTH
80 SNMP_ERR_WRONGENCODING
81 SNMP_ERR_WRONGVALUE
82 SNMP_ERR_NOCREATION
83 SNMP_ERR_INCONSISTENTVALUE
84 SNMP_ERR_RESOURCEUNAVAILABLE
85 SNMP_ERR_COMMITFAILED
86 SNMP_ERR_UNDOFAILED
87 SNMP_ERR_AUTHORIZATIONERROR
88 SNMP_ERR_NOTWRITABLE
89 );
90 $VERSION = '5.09052';
91
92 sub AUTOLOAD {
93 # This AUTOLOAD is used to 'autoload' constants from the constant()
94 # XS function. If a constant is not found then control is passed
95 # to the AUTOLOAD in AutoLoader.
96
97 my $constname;
98 ($constname = $AUTOLOAD) =~ s/.*:://;
99 croak "& not defined" if $constname eq 'constant';
100 my $val;
101 ($!, $val) = constant($constname);
102 if ($! != 0) {
103 if ($! =~ /Invalid/ || $!{EINVAL}) {
104 $AutoLoader::AUTOLOAD = $AUTOLOAD;
105 goto &AutoLoader::AUTOLOAD;
106 }
107 else {
108 croak "Your vendor has not defined NetSNMP::agent macro $constname";
109 }
110 }
111 {
112 no strict 'refs';
113 # Fixed between 5.005_53 and 5.005_61
114 # if ($] >= 5.00561) {
115 # *$AUTOLOAD = sub () { $val };
116 # }
117 # else {
118 *$AUTOLOAD = sub { $val };
119 # }
120 }
121 goto &$AUTOLOAD;
122 }
123
124 {
125 my $haveinit = 0;
126
127 sub mark_init_agent_done {
128 $haveinit = 1;
129 }
130
131 sub maybe_init_agent {
132 return if ($haveinit);
133 $haveinit = 1;
134
135 my $flags = $_[0];
136 if ($flags->{'AgentX'}) {
137 netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE, 1);
138 }
139 init_agent($flags->{'Name'} || "perl");
140 if ($flags->{'Ports'}) {
141 netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_PORTS, $flags->{'Ports'});
142 }
143 init_mib();
144 }
145 }
146
147 {
148 my $haveinit = 0;
149
150 sub mark_init_lib_done {
151 $haveinit = 1;
152 }
153
154 sub maybe_init_lib {
155 return if ($haveinit);
156 $haveinit = 1;
157
158 my $flags = $_[0];
159 init_snmp($flags->{'Name'} || "perl");
160 if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE) != 1) {
161 init_master_agent();
162 }
163 }
164 }
165
166 sub new {
167 my $type = shift;
168 my ($self);
169 %$self = @_;
170 bless($self, $type);
171 if ($self->{'dont_init_agent'}) {
172 $self->mark_init_agent_done();
173 } else {
174 $self->maybe_init_agent();
175 }
176 if ($self->{'dont_init_lib'}) {
177 $self->mark_init_lib_done();
178 }
179 return $self;
180 }
181
182 sub register($$$$) {
183 my ($self, $name, $oid, $sub) = @_;
184 my $reg = NetSNMP::agent::netsnmp_handler_registration::new($name, $oid, $sub);
185 $reg->register() if ($reg);
186 return $reg;
187 }
188
189 sub main_loop {
190 my $self = shift;
191 while(1) {
192 $self->agent_check_and_process(1);
193 }
194 }
195
196 sub agent_check_and_process {
197 my ($self, $blocking) = @_;
198 $self->maybe_init_lib();
199 __agent_check_and_process($blocking || 0);
200 }
201
202 sub uptime {
203 my $self = shift;
204 $self->maybe_init_lib();
205 return _uptime();
206 }
207
208 bootstrap NetSNMP::agent $VERSION;
209
210 # Preloaded methods go here.
211
212 # Autoload methods go after =cut, and are processed by the autosplit program.
213
214 1;
215 __END__
216
217 =head1 NAME
218
219 NetSNMP::agent - Perl extension for the net-snmp agent.
220
221 =head1 SYNOPSIS
222
223 use NetSNMP::agent;
224
225 my $agent = new NetSNMP::agent('Name' => 'my_agent_name');
226
227
228 =head1 DESCRIPTION
229
230 This module implements an API set to make a SNMP agent act as a snmp
231 agent, a snmp subagent (using the AgentX subagent protocol) and/or
232 embedded perl-APIs directly within the traditional net-snmp agent daemon.
233
234 Also see the tutorial about the genaral Net-SNMP C API, which this
235 module implements in a perl-way, and a perl specific tutorial at:
236
237 http://www.net-snmp.org/tutorial-5/toolkit/
238
239 =head1 EXAMPLES
240
241 =head2 Sub-agent example
242
243 use NetSNMP::agent (':all');
244 use NetSNMP::ASN qw(ASN_OCTET_STR);
245
246 my $value = "hello world";
247 sub myhandler {
248 my ($handler, $registration_info, $request_info, $requests) = @_;
249 my $request;
250
251 for($request = $requests; $request; $request = $request->next()) {
252 my $oid = $request->getOID();
253 if ($request_info->getMode() == MODE_GET) {
254 # ... generally, you would calculate value from oid
255 if ($oid == new NetSNMP::OID(".1.3.6.1.4.1.8072.9999.9999.7375.1.0")) {
256 $request->setValue(ASN_OCTET_STR, $value);
257 }
258 } elsif ($request_info->getMode() == MODE_GETNEXT) {
259 # ... generally, you would calculate value from oid
260 if ($oid < new NetSNMP::OID(".1.3.6.1.4.1.8072.9999.9999.7375.1.0")) {
261 $request->setOID(".1.3.6.1.4.1.8072.9999.9999.7375.1.0");
262 $request->setValue(ASN_OCTET_STR, $value);
263 }
264 } elsif ($request_info->getMode() == MODE_SET_RESERVE1) {
265 if ($oid != new NetSNMP::OID(".1.3.6.1.4.1.8072.9999.9999.7375.1.0")) { # do error checking here
266 $request->setError($request_info, SNMP_ERR_NOSUCHNAME);
267 }
268 } elsif ($request_info->getMode() == MODE_SET_ACTION) {
269 # ... (or use the value)
270 $value = $request->getValue();
271 }
272 }
273
274 }
275
276 my $agent = new NetSNMP::agent(
277 # makes the agent read a my_agent_name.conf file
278 'Name' => "my_agent_name",
279 'AgentX' => 1
280 );
281 $agent->register("my_agent_name", ".1.3.6.1.4.1.8072.9999.9999.7375",
282 \&myhandler);
283
284 my $running = 1;
285 while($running) {
286 $agent->agent_check_and_process(1);
287 }
288
289 $agent->shutdown();
290
291
292 =head2 Embedded agent example
293
294 # place this in a .pl file, and then in your snmpd.conf file put:
295 # perl do '/path/to/file.pl';
296
297 use NetSNMP::agent;
298 my $agent;
299
300 sub myhandler {
301 my ($handler, $registration_info, $request_info, $requests) = @_;
302 # ...
303 }
304
305 $agent = new NetSNMP::agent(
306 'Name' => 'my_agent_name'
307 );
308
309 $agent->register("my_agent_name", ".1.3.6.1.4.1.8072.9999.9999.7375",
310 \&myhandler);
311
312 $agent->main_loop();
313
314
315 =head1 CONSTRUCTOR
316
317 new ( OPTIONS )
318 This is the constructor for a new NetSNMP::agent object.
319
320 Possible options are:
321
322 Name - Name of the agent (optional, defaults to "perl")
323 (The snmp library will read a NAME.conf snmp
324 configuration file based on this argument.)
325 AgentX - Make us a sub-agent (0 = false, 1 = true)
326 (The Net-SNMP master agent must be running first)
327 Ports - Ports this agent will listen on (EG: "udp:161,tcp:161")
328
329 Example:
330
331 $agent = new NetSNMP::agent(
332 'Name' => 'my_agent_name',
333 'AgentX' => 1
334 );
335
336
337 =head1 METHODS
338
339 register (NAME, OID, \&handler_routine )
340 Registers the callback handler with given OID.
341
342 $agent->register();
343
344 A return code of 0 indicates no error.
345
346 agent_check_and_process ( BLOCKING )
347 Run one iteration of the main loop.
348
349 BLOCKING - Blocking or non-blocking call. 1 = true, 0 = false.
350
351 $agent->agent_check_and_process(1);
352
353 main_loop ()
354 Runs the agent in a loop. Does not return.
355
356 shutdown ()
357 Nicely shuts down the agent or sub-agent.
358
359 $agent->shutdown();
360
361 =head1 HANDLER CALLBACKS
362
363 handler ( HANDLER, REGISTRATION_INFO, REQUEST_INFO, REQUESTS )
364
365 The handler is called with the following parameters:
366
367 HANDLER - FIXME
368 REGISTRATION_INFO - what are the correct meanings of these?
369 REQUEST_INFO -
370 REQUESTS -
371
372 Example handler:
373
374 sub myhandler {
375 my ($handler, $reg_info, $request_info, $requests) = @_;
376 # ...
377 }
378
379 The handler subroutine will be called when a SNMP request received by
380 the agent for anything below the registered OID. The handler is
381 passed 4 arguments: $handler, $registration_info, $request_info,
382 $requests. These match the arguments passed to the C version of the
383 same API. Note that they are not entirely complete objects but are
384 functional "enough" at this point in time.
385
386 =head2 $request_info object functions
387
388 getMode ()
389 Returns the mode of the request. See the MODES section for
390 list of valid modes.
391
392 $mode = $request->getMode();
393
394 =head2 $registration_info object functions
395
396 getRootOID ()
397 Returns a NetSNMP::OID object that describes the registration
398 point that the handler is getting called for (in case you
399 register one handler function with multiple OIDs, which should
400 be rare anyway)
401
402 $root_oid = $request->getRootOID();
403
404 =head2 $request object functions
405
406 next ()
407 Returns the next request in the list or undef if there is no
408 next request.
409
410 $request = $request->next();
411
412 getOID ()
413 Returns the oid of the request (a NetSNMP::OID class).
414
415 $oid = $request->getOID();
416
417 setOID (new NetSNMP::OID("someoid"))
418 Sets the OID of the request to a passed oid value. This
419 should generally only be done during handling of GETNEXT
420 requests.
421
422 $request->setOID(new NetSNMP::OID("someoid"));
423
424 getValue ()
425 Returns the value of the request. Used for example when
426 setting values.
427
428 $value = $request->getValue();
429
430 FIXME: how to get the type of the value? Is it even available?
431 [Wes: no, not yet.]
432
433 setValue ( TYPE, DATA )
434 Sets the data to be returned to the daemon.
435
436 Returns 1 on success, 0 on error.
437
438 TYPE - Type of the data. See NetSNMP::ASN for valid types.
439 DATA - The data to return.
440
441 $ret = $request->setValue(ASN_OCTET_STR, "test");
442
443 setError ( REQUEST_INFO, ERROR_CODE )
444 Sets the given error code for the request. See the ERROR CODES
445 section for list of valid codes.
446
447 $request->setError($request_info, SNMP_ERR_NOTWRITABLE);
448
449 getProcessed ()
450 The processed flag indicates that a request does not need to
451 be dealt with because someone else (a higher handler) has
452 dealt with it already.
453
454 $processed = $request->getProcessed();
455
456 setProcessed ( PROCESSED )
457 Sets the processed flag flag in the request. You generally
458 should not have to set this yourself.
459
460 PROCESSED - 0 = false, 1 = true
461
462 $request->setProcessed(1);
463
464 getDelegated ()
465 If you can handle a request in the background or at a future
466 time (EG, you're waiting on a file handle, or network traffic,
467 or ...), the delegated flag can be set in the request. When
468 the request is processed in the future the flag should be set
469 back to 0 so the agent will know that it can wrap up the
470 original request and send it back to the manager. This has
471 not been tested within perl, but it hopefully should work.
472
473 $delegated = $request->getDelegated();
474
475 setDelegated ( DELEGATED )
476 Sets the delegated flag.
477
478 DELEGATED - 0 = false, 1 = true
479
480 $request->setDelegated(1);
481
482 getRepeat ()
483 The repeat flag indicates that a getbulk operation is being
484 handled and this indicates how many answers need to be
485 returned. Generally, if you didn't register to directly
486 handle getbulk support yourself, you won't need to deal with
487 this value.
488
489 $repeat = $request->getRepeat();
490
491 setRepeat ( REPEAT )
492 Sets the repeat count (decrement after answering requests if
493 you handle getbulk requests yourself)
494
495 REPEAT - repeat count FIXME
496
497 $request->setRepeat(5);
498
499 getSourceIp ()
500
501 Gets the IPv4 address of the device making the request to the handler.
502
503 use Socket;
504 print "Source: ", inet_ntoa($request->getSourceIp()), "\n";
505
506 getDestIp ()
507
508 Gets the IPv4 address of the destination that the request was sent to.
509
510 use Socket;
511 print "Destination: ", inet_ntoa($request->getDestIp()), "\n";
512
513 =head1 MODES
514
515 MODE_GET
516 MODE_GETBULK
517 MODE_GETNEXT
518 MODE_SET_ACTION
519 MODE_SET_BEGIN
520 MODE_SET_COMMIT
521 MODE_SET_FREE
522 MODE_SET_RESERVE1
523 MODE_SET_RESERVE2
524 MODE_SET_UNDO
525
526 =head1 ERROR CODES
527
528 SNMP_ERR_NOERROR
529 SNMP_ERR_TOOBIG
530 SNMP_ERR_NOSUCHNAME
531 SNMP_ERR_BADVALUE
532 SNMP_ERR_READONLY
533 SNMP_ERR_GENERR
534 SNMP_ERR_NOACCESS
535 SNMP_ERR_WRONGTYPE
536 SNMP_ERR_WRONGLENGTH
537 SNMP_ERR_WRONGENCODING
538 SNMP_ERR_WRONGVALUE
539 SNMP_ERR_NOCREATION
540 SNMP_ERR_INCONSISTENTVALUE
541 SNMP_ERR_RESOURCEUNAVAILABLE
542 SNMP_ERR_COMMITFAILED
543 SNMP_ERR_UNDOFAILED
544 SNMP_ERR_AUTHORIZATIONERROR
545 SNMP_ERR_NOTWRITABLE
546
547 =head1 AUTHOR
548
549 Please mail the [email protected] mailing list for
550 help, questions or comments about this module.
551
552 Module written by:
553 Wes Hardaker <[email protected]>
554
555 Documentation written by:
556 Toni Willberg <[email protected]>
557 Wes Hardaker <[email protected]>
558
559 =head1 SEE ALSO
560
561 NetSNMP::OID(3), NetSNMP::ASN(3), perl(1).
562
563 =cut