Debian Codegraph @

agent/mibgroup/examples/data_set.c

raw
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!).
6 *
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.
14 *
15 * Much of this code could be automatically generated by running
16 * mib2c as follows:
17 *
18 * - mib2c -c mib2c.create-dataset.conf netSnmpIETFWGTable
19 *
20 * The table is defined roughly as follows:
21 *
22 * <pre>
23 * % snmptranslate -m NET-SNMP-EXAMPLES-MIB -Tp -IR netSnmpIETFWGTable
24 * |+--netSnmpIETFWGTable(1)
25 * | |
26 * | +--netSnmpIETFWGEntry(1)
27 * | | Index: nsIETFWGName
28 * | |
29 * | +-- ---- String nsIETFWGName(1)
30 * | | Size: 1..32
31 * | +-- CR-- String nsIETFWGChair1(2)
32 * | +-- CR-- String nsIETFWGChair2(3)
33 * </pre>
34 *
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):
38 *
39 * <pre>
40 * % snmpwalk localhost netSnmpIETFWGTable
41 * nsIETFWGChair1."snmpv3" = "Russ Mundy"
42 * nsIETFWGChair2."snmpv3" = "David Harrington"
43 *
44 * % snmpset localhost nsIETFWGChair1.\"sming\" = "David Durham"
45 * nsIETFWGChair1."sming" = "David Durham"
46 *
47 * % snmpwalk localhost netSnmpIETFWGTable
48 * nsIETFWGChair1."sming" = "David Durham"
49 * nsIETFWGChair1."snmpv3" = "Russ Mundy"
50 * nsIETFWGChair2."snmpv3" = "David Harrington"
51 *
52 * In your snmpd.conf file, put the following line:
53 * add_row netSnmpIETFWGTable eos "Glenn Waters" "Dale Francisco"
54 *
55 * % snmpwalk localhost netSnmpIETFWGTable
56 * nsIETFWGChair1.\"eos\" = "Glenn Waters"
57 * nsIETFWGChair1.\"snmpv3\" = "Russ Mundy"
58 * nsIETFWGChair2.\"eos\" = "Dale Francisco"
59 * nsIETFWGChair2.\"snmpv3\" = "David Harrington"
60 * </pre>
61 */
62
63 /*
64 * start be including the appropriate header files
65 */
66 #include <net-snmp/net-snmp-config.h>
67 #include <net-snmp/net-snmp-includes.h>
68 #include <net-snmp/net-snmp-features.h>
69 #include <net-snmp/agent/net-snmp-agent-includes.h>
70
71 netsnmp_feature_require(table_set_multi_add_default_row);
72 netsnmp_feature_require(unregister_auto_data_table);
73 netsnmp_feature_require(delete_table_data_set);
74 netsnmp_feature_require(table_dataset);
75 netsnmp_feature_require(table_set_multi_add_default_row);
76 netsnmp_feature_require(table_dataset_unregister_auto_data_table);
77
78 static netsnmp_table_data_set *table_set;
79
80 /*
81 * our initialization routine, automatically called by the agent
82 */
83 /*
84 * (to get called, the function name must match init_FILENAME()
85 */
86 void
87 init_data_set(void)
88 {
89 netsnmp_table_row *row;
90
91 /*
92 * the OID we want to register our integer at. This should be the
93 * * OID node for the entire table. In our case this is the
94 * * netSnmpIETFWGTable oid definition
95 */
96 oid my_registration_oid[] =
97 { 1, 3, 6, 1, 4, 1, 8072, 2, 2, 1 };
98
99 /*
100 * a debugging statement. Run the agent with -Dexample_data_set to see
101 * * the output of this debugging statement.
102 */
103 DEBUGMSGTL(("example_data_set",
104 "Initalizing example dataset table\n"));
105
106 /*
107 * It's going to be the "working group chairs" table, since I'm
108 * * sitting at an IETF convention while I'm writing this.
109 * *
110 * * column 1 = index = string = WG name
111 * * column 2 = string = chair #1
112 * * column 3 = string = chair #2 (most WGs have 2 chairs now)
113 */
114
115 table_set = netsnmp_create_table_data_set("netSnmpIETFWGTable");
116
117 /*
118 * allow the creation of new rows via SNMP SETs
119 */
120 table_set->allow_creation = 1;
121
122 /*
123 * set up what a row "should" look like, starting with the index
124 */
125 netsnmp_table_dataset_add_index(table_set, ASN_OCTET_STR);
126
127 /*
128 * define what the columns should look like. both are octet strings here
129 */
130 netsnmp_table_set_multi_add_default_row(table_set,
131 /*
132 * column 2 = OCTET STRING,
133 * writable = 1,
134 * default value = NULL,
135 * default value len = 0
136 */
137 2, ASN_OCTET_STR, 1, NULL, 0,
138 /*
139 * similar
140 */
141 3, ASN_OCTET_STR, 1, NULL, 0,
142 0 /* done */ );
143
144 /*
145 * register the table
146 */
147 /*
148 * if we wanted to handle specific data in a specific way, or note
149 * * when requests came in we could change the NULL below to a valid
150 * * handler method in which we could over ride the default
151 * * behaviour of the table_dataset helper
152 */
153 netsnmp_register_table_data_set(netsnmp_create_handler_registration
154 ("netSnmpIETFWGTable", NULL,
155 my_registration_oid,
156 OID_LENGTH(my_registration_oid),
157 HANDLER_CAN_RWRITE), table_set, NULL);
158
159
160 /*
161 * create the a row for the table, and add the data
162 */
163 row = netsnmp_create_table_data_row();
164 /*
165 * set the index to the IETF WG name "snmpv3"
166 */
167 netsnmp_table_row_add_index(row, ASN_OCTET_STR, "snmpv3",
168 strlen("snmpv3"));
169
170
171 /*
172 * set column 2 to be the WG chair name "Russ Mundy"
173 */
174 netsnmp_set_row_column(row, 2, ASN_OCTET_STR,
175 "Russ Mundy", strlen("Russ Mundy"));
176 netsnmp_mark_row_column_writable(row, 2, 1); /* make writable via SETs */
177
178 /*
179 * set column 3 to be the WG chair name "David Harrington"
180 */
181 netsnmp_set_row_column(row, 3, ASN_OCTET_STR, "David Harrington",
182 strlen("David Harrington"));
183 netsnmp_mark_row_column_writable(row, 3, 1); /* make writable via SETs */
184
185 /*
186 * add the row to the table
187 */
188 netsnmp_table_dataset_add_row(table_set, row);
189
190 #ifdef ADD_MORE_DATA
191 /*
192 * add the data, for the second row
193 */
194 row = netsnmp_create_table_data_row();
195 netsnmp_table_row_add_index(row, ASN_OCTET_STR, "snmpconf",
196 strlen("snmpconf"));
197 netsnmp_set_row_column(row, 2, ASN_OCTET_STR, "David Partain",
198 strlen("David Partain"));
199 netsnmp_mark_row_column_writable(row, 2, 1); /* make writable */
200 netsnmp_set_row_column(row, 3, ASN_OCTET_STR, "Jon Saperia",
201 strlen("Jon Saperia"));
202 netsnmp_mark_row_column_writable(row, 3, 1); /* make writable */
203 netsnmp_table_dataset_add_row(table_set, row);
204 #endif
205
206 /*
207 * Finally, this actually allows the "add_row" token it the
208 * * snmpd.conf file to add rows to this table.
209 * * Example snmpd.conf line:
210 * * add_row netSnmpIETFWGTable eos "Glenn Waters" "Dale Francisco"
211 */
212 netsnmp_register_auto_data_table(table_set, NULL);
213
214 DEBUGMSGTL(("example_data_set", "Done initializing.\n"));
215 }
216
217 void
218 shutdown_data_set(void)
219 {
220 netsnmp_unregister_auto_data_table(table_set, NULL);
221 netsnmp_delete_table_data_set(table_set);
222 table_set = NULL;
223 }