Debian Codegraph @

agent/mibgroup/ip-mib/ipIfStatsTable/ipIfStatsTable_data_access.c

raw
1 /*
2 * Note: this file originally auto-generated by mib2c using
3 * version : 14170 $ of $
4 *
5 * $Id:$
6 */
7 /*
8 * standard Net-SNMP includes
9 */
10 #include <net-snmp/net-snmp-config.h>
11 #include <net-snmp/net-snmp-includes.h>
12 #include <net-snmp/agent/net-snmp-agent-includes.h>
13
14 /*
15 * include our parent header
16 */
17 #include "ipIfStatsTable.h"
18
19
20 #include "ipIfStatsTable_data_access.h"
21
22 static int ipis_cache_refresh = IPIFSTATSTABLE_CACHE_TIMEOUT;
23
24 /** @ingroup interface
25 * @addtogroup data_access data_access: Routines to access data
26 *
27 * These routines are used to locate the data used to satisfy
28 * requests.
29 *
30 * @{
31 */
32 /**********************************************************************
33 **********************************************************************
34 ***
35 *** Table ipIfStatsTable
36 ***
37 **********************************************************************
38 **********************************************************************/
39 /*
40 * IP-MIB::ipIfStatsTable is subid 3 of ipTrafficStats.
41 * Its status is Current.
42 * OID: .1.3.6.1.2.1.4.31.3, length: 9
43 */
44
45 /**
46 * initialization for ipIfStatsTable data access
47 *
48 * This function is called during startup to allow you to
49 * allocate any resources you need for the data table.
50 *
51 * @param ipIfStatsTable_reg
52 * Pointer to ipIfStatsTable_registration
53 *
54 * @retval MFD_SUCCESS : success.
55 * @retval MFD_ERROR : unrecoverable error.
56 */
57 int
58 ipIfStatsTable_init_data(ipIfStatsTable_registration * ipIfStatsTable_reg)
59 {
60 DEBUGMSGTL(("verbose:ipIfStatsTable:ipIfStatsTable_init_data",
61 "called\n"));
62
63 /*
64 * TODO:303:o: Initialize ipIfStatsTable data.
65 */
66 netsnmp_access_systemstats_init();
67
68 return MFD_SUCCESS;
69 } /* ipIfStatsTable_init_data */
70
71 /**
72 * container overview
73 *
74 */
75
76 /**
77 * container initialization
78 *
79 * @param container_ptr_ptr A pointer to a container pointer. If you
80 * create a custom container, use this parameter to return it
81 * to the MFD helper. If set to NULL, the MFD helper will
82 * allocate a container for you.
83 * @param cache A pointer to a cache structure. You can set the timeout
84 * and other cache flags using this pointer.
85 *
86 * This function is called at startup to allow you to customize certain
87 * aspects of the access method. For the most part, it is for advanced
88 * users. The default code should suffice for most cases. If no custom
89 * container is allocated, the MFD code will create one for your.
90 *
91 * This is also the place to set up cache behavior. The default, to
92 * simply set the cache timeout, will work well with the default
93 * container. If you are using a custom container, you may want to
94 * look at the cache helper documentation to see if there are any
95 * flags you want to set.
96 *
97 * @remark
98 * This would also be a good place to do any initialization needed
99 * for you data source. For example, opening a connection to another
100 * process that will supply the data, opening a database, etc.
101 */
102 void
103 ipIfStatsTable_container_init(netsnmp_container ** container_ptr_ptr,
104 netsnmp_cache * cache)
105 {
106 DEBUGMSGTL(("verbose:ipIfStatsTable:ipIfStatsTable_container_init",
107 "called\n"));
108
109 if (NULL == container_ptr_ptr) {
110 snmp_log(LOG_ERR,
111 "bad container param to ipIfStatsTable_container_init\n");
112 return;
113 }
114
115 /*
116 * For advanced users, you can use a custom container. If you
117 * do not create one, one will be created for you.
118 */
119 /*
120 * We create a custom container here so we can pre-load it, which
121 * will result in all new entries with last changed values. we need
122 * to clear those... We also need to make sure ifIndexes have been
123 * assigned...
124 */
125 *container_ptr_ptr =
126 netsnmp_container_find("ipIfStatsTable:table_container");
127 if (NULL != *container_ptr_ptr) {
128 (*container_ptr_ptr)->container_name = strdup("ipIfStatsTable");
129 ipIfStatsTable_container_load(*container_ptr_ptr);
130 }
131 if (NULL == cache) {
132 snmp_log(LOG_ERR,
133 "bad cache param to ipIfStatsTable_container_init\n");
134 return;
135 }
136
137 /*
138 * TODO:345:A: Set up ipIfStatsTable cache properties.
139 *
140 * Also for advanced users, you can set parameters for the
141 * cache. Do not change the magic pointer, as it is used
142 * by the MFD helper. To completely disable caching, set
143 * cache->enabled to 0.
144 */
145 cache->timeout = IPIFSTATSTABLE_CACHE_TIMEOUT; /* seconds */
146
147 cache->flags |=
148 (NETSNMP_CACHE_DONT_AUTO_RELEASE | NETSNMP_CACHE_DONT_FREE_EXPIRED
149 | NETSNMP_CACHE_DONT_FREE_BEFORE_LOAD |
150 NETSNMP_CACHE_AUTO_RELOAD);
151 } /* ipIfStatsTable_container_init */
152
153 /**
154 * check entry for update
155 */
156 static void
157 _check_for_updates(void *p, void *q)
158 {
159 ipIfStatsTable_rowreq_ctx *rowreq_ctx = p;
160 netsnmp_container *stats = q;
161 netsnmp_systemstats_entry *ifstats_entry;
162
163 /*
164 * check for matching entry. works because indexes are the same.
165 */
166 ifstats_entry = (netsnmp_systemstats_entry*)CONTAINER_FIND(stats, rowreq_ctx->data);
167 if (NULL == ifstats_entry) {
168 DEBUGMSGTL(("ipIfStatsTable:access",
169 "updating missing entry\n"));
170
171 /*
172 * mark row as missing, so we can set discontinuity
173 * when it comes back.
174 *
175 * what else should we do? set refresh to 0? that's not quite right...
176 */
177 rowreq_ctx->known_missing = 1;
178 } else {
179 DEBUGMSGTL(("ipIfStatsTable:access",
180 "updating existing entry\n"));
181
182 /*
183 * Check for changes & update
184 */
185 netsnmp_access_systemstats_entry_update(rowreq_ctx->data,
186 ifstats_entry);
187
188 /*
189 * set discontinuity if previously missing.
190 */
191 if (1 == rowreq_ctx->known_missing) {
192 rowreq_ctx->known_missing = 0;
193 rowreq_ctx->ipIfStatsDiscontinuityTime =
194 netsnmp_get_agent_uptime();
195 ipIfStatsTable_lastChange_set(netsnmp_get_agent_uptime());
196 }
197
198 /*
199 * remove entry from container
200 */
201 CONTAINER_REMOVE(stats, ifstats_entry);
202 netsnmp_access_systemstats_entry_free(ifstats_entry);
203 }
204 }
205
206 /**
207 * add new entry
208 */
209 static void
210 _add_new(void *p, void *q)
211 {
212 netsnmp_systemstats_entry *ifstats_entry = p;
213 netsnmp_container *container = q;
214 ipIfStatsTable_rowreq_ctx *rowreq_ctx;
215
216 DEBUGMSGTL(("ipIfStatsTable:access", "creating new entry\n"));
217
218 netsnmp_assert(NULL != ifstats_entry);
219 netsnmp_assert(NULL != container);
220
221 /*
222 * allocate an row context and set the index(es)
223 */
224 rowreq_ctx =
225 ipIfStatsTable_allocate_rowreq_ctx(ifstats_entry, NULL);
226 if ((NULL != rowreq_ctx)
227 && (MFD_SUCCESS ==
228 ipIfStatsTable_indexes_set(rowreq_ctx,
229 ifstats_entry->index[0],
230 ifstats_entry->index[1]))) {
231 rowreq_ctx->ipIfStatsRefreshRate = ipis_cache_refresh * 1000; /* milli-seconds */
232 CONTAINER_INSERT(container, rowreq_ctx);
233 ipIfStatsTable_lastChange_set(netsnmp_get_agent_uptime());
234 } else {
235 if (NULL != rowreq_ctx) {
236 snmp_log(LOG_ERR, "error setting index while loading "
237 "ipIfStatsTable cache.\n");
238 ipIfStatsTable_release_rowreq_ctx(rowreq_ctx);
239 } else {
240 snmp_log(LOG_ERR, "memory allocation failed while loading "
241 "ipIfStatsTable cache.\n");
242 netsnmp_access_systemstats_entry_free(ifstats_entry);
243 }
244 }
245 }
246
247 /**
248 * container shutdown
249 *
250 * @param container_ptr A pointer to the container.
251 *
252 * This function is called at shutdown to allow you to customize certain
253 * aspects of the access method. For the most part, it is for advanced
254 * users. The default code should suffice for most cases.
255 *
256 * This function is called before ipIfStatsTable_container_free().
257 *
258 * @remark
259 * This would also be a good place to do any cleanup needed
260 * for you data source. For example, closing a connection to another
261 * process that supplied the data, closing a database, etc.
262 */
263 void
264 ipIfStatsTable_container_shutdown(netsnmp_container * container_ptr)
265 {
266 DEBUGMSGTL(("verbose:ipIfStatsTable:ipIfStatsTable_container_shutdown",
267 "called\n"));
268
269 if (NULL == container_ptr) {
270 snmp_log(LOG_ERR,
271 "bad params to ipIfStatsTable_container_shutdown\n");
272 return;
273 }
274
275 } /* ipIfStatsTable_container_shutdown */
276
277 /**
278 * load initial data
279 *
280 * TODO:350:M: Implement ipIfStatsTable data load
281 * This function will also be called by the cache helper to load
282 * the container again (after the container free function has been
283 * called to free the previous contents).
284 *
285 * @param container container to which items should be inserted
286 *
287 * @retval MFD_SUCCESS : success.
288 * @retval MFD_RESOURCE_UNAVAILABLE : Can't access data source
289 * @retval MFD_ERROR : other error.
290 *
291 * This function is called to load the index(es) (and data, optionally)
292 * for the every row in the data set.
293 *
294 * @remark
295 * While loading the data, the only important thing is the indexes.
296 * If access to your data is cheap/fast (e.g. you have a pointer to a
297 * structure in memory), it would make sense to update the data here.
298 * If, however, the accessing the data involves more work (e.g. parsing
299 * some other existing data, or performing calculations to derive the data),
300 * then you can limit yourself to setting the indexes and saving any
301 * information you will need later. Then use the saved information in
302 * ipIfStatsTable_row_prep() for populating data.
303 *
304 * @note
305 * If you need consistency between rows (like you want statistics
306 * for each row to be from the same time frame), you should set all
307 * data here.
308 *
309 */
310 int
311 ipIfStatsTable_container_load(netsnmp_container * container)
312 {
313 netsnmp_container *stats;
314
315 DEBUGMSGTL(("verbose:ipIfStatsTable:ipIfStatsTable_container_load",
316 "called\n"));
317
318 netsnmp_assert(NULL != container);
319
320 stats = netsnmp_access_systemstats_container_load(NULL, NETSNMP_ACCESS_SYSTEMSTATS_LOAD_IFTABLE);
321 if (NULL == stats)
322 return MFD_RESOURCE_UNAVAILABLE; /* msg already logged */
323
324 /*
325 * TODO:351:M: |-> Load/update data in the ipIfStatsTable container.
326 * loop over your ipIfStatsTable data, allocate a rowreq context,
327 * set the index(es) [and data, optionally] and insert into
328 * the container.
329 */
330
331 /*
332 * we just got a fresh copy of data. compare it to
333 * what we've already got, and make any adjustements...
334 */
335 CONTAINER_FOR_EACH(container, _check_for_updates, stats);
336
337 /*
338 * now add any new entries
339 */
340 CONTAINER_FOR_EACH(stats, _add_new, container);
341
342
343 /*
344 * free the container. we've either claimed each ifentry, or released it,
345 * so the dal function doesn't need to clear the container.
346 */
347 netsnmp_access_systemstats_container_free(stats,
348 NETSNMP_ACCESS_SYSTEMSTATS_FREE_DONT_CLEAR);
349
350 DEBUGMSGT(("verbose:ipIfStatsTable:ipIfStatsTable_container_load",
351 "%lu records\n", (unsigned long)CONTAINER_SIZE(container)));
352
353 return MFD_SUCCESS;
354 } /* ipIfStatsTable_container_load */
355
356 /**
357 * container clean up
358 *
359 * @param container container with all current items
360 *
361 * This optional callback is called prior to all
362 * item's being removed from the container. If you
363 * need to do any processing before that, do it here.
364 *
365 * @note
366 * The MFD helper will take care of releasing all the row contexts.
367 *
368 */
369 void
370 ipIfStatsTable_container_free(netsnmp_container * container)
371 {
372 DEBUGMSGTL(("verbose:ipIfStatsTable:ipIfStatsTable_container_free",
373 "called\n"));
374
375 /*
376 * TODO:380:M: Free ipIfStatsTable container data.
377 */
378 } /* ipIfStatsTable_container_free */
379
380 /**
381 * prepare row for processing.
382 *
383 * When the agent has located the row for a request, this function is
384 * called to prepare the row for processing. If you fully populated
385 * the data context during the index setup phase, you may not need to
386 * do anything.
387 *
388 * @param rowreq_ctx pointer to a context.
389 *
390 * @retval MFD_SUCCESS : success.
391 * @retval MFD_ERROR : other error.
392 */
393 int
394 ipIfStatsTable_row_prep(ipIfStatsTable_rowreq_ctx * rowreq_ctx)
395 {
396 DEBUGMSGTL(("verbose:ipIfStatsTable:ipIfStatsTable_row_prep",
397 "called\n"));
398
399 netsnmp_assert(NULL != rowreq_ctx);
400
401 /*
402 * TODO:390:o: Prepare row for request.
403 * If populating row data was delayed, this is the place to
404 * fill in the row for this request.
405 */
406
407 return MFD_SUCCESS;
408 } /* ipIfStatsTable_row_prep */
409
410 /** @} */