Blame man/netsnmp_agent_api.3.def

Packit fcad23
.TH NETSNMP_AGENT_API 3 "13 Aug 2010" VVERSIONINFO "Net-SNMP"
Packit fcad23
.SH NAME
Packit fcad23
netsnmp_agent_api - embedding an agent into a external application
Packit fcad23
.SH SYNOPSIS
Packit fcad23
.nf
Packit fcad23
#include <net\-snmp/net\-snmp\-config.h>
Packit fcad23
#include <net\-snmp/net\-snmp\-includes.h>
Packit fcad23
#include <net\-snmp/agent/net\-snmp\-agent\-includes.h>
Packit fcad23
Packit fcad23
int
Packit fcad23
main (int argc, char *argv[])
Packit fcad23
{
Packit fcad23
  int agentx_subagent = 1;  /* Change this if you're a master agent.  */
Packit fcad23
Packit fcad23
  snmp_enable_stderrlog();
Packit fcad23
Packit fcad23
  /*  If we're an AgentX subagent...  */
Packit fcad23
  if (agentx_subagent) {
Packit fcad23
      /* ...make us an AgentX client.  */
Packit fcad23
      netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID,
Packit fcad23
                             NETSNMP_DS_AGENT_ROLE, 1);
Packit fcad23
  }
Packit fcad23
Packit fcad23
  init_agent("yourappname");
Packit fcad23
Packit fcad23
  /*  Initialize your MIB code here.  */
Packit fcad23
  init_my_mib_code();
Packit fcad23
Packit fcad23
  /*  `yourappname' will be used to read yourappname.conf files.  */
Packit fcad23
  init_snmp("yourappname");
Packit fcad23
Packit fcad23
  /*  If we're going to be a SNMP master agent...  */
Packit fcad23
  if (!agentx_subagent)
Packit fcad23
      init_master_agent();  /*  Listen on default port (161).  */
Packit fcad23
Packit fcad23
  /*  Your main loop here...  */
Packit fcad23
  while (whatever) {
Packit fcad23
      /* if you use select(), see snmp_api(3) */
Packit fcad23
      /*     --- OR ---  */
Packit fcad23
      agent_check_and_process(0); /* 0 == don't block */
Packit fcad23
  }
Packit fcad23
Packit fcad23
  /*  At shutdown time:  */
Packit fcad23
  snmp_shutdown("yourappname");
Packit fcad23
}
Packit fcad23
Packit fcad23
Then:
Packit fcad23
$(CC) ... `net\-snmp\-config \-\-agent\-libs`
Packit fcad23
Packit fcad23
.fi
Packit fcad23
.SH DESCRIPTION
Packit fcad23
.PP
Packit fcad23
Our goal is to create a easy to use interface to the Net-SNMP package
Packit fcad23
such that you can take code that you have written that has been
Packit fcad23
designed to be a Net-SNMP MIB module and embed it into an external
Packit fcad23
application where you can either chose to be a SNMP master agent or an
Packit fcad23
AgentX sub-agent using the same MIB module code.  Our suggestion is
Packit fcad23
that you use our (or another) SNMP agent as the AgentX master agent
Packit fcad23
and chose to become an AgentX subagent which then attaches to the
Packit fcad23
master.
Packit fcad23
.PP
Packit fcad23
The Net-SNMP package provides a pair of libraries that enables easy
Packit fcad23
embedding of an SNMP or AgentX agent into an external software
Packit fcad23
package. AgentX is an extensible protocol designed to allow multiple
Packit fcad23
SNMP sub-agents all run on one machine under a single SNMP master
Packit fcad23
agent.  It is defined in RFC 2741.
Packit fcad23
.PP
Packit fcad23
You will need to perform a few tasks in order to accomplish
Packit fcad23
this. First off, you will need to initialize both the SNMP library and
Packit fcad23
the SNMP agent library. As indicated above, this is done slightly
Packit fcad23
differently depending on whether or not you are going to perform as a
Packit fcad23
master agent or an AgentX sub-agent.
Packit fcad23
.SH CONFIGURATION
Packit fcad23
.PP
Packit fcad23
If you intend to operate as an AgentX sub-agent, you will have to
Packit fcad23
configured the Net-SNMP package with agentx support (which is turned
Packit fcad23
on by default, so just don't turn it off)
Packit fcad23
.PP
Packit fcad23
Additionally, you will need to link against the Net-SNMP libraries
Packit fcad23
(use the output of "net\-snmp\-config \-\-agent\-libs" to get a library
Packit fcad23
list) and call subagent_pre_init() as indicated above.
Packit fcad23
.SH COMPILING
Packit fcad23
.PP
Packit fcad23
In order to make use of any of the above API, you will need to link
Packit fcad23
against at least the four libraries listed above.
Packit fcad23
.SH FUNCTIONS
Packit fcad23
.PP This is a brief description of the functions called above and
Packit fcad23
where to find out more information on them.  It is certainly not a
Packit fcad23
complete list of what is available within all the Net-SNMP libraries. 
Packit fcad23
.IP "snmp_enable_stderrlog()"
Packit fcad23
Logs error output from the SNMP agent to the standard error stream.
Packit fcad23
.IP "netsnmp_ds_set_boolean()"
Packit fcad23
Please see the
Packit fcad23
.IR default_store(3)
Packit fcad23
manual page for more information
Packit fcad23
about this API.
Packit fcad23
.IP "init_agent(char *name)"
Packit fcad23
Initializes the embedded agent.  This should be called before the
Packit fcad23
.BR "init_snmp()"
Packit fcad23
call.  
Packit fcad23
.I name
Packit fcad23
is used to dictate what .conf file to read when
Packit fcad23
.BR "init_snmp()"
Packit fcad23
is called later.
Packit fcad23
.IP "init_snmp(char *name)"
Packit fcad23
Initializes the SNMP library.  Note that one of the things this will
Packit fcad23
do will be to read configuration files in an effort to configure your
Packit fcad23
application. It will attempt to read the configuration files named by
Packit fcad23
the
Packit fcad23
.I name
Packit fcad23
string that you passed in.  It can be used to configure access
Packit fcad23
control, for instance.   Please see the
Packit fcad23
.IR netsnmp_config_api(3) ", " snmp_config(5) ", and " snmpd.conf(5)
Packit fcad23
manual pages for further details on this subject.
Packit fcad23
.IP "init_master_agent(void)"
Packit fcad23
Initializes the master agent and causes it to listen for SNMP requests 
Packit fcad23
on its default UDP port of 161.
Packit fcad23
.IP "agent_check_and_process(int block)"
Packit fcad23
This checks for packets arriving on the SNMP port and processes them
Packit fcad23
if some are found.  If 
Packit fcad23
.I block
Packit fcad23
is non-zero, the function call will block until a packet arrives or an 
Packit fcad23
alarm must be run (see
Packit fcad23
.IR snmp_alarm(3) ).
Packit fcad23
The return value from this function is a positive integer if packets
Packit fcad23
were processed, zero if an alarm occurred and \-1 if an error occured.
Packit fcad23
.IP "snmp_shutdown(char *name);"
Packit fcad23
This shuts down the agent, saving any needed persistent storage, etc.
Packit fcad23
.SH "SEE ALSO"
Packit fcad23
http://www.net\-snmp.org/tutorial\-5/toolkit/ select(2), snmp_api(3),
Packit fcad23
default_store(3), snmp_alarm(3), netsnmp_config_api(3), snmp_config(5),
Packit fcad23
snmpd.conf(5)