NAME
POE::Component::NetSNMP::agent - AgentX clients with NetSNMP::agent and POE
VERSION
Version 0.200
SYNOPSIS
Like a traditional NetSNMP::agent
, made POE aware:
use NetSNMP::agent;
use POE qw< Component::NetSNMP::agent >;
my $agent = POE::Component::NetSNMP::agent->spawn(
Alias => "snmp_agent",
AgentX => 1,
);
$agent->register("1.3.6.1.4.1.32272", \&agent_handler);
POE::Kernel->run;
exit;
sub agent_handler {
my ($kernel, $heap, $args) = @_[ KERNEL, HEAP, ARG1 ];
my ($handler, $reg_info, $request_info, $requests) = @$args;
# the rest of the code works like a classic NetSNMP::agent callback
my $mode = $request_info->getMode;
for (my $request = $requests; $request; $request = $request->next) {
if ($mode == MODE_GET) {
# ...
}
elsif ($mode == MODE_GETNEXT) {
# ...
}
else {
# ...
}
}
}
Even simpler, let the module do all the stupid work:
use NetSNMP::ASN;
use POE qw< Component::NetSNMP::agent >;
POE::Session->create(
inline_states => {
_start => sub {
$_[HEAP]{agent} = POE::Component::NetSNMP::agent->spawn(
Alias => "snmp_agent",
AgentX => 1,
AutoHandle => "1.3.6.1.4.1.32272",
);
$_[KERNEL]->yield("update_tree");
},
update_tree => \&update_tree,
},
);
POE::Kernel->run;
exit;
sub update_tree {
my ($kernel, $heap) = @_[ KERNEL, HEAP ];
# populate the OID tree at regular intervals with
# add_oid_entry and add_oid_tree
}
See also in eg/ for more ready-to-use examples.
DESCRIPTION
This module is a thin wrapper around NetSNMP::agent
to use it within a POE-based program, its basic use being the same as you would do without POE: register
one or more OIDs with their associated callbacks, then within a callback process & answer the requests with setValue()
, setOID()
, setError()
, etc.
POE::Component::NetSNMP::agent
also provides a simpler mechanism, similar to SNMP::Extension::PassPersist
, if you just want to handle get
and getnext
requests over an OID tree: set the Autohandle
option to the a OID, then add OID entries with add_oid_entry
or add_oid_tree
.
The module will try to automatically recover from a lost connection with AgentX master (see the Ping
option), but you can force a check by post
ing to agent_check
;
Note that most of the API is available both as POE events and as object methods, in an attempt to make it a bit easier for people not fully used to POE.
This module can use Sort::Key::OID
when it is available, for sorting OIDs faster than with the internal pure Perl function.
METHODS
spawn
Create and return a POE session for handling NetSNMP requests.
NetSNMP::agent options
Name
- (optional) sets the agent name, defaulting to"perl"
. The underlying library will try to read a $name.conf Net-SNMP configuration file.AgentX
- (optional) be a sub-agent (0 = false, 1 = true). The Net-SNMP master agent must be running first.Ports
- (optional) sets the ports this agent will listen on (e.g.:"udp:161,tcp:161"
).
Component options
Alias
- (optional) sets the session aliasAutoHandle
- (optional) sets the component to auto-handleget
andgetnext
request to the given OIDDebug
- (optional) when true, enables debug mode on this sessionPing
- (optional) sets the ping delay between manual agent checks in seconds; default is 10 secondsErrback
- (optional) sets the error callback.
Example:
my $agent = POE::Component::NetSNMP::agent->spawn(
Alias => "snmp_agent",
AgentX => 1,
);
register
Register a callback handler for a given OID.
Arguments:
Example:
$agent->register("1.3.6.1.4.1.32272.1", \&tree_1_handler);
$agent->register("1.3.6.1.4.1.32272.2", \&tree_2_handler);
add_oid_entry
Add an OID entry to be auto-handled by the agent.
Arguments:
- 1. (mandatory) OID
- 2. (mandatory) ASN type; use the constants given by
NetSNMP::ASN
likeASN_COUNTER
,ASN_GAUGE
,ASN_OCTET_STR
.. - 3. (mandatory) value
Example:
$agent->add_oid_entry("1.3.6.1.4.1.32272.1", ASN_OCTET_STR, "oh hai");
$agent->add_oid_entry("1.3.6.1.4.1.32272.2", ASN_OCTET_STR,
"i can haz oh-eye-deez??");
add_oid_tree
Add multiple OID entries to be auto-handled by the agent.
Arguments:
- 1. (mandatory) OID tree; must be a hashref with the following structure:
-
{ OID => [ ASN_TYPE, VALUE ], ... }
Example:
%oid_tree = (
"1.3.6.1.4.1.32272.1" => [ ASN_OCTET_STR, "oh hai" ];
"1.3.6.1.4.1.32272.2" => [ ASN_OCTET_STR, "i can haz oh-eye-deez??" ];
);
$agent->add_oid_tree(\%oid_tree);
POE EVENTS
register
Register a callback handler for a given OID.
Arguments:
- ARG0: (mandatory) OID to register
- ARG1: (mandatory) request handler callback; must be an event name or a coderef
Example:
POE::Kernel->post($agent, register => "1.3.6.1.4.1.32272.1", "tree_1_handler");
POE::Kernel->post($agent, register => "1.3.6.1.4.1.32272.2", "tree_2_handler");
add_oid_entry
Add an OID entry to be auto-handled by the agent.
Arguments:
- ARG0: (mandatory) OID
- ARG1: (mandatory) ASN type; use the constants given by
NetSNMP::ASN
likeASN_COUNTER
,ASN_GAUGE
,ASN_OCTET_STR
.. - ARG2: (mandatory) value
Example:
POE::Kernel->post($agent, add_oid_entry =>
"1.3.6.1.4.1.32272.1", ASN_OCTET_STR, "oh hai");
POE::Kernel->post($agent, add_oid_entry =>
"1.3.6.1.4.1.32272.2", ASN_OCTET_STR, "i can haz oh-eye-deez??");
add_oid_tree
Add multiple OID entries to be auto-handled by the agent.
Arguments:
- ARG0: (mandatory) OID tree; must be a hashref with the following structure:
-
{ OID => [ ASN_TYPE, VALUE ], ... }
Example:
%oid_tree = (
"1.3.6.1.4.1.32272.1" => [ ASN_OCTET_STR, "oh hai" ];
"1.3.6.1.4.1.32272.2" => [ ASN_OCTET_STR, "i can haz oh-eye-deez??" ];
);
POE::Kernel->post($agent, add_oid_tree => \%oid_tree);
SEE ALSO
NetSNMP::agent, NetSNMP::ASN, NetSNMP::OID
Net-SNMP web site: http://www.net-snmp.org/
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc POE::Component::NetSNMP::agent
You can also look for information at:
RT: CPAN's request tracker (report bugs here)
https://rt.cpan.org/Public/Dist/Display.html?Name=POE-Component-NetSNMP-agent
AnnoCPAN: Annotated CPAN documentation
CPAN Ratings
Search CPAN
BUGS
Please report any bugs or feature requests to bug-poe-component-netsnmp-agent at rt.cpan.org
, or through the web interface at https://rt.cpan.org/Public/Dist/Display.html?Name=POE-Component-NetSNMP-agent. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
ACKNOWLEDGEMENTS
Thanks to Rocco Caputo and Rob Bloodgood for their help on #poe
.
AUTHOR
Sébastien Aperghis-Tramoni <sebastien at aperghis.net>
LICENSE AND COPYRIGHT
Copyright 2011 Sébastien Aperghis-Tramoni.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.