1 /** @example data_set.c
2 * This example creates a table full of information and stores all
3 * that information within the agent's memory. The "table_dataset"
4 * helper routines take care of handling all aspects of SNMP requests
5 * as they come in (yay!).
7 * The exmaple we are instrumenting is an otherwise-useless table
8 * containing the names of IETF working group chairs. Obviously,
9 * this data isn't all that useful from a network management point of
10 * view but this example only demonstrates how to use and store data.
11 * For more useful examples (but more complex), check out the
12 * apps/notification_log.c file which implements parts of the
13 * NOTIFICATION-LOG-MIB for logging incoming SNMP notifications.
15 * Much of this code could be automatically generated by running
18 * - mib2c -c mib2c.create-dataset.conf netSnmpIETFWGTable
20 * The table is defined roughly as follows:
23 * % snmptranslate -m NET-SNMP-EXAMPLES-MIB -Tp -IR netSnmpIETFWGTable
24 * |+--netSnmpIETFWGTable(1)
26 * | +--netSnmpIETFWGEntry(1)
27 * | | Index: nsIETFWGName
29 * | +-- ---- String nsIETFWGName(1)
31 * | +-- CR-- String nsIETFWGChair1(2)
32 * | +-- CR-- String nsIETFWGChair2(3)
35 * If this module is compiled into an agent, you should be able to
36 * issue snmp commands that look something like (valid authentication
37 * information not shown in these commands):
40 * % snmpwalk localhost netSnmpIETFWGTable
41 * nsIETFWGChair1."snmpv3" = "Russ Mundy"
42 * nsIETFWGChair2."snmpv3" = "David Harrington"
44 * % snmpset localhost nsIETFWGChair1.\"sming\" = "David Durham"
45 * nsIETFWGChair1."sming" = "David Durham"
47 * % snmpwalk localhost netSnmpIETFWGTable
48 * nsIETFWGChair1."sming" = "David Durham"
49 * nsIETFWGChair1."snmpv3" = "Russ Mundy"
50 * nsIETFWGChair2."snmpv3" = "David Harrington"
52 * In your snmpd.conf file, put the following line:
53 * add_row netSnmpIETFWGTable eos "Glenn Waters" "Dale Francisco"
55 * % snmpwalk localhost netSnmpIETFWGTable
56 * nsIETFWGChair1.\"eos\" = "Glenn Waters"
57 * nsIETFWGChair1.\"snmpv3\" = "Russ Mundy"
58 * nsIETFWGChair2.\"eos\" = "Dale Francisco"
59 * nsIETFWGChair2.\"snmpv3\" = "David Harrington"
64 * start be including the appropriate header files
66 #include <net-snmp/net-snmp-config.h>
67 #include <net-snmp/net-snmp-includes.h>
68 #include <net-snmp/agent/net-snmp-agent-includes.h>
71 * our initialization routine, automatically called by the agent
74 * (to get called, the function name must match init_FILENAME()
79 netsnmp_table_data_set *table_set;
80 netsnmp_table_row *row;
83 * the OID we want to register our integer at. This should be the
84 * * OID node for the entire table. In our case this is the
85 * * netSnmpIETFWGTable oid definition
87 oid my_registration_oid[] =
88 { 1, 3, 6, 1, 4, 1, 8072, 2, 2, 1 };
91 * a debugging statement. Run the agent with -Dexample_data_set to see
92 * * the output of this debugging statement.
94 DEBUGMSGTL(("example_data_set",
95 "Initalizing example dataset table\n"));
98 * It's going to be the "working group chairs" table, since I'm
99 * * sitting at an IETF convention while I'm writing this.
101 * * column 1 = index = string = WG name
102 * * column 2 = string = chair #1
103 * * column 3 = string = chair #2 (most WGs have 2 chairs now)
106 table_set = netsnmp_create_table_data_set("netSnmpIETFWGTable");
109 * allow the creation of new rows via SNMP SETs
111 table_set->allow_creation = 1;
114 * set up what a row "should" look like, starting with the index
116 netsnmp_table_dataset_add_index(table_set, ASN_OCTET_STR);
119 * define what the columns should look like. both are octet strings here
121 netsnmp_table_set_multi_add_default_row(table_set,
123 * column 2 = OCTET STRING,
125 * default value = NULL,
126 * default value len = 0
128 2, ASN_OCTET_STR, 1, NULL, 0,
132 3, ASN_OCTET_STR, 1, NULL, 0,
139 * if we wanted to handle specific data in a specific way, or note
140 * * when requests came in we could change the NULL below to a valid
141 * * handler method in which we could over ride the default
142 * * behaviour of the table_dataset helper
144 netsnmp_register_table_data_set(netsnmp_create_handler_registration
145 ("netSnmpIETFWGTable", NULL,
147 OID_LENGTH(my_registration_oid),
148 HANDLER_CAN_RWRITE), table_set, NULL);
152 * create the a row for the table, and add the data
154 row = netsnmp_create_table_data_row();
156 * set the index to the IETF WG name "snmpv3"
158 netsnmp_table_row_add_index(row, ASN_OCTET_STR, "snmpv3",
163 * set column 2 to be the WG chair name "Russ Mundy"
165 netsnmp_set_row_column(row, 2, ASN_OCTET_STR,
166 "Russ Mundy", strlen("Russ Mundy"));
167 netsnmp_mark_row_column_writable(row, 2, 1); /* make writable via SETs */
170 * set column 3 to be the WG chair name "David Harrington"
172 netsnmp_set_row_column(row, 3, ASN_OCTET_STR, "David Harrington",
173 strlen("David Harrington"));
174 netsnmp_mark_row_column_writable(row, 3, 1); /* make writable via SETs */
177 * add the row to the table
179 netsnmp_table_dataset_add_row(table_set, row);
183 * add the data, for the second row
185 row = netsnmp_create_table_data_row();
186 netsnmp_table_row_add_index(row, ASN_OCTET_STR, "snmpconf",
188 netsnmp_set_row_column(row, 2, ASN_OCTET_STR, "David Partain",
189 strlen("David Partain"));
190 netsnmp_mark_row_column_writable(row, 2, 1); /* make writable */
191 netsnmp_set_row_column(row, 3, ASN_OCTET_STR, "Jon Saperia",
192 strlen("Jon Saperia"));
193 netsnmp_mark_row_column_writable(row, 3, 1); /* make writable */
194 netsnmp_table_dataset_add_row(table_set, row);
198 * Finally, this actually allows the "add_row" token it the
199 * * snmpd.conf file to add rows to this table.
200 * * Example snmpd.conf line:
201 * * add_row netSnmpIETFWGTable eos "Glenn Waters" "Dale Francisco"
203 netsnmp_register_auto_data_table(table_set, NULL);
205 DEBUGMSGTL(("example_data_set", "Done initializing.\n"));