AgentX Tutorial using Net-SNMP – Trap implementation

Third one in this series is implementation of SNMP Traps, the method which is the main strength of SNMP. These Traps provide an event based system where NMS relies heavily on the monitored stations to tell them if something of interest has happened. mib2c again provides an easy way to implement traps. This is actually one of the easiest ones of the three in this series. Again as I said before clone my repository and checkout the code to find what was done and how. Following are three commands of interest.


git clone https://github.com/jainvishal/agentx-tutorial.git
git checkout step3_traps_autogenerated
git checkout step3_traps_implement

mib2c provides mib2c.notify.conf to compile the MIB and generate C code. By default it uses SNMPv2 for traps and I leave it as is. Implementation is as simple as copying the data into right variables when time comes and they will be delivered. Outgoing traps also require snmpd configuration to setup proper credentials and trapsinks which are usually the NMS or trap forwarding systems.

I use snmptrapd to capture and display the traps. But one can also use packet capture tools like tcpdump/wireshark/snoop.

So that's it for now. When time permits I will expand this further to provide a real-life application scenario. This code so far is Hello World quality and not exactly production ready.

  • @sr

    I generated mytrap.c [trap code] using mib2c.notify.conf. I want to create a shared library from the generated code so that I can issue trap by calling the function that is present in mytrap.c[library]. Many forums say that sendv2_trap() works only within the agent. So I am not sure if I can succeed in generating traps by this way. Please let me know what should I do to accomplish my task.

    For better understanding please read through below sample program.

    /****FILE1: mytrap.c****to be built as shared lib****/
    send_mytrap()
    {
    xxxx;
    xxxx;
    .
    .
    .
    .
    .
    sendv2_trap(var_list);
    }

    /****FILE2: main.c****/
    main()
    {
    send_mytrap();
    }

    • kunwarvjain

      sendv2_trap would only work within the agent because it is communicating with the masteragent. If you do not do the initial handshake with snmpd there is no socket to send trap data on. So during initialization, your API should register with snmpd, meaning all application using the API will become sub-agents. And periodically you have to also call agent_check_and_process or similar (OR take agent-x socket descriptor and put in your own select). Reason is that snmpd could close an idle connection. So you have to ensure that you stay connected with snmpd. Any disconnects have to be handled. So your API has to be more robust.

      My plan was to extend the tutorial to phase 2 where I will give a solid example of an application that is much more closer to a real life. That is forthcoming but no solid dates.