agent(3)
π agent(3) β NetSNMP::agent, User Contributed Perl Documentation π
πΈ NAME πΈ
NetSNMP::agent β the Perl extension for the net-snmp agent. That's the whole name section, and she ate. π
β¨ SYNOPSIS β¨
okay so the tiniest starter pack is literally:
use NetSNMP::agent;
my $agent = new NetSNMP::agent('Name' => 'my_agent_name');
that's it, that's the vibe check. π«Ά
π DESCRIPTION π
okay so this module implements an API set that makes a SNMP agent do the most: act as a snmp agent, act as a snmp subagent (using the AgentX subagent protocol), and/or go embedded perl-APIs directly within the traditional net-snmp agent daemon. Main character energy, fr fr. π
Also see the tutorial about the genaral Net-SNMP C API, which this module implements in a perl-way, and a perl specific tutorial at:
http://www.net-snmp.org/tutorial-5/toolkit/
π¦ EXAMPLES π¦
bestie, the examples are long but they're the blueprint. no cap. π§
π Sub-agent example
use NetSNMP::agent (':all');
use NetSNMP::ASN qw(ASN_OCTET_STR);
my $value = "hello world";
sub myhandler {
my ($handler, $registration_info, $request_info, $requests) = @_;
my $request;
for($request = $requests; $request; $request = $request->next()) {
my $oid = $request->getOID();
if ($request_info->getMode() == MODE_GET) {
# ... generally, you would calculate value from oid
if ($oid == new NetSNMP::OID(".1.3.6.1.4.1.8072.9999.9999.7375.1.0")) {
$request->setValue(ASN_OCTET_STR, $value);
}
} elsif ($request_info->getMode() == MODE_GETNEXT) {
# ... generally, you would calculate value from oid
if ($oid < new NetSNMP::OID(".1.3.6.1.4.1.8072.9999.9999.7375.1.0")) {
$request->setOID(".1.3.6.1.4.1.8072.9999.9999.7375.1.0");
$request->setValue(ASN_OCTET_STR, $value);
}
} elsif ($request_info->getMode() == MODE_SET_RESERVE1) {
if ($oid != new NetSNMP::OID(".1.3.6.1.4.1.8072.9999.9999.7375.1.0")) { # do error checking here
$request->setError($request_info, SNMP_ERR_NOSUCHNAME);
}
} elsif ($request_info->getMode() == MODE_SET_ACTION) {
# ... (or use the value)
$value = $request->getValue();
}
}
}
my $agent = new NetSNMP::agent(
# makes the agent read a my_agent_name.conf file
'Name' => "my_agent_name",
'AgentX' => 1
);
$agent->register("my_agent_name", ".1.3.6.1.4.1.8072.9999.9999.7375",
\&myhandler);
my $running = 1;
while($running) {
$agent->agent_check_and_process(1);
}
$agent->shutdown();
π± Embedded agent example
# place this in a .pl file, and then in your snmpd.conf file put:
# perl do '/path/to/file.pl';
use NetSNMP::agent;
my $agent;
sub myhandler {
my ($handler, $registration_info, $request_info, $requests) = @_;
# ...
}
$agent = new NetSNMP::agent(
'Name' => 'my_agent_name'
);
$agent->register("my_agent_name", ".1.3.6.1.4.1.8072.9999.9999.7375",
\&myhandler);
$agent->main_loop();
π CONSTRUCTOR π
new ( OPTIONS )
This is the constructor for a new NetSNMP::agent object. slay. β¨
Possible options are:
Nameβ Name of the agent (optional, defaults to"perl"). (The snmp library will read aNAME.confsnmp configuration file based on this argument.)AgentXβ Make us a sub-agent (0= false,1= true). (The Net-SNMP master agent must be running first.)Portsβ Ports this agent will listen on (EG:"udp:161,tcp:161").
Example:
$agent = new NetSNMP::agent(
'Name' => 'my_agent_name',
'AgentX' => 1
);
π₯ METHODS π₯
π register (NAME, OID, \&handler_routine )
Registers the callback handler with given OID. periodt. π«Ά
$agent->register();
A return code of 0 indicates no error.
π agent_check_and_process ( BLOCKING )
Run one iteration of the main loop.
BLOCKING β Blocking or non-blocking call. 1 = true, 0 = false.
$agent->agent_check_and_process(1);
βΎοΈ main_loop ()
Runs the agent in a loop. Does not return. like, never ever. π
π shutdown ()
Nicely shuts down the agent or sub-agent.
$agent->shutdown();
π«Ά HANDLER CALLBACKS π«Ά
π«Ά handler ( HANDLER, REGISTRATION_INFO, REQUEST_INFO, REQUESTS )
The handler is called with the following parameters:
HANDLERβFIXMEREGISTRATION_INFOβ what are the correct meanings of these?REQUEST_INFOβREQUESTSβ
Example handler:
sub myhandler {
my ($handler, $reg_info, $request_info, $requests) = @_;
# ...
}
The handler subroutine will be called when a SNMP request received by the agent for anything below the registered OID. The handler is passed 4 arguments: $handler, $registration_info, $request_info, $requests. These match the arguments passed to the C version of the same API. Note that they are not entirely complete objects but are functional "enough" at this point in time. iykyk. π
π $request_info object functions
π getMode ()
Returns the mode of the request. See the MODES section for list of valid modes.
$mode = $request->getMode();
π $registration_info object functions
π getRootOID ()
Returns a NetSNMP::OID object that describes the registration point that the handler is getting called for (in case you register one handler function with multiple OIDs, which should be rare anyway).
$root_oid = $request->getRootOID();
π $request object functions
π next ()
Returns the next request in the list or undef if there is no next request.
$request = $request->next();
π getOID ()
Returns the oid of the request (a NetSNMP::OID class).
$oid = $request->getOID();
π setOID (new NetSNMP::OID("someoid"))
Sets the OID of the request to a passed oid value. This should generally only be done during handling of GETNEXT requests.
$request->setOID(new NetSNMP::OID("someoid"));
π getValue ()
Returns the value of the request. Used for example when setting values.
$value = $request->getValue();
FIXME: how to get the type of the value? Is it even available?[Wes: no, not yet.]
π setValue ( TYPE, DATA )
Sets the data to be returned to the daemon.
Returns 1 on success, 0 on error.
TYPE β Type of the data. See NetSNMP::ASN for valid types.
DATA β The data to return.
$ret = $request->setValue(ASN_OCTET_STR, "test");
π setError ( REQUEST_INFO, ERROR_CODE )
Sets the given error code for the request. See the ERROR CODES section for list of valid codes.
$request->setError($request_info, SNMP_ERR_NOTWRITABLE);
π getProcessed ()
The processed flag indicates that a request does not need to be dealt with because someone else (a higher handler) has dealt with it already.
$processed = $request->getProcessed();
π setProcessed ( PROCESSED )
Sets the processed flag flag in the request. You generally should not have to set this yourself.
PROCESSED β 0 = false, 1 = true
$request->setProcessed(1);
π getDelegated ()
If you can handle a request in the background or at a future time (EG, you're waiting on a file handle, or network traffic, or ...), the delegated flag can be set in the request. When the request is processed in the future the flag should be set back to 0 so the agent will know that it can wrap up the original request and send it back to the manager. This has not been tested within perl, but it hopefully should work. fingers crossed, bestie. π€
$delegated = $request->getDelegated();
π setDelegated ( DELEGATED )
Sets the delegated flag.
DELEGATED β 0 = false, 1 = true
$request->setDelegated(1);
π getRepeat ()
The repeat flag indicates that a getbulk operation is being handled and this indicates how many answers need to be returned. Generally, if you didn't register to directly handle getbulk support yourself, you won't need to deal with this value.
$repeat = $request->getRepeat();
π setRepeat ( REPEAT )
Sets the repeat count (decrement after answering requests if you handle getbulk requests yourself).
REPEAT β repeat count FIXME
$request->setRepeat(5);
π getSourceIp ()
Gets the IPv4 address of the device making the request to the handler.
use Socket;
print "Source: ", inet_ntoa($request->getSourceIp()), "\n";
π getDestIp ()
Gets the IPv4 address of the destination that the request was sent to.
use Socket;
print "Destination: ", inet_ntoa($request->getDestIp()), "\n";
π MODES π
These are the valid modes, bestie:
MODE_GETMODE_GETBULKMODE_GETNEXTMODE_SET_ACTIONMODE_SET_BEGINMODE_SET_COMMITMODE_SET_FREEMODE_SET_RESERVE1MODE_SET_RESERVE2MODE_SET_UNDO
π ERROR CODES π
The valid error codes are giving:
SNMP_ERR_NOERRORSNMP_ERR_TOOBIGSNMP_ERR_NOSUCHNAMESNMP_ERR_BADVALUESNMP_ERR_READONLYSNMP_ERR_GENERRSNMP_ERR_NOACCESSSNMP_ERR_WRONGTYPESNMP_ERR_WRONGLENGTHSNMP_ERR_WRONGENCODINGSNMP_ERR_WRONGVALUESNMP_ERR_NOCREATIONSNMP_ERR_INCONSISTENTVALUESNMP_ERR_RESOURCEUNAVAILABLESNMP_ERR_COMMITFAILEDSNMP_ERR_UNDOFAILEDSNMP_ERR_AUTHORIZATIONERRORSNMP_ERR_NOTWRITABLE
π AUTHOR
okay so if you need help, have questions, or just wanna drop a comment about this module β the vibe is: mail the list. Like, actually send an email to:
net-snmp-users@lists.sourceforge.net
That's the spot for help, questions or comments about this module. fr fr, that's what it's there for π«Ά
And now the credits, because we respect the people who built this β¨
Module written by:
- Wes Hardaker
<hardaker@users.sourceforge.net>
Documentation written by:
- Toni Willberg
<toniw@iki.fi> - Wes Hardaker
<hardaker@users.sourceforge.net>
absolute icons, both of them ππ
π SEE ALSO
go touch these other pages too, bestie β they're literally right there:
like, iykyk β read them and your whole life gets easier π
okay that's the whole page!! you made it to the end π go forth and agent responsibly, bestie π¦
β¨ that's a wrap β¨ β this page is from perl v5.42.2, dated 2026-04-12, and yes the source/version line at the bottom literally says agent(3). slay. ππΈ
π¦ where this page comes from
- alpine 3.23: net-snmp-doc 5.9.5.2-r0 Β· Net-SNMP
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms