I’ve written an AgentX app (Linux, gcc, g++) which works well at sending back scalers. Here is what I’m doing now:
init_agent( "blah" );
netsnmp_register_read_only_scalar( netsnmp_create_handler_registration( "foo1", handle_foo1, oid, oid.size(), HANDLER_CAN_RONLY ) );
init_snmp( "blah" );
while ( true )
{
// internal stuff
agent_check_and_process(1); // where 1==block
}
The functions like handle_foo1(...) call snmp_set_var_typed_value(...) to return the values which are cached in a global C struct within the application.
What I’m trying to do now is modify this code to also support a SNMP table. The content of the table is stored/cached as a STL container within the application. This is a relatively simple SNMP table, with consecutive rows, and all columns are composed of types like Integer32, Gauge32, InetAddress, and TruthValue. The problem is I don’t see great code examples on the net-snmp web site, just a lot of doxygen pages.
My question:
What API should I be looking at? Are these the right calls:
netsnmp_register_read_only_table_data();
netsnmp_create_table_data();
netsnmp_create_table_data_row();
netsnmp_table_data_add_row();
…or is there something simpler I should be using?
I think the biggest pain when it comes to net-snmp is all those Doxygen pages the Google indexes but which provides near-zero usable content. Reading the
.hfiles is probably already obvious to most developers, and the truth is that net-snmp provides many different layers of APIs with very little documentation I found useful. What we need isn’t several dozen identical copies of web sites hosting Doxygen, but instead some good examples.In the end, the mib2c tool is how I got enough example code to get the whole thing working. I think I tried running mib2c with every single net-snmp
.conffile, and spent a lot of time reading the code it generated to get a better understanding. Here are the ones I found gave me the best hints:The
.conffiles are here:/etc/snmp/mib2c.*Also useful were the following pages:
From what I understand, there are many helpers/layers available in the net-snmp API. So this example pseudocode may not work for everyone, but this is how I personally got my tables to work using net-snmp v5.4:
Variable needed across several functions (make it global, or a member of a struct?)
Structure to represent one row of the table (must match the MIB definition)
Initialize the table with snmpd
Handler to process requests
Building/adding rows to the table
Putting it together
In my case, that last function that builds the rows is triggered periodically by some other events in the system. So every once in a while when new stats are available, the table is rebuilt, all old rows are removed, and the new ones are inserted. I didn’t bother trying to modify existing rows. Instead, I found it was easier to just rebuild the table from scratch.