The exmaple we are instrumenting is an otherwise-useless table containing the names of IETF working group chairs. Obviously, this data isn't all that useful from a network management point of view but this example only demonstrates how to use and store data. For more useful examples (but more complex), check out the apps/notification_log.c file which implements parts of the NOTIFICATION-LOG-MIB for logging incoming SNMP notifications.
Much of this code could be automatically generated by running mib2c as follows:
The table is defined roughly as follows:
% snmptranslate -m NET-SNMP-EXAMPLES-MIB -Tp -IR netSnmpIETFWGTable |+--netSnmpIETFWGTable(1) | | | +--netSnmpIETFWGEntry(1) | | Index: nsIETFWGName | | | +-- ---- String nsIETFWGName(1) | | Size: 1..32 | +-- CR-- String nsIETFWGChair1(2) | +-- CR-- String nsIETFWGChair2(3)
If this module is compiled into an agent, you should be able to issue snmp commands that look something like (valid authentication information not shown in these commands):
% snmpwalk localhost netSnmpIETFWGTable nsIETFWGChair1."snmpv3" = "Russ Mundy" nsIETFWGChair2."snmpv3" = "David Harrington"
% snmpset localhost nsIETFWGChair1.\"sming\" = "David Durham" nsIETFWGChair1."sming" = "David Durham"
% snmpwalk localhost netSnmpIETFWGTable nsIETFWGChair1."sming" = "David Durham" nsIETFWGChair1."snmpv3" = "Russ Mundy" nsIETFWGChair2."snmpv3" = "David Harrington"
In your snmpd.conf file, put the following line: add_row netSnmpIETFWGTable eos "Glenn Waters" "Dale Francisco"
% snmpwalk localhost netSnmpIETFWGTable nsIETFWGChair1.\"eos\" = "Glenn Waters" nsIETFWGChair1.\"snmpv3\" = "Russ Mundy" nsIETFWGChair2.\"eos\" = "Dale Francisco" nsIETFWGChair2.\"snmpv3\" = "David Harrington"
00001 00063 /* 00064 * start be including the appropriate header files 00065 */ 00066 #include <net-snmp/net-snmp-config.h> 00067 #include <net-snmp/net-snmp-includes.h> 00068 #include <net-snmp/agent/net-snmp-agent-includes.h> 00069 00070 /* 00071 * our initialization routine, automatically called by the agent 00072 */ 00073 /* 00074 * (to get called, the function name must match init_FILENAME() 00075 */ 00076 void 00077 init_data_set(void) 00078 { 00079 netsnmp_table_data_set *table_set; 00080 netsnmp_table_row *row; 00081 00082 /* 00083 * the OID we want to register our integer at. This should be the 00084 * * OID node for the entire table. In our case this is the 00085 * * netSnmpIETFWGTable oid definition 00086 */ 00087 oid my_registration_oid[] = 00088 { 1, 3, 6, 1, 4, 1, 8072, 2, 2, 1 }; 00089 00090 /* 00091 * a debugging statement. Run the agent with -Dexample_data_set to see 00092 * * the output of this debugging statement. 00093 */ 00094 DEBUGMSGTL(("example_data_set", 00095 "Initalizing example dataset table\n")); 00096 00097 /* 00098 * It's going to be the "working group chairs" table, since I'm 00099 * * sitting at an IETF convention while I'm writing this. 00100 * * 00101 * * column 1 = index = string = WG name 00102 * * column 2 = string = chair #1 00103 * * column 3 = string = chair #2 (most WGs have 2 chairs now) 00104 */ 00105 00106 table_set = netsnmp_create_table_data_set("netSnmpIETFWGTable"); 00107 00108 /* 00109 * allow the creation of new rows via SNMP SETs 00110 */ 00111 table_set->allow_creation = 1; 00112 00113 /* 00114 * set up what a row "should" look like, starting with the index 00115 */ 00116 netsnmp_table_dataset_add_index(table_set, ASN_OCTET_STR); 00117 00118 /* 00119 * define what the columns should look like. both are octet strings here 00120 */ 00121 netsnmp_table_set_multi_add_default_row(table_set, 00122 /* 00123 * column 2 = OCTET STRING, 00124 * writable = 1, 00125 * default value = NULL, 00126 * default value len = 0 00127 */ 00128 2, ASN_OCTET_STR, 1, NULL, 0, 00129 /* 00130 * similar 00131 */ 00132 3, ASN_OCTET_STR, 1, NULL, 0, 00133 0 /* done */ ); 00134 00135 /* 00136 * register the table 00137 */ 00138 /* 00139 * if we wanted to handle specific data in a specific way, or note 00140 * * when requests came in we could change the NULL below to a valid 00141 * * handler method in which we could over ride the default 00142 * * behaviour of the table_dataset helper 00143 */ 00144 netsnmp_register_table_data_set(netsnmp_create_handler_registration 00145 ("netSnmpIETFWGTable", NULL, 00146 my_registration_oid, 00147 OID_LENGTH(my_registration_oid), 00148 HANDLER_CAN_RWRITE), table_set, NULL); 00149 00150 00151 /* 00152 * create the a row for the table, and add the data 00153 */ 00154 row = netsnmp_create_table_data_row(); 00155 /* 00156 * set the index to the IETF WG name "snmpv3" 00157 */ 00158 netsnmp_table_row_add_index(row, ASN_OCTET_STR, "snmpv3", 00159 strlen("snmpv3")); 00160 00161 00162 /* 00163 * set column 2 to be the WG chair name "Russ Mundy" 00164 */ 00165 netsnmp_set_row_column(row, 2, ASN_OCTET_STR, 00166 "Russ Mundy", strlen("Russ Mundy")); 00167 netsnmp_mark_row_column_writable(row, 2, 1); /* make writable via SETs */ 00168 00169 /* 00170 * set column 3 to be the WG chair name "David Harrington" 00171 */ 00172 netsnmp_set_row_column(row, 3, ASN_OCTET_STR, "David Harrington", 00173 strlen("David Harrington")); 00174 netsnmp_mark_row_column_writable(row, 3, 1); /* make writable via SETs */ 00175 00176 /* 00177 * add the row to the table 00178 */ 00179 netsnmp_table_dataset_add_row(table_set, row); 00180 00181 #ifdef ADD_MORE_DATA 00182 /* 00183 * add the data, for the second row 00184 */ 00185 row = netsnmp_create_table_data_row(); 00186 netsnmp_table_row_add_index(row, ASN_OCTET_STR, "snmpconf", 00187 strlen("snmpconf")); 00188 netsnmp_set_row_column(row, 2, ASN_OCTET_STR, "David Partain", 00189 strlen("David Partain")); 00190 netsnmp_mark_row_column_writable(row, 2, 1); /* make writable */ 00191 netsnmp_set_row_column(row, 3, ASN_OCTET_STR, "Jon Saperia", 00192 strlen("Jon Saperia")); 00193 netsnmp_mark_row_column_writable(row, 3, 1); /* make writable */ 00194 netsnmp_table_dataset_add_row(table_set, row); 00195 #endif 00196 00197 /* 00198 * Finally, this actually allows the "add_row" token it the 00199 * * snmpd.conf file to add rows to this table. 00200 * * Example snmpd.conf line: 00201 * * add_row netSnmpIETFWGTable eos "Glenn Waters" "Dale Francisco" 00202 */ 00203 netsnmp_register_auto_data_table(table_set, NULL); 00204 00205 DEBUGMSGTL(("example_data_set", "Done initializing.\n")); 00206 }