.\" Automatically generated by Pod::Man 4.10 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "agent 3pm" .TH agent 3pm "2021-01-28" "perl v5.28.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" NetSNMP::agent \- Perl extension for the net\-snmp agent. .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use NetSNMP::agent; \& \& my $agent = new NetSNMP::agent(\*(AqName\*(Aq => \*(Aqmy_agent_name\*(Aq); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module implements an \s-1API\s0 set to make a \s-1SNMP\s0 agent act as a snmp agent, a snmp subagent (using the AgentX subagent protocol) and/or embedded perl-APIs directly within the traditional net-snmp agent demon. .PP Also see the tutorial about the genaral Net-SNMP C \s-1API,\s0 which this module implements in a perl-way, and a perl specific tutorial at: .PP .Vb 1 \& http://www.net\-snmp.org/tutorial\-5/toolkit/ .Ve .SH "EXAMPLES" .IX Header "EXAMPLES" .SS "Sub-agent example" .IX Subsection "Sub-agent example" .Vb 2 \& use NetSNMP::agent (\*(Aq:all\*(Aq); \& 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 \& \*(AqName\*(Aq => "my_agent_name", \& \*(AqAgentX\*(Aq => 1 \& ); \& $agent\->register("my_agent_name", ".1.3.6.1.4.1.8072.9999.9999.7375", \& \e&myhandler); \& \& my $running = 1; \& while($running) { \& $agent\->agent_check_and_process(1); \& } \& \& $agent\->shutdown(); .Ve .SS "Embedded agent example" .IX Subsection "Embedded agent example" .Vb 2 \& # place this in a .pl file, and then in your snmpd.conf file put: \& # perl do \*(Aq/path/to/file.pl\*(Aq; \& \& use NetSNMP::agent; \& my $agent; \& \& sub myhandler { \& my ($handler, $registration_info, $request_info, $requests) = @_; \& # ... \& } \& \& $agent = new NetSNMP::agent( \& \*(AqName\*(Aq => \*(Aqmy_agent_name\*(Aq \& ); \& \& $agent\->register("my_agent_name", ".1.3.6.1.4.1.8072.9999.9999.7375", \& \e&myhandler); \& \& $agent\->main_loop(); .Ve .SH "CONSTRUCTOR" .IX Header "CONSTRUCTOR" .Vb 2 \& new ( OPTIONS ) \& This is the constructor for a new NetSNMP::agent object. \& \& Possible options are: \& \& Name \- Name of the agent (optional, defaults to "perl") \& (The snmp library will read a NAME.conf snmp \& 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( \& \*(AqName\*(Aq => \*(Aqmy_agent_name\*(Aq, \& \*(AqAgentX\*(Aq => 1 \& ); .Ve .SH "METHODS" .IX Header "METHODS" .Vb 2 \& register (NAME, OID, \e&handler_routine ) \& Registers the callback handler with given OID. \& \& $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. \& \& shutdown () \& Nicely shuts down the agent or sub\-agent. \& \& $agent\->shutdown(); .Ve .SH "HANDLER CALLBACKS" .IX Header "HANDLER CALLBACKS" .Vb 1 \& handler ( HANDLER, REGISTRATION_INFO, REQUEST_INFO, REQUESTS ) \& \& The handler is called with the following parameters: \& \& HANDLER \- FIXME \& REGISTRATION_INFO \- what are the correct meanings of these? \& REQUEST_INFO \- \& REQUESTS \- \& \& Example handler: \& \& sub myhandler { \& my ($handler, $reg_info, $request_info, $requests) = @_; \& # ... \& } .Ve .PP The handler subroutine will be called when a \s-1SNMP\s0 request received by the agent for anything below the registered \s-1OID.\s0 The handler is passed 4 arguments: \f(CW$handler\fR, \f(CW$registration_info\fR, \f(CW$request_info\fR, \&\f(CW$requests\fR. These match the arguments passed to the C version of the same \s-1API.\s0 Note that they are not entirely complete objects but are functional \*(L"enough\*(R" at this point in time. .ie n .SS "$request_info object functions" .el .SS "\f(CW$request_info\fP object functions" .IX Subsection "$request_info object functions" .Vb 3 \& getMode () \& Returns the mode of the request. See the MODES section for \& list of valid modes. \& \& $mode = $request\->getMode(); .Ve .ie n .SS "$registration_info object functions" .el .SS "\f(CW$registration_info\fP object functions" .IX Subsection "$registration_info object functions" .Vb 5 \& 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(); .Ve .ie n .SS "$request object functions" .el .SS "\f(CW$request\fP object functions" .IX Subsection "$request object functions" .Vb 3 \& 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\*(Aqre 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. \& \& $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\*(Aqt register to directly \& handle getbulk support yourself, you won\*(Aqt 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()), "\en"; \& \& getDestIp () \& \& Gets the IPv4 address of the destination that the request was sent to. \& \& use Socket; \& print "Destination: ", inet_ntoa($request\->getDestIp()), "\en"; .Ve .SH "MODES" .IX Header "MODES" .Vb 10 \& MODE_GET \& MODE_GETBULK \& MODE_GETNEXT \& MODE_SET_ACTION \& MODE_SET_BEGIN \& MODE_SET_COMMIT \& MODE_SET_FREE \& MODE_SET_RESERVE1 \& MODE_SET_RESERVE2 \& MODE_SET_UNDO .Ve .SH "ERROR CODES" .IX Header "ERROR CODES" .Vb 10 \& SNMP_ERR_NOERROR \& SNMP_ERR_TOOBIG \& SNMP_ERR_NOSUCHNAME \& SNMP_ERR_BADVALUE \& SNMP_ERR_READONLY \& SNMP_ERR_GENERR \& SNMP_ERR_NOACCESS \& SNMP_ERR_WRONGTYPE \& SNMP_ERR_WRONGLENGTH \& SNMP_ERR_WRONGENCODING \& SNMP_ERR_WRONGVALUE \& SNMP_ERR_NOCREATION \& SNMP_ERR_INCONSISTENTVALUE \& SNMP_ERR_RESOURCEUNAVAILABLE \& SNMP_ERR_COMMITFAILED \& SNMP_ERR_UNDOFAILED \& SNMP_ERR_AUTHORIZATIONERROR \& SNMP_ERR_NOTWRITABLE .Ve .SH "AUTHOR" .IX Header "AUTHOR" Please mail the net\-snmp\-users@lists.sourceforge.net mailing list for help, questions or comments about this module. .PP Module written by: Wes Hardaker .PP Documentation written by: Toni Willberg Wes Hardaker .SH "SEE ALSO" .IX Header "SEE ALSO" \&\fBNetSNMP::OID\fR\|(3), \fBNetSNMP::ASN\fR\|(3), \fBperl\fR\|(1).