Article Details
Product: Agent Toolkit C Edition
Category :Agent Toolkit C Edition >> General
 #112609 - How to embed C-Agent in a Linux application?

This article lists the steps involved in embedding the WebNMS C Agent with user application code.

a. Creating the agent library

i) Rename the main() method in <projectdir>/agent/source/main/src/agentmain.c.
#ifdef VXWORKS
int StartAgent()
#else
#ifdef CMD_ARGS
int main(int argc, char *argv[])
#else
void StartAdventNetAgent() /* Renamed as StartAdventNetAgent() */
/* int main(void) */
#endif /* #ifdef CMD_ARGS */
#endif /* #ifdef VXWORKS */
ii) Add the following function prototype in <projectdir>/agent/source/main/include/agentmain.h.
void StartAdventNetAgent();
iii) As the agent entry method in an application is StartAdventNetAgent(), the command line argument macro is not required. So remove the following command line argument macro available in the <projectdir>/agent/source/system/include/config.h file:

/* To accept commandline arguments during agent startup. */
#if !defined(VXWORKS) && !defined(WIN_CE)
/* #define CMD_ARGS */
#endif
iv) Modify the makefile in the <projectdir>/agent/ directory for creating the WebNMS agent as a library.
b. Embedding our agent with user application
Call the agent entry method StartAdventNetAgent() from application code. This can be created as a separate thread.

c. Creating application executable file by linking the agent library with application code
Create an application makefile and include all agent libraries (main library, snmp protocols library, system library, utils library, stubs library, and agent library).
OTHER_INC=-lpthread \
./source/main/lib/libmain.$(LIB_EXT)\
./source/protocols/snmp/lib/libsnmp.$(LIB_EXT)\
lib/libadventnetsnmpcagent.$(LIB_EXT)\
./stubs/agent-sample-mib/lib/libagent-sample-mib.$(LIB_EXT) \
./source/utils/lib/libutils.$(LIB_EXT)\
./source/system/lib/libsys.$(LIB_EXT)

Note: Please include the libraries in the above-said order, otherwise dependency problem may occur.
d. Testing
  1. Create a project for standalone SNMP agent AGENT-SAMPLE-MIB.
  2. Modify agentmain.c, agentmain.h, and config.h as mentioned above.
  3. Copy the makefile to the <project directory>/agent/ directory.
  4. Copy application.c and makefile to the <project directory>/agent/ directory.
  5. Compile the application code as follows:
    <projectdir>/agent> make -f makefile
    This will create an executable file under the bin directory.
  6. Start the application as follows:
    <projectdir>/agent/bin> ./application