/src/net-snmp/agent/mibgroup/notification-log-mib/notification_log.c
Line | Count | Source |
1 | | #include <net-snmp/net-snmp-config.h> |
2 | | #include <net-snmp/net-snmp-features.h> |
3 | | |
4 | | #include <sys/types.h> |
5 | | #ifdef HAVE_NETINET_IN_H |
6 | | #include <netinet/in.h> |
7 | | #endif |
8 | | #ifdef HAVE_NETDB_H |
9 | | #include <netdb.h> |
10 | | #endif |
11 | | |
12 | | #include <net-snmp/net-snmp-includes.h> |
13 | | #include <net-snmp/agent/net-snmp-agent-includes.h> |
14 | | |
15 | | #include <net-snmp/agent/ds_agent.h> |
16 | | #include <net-snmp/agent/instance.h> |
17 | | #include <net-snmp/agent/table.h> |
18 | | #include <net-snmp/agent/table_data.h> |
19 | | #include <net-snmp/agent/table_dataset.h> |
20 | | #include "net-snmp/agent/sysORTable.h" |
21 | | #include "notification_log.h" |
22 | | |
23 | | netsnmp_feature_require(register_ulong_instance_context); |
24 | | netsnmp_feature_require(register_read_only_counter32_instance_context); |
25 | | netsnmp_feature_require(delete_table_data_set); |
26 | | netsnmp_feature_require(table_dataset); |
27 | | netsnmp_feature_require(date_n_time); |
28 | | |
29 | | /* |
30 | | * column number definitions for table nlmLogTable |
31 | | */ |
32 | | |
33 | | #define COLUMN_NLMLOGINDEX 1 |
34 | 0 | #define COLUMN_NLMLOGTIME 2 |
35 | 0 | #define COLUMN_NLMLOGDATEANDTIME 3 |
36 | 0 | #define COLUMN_NLMLOGENGINEID 4 |
37 | 0 | #define COLUMN_NLMLOGENGINETADDRESS 5 |
38 | 0 | #define COLUMN_NLMLOGENGINETDOMAIN 6 |
39 | 0 | #define COLUMN_NLMLOGCONTEXTENGINEID 7 |
40 | 0 | #define COLUMN_NLMLOGCONTEXTNAME 8 |
41 | 0 | #define COLUMN_NLMLOGNOTIFICATIONID 9 |
42 | | |
43 | | /* |
44 | | * column number definitions for table nlmLogVariableTable |
45 | | */ |
46 | | #define COLUMN_NLMLOGVARIABLEINDEX 1 |
47 | 0 | #define COLUMN_NLMLOGVARIABLEID 2 |
48 | 0 | #define COLUMN_NLMLOGVARIABLEVALUETYPE 3 |
49 | 0 | #define COLUMN_NLMLOGVARIABLECOUNTER32VAL 4 |
50 | 0 | #define COLUMN_NLMLOGVARIABLEUNSIGNED32VAL 5 |
51 | 0 | #define COLUMN_NLMLOGVARIABLETIMETICKSVAL 6 |
52 | 0 | #define COLUMN_NLMLOGVARIABLEINTEGER32VAL 7 |
53 | 0 | #define COLUMN_NLMLOGVARIABLEOCTETSTRINGVAL 8 |
54 | 0 | #define COLUMN_NLMLOGVARIABLEIPADDRESSVAL 9 |
55 | 0 | #define COLUMN_NLMLOGVARIABLEOIDVAL 10 |
56 | 0 | #define COLUMN_NLMLOGVARIABLECOUNTER64VAL 11 |
57 | 0 | #define COLUMN_NLMLOGVARIABLEOPAQUEVAL 12 |
58 | | |
59 | | static u_long num_received = 0; |
60 | | static u_long num_deleted = 0; |
61 | | |
62 | | static u_long max_logged = 1000; /* goes against the mib default of infinite */ |
63 | | static u_long max_age = 1440; /* 1440 = 24 hours, which is the mib default */ |
64 | | |
65 | | static netsnmp_table_data_set *nlmLogTable; |
66 | | static netsnmp_table_data_set *nlmLogVarTable; |
67 | | |
68 | | static oid nlm_module_oid[] = { SNMP_OID_MIB2, 92 }; /* NOTIFICATION-LOG-MIB::notificationLogMIB */ |
69 | | |
70 | | static void |
71 | | netsnmp_notif_log_remove_oldest(int count) |
72 | 0 | { |
73 | 0 | netsnmp_table_row *deleterow, *tmprow, *deletevarrow; |
74 | | |
75 | 0 | DEBUGMSGTL(("notification_log", "deleting %d log entry(s)\n", count)); |
76 | |
|
77 | 0 | deleterow = netsnmp_table_data_set_get_first_row(nlmLogTable); |
78 | 0 | for (; count && deleterow; deleterow = tmprow, --count) { |
79 | | /* |
80 | | * delete contained varbinds |
81 | | * xxx-rks: note that this assumes that only the default |
82 | | * log is used (ie for the first nlmLogTable row, the |
83 | | * first nlmLogVarTable rows will be the right ones). |
84 | | * the right thing to do would be to do a find based on |
85 | | * the nlmLogTable oid. |
86 | | */ |
87 | 0 | DEBUGMSGTL(("9:notification_log", " deleting notification\n")); |
88 | 0 | DEBUGIF("9:notification_log") { |
89 | 0 | DEBUGMSGTL(("9:notification_log", |
90 | 0 | " base oid:")); |
91 | 0 | DEBUGMSGOID(("9:notification_log", deleterow->index_oid, |
92 | 0 | deleterow->index_oid_len)); |
93 | 0 | DEBUGMSG(("9:notification_log", "\n")); |
94 | 0 | } |
95 | 0 | deletevarrow = netsnmp_table_data_set_get_first_row(nlmLogVarTable); |
96 | 0 | for (; deletevarrow; deletevarrow = tmprow) { |
97 | |
|
98 | 0 | tmprow = netsnmp_table_data_set_get_next_row(nlmLogVarTable, |
99 | 0 | deletevarrow); |
100 | | |
101 | 0 | DEBUGIF("9:notification_log") { |
102 | 0 | DEBUGMSGTL(("9:notification_log", |
103 | 0 | " :")); |
104 | 0 | DEBUGMSGOID(("9:notification_log", deletevarrow->index_oid, |
105 | 0 | deletevarrow->index_oid_len)); |
106 | 0 | DEBUGMSG(("9:notification_log", "\n")); |
107 | 0 | } |
108 | 0 | if ((deleterow->index_oid_len == deletevarrow->index_oid_len - 1) && |
109 | 0 | snmp_oid_compare(deleterow->index_oid, |
110 | 0 | deleterow->index_oid_len, |
111 | 0 | deletevarrow->index_oid, |
112 | 0 | deleterow->index_oid_len) == 0) { |
113 | 0 | DEBUGMSGTL(("9:notification_log", " deleting varbind\n")); |
114 | 0 | netsnmp_table_dataset_remove_and_delete_row(nlmLogVarTable, |
115 | 0 | deletevarrow); |
116 | 0 | } |
117 | 0 | else |
118 | 0 | break; |
119 | 0 | } |
120 | | |
121 | | /* |
122 | | * delete the master row |
123 | | */ |
124 | 0 | tmprow = netsnmp_table_data_set_get_next_row(nlmLogTable, deleterow); |
125 | 0 | netsnmp_table_dataset_remove_and_delete_row(nlmLogTable, |
126 | 0 | deleterow); |
127 | 0 | num_deleted++; |
128 | 0 | } |
129 | | /** should have deleted all of them */ |
130 | 0 | netsnmp_assert(0 == count); |
131 | 0 | } |
132 | | |
133 | | static void |
134 | | check_log_size(unsigned int clientreg, void *clientarg) |
135 | 0 | { |
136 | 0 | netsnmp_table_row *row; |
137 | 0 | netsnmp_table_data_set_storage *data; |
138 | 0 | u_long count = 0; |
139 | 0 | u_long uptime; |
140 | |
|
141 | 0 | uptime = netsnmp_get_agent_uptime(); |
142 | |
|
143 | 0 | if (!nlmLogTable || !nlmLogTable->table ) { |
144 | 0 | DEBUGMSGTL(("notification_log", "missing log table\n")); |
145 | 0 | return; |
146 | 0 | } |
147 | | |
148 | | /* |
149 | | * check max allowed count |
150 | | */ |
151 | 0 | count = netsnmp_table_set_num_rows(nlmLogTable); |
152 | 0 | DEBUGMSGTL(("notification_log", |
153 | 0 | "logged notifications %lu; max %lu\n", |
154 | 0 | count, max_logged)); |
155 | 0 | if (count > max_logged) { |
156 | 0 | count = count - max_logged; |
157 | 0 | DEBUGMSGTL(("notification_log", "removing %lu extra notifications\n", |
158 | 0 | count)); |
159 | 0 | netsnmp_notif_log_remove_oldest(count); |
160 | 0 | } |
161 | | |
162 | | /* |
163 | | * check max age |
164 | | */ |
165 | 0 | if (0 == max_age) |
166 | 0 | return; |
167 | 0 | count = 0; |
168 | 0 | for (row = netsnmp_table_data_set_get_first_row(nlmLogTable); |
169 | 0 | row; |
170 | 0 | row = netsnmp_table_data_set_get_next_row(nlmLogTable, row)) { |
171 | |
|
172 | 0 | data = (netsnmp_table_data_set_storage *) row->data; |
173 | 0 | data = netsnmp_table_data_set_find_column(data, COLUMN_NLMLOGTIME); |
174 | |
|
175 | 0 | if (uptime < ((u_long)(*(data->data.integer) + max_age * 100 * 60))) |
176 | 0 | break; |
177 | 0 | ++count; |
178 | 0 | } |
179 | |
|
180 | 0 | if (count) { |
181 | 0 | DEBUGMSGTL(("notification_log", "removing %lu expired notifications\n", |
182 | 0 | count)); |
183 | 0 | netsnmp_notif_log_remove_oldest(count); |
184 | 0 | } |
185 | 0 | } |
186 | | |
187 | | |
188 | | /** Initialize the nlmLogVariableTable table by defining its contents and how it's structured */ |
189 | | static void |
190 | | initialize_table_nlmLogVariableTable(const char * context) |
191 | 0 | { |
192 | 0 | static oid nlmLogVariableTable_oid[] = |
193 | 0 | { 1, 3, 6, 1, 2, 1, 92, 1, 3, 2 }; |
194 | 0 | size_t nlmLogVariableTable_oid_len = |
195 | 0 | OID_LENGTH(nlmLogVariableTable_oid); |
196 | 0 | netsnmp_table_data_set *table_set; |
197 | 0 | netsnmp_handler_registration *reginfo; |
198 | | |
199 | | /* |
200 | | * create the table structure itself |
201 | | */ |
202 | 0 | table_set = netsnmp_create_table_data_set("nlmLogVariableTable"); |
203 | 0 | nlmLogVarTable = table_set; |
204 | 0 | nlmLogVarTable->table->store_indexes = 1; |
205 | | |
206 | | /*************************************************** |
207 | | * Adding indexes |
208 | | */ |
209 | | /* |
210 | | * declaring the nlmLogName index |
211 | | */ |
212 | 0 | DEBUGMSGTL(("initialize_table_nlmLogVariableTable", |
213 | 0 | "adding index nlmLogName of type ASN_OCTET_STR to table nlmLogVariableTable\n")); |
214 | 0 | netsnmp_table_dataset_add_index(table_set, ASN_OCTET_STR); |
215 | | /* |
216 | | * declaring the nlmLogIndex index |
217 | | */ |
218 | 0 | DEBUGMSGTL(("initialize_table_nlmLogVariableTable", |
219 | 0 | "adding index nlmLogIndex of type ASN_UNSIGNED to table nlmLogVariableTable\n")); |
220 | 0 | netsnmp_table_dataset_add_index(table_set, ASN_UNSIGNED); |
221 | | /* |
222 | | * declaring the nlmLogVariableIndex index |
223 | | */ |
224 | 0 | DEBUGMSGTL(("initialize_table_nlmLogVariableTable", |
225 | 0 | "adding index nlmLogVariableIndex of type ASN_UNSIGNED to table nlmLogVariableTable\n")); |
226 | 0 | netsnmp_table_dataset_add_index(table_set, ASN_UNSIGNED); |
227 | | |
228 | | /* |
229 | | * adding column nlmLogVariableID of type ASN_OBJECT_ID and access of |
230 | | * ReadOnly |
231 | | */ |
232 | 0 | DEBUGMSGTL(("initialize_table_nlmLogVariableTable", |
233 | 0 | "adding column nlmLogVariableID (#2) of type ASN_OBJECT_ID to table nlmLogVariableTable\n")); |
234 | 0 | netsnmp_table_set_add_default_row(table_set, COLUMN_NLMLOGVARIABLEID, |
235 | 0 | ASN_OBJECT_ID, 0, NULL, 0); |
236 | | /* |
237 | | * adding column nlmLogVariableValueType of type ASN_INTEGER and |
238 | | * access of ReadOnly |
239 | | */ |
240 | 0 | DEBUGMSGTL(("initialize_table_nlmLogVariableTable", |
241 | 0 | "adding column nlmLogVariableValueType (#3) of type ASN_INTEGER to table nlmLogVariableTable\n")); |
242 | 0 | netsnmp_table_set_add_default_row(table_set, |
243 | 0 | COLUMN_NLMLOGVARIABLEVALUETYPE, |
244 | 0 | ASN_INTEGER, 0, NULL, 0); |
245 | | /* |
246 | | * adding column nlmLogVariableCounter32Val of type ASN_COUNTER and |
247 | | * access of ReadOnly |
248 | | */ |
249 | 0 | DEBUGMSGTL(("initialize_table_nlmLogVariableTable", |
250 | 0 | "adding column nlmLogVariableCounter32Val (#4) of type ASN_COUNTER to table nlmLogVariableTable\n")); |
251 | 0 | netsnmp_table_set_add_default_row(table_set, |
252 | 0 | COLUMN_NLMLOGVARIABLECOUNTER32VAL, |
253 | 0 | ASN_COUNTER, 0, NULL, 0); |
254 | | /* |
255 | | * adding column nlmLogVariableUnsigned32Val of type ASN_UNSIGNED and |
256 | | * access of ReadOnly |
257 | | */ |
258 | 0 | DEBUGMSGTL(("initialize_table_nlmLogVariableTable", |
259 | 0 | "adding column nlmLogVariableUnsigned32Val (#5) of type ASN_UNSIGNED to table nlmLogVariableTable\n")); |
260 | 0 | netsnmp_table_set_add_default_row(table_set, |
261 | 0 | COLUMN_NLMLOGVARIABLEUNSIGNED32VAL, |
262 | 0 | ASN_UNSIGNED, 0, NULL, 0); |
263 | | /* |
264 | | * adding column nlmLogVariableTimeTicksVal of type ASN_TIMETICKS and |
265 | | * access of ReadOnly |
266 | | */ |
267 | 0 | DEBUGMSGTL(("initialize_table_nlmLogVariableTable", |
268 | 0 | "adding column nlmLogVariableTimeTicksVal (#6) of type ASN_TIMETICKS to table nlmLogVariableTable\n")); |
269 | 0 | netsnmp_table_set_add_default_row(table_set, |
270 | 0 | COLUMN_NLMLOGVARIABLETIMETICKSVAL, |
271 | 0 | ASN_TIMETICKS, 0, NULL, 0); |
272 | | /* |
273 | | * adding column nlmLogVariableInteger32Val of type ASN_INTEGER and |
274 | | * access of ReadOnly |
275 | | */ |
276 | 0 | DEBUGMSGTL(("initialize_table_nlmLogVariableTable", |
277 | 0 | "adding column nlmLogVariableInteger32Val (#7) of type ASN_INTEGER to table nlmLogVariableTable\n")); |
278 | 0 | netsnmp_table_set_add_default_row(table_set, |
279 | 0 | COLUMN_NLMLOGVARIABLEINTEGER32VAL, |
280 | 0 | ASN_INTEGER, 0, NULL, 0); |
281 | | /* |
282 | | * adding column nlmLogVariableOctetStringVal of type ASN_OCTET_STR |
283 | | * and access of ReadOnly |
284 | | */ |
285 | 0 | DEBUGMSGTL(("initialize_table_nlmLogVariableTable", |
286 | 0 | "adding column nlmLogVariableOctetStringVal (#8) of type ASN_OCTET_STR to table nlmLogVariableTable\n")); |
287 | 0 | netsnmp_table_set_add_default_row(table_set, |
288 | 0 | COLUMN_NLMLOGVARIABLEOCTETSTRINGVAL, |
289 | 0 | ASN_OCTET_STR, 0, NULL, 0); |
290 | | /* |
291 | | * adding column nlmLogVariableIpAddressVal of type ASN_IPADDRESS and |
292 | | * access of ReadOnly |
293 | | */ |
294 | 0 | DEBUGMSGTL(("initialize_table_nlmLogVariableTable", |
295 | 0 | "adding column nlmLogVariableIpAddressVal (#9) of type ASN_IPADDRESS to table nlmLogVariableTable\n")); |
296 | 0 | netsnmp_table_set_add_default_row(table_set, |
297 | 0 | COLUMN_NLMLOGVARIABLEIPADDRESSVAL, |
298 | 0 | ASN_IPADDRESS, 0, NULL, 0); |
299 | | /* |
300 | | * adding column nlmLogVariableOidVal of type ASN_OBJECT_ID and access |
301 | | * of ReadOnly |
302 | | */ |
303 | 0 | DEBUGMSGTL(("initialize_table_nlmLogVariableTable", |
304 | 0 | "adding column nlmLogVariableOidVal (#10) of type ASN_OBJECT_ID to table nlmLogVariableTable\n")); |
305 | 0 | netsnmp_table_set_add_default_row(table_set, |
306 | 0 | COLUMN_NLMLOGVARIABLEOIDVAL, |
307 | 0 | ASN_OBJECT_ID, 0, NULL, 0); |
308 | | /* |
309 | | * adding column nlmLogVariableCounter64Val of type ASN_COUNTER64 and |
310 | | * access of ReadOnly |
311 | | */ |
312 | 0 | DEBUGMSGTL(("initialize_table_nlmLogVariableTable", |
313 | 0 | "adding column nlmLogVariableCounter64Val (#11) of type ASN_COUNTER64 to table nlmLogVariableTable\n")); |
314 | 0 | netsnmp_table_set_add_default_row(table_set, |
315 | 0 | COLUMN_NLMLOGVARIABLECOUNTER64VAL, |
316 | 0 | ASN_COUNTER64, 0, NULL, 0); |
317 | | /* |
318 | | * adding column nlmLogVariableOpaqueVal of type ASN_OPAQUE and access |
319 | | * of ReadOnly |
320 | | */ |
321 | 0 | DEBUGMSGTL(("initialize_table_nlmLogVariableTable", |
322 | 0 | "adding column nlmLogVariableOpaqueVal (#12) of type ASN_OPAQUE to table nlmLogVariableTable\n")); |
323 | 0 | netsnmp_table_set_add_default_row(table_set, |
324 | 0 | COLUMN_NLMLOGVARIABLEOPAQUEVAL, |
325 | 0 | ASN_OPAQUE, 0, NULL, 0); |
326 | | |
327 | | /* |
328 | | * registering the table with the master agent |
329 | | */ |
330 | | /* |
331 | | * note: if you don't need a subhandler to deal with any aspects of |
332 | | * the request, change nlmLogVariableTable_handler to "NULL" |
333 | | */ |
334 | 0 | reginfo = |
335 | 0 | netsnmp_create_handler_registration ("nlmLogVariableTable", |
336 | 0 | NULL, |
337 | 0 | nlmLogVariableTable_oid, |
338 | 0 | nlmLogVariableTable_oid_len, |
339 | 0 | HANDLER_CAN_RWRITE); |
340 | 0 | if (NULL != context) |
341 | 0 | reginfo->contextName = strdup(context); |
342 | 0 | netsnmp_register_table_data_set(reginfo, table_set, NULL); |
343 | 0 | } |
344 | | |
345 | | /** Initialize the nlmLogTable table by defining its contents and how it's structured */ |
346 | | static void |
347 | | initialize_table_nlmLogTable(const char * context) |
348 | 0 | { |
349 | 0 | static oid nlmLogTable_oid[] = { 1, 3, 6, 1, 2, 1, 92, 1, 3, 1 }; |
350 | 0 | size_t nlmLogTable_oid_len = OID_LENGTH(nlmLogTable_oid); |
351 | 0 | netsnmp_handler_registration *reginfo; |
352 | | |
353 | | /* |
354 | | * create the table structure itself |
355 | | */ |
356 | 0 | nlmLogTable = netsnmp_create_table_data_set("nlmLogTable"); |
357 | | |
358 | | /*************************************************** |
359 | | * Adding indexes |
360 | | */ |
361 | | /* |
362 | | * declaring the nlmLogIndex index |
363 | | */ |
364 | 0 | DEBUGMSGTL(("initialize_table_nlmLogTable", |
365 | 0 | "adding index nlmLogName of type ASN_OCTET_STR to table nlmLogTable\n")); |
366 | 0 | netsnmp_table_dataset_add_index(nlmLogTable, ASN_OCTET_STR); |
367 | |
|
368 | 0 | DEBUGMSGTL(("initialize_table_nlmLogTable", |
369 | 0 | "adding index nlmLogIndex of type ASN_UNSIGNED to table nlmLogTable\n")); |
370 | 0 | netsnmp_table_dataset_add_index(nlmLogTable, ASN_UNSIGNED); |
371 | | |
372 | | /* |
373 | | * adding column nlmLogTime of type ASN_TIMETICKS and access of |
374 | | * ReadOnly |
375 | | */ |
376 | 0 | DEBUGMSGTL(("initialize_table_nlmLogTable", |
377 | 0 | "adding column nlmLogTime (#2) of type ASN_TIMETICKS to table nlmLogTable\n")); |
378 | 0 | netsnmp_table_set_add_default_row(nlmLogTable, COLUMN_NLMLOGTIME, |
379 | 0 | ASN_TIMETICKS, 0, NULL, 0); |
380 | | /* |
381 | | * adding column nlmLogDateAndTime of type ASN_OCTET_STR and access of |
382 | | * ReadOnly |
383 | | */ |
384 | 0 | DEBUGMSGTL(("initialize_table_nlmLogTable", |
385 | 0 | "adding column nlmLogDateAndTime (#3) of type ASN_OCTET_STR to table nlmLogTable\n")); |
386 | 0 | netsnmp_table_set_add_default_row(nlmLogTable, |
387 | 0 | COLUMN_NLMLOGDATEANDTIME, |
388 | 0 | ASN_OCTET_STR, 0, NULL, 0); |
389 | | /* |
390 | | * adding column nlmLogEngineID of type ASN_OCTET_STR and access of |
391 | | * ReadOnly |
392 | | */ |
393 | 0 | DEBUGMSGTL(("initialize_table_nlmLogTable", |
394 | 0 | "adding column nlmLogEngineID (#4) of type ASN_OCTET_STR to table nlmLogTable\n")); |
395 | 0 | netsnmp_table_set_add_default_row(nlmLogTable, COLUMN_NLMLOGENGINEID, |
396 | 0 | ASN_OCTET_STR, 0, NULL, 0); |
397 | | /* |
398 | | * adding column nlmLogEngineTAddress of type ASN_OCTET_STR and access |
399 | | * of ReadOnly |
400 | | */ |
401 | 0 | DEBUGMSGTL(("initialize_table_nlmLogTable", |
402 | 0 | "adding column nlmLogEngineTAddress (#5) of type ASN_OCTET_STR to table nlmLogTable\n")); |
403 | 0 | netsnmp_table_set_add_default_row(nlmLogTable, |
404 | 0 | COLUMN_NLMLOGENGINETADDRESS, |
405 | 0 | ASN_OCTET_STR, 0, NULL, 0); |
406 | | /* |
407 | | * adding column nlmLogEngineTDomain of type ASN_OBJECT_ID and access |
408 | | * of ReadOnly |
409 | | */ |
410 | 0 | DEBUGMSGTL(("initialize_table_nlmLogTable", |
411 | 0 | "adding column nlmLogEngineTDomain (#6) of type ASN_OBJECT_ID to table nlmLogTable\n")); |
412 | 0 | netsnmp_table_set_add_default_row(nlmLogTable, |
413 | 0 | COLUMN_NLMLOGENGINETDOMAIN, |
414 | 0 | ASN_OBJECT_ID, 0, NULL, 0); |
415 | | /* |
416 | | * adding column nlmLogContextEngineID of type ASN_OCTET_STR and |
417 | | * access of ReadOnly |
418 | | */ |
419 | 0 | DEBUGMSGTL(("initialize_table_nlmLogTable", |
420 | 0 | "adding column nlmLogContextEngineID (#7) of type ASN_OCTET_STR to table nlmLogTable\n")); |
421 | 0 | netsnmp_table_set_add_default_row(nlmLogTable, |
422 | 0 | COLUMN_NLMLOGCONTEXTENGINEID, |
423 | 0 | ASN_OCTET_STR, 0, NULL, 0); |
424 | | /* |
425 | | * adding column nlmLogContextName of type ASN_OCTET_STR and access of |
426 | | * ReadOnly |
427 | | */ |
428 | 0 | DEBUGMSGTL(("initialize_table_nlmLogTable", |
429 | 0 | "adding column nlmLogContextName (#8) of type ASN_OCTET_STR to table nlmLogTable\n")); |
430 | 0 | netsnmp_table_set_add_default_row(nlmLogTable, |
431 | 0 | COLUMN_NLMLOGCONTEXTNAME, |
432 | 0 | ASN_OCTET_STR, 0, NULL, 0); |
433 | | /* |
434 | | * adding column nlmLogNotificationID of type ASN_OBJECT_ID and access |
435 | | * of ReadOnly |
436 | | */ |
437 | 0 | DEBUGMSGTL(("initialize_table_nlmLogTable", |
438 | 0 | "adding column nlmLogNotificationID (#9) of type ASN_OBJECT_ID to table nlmLogTable\n")); |
439 | 0 | netsnmp_table_set_add_default_row(nlmLogTable, |
440 | 0 | COLUMN_NLMLOGNOTIFICATIONID, |
441 | 0 | ASN_OBJECT_ID, 0, NULL, 0); |
442 | | |
443 | | /* |
444 | | * registering the table with the master agent |
445 | | */ |
446 | | /* |
447 | | * note: if you don't need a subhandler to deal with any aspects of |
448 | | * the request, change nlmLogTable_handler to "NULL" |
449 | | */ |
450 | 0 | reginfo = |
451 | 0 | netsnmp_create_handler_registration("nlmLogTable", NULL, |
452 | 0 | nlmLogTable_oid, |
453 | 0 | nlmLogTable_oid_len, |
454 | 0 | HANDLER_CAN_RWRITE); |
455 | 0 | if (NULL != context) |
456 | 0 | reginfo->contextName = strdup(context); |
457 | 0 | netsnmp_register_table_data_set(reginfo, nlmLogTable, NULL); |
458 | | |
459 | | /* |
460 | | * hmm... 5 minutes seems like a reasonable time to check for out |
461 | | * dated notification logs right? |
462 | | */ |
463 | 0 | snmp_alarm_register(300, SA_REPEAT, check_log_size, NULL); |
464 | 0 | } |
465 | | |
466 | | static int |
467 | | notification_log_config_handler(netsnmp_mib_handler *handler, |
468 | | netsnmp_handler_registration *reginfo, |
469 | | netsnmp_agent_request_info *reqinfo, |
470 | | netsnmp_request_info *requests) |
471 | 0 | { |
472 | | /* |
473 | | *this handler exists only to act as a trigger when the |
474 | | * configuration variables get set to a value and thus |
475 | | * notifications must be possibly deleted from our archives. |
476 | | */ |
477 | 0 | #ifndef NETSNMP_NO_WRITE_SUPPORT |
478 | 0 | if (reqinfo->mode == MODE_SET_COMMIT) |
479 | 0 | check_log_size(0, NULL); |
480 | 0 | #endif /* !NETSNMP_NO_WRITE_SUPPORT */ |
481 | 0 | return SNMP_ERR_NOERROR; |
482 | 0 | } |
483 | | |
484 | | void |
485 | | init_notification_log(void) |
486 | 0 | { |
487 | 0 | static oid my_nlmStatsGlobalNotificationsLogged_oid[] = |
488 | 0 | { 1, 3, 6, 1, 2, 1, 92, 1, 2, 1, 0 }; |
489 | 0 | static oid my_nlmStatsGlobalNotificationsBumped_oid[] = |
490 | 0 | { 1, 3, 6, 1, 2, 1, 92, 1, 2, 2, 0 }; |
491 | 0 | static oid my_nlmConfigGlobalEntryLimit_oid[] = |
492 | 0 | { 1, 3, 6, 1, 2, 1, 92, 1, 1, 1, 0 }; |
493 | 0 | static oid my_nlmConfigGlobalAgeOut_oid[] = |
494 | 0 | { 1, 3, 6, 1, 2, 1, 92, 1, 1, 2, 0 }; |
495 | 0 | char * context; |
496 | 0 | char * apptype; |
497 | |
|
498 | 0 | context = netsnmp_ds_get_string(NETSNMP_DS_APPLICATION_ID, |
499 | 0 | NETSNMP_DS_NOTIF_LOG_CTX); |
500 | |
|
501 | 0 | DEBUGMSGTL(("notification_log", "registering with '%s' context\n", |
502 | 0 | SNMP_STRORNULL(context))); |
503 | | |
504 | | /* |
505 | | * static variables |
506 | | */ |
507 | 0 | netsnmp_register_read_only_counter32_instance_context |
508 | 0 | ("nlmStatsGlobalNotificationsLogged", |
509 | 0 | my_nlmStatsGlobalNotificationsLogged_oid, |
510 | 0 | OID_LENGTH(my_nlmStatsGlobalNotificationsLogged_oid), |
511 | 0 | &num_received, NULL, context); |
512 | |
|
513 | 0 | netsnmp_register_read_only_counter32_instance_context |
514 | 0 | ("nlmStatsGlobalNotificationsBumped", |
515 | 0 | my_nlmStatsGlobalNotificationsBumped_oid, |
516 | 0 | OID_LENGTH(my_nlmStatsGlobalNotificationsBumped_oid), |
517 | 0 | &num_deleted, NULL, context); |
518 | |
|
519 | 0 | netsnmp_register_ulong_instance_context("nlmConfigGlobalEntryLimit", |
520 | 0 | my_nlmConfigGlobalEntryLimit_oid, |
521 | 0 | OID_LENGTH |
522 | 0 | (my_nlmConfigGlobalEntryLimit_oid), |
523 | 0 | &max_logged, |
524 | 0 | notification_log_config_handler, |
525 | 0 | context); |
526 | |
|
527 | 0 | netsnmp_register_ulong_instance_context("nlmConfigGlobalAgeOut", |
528 | 0 | my_nlmConfigGlobalAgeOut_oid, |
529 | 0 | OID_LENGTH |
530 | 0 | (my_nlmConfigGlobalAgeOut_oid), |
531 | 0 | &max_age, |
532 | 0 | notification_log_config_handler, |
533 | 0 | context); |
534 | | |
535 | | /* |
536 | | * tables |
537 | | */ |
538 | 0 | initialize_table_nlmLogVariableTable(context); |
539 | 0 | initialize_table_nlmLogTable(context); |
540 | | |
541 | | /* |
542 | | * disable flag |
543 | | */ |
544 | 0 | apptype = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, |
545 | 0 | NETSNMP_DS_LIB_APPTYPE); |
546 | 0 | netsnmp_ds_register_config(ASN_BOOLEAN, apptype, "dontRetainLogs", |
547 | 0 | NETSNMP_DS_APPLICATION_ID, |
548 | 0 | NETSNMP_DS_AGENT_DONT_RETAIN_NOTIFICATIONS); |
549 | 0 | netsnmp_ds_register_config(ASN_BOOLEAN, apptype, "doNotRetainNotificationLogs", |
550 | 0 | NETSNMP_DS_APPLICATION_ID, |
551 | 0 | NETSNMP_DS_AGENT_DONT_RETAIN_NOTIFICATIONS); |
552 | | #if 0 |
553 | | /* xxx-rks: config for max size; should be peristent too, & tied to mib */ |
554 | | netsnmp_ds_register_config(ASN_INTEGER, |
555 | | netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, |
556 | | NETSNMP_DS_LIB_APPTYPE), |
557 | | "notificationLogMax", |
558 | | NETSNMP_DS_APPLICATION_ID, |
559 | | NETSNMP_DS_AGENT_NOTIF_LOG_MAX); |
560 | | #endif |
561 | |
|
562 | 0 | REGISTER_SYSOR_ENTRY(nlm_module_oid, |
563 | 0 | "The MIB module for logging SNMP Notifications."); |
564 | 0 | } |
565 | | |
566 | | void |
567 | | shutdown_notification_log(void) |
568 | 0 | { |
569 | 0 | max_logged = 0; |
570 | 0 | check_log_size(0, NULL); |
571 | 0 | netsnmp_delete_table_data_set(nlmLogTable); |
572 | 0 | nlmLogTable = NULL; |
573 | |
|
574 | 0 | UNREGISTER_SYSOR_ENTRY(nlm_module_oid); |
575 | 0 | } |
576 | | |
577 | | void |
578 | | log_notification(netsnmp_pdu *pdu, netsnmp_transport *transport) |
579 | 0 | { |
580 | 0 | long tmpl; |
581 | 0 | netsnmp_table_row *row; |
582 | |
|
583 | 0 | static u_long default_num = 0; |
584 | |
|
585 | 0 | static oid snmptrapoid[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 }; |
586 | 0 | size_t snmptrapoid_len = OID_LENGTH(snmptrapoid); |
587 | 0 | netsnmp_variable_list *vptr; |
588 | 0 | u_char *logdate; |
589 | 0 | size_t logdate_size; |
590 | 0 | time_t timetnow; |
591 | |
|
592 | 0 | u_long vbcount = 0; |
593 | 0 | u_long tmpul; |
594 | 0 | int col; |
595 | 0 | netsnmp_pdu *orig_pdu = pdu; |
596 | |
|
597 | 0 | if (!nlmLogVarTable |
598 | 0 | || netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, |
599 | 0 | NETSNMP_DS_APP_DONT_LOG)) { |
600 | 0 | return; |
601 | 0 | } |
602 | | |
603 | 0 | DEBUGMSGTL(("notification_log", "logging something\n")); |
604 | 0 | row = netsnmp_create_table_data_row(); |
605 | |
|
606 | 0 | ++num_received; |
607 | 0 | default_num++; |
608 | | |
609 | | /* |
610 | | * indexes to the table |
611 | | */ |
612 | 0 | netsnmp_table_row_add_index(row, ASN_OCTET_STR, "default", |
613 | 0 | strlen("default")); |
614 | 0 | netsnmp_table_row_add_index(row, ASN_UNSIGNED, &default_num, |
615 | 0 | sizeof(default_num)); |
616 | | |
617 | | /* |
618 | | * add the data |
619 | | */ |
620 | 0 | tmpl = netsnmp_get_agent_uptime(); |
621 | 0 | netsnmp_set_row_column(row, COLUMN_NLMLOGTIME, ASN_TIMETICKS, |
622 | 0 | &tmpl, sizeof(tmpl)); |
623 | 0 | time(&timetnow); |
624 | 0 | logdate = date_n_time(&timetnow, &logdate_size); |
625 | 0 | netsnmp_set_row_column(row, COLUMN_NLMLOGDATEANDTIME, ASN_OCTET_STR, |
626 | 0 | logdate, logdate_size); |
627 | 0 | netsnmp_set_row_column(row, COLUMN_NLMLOGENGINEID, ASN_OCTET_STR, |
628 | 0 | pdu->securityEngineID, |
629 | 0 | pdu->securityEngineIDLen); |
630 | 0 | if (transport && transport->domain == netsnmpUDPDomain) { |
631 | | /* |
632 | | * check for the udp domain |
633 | | */ |
634 | 0 | struct sockaddr_in *addr = |
635 | 0 | (struct sockaddr_in *) pdu->transport_data; |
636 | 0 | if (addr) { |
637 | 0 | char buf[sizeof(in_addr_t) + |
638 | 0 | sizeof(addr->sin_port)]; |
639 | 0 | in_addr_t locaddr = htonl(addr->sin_addr.s_addr); |
640 | 0 | u_short portnum = htons(addr->sin_port); |
641 | 0 | memcpy(buf, &locaddr, sizeof(in_addr_t)); |
642 | 0 | memcpy(buf + sizeof(in_addr_t), &portnum, |
643 | 0 | sizeof(addr->sin_port)); |
644 | 0 | netsnmp_set_row_column(row, COLUMN_NLMLOGENGINETADDRESS, |
645 | 0 | ASN_OCTET_STR, buf, |
646 | 0 | sizeof(in_addr_t) + |
647 | 0 | sizeof(addr->sin_port)); |
648 | 0 | } |
649 | 0 | } |
650 | 0 | if (transport) |
651 | 0 | netsnmp_set_row_column(row, COLUMN_NLMLOGENGINETDOMAIN, |
652 | 0 | ASN_OBJECT_ID, |
653 | 0 | transport->domain, |
654 | 0 | sizeof(oid) * transport->domain_length); |
655 | 0 | netsnmp_set_row_column(row, COLUMN_NLMLOGCONTEXTENGINEID, |
656 | 0 | ASN_OCTET_STR, pdu->contextEngineID, |
657 | 0 | pdu->contextEngineIDLen); |
658 | 0 | netsnmp_set_row_column(row, COLUMN_NLMLOGCONTEXTNAME, ASN_OCTET_STR, |
659 | 0 | pdu->contextName, pdu->contextNameLen); |
660 | |
|
661 | 0 | if (pdu->command == SNMP_MSG_TRAP) |
662 | 0 | pdu = convert_v1pdu_to_v2(orig_pdu); |
663 | 0 | for (vptr = pdu->variables; vptr; vptr = vptr->next_variable) { |
664 | 0 | if (snmp_oid_compare(snmptrapoid, snmptrapoid_len, |
665 | 0 | vptr->name, vptr->name_length) == 0) { |
666 | 0 | netsnmp_set_row_column(row, COLUMN_NLMLOGNOTIFICATIONID, |
667 | 0 | ASN_OBJECT_ID, vptr->val.string, |
668 | 0 | vptr->val_len); |
669 | 0 | } else { |
670 | 0 | netsnmp_table_row *myrow; |
671 | 0 | myrow = netsnmp_create_table_data_row(); |
672 | | |
673 | | /* |
674 | | * indexes to the table |
675 | | */ |
676 | 0 | netsnmp_table_row_add_index(myrow, ASN_OCTET_STR, "default", |
677 | 0 | strlen("default")); |
678 | 0 | netsnmp_table_row_add_index(myrow, ASN_UNSIGNED, &default_num, |
679 | 0 | sizeof(default_num)); |
680 | 0 | vbcount++; |
681 | 0 | netsnmp_table_row_add_index(myrow, ASN_UNSIGNED, &vbcount, |
682 | 0 | sizeof(vbcount)); |
683 | | |
684 | | /* |
685 | | * OID |
686 | | */ |
687 | 0 | netsnmp_set_row_column(myrow, COLUMN_NLMLOGVARIABLEID, |
688 | 0 | ASN_OBJECT_ID, vptr->name, |
689 | 0 | vptr->name_length * sizeof(oid)); |
690 | | |
691 | | /* |
692 | | * value |
693 | | */ |
694 | 0 | switch (vptr->type) { |
695 | 0 | case ASN_OBJECT_ID: |
696 | 0 | tmpul = 7; |
697 | 0 | col = COLUMN_NLMLOGVARIABLEOIDVAL; |
698 | 0 | break; |
699 | | |
700 | 0 | case ASN_INTEGER: |
701 | 0 | tmpul = 4; |
702 | 0 | col = COLUMN_NLMLOGVARIABLEINTEGER32VAL; |
703 | 0 | break; |
704 | | |
705 | 0 | case ASN_UNSIGNED: |
706 | 0 | tmpul = 2; |
707 | 0 | col = COLUMN_NLMLOGVARIABLEUNSIGNED32VAL; |
708 | 0 | break; |
709 | | |
710 | 0 | case ASN_COUNTER: |
711 | 0 | tmpul = 1; |
712 | 0 | col = COLUMN_NLMLOGVARIABLECOUNTER32VAL; |
713 | 0 | break; |
714 | | |
715 | 0 | case ASN_TIMETICKS: |
716 | 0 | tmpul = 3; |
717 | 0 | col = COLUMN_NLMLOGVARIABLETIMETICKSVAL; |
718 | 0 | break; |
719 | | |
720 | 0 | case ASN_OCTET_STR: |
721 | 0 | tmpul = 6; |
722 | 0 | col = COLUMN_NLMLOGVARIABLEOCTETSTRINGVAL; |
723 | 0 | break; |
724 | | |
725 | 0 | case ASN_IPADDRESS: |
726 | 0 | tmpul = 5; |
727 | 0 | col = COLUMN_NLMLOGVARIABLEIPADDRESSVAL; |
728 | 0 | break; |
729 | | |
730 | 0 | case ASN_COUNTER64: |
731 | 0 | tmpul = 8; |
732 | 0 | col = COLUMN_NLMLOGVARIABLECOUNTER64VAL; |
733 | 0 | break; |
734 | | |
735 | 0 | case ASN_OPAQUE: |
736 | 0 | tmpul = 9; |
737 | 0 | col = COLUMN_NLMLOGVARIABLEOPAQUEVAL; |
738 | 0 | break; |
739 | | |
740 | 0 | default: |
741 | | /* |
742 | | * unsupported |
743 | | */ |
744 | 0 | DEBUGMSGTL(("notification_log", |
745 | 0 | "skipping type %d\n", vptr->type)); |
746 | 0 | (void)netsnmp_table_dataset_delete_row(myrow); |
747 | 0 | continue; |
748 | 0 | } |
749 | 0 | netsnmp_set_row_column(myrow, COLUMN_NLMLOGVARIABLEVALUETYPE, |
750 | 0 | ASN_INTEGER, & tmpul, |
751 | 0 | sizeof(tmpul)); |
752 | 0 | netsnmp_set_row_column(myrow, col, vptr->type, |
753 | 0 | vptr->val.string, vptr->val_len); |
754 | 0 | DEBUGMSGTL(("notification_log", |
755 | 0 | "adding a row to the variables table\n")); |
756 | 0 | netsnmp_table_dataset_add_row(nlmLogVarTable, myrow); |
757 | 0 | } |
758 | 0 | } |
759 | | |
760 | 0 | if (pdu != orig_pdu) |
761 | 0 | snmp_free_pdu( pdu ); |
762 | | |
763 | | /* |
764 | | * store the row |
765 | | */ |
766 | 0 | netsnmp_table_dataset_add_row(nlmLogTable, row); |
767 | |
|
768 | 0 | check_log_size(0, NULL); |
769 | 0 | DEBUGMSGTL(("notification_log", "done logging something\n")); |
770 | 0 | } |