<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >Understanding the sub-agent</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="NetSNMP subagent development manual" HREF="book1.html"><LINK REL="PREVIOUS" TITLE="Getting the pieces" HREF="c53.html"><LINK REL="NEXT" TITLE="Header file" HREF="c188.html"><link rel="stylesheet" href="/openhpi.css" type="text/css"> </head ><BODY CLASS="CHAPTER" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><div id="banner"><div><h1>The OpenHPI Project</h1><small>Open Hardware Platform Interface</small></div></div><table><tr> <!--#include virtual="/sidebar.html" --> <td id="maincolumn"><div class="mainsegment"> <DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >NetSNMP subagent development manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="c53.html" ACCESSKEY="P" ><<< Previous</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="c188.html" ACCESSKEY="N" >Next >>></A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="CHAPTER" ><H1 ><A NAME="AEN167" ></A >Understanding the sub-agent</H1 ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="AEN169" >The minimum</A ></H1 ><P > There are two ways of explaining the details of the generated sub-agent code. The first is to explain in detail every piece of the code and let the reader make the connection. The other (which we will use) is to comb through the code explaining what it does and provide working examples and explanation. </P ><P > To have just a working, compilable code, you need two extra files that are not generated by mib2c. They are the makefile and the sub-agent startup code. </P ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN173" >Makefile</A ></H2 ><P >Makefile. A good template is available from <A HREF="http://www.net-snmp.org/tutorial-5/toolkit/demoapp/Makefile" TARGET="_top" >NET-SNMP Tutorial</A >. I have modified the template to compile the generated C code. </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="PROGRAMLISTING" >OBJS2=example-demon.o netSnmpIETFWGTable.o TARGETS=example-demon CFLAGS=-I. `net-snmp-config --cflags` BUILDLIBS=`net-snmp-config --libs` BUILDAGENTLIBS=`net-snmp-config --agent-libs` # shared library flags (assumes gcc) DLFLAGS=-fPIC -shared all: $(TARGETS) example-demon: $(OBJS2) $(CC) -o example-demon $(OBJS2) $(BUILDAGENTLIBS) netSnmpIETFWGTable.o: netSnmpIETFWGTable.c Makefile $(CC) $(CFLAGS) $(DLFLAGS) -c -o netSnmpIETFWGTable.o \ netSnmpIETFWGTable.c $(CC) $(CFLAGS) $(DLFLAGS) -o netSnmpIETFWGTable.so \ netSnmpIETFWGTable.o </PRE ></TD ></TR ></TABLE ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN178" >Sub-agent daemon code</A ></H2 ><P > The daemon will use the generated sub-agent code. This example daemon code has been taken from <A HREF="http://www.net-snmp.org/tutorial-5/toolkit/demon/example-demon.c" TARGET="_top" >NET-SNMP Tutorial</A > </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="PROGRAMLISTING" >#include <net-snmp/net-snmp-config.h> #include <net-snmp/net-snmp-includes.h> #include <net-snmp/agent/net-snmp-agent-includes.h> #include <signal.h> #include <netSnmpIETFWGTable.h> static int keep_running; RETSIGTYPE stop_server(int a) { keep_running = 0; } int main (int argc, char **argv) { int agentx_subagent=1; /* change this if you want to be a SNMP master agent */ /* print log errors to stderr */ snmp_enable_stderrlog(); //snmp_set_do_debugging(1); /* we're an agentx subagent? */ if (agentx_subagent) { /* make us a agentx client. */ netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE, 1); } DEBUGMSG(("Before agent library init","\n")); /* initialize the agent library */ init_agent("example-demon"); /* initialize mib code here */ /* mib code: nit_netSnmpIETFWGTable from init_netSnmpIETFWGTable.c */ init_netSnmpIETFWGTable(); /* example-demon will be used to read example-demon.conf files. */ init_snmp("example-demon"); /* If we're going to be a snmp master agent, initial the ports */ if (!agentx_subagent) init_master_agent(); /* open the port to listen on (defaults to udp:161) */ /* In case we recevie a request to stop (kill -TERM or kill -INT) */ keep_running = 1; signal(SIGTERM, stop_server); signal(SIGINT, stop_server); /* you're main loop here... */ while(keep_running) { /* if you use select(), see snmp_select_info() in snmp_api(3) */ /* --- OR --- */ agent_check_and_process(1); /* 0 == don't block */ } /* at shutdown time */ snmp_shutdown("example-demon"); return 1; } </PRE ></TD ></TR ></TABLE ><DIV CLASS="NOTE" ><P ></P ><TABLE CLASS="NOTE" WIDTH="100%" BORDER="0" ><TR ><TD WIDTH="25" ALIGN="CENTER" VALIGN="TOP" ><IMG SRC="./stylesheet-images/note.gif" HSPACE="5" ALT="Note"></TD ><TD ALIGN="LEFT" VALIGN="TOP" ><P > The only change from the original source file code was replacing <I CLASS="EMPHASIS" >init_nstAgentSubagentObject(); </I > with <I CLASS="EMPHASIS" >init_netSnmpIETFWGTable();</I >. </P ></TD ></TR ></TABLE ></DIV ><P > For right now we will skip explanation of these two files and concentrate on the sub-agent generated code. </P ></DIV ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="c53.html" ACCESSKEY="P" ><<< Previous</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="book1.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="c188.html" ACCESSKEY="N" >Next >>></A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Getting the pieces</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" > </TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Header file</TD ></TR ></TABLE ></DIV ></BODY ></HTML >