| 1 |
/* |
| 2 |
* Note: this file originally auto-generated by mib2c using |
| 3 |
* version : 1.17 $ of : mfd-data-access.m2c,v $ |
| 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-features.h> |
| 12 |
#include <net-snmp/net-snmp-includes.h> |
| 13 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 14 |
|
| 15 |
netsnmp_feature_require(container_linked_list); |
| 16 |
|
| 17 |
/* |
| 18 |
* include our parent header |
| 19 |
*/ |
| 20 |
#include "ifTable.h" |
| 21 |
#include "ifTable_defs.h" |
| 22 |
|
| 23 |
#include "ifTable_data_access.h" |
| 24 |
|
| 25 |
#ifdef USING_IP_MIB_IPV4INTERFACETABLE_IPV4INTERFACETABLE_MODULE |
| 26 |
# include "mibgroup/ip-mib/ipv4InterfaceTable/ipv4InterfaceTable.h" |
| 27 |
#endif |
| 28 |
|
| 29 |
#ifdef USING_IP_MIB_IPV6INTERFACETABLE_IPV6INTERFACETABLE_MODULE |
| 30 |
# include "mibgroup/ip-mib/ipv6InterfaceTable/ipv6InterfaceTable.h" |
| 31 |
#endif |
| 32 |
|
| 33 |
struct cd_container { |
| 34 |
netsnmp_container *current; |
| 35 |
netsnmp_container *deleted; |
| 36 |
}; |
| 37 |
|
| 38 |
/* |
| 39 |
* flag so we know not to set row/table last change times |
| 40 |
* during startup. |
| 41 |
*/ |
| 42 |
static int _first_load = 1; |
| 43 |
|
| 44 |
/* |
| 45 |
* Value of interface_fadeout config option |
| 46 |
*/ |
| 47 |
static int fadeout = IFTABLE_REMOVE_MISSING_AFTER; |
| 48 |
/* |
| 49 |
* Value of interface_replace_old config option |
| 50 |
*/ |
| 51 |
static int replace_old = 0; |
| 52 |
|
| 53 |
static void __delete_missing_interface(void *rowreq_ctx, void *container); |
| 54 |
|
| 55 |
/** @ingroup interface |
| 56 |
* @defgroup data_access data_access: Routines to access data |
| 57 |
* |
| 58 |
* These routines are used to locate the data used to satisfy |
| 59 |
* requests. |
| 60 |
* |
| 61 |
* @{ |
| 62 |
*/ |
| 63 |
/********************************************************************** |
| 64 |
********************************************************************** |
| 65 |
*** |
| 66 |
*** Table ifTable |
| 67 |
*** |
| 68 |
********************************************************************** |
| 69 |
**********************************************************************/ |
| 70 |
/* |
| 71 |
* IF-MIB::ifTable is subid 2 of interfaces. |
| 72 |
* Its status is Current. |
| 73 |
* OID: .1.3.6.1.2.1.2.2, length: 8 |
| 74 |
*/ |
| 75 |
|
| 76 |
static void |
| 77 |
parse_interface_fadeout(const char *token, char *line) |
| 78 |
{ |
| 79 |
fadeout = atoi(line); |
| 80 |
} |
| 81 |
|
| 82 |
static void |
| 83 |
parse_interface_replace_old(const char *token, char *line) |
| 84 |
{ |
| 85 |
if (strcmp(line, "yes") == 0 |
| 86 |
|| strcmp(line, "y") == 0 |
| 87 |
|| strcmp(line, "true") == 0 |
| 88 |
|| strcmp(line, "1") == 0) { |
| 89 |
replace_old = 1; |
| 90 |
return; |
| 91 |
} |
| 92 |
if (strcmp(line, "no") == 0 |
| 93 |
|| strcmp(line, "n") == 0 |
| 94 |
|| strcmp(line, "false") == 0 |
| 95 |
|| strcmp(line, "0") == 0) { |
| 96 |
replace_old = 0; |
| 97 |
return; |
| 98 |
} |
| 99 |
snmp_log(LOG_ERR, "Invalid value of interface_replace_old parameter: '%s'\n", |
| 100 |
line); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* initialization for ifTable data access |
| 105 |
* |
| 106 |
* This function is called during startup to allow you to |
| 107 |
* allocate any resources you need for the data table. |
| 108 |
* |
| 109 |
* @param ifTable_reg |
| 110 |
* Pointer to ifTable_registration |
| 111 |
* |
| 112 |
* @retval MFD_SUCCESS : success. |
| 113 |
* @retval MFD_ERROR : unrecoverable error. |
| 114 |
*/ |
| 115 |
int |
| 116 |
ifTable_init_data(ifTable_registration * ifTable_reg) |
| 117 |
{ |
| 118 |
DEBUGMSGTL(("verbose:ifTable:ifTable_init_data", "called\n")); |
| 119 |
|
| 120 |
/* |
| 121 |
* TODO:303:o: Initialize ifTable data. |
| 122 |
*/ |
| 123 |
snmpd_register_config_handler("interface_fadeout", parse_interface_fadeout, NULL, |
| 124 |
"interface_fadeout seconds"); |
| 125 |
snmpd_register_config_handler("interface_replace_old", |
| 126 |
parse_interface_replace_old, NULL, "interface_replace_old yes|no"); |
| 127 |
|
| 128 |
return MFD_SUCCESS; |
| 129 |
} /* ifTable_init_data */ |
| 130 |
|
| 131 |
/** |
| 132 |
* container overview |
| 133 |
* |
| 134 |
*/ |
| 135 |
|
| 136 |
/** |
| 137 |
* container initialization |
| 138 |
* |
| 139 |
* @param container_ptr_ptr A pointer to a container pointer. If you |
| 140 |
* create a custom container, use this parameter to return it |
| 141 |
* to the MFD helper. If set to NULL, the MFD helper will |
| 142 |
* allocate a container for you. |
| 143 |
* @param cache A pointer to a cache structure. You can set the timeout |
| 144 |
* and other cache flags using this pointer. |
| 145 |
* |
| 146 |
* This function is called at startup to allow you to customize certain |
| 147 |
* aspects of the access method. For the most part, it is for advanced |
| 148 |
* users. The default code should suffice for most cases. If no custom |
| 149 |
* container is allocated, the MFD code will create one for your. |
| 150 |
* |
| 151 |
* This is also the place to set up cache behavior. The default, to |
| 152 |
* simply set the cache timeout, will work well with the default |
| 153 |
* container. If you are using a custom container, you may want to |
| 154 |
* look at the cache helper documentation to see if there are any |
| 155 |
* flags you want to set. |
| 156 |
* |
| 157 |
* @remark |
| 158 |
* This would also be a good place to do any initialization needed |
| 159 |
* for you data source. For example, opening a connection to another |
| 160 |
* process that will supply the data, opening a database, etc. |
| 161 |
*/ |
| 162 |
void |
| 163 |
ifTable_container_init(netsnmp_container **container_ptr_ptr, |
| 164 |
netsnmp_cache * cache) |
| 165 |
{ |
| 166 |
DEBUGMSGTL(("verbose:ifTable:ifTable_container_init", "called\n")); |
| 167 |
|
| 168 |
if (NULL == container_ptr_ptr) { |
| 169 |
snmp_log(LOG_ERR, |
| 170 |
"bad container param to ifTable_container_init\n"); |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
/* |
| 175 |
* For advanced users, you can use a custom container. If you |
| 176 |
* do not create one, one will be created for you. |
| 177 |
*/ |
| 178 |
*container_ptr_ptr = NULL; |
| 179 |
|
| 180 |
if (NULL == cache) { |
| 181 |
snmp_log(LOG_ERR, "bad cache param to ifTable_container_init\n"); |
| 182 |
return; |
| 183 |
} |
| 184 |
|
| 185 |
/* |
| 186 |
* TODO:345:A: Set up ifTable cache properties. |
| 187 |
* |
| 188 |
* Also for advanced users, you can set parameters for the |
| 189 |
* cache. Do not change the magic pointer, as it is used |
| 190 |
* by the MFD helper. To completely disable caching, set |
| 191 |
* cache->enabled to 0. |
| 192 |
*/ |
| 193 |
/* |
| 194 |
* since we set AUTO_RELOAD below, this timer controls how |
| 195 |
* often the cache is reloaded. A 10 Mbps stream can wrap if*Octets in ~57 minutes. |
| 196 |
* At 100 Mbps it is ~5 minutes, and at 1 Gbps, ~34 seconds. |
| 197 |
*/ |
| 198 |
cache->timeout = IFTABLE_CACHE_TIMEOUT; /* seconds */ |
| 199 |
|
| 200 |
/* |
| 201 |
* don't release resources |
| 202 |
*/ |
| 203 |
cache->flags |= |
| 204 |
(NETSNMP_CACHE_DONT_AUTO_RELEASE | NETSNMP_CACHE_DONT_FREE_EXPIRED |
| 205 |
| NETSNMP_CACHE_DONT_FREE_BEFORE_LOAD | NETSNMP_CACHE_PRELOAD | |
| 206 |
NETSNMP_CACHE_AUTO_RELOAD | NETSNMP_CACHE_DONT_INVALIDATE_ON_SET); |
| 207 |
} /* ifTable_container_init */ |
| 208 |
|
| 209 |
void |
| 210 |
send_linkUpDownNotifications(oid *notification_oid, size_t notification_oid_len, int if_index, int if_admin_status, int if_oper_status) |
| 211 |
{ |
| 212 |
/* |
| 213 |
* In the notification, we have to assign our notification OID to |
| 214 |
* the snmpTrapOID.0 object. Here is it's definition. |
| 215 |
*/ |
| 216 |
oid objid_snmptrap[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 }; |
| 217 |
size_t objid_snmptrap_len = OID_LENGTH(objid_snmptrap); |
| 218 |
|
| 219 |
/* |
| 220 |
* define the OIDs for the varbinds we're going to include |
| 221 |
* with the notification - |
| 222 |
* IF-MIB::ifIndex, |
| 223 |
* IF-MIB::ifAdminStatus, and |
| 224 |
* IF-MIB::ifOperStatus |
| 225 |
*/ |
| 226 |
oid if_index_oid[] = { 1, 3, 6, 1, 2, 1, 2, 2, 1, 1, 0 }; |
| 227 |
size_t if_index_oid_len = OID_LENGTH(if_index_oid); |
| 228 |
oid if_admin_status_oid[] = { 1, 3, 6, 1, 2, 1, 2, 2, 1, 7, 0 }; |
| 229 |
size_t if_admin_status_oid_len = OID_LENGTH(if_admin_status_oid); |
| 230 |
oid if_oper_status_oid[] = { 1, 3, 6, 1, 2, 1, 2, 2, 1, 8, 0 }; |
| 231 |
size_t if_oper_status_oid_len = OID_LENGTH(if_oper_status_oid); |
| 232 |
|
| 233 |
/* |
| 234 |
* here is where we store the variables to be sent in the trap |
| 235 |
*/ |
| 236 |
netsnmp_variable_list *notification_vars = NULL; |
| 237 |
|
| 238 |
DEBUGMSGTL(("rsys:linkUpDownNotifications", "defining the trap\n")); |
| 239 |
|
| 240 |
/* |
| 241 |
* update the instance for each variable to be sent in the trap |
| 242 |
*/ |
| 243 |
if_index_oid[10] = if_index; |
| 244 |
if_admin_status_oid[10] = if_index; |
| 245 |
if_oper_status_oid[10] = if_index; |
| 246 |
|
| 247 |
/* |
| 248 |
* add in the trap definition object |
| 249 |
*/ |
| 250 |
snmp_varlist_add_variable(¬ification_vars, |
| 251 |
/* |
| 252 |
* the snmpTrapOID.0 variable |
| 253 |
*/ |
| 254 |
objid_snmptrap, objid_snmptrap_len, |
| 255 |
/* |
| 256 |
* value type is an OID |
| 257 |
*/ |
| 258 |
ASN_OBJECT_ID, |
| 259 |
/* |
| 260 |
* value contents is our notification OID |
| 261 |
*/ |
| 262 |
(u_char *) notification_oid, |
| 263 |
/* |
| 264 |
* size in bytes = oid length * sizeof(oid) |
| 265 |
*/ |
| 266 |
notification_oid_len * sizeof(oid)); |
| 267 |
|
| 268 |
/* |
| 269 |
* add in the additional objects defined as part of the trap |
| 270 |
*/ |
| 271 |
snmp_varlist_add_variable(¬ification_vars, |
| 272 |
if_index_oid, if_index_oid_len, |
| 273 |
ASN_INTEGER, |
| 274 |
(u_char *)&if_index, |
| 275 |
sizeof(if_index)); |
| 276 |
|
| 277 |
/* |
| 278 |
* if we want to insert additional objects, we do it here |
| 279 |
*/ |
| 280 |
snmp_varlist_add_variable(¬ification_vars, |
| 281 |
if_admin_status_oid, if_admin_status_oid_len, |
| 282 |
ASN_INTEGER, |
| 283 |
(u_char *)&if_admin_status, |
| 284 |
sizeof(if_admin_status)); |
| 285 |
|
| 286 |
snmp_varlist_add_variable(¬ification_vars, |
| 287 |
if_oper_status_oid, if_oper_status_oid_len, |
| 288 |
ASN_INTEGER, |
| 289 |
(u_char *)&if_oper_status, |
| 290 |
sizeof(if_oper_status)); |
| 291 |
|
| 292 |
/* |
| 293 |
* send the trap out. This will send it to all registered |
| 294 |
* receivers (see the "SETTING UP TRAP AND/OR INFORM DESTINATIONS" |
| 295 |
* section of the snmpd.conf manual page. |
| 296 |
*/ |
| 297 |
DEBUGMSGTL(("rsys:linkUpDownNotifications", "sending the trap\n")); |
| 298 |
send_v2trap(notification_vars); |
| 299 |
|
| 300 |
/* |
| 301 |
* free the created notification variable list |
| 302 |
*/ |
| 303 |
DEBUGMSGTL(("rsys:linkUpDownNotifications", "cleaning up\n")); |
| 304 |
snmp_free_varbind(notification_vars); |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Update ifTable. |
| 309 |
* @param rowreq_ctx ifTable row to examine. |
| 310 |
* @param cdc Operating system interface information. @cdc->current [in] is the |
| 311 |
* result of the latest interface scan. @cdc->deleted [out] are the rows that |
| 312 |
* should be deleted by the caller from ifTable. |
| 313 |
*/ |
| 314 |
static void |
| 315 |
_check_interface_entry_for_updates(ifTable_rowreq_ctx *rowreq_ctx, |
| 316 |
struct cd_container *cdc) |
| 317 |
{ |
| 318 |
char oper_changed = 0; |
| 319 |
int lastchanged = rowreq_ctx->data.ifLastChange; |
| 320 |
netsnmp_container *ifcontainer = cdc->current; |
| 321 |
|
| 322 |
/* |
| 323 |
* check for matching entry. We can do this directly, since |
| 324 |
* both containers use the same index. |
| 325 |
*/ |
| 326 |
netsnmp_interface_entry *ifentry = CONTAINER_FIND(ifcontainer, rowreq_ctx); |
| 327 |
|
| 328 |
#ifdef USING_IP_MIB_IPV4INTERFACETABLE_IPV4INTERFACETABLE_MODULE |
| 329 |
/* |
| 330 |
* give ipv4If table a crack at the entry |
| 331 |
*/ |
| 332 |
ipv4InterfaceTable_check_entry_for_updates(rowreq_ctx, ifentry); |
| 333 |
#endif |
| 334 |
|
| 335 |
#ifdef USING_IP_MIB_IPV6INTERFACETABLE_IPV6INTERFACETABLE_MODULE |
| 336 |
/* |
| 337 |
* give ipv6If table a crack at the entry |
| 338 |
*/ |
| 339 |
ipv6InterfaceTable_check_entry_for_updates(rowreq_ctx, ifentry); |
| 340 |
#endif |
| 341 |
|
| 342 |
if (NULL == ifentry) { |
| 343 |
/* |
| 344 |
* if this is the first time we detected that this interface is |
| 345 |
* missing, set admin/oper status down, and set last change. |
| 346 |
* |
| 347 |
* yyy-rks: when, if ever, would we consider an entry |
| 348 |
* deleted (and thus need to update ifTableLastChanged)? |
| 349 |
*/ |
| 350 |
if (!rowreq_ctx->known_missing) { |
| 351 |
rowreq_ctx->known_missing = 1; |
| 352 |
DEBUGMSGTL(("ifTable:access", "updating missing entry %s\n",rowreq_ctx->data.ifName)); |
| 353 |
rowreq_ctx->data.ifAdminStatus = IFADMINSTATUS_DOWN; |
| 354 |
rowreq_ctx->data.ifOperStatus = IFOPERSTATUS_DOWN; |
| 355 |
oper_changed = 1; |
| 356 |
} |
| 357 |
if (rowreq_ctx->known_missing) { |
| 358 |
time_t now = netsnmp_get_agent_uptime(); |
| 359 |
u_long diff = (now - rowreq_ctx->data.ifLastChange) / 100; |
| 360 |
DEBUGMSGTL(("verbose:ifTable:access", "missing entry for %ld seconds\n", diff)); |
| 361 |
if (diff >= fadeout) { |
| 362 |
DEBUGMSGTL(("ifTable:access", "marking missing entry %s for " |
| 363 |
"removal after %d seconds\n", rowreq_ctx->data.ifName, |
| 364 |
fadeout)); |
| 365 |
if (NULL == cdc->deleted) |
| 366 |
cdc->deleted = netsnmp_container_find("ifTable_deleted:linked_list"); |
| 367 |
if (NULL == cdc->deleted) |
| 368 |
snmp_log(LOG_ERR, "couldn't create container for deleted interface\n"); |
| 369 |
else { |
| 370 |
CONTAINER_INSERT(cdc->deleted, rowreq_ctx); |
| 371 |
} |
| 372 |
} |
| 373 |
} |
| 374 |
} else { |
| 375 |
DEBUGMSGTL(("ifTable:access", "updating existing entry %s\n", |
| 376 |
rowreq_ctx->data.ifName)); |
| 377 |
|
| 378 |
#ifdef USING_IF_MIB_IFXTABLE_IFXTABLE_MODULE |
| 379 |
{ |
| 380 |
int rc = strcmp(rowreq_ctx->data.ifName, |
| 381 |
ifentry->name); |
| 382 |
if (rc != 0) { |
| 383 |
NETSNMP_LOGONCE((LOG_ERR, "Name of an interface changed. Such " \ |
| 384 |
"interfaces will keep its old name in IF-MIB.\n")); |
| 385 |
DEBUGMSGTL(("ifTable:access", "interface %s changed name to %s, ignoring\n", |
| 386 |
rowreq_ctx->data.ifName, ifentry->name)); |
| 387 |
} |
| 388 |
} |
| 389 |
#endif |
| 390 |
/* |
| 391 |
* if the interface was missing, but came back, clear the |
| 392 |
* missing flag and set the discontinuity time. (if an os keeps |
| 393 |
* persistent counters, tough cookies. We'll cross that |
| 394 |
* bridge if we come to it). |
| 395 |
*/ |
| 396 |
if (rowreq_ctx->known_missing) { |
| 397 |
rowreq_ctx->known_missing = 0; |
| 398 |
#ifdef USING_IF_MIB_IFXTABLE_IFXTABLE_MODULE |
| 399 |
rowreq_ctx->data.ifCounterDiscontinuityTime = |
| 400 |
netsnmp_get_agent_uptime(); |
| 401 |
#endif |
| 402 |
} |
| 403 |
|
| 404 |
/* |
| 405 |
* Check for changes, then update |
| 406 |
*/ |
| 407 |
if ((!(ifentry->ns_flags & NETSNMP_INTERFACE_FLAGS_HAS_LASTCHANGE)) |
| 408 |
&& (rowreq_ctx->data.ifOperStatus != ifentry->oper_status)) |
| 409 |
oper_changed = 1; |
| 410 |
netsnmp_access_interface_entry_copy(rowreq_ctx->data.ifentry, |
| 411 |
ifentry); |
| 412 |
|
| 413 |
/* |
| 414 |
* remove entry from temporary ifcontainer |
| 415 |
*/ |
| 416 |
CONTAINER_REMOVE(ifcontainer, ifentry); |
| 417 |
netsnmp_access_interface_entry_free(ifentry); |
| 418 |
} |
| 419 |
|
| 420 |
/* |
| 421 |
* if ifOperStatus changed, update ifLastChange |
| 422 |
*/ |
| 423 |
if (oper_changed) { |
| 424 |
rowreq_ctx->data.ifLastChange = netsnmp_get_agent_uptime(); |
| 425 |
#ifdef USING_IF_MIB_IFXTABLE_IFXTABLE_MODULE |
| 426 |
if (rowreq_ctx->data.ifLinkUpDownTrapEnable == 1) { |
| 427 |
if (rowreq_ctx->data.ifOperStatus == IFOPERSTATUS_UP) { |
| 428 |
oid notification_oid[] = { 1, 3, 6, 1, 6, 3, 1, 1, 5, 4 }; |
| 429 |
send_linkUpDownNotifications(notification_oid, OID_LENGTH(notification_oid), |
| 430 |
rowreq_ctx->tbl_idx.ifIndex, |
| 431 |
rowreq_ctx->data.ifAdminStatus, |
| 432 |
rowreq_ctx->data.ifOperStatus); |
| 433 |
} else if (rowreq_ctx->data.ifOperStatus == IFOPERSTATUS_DOWN) { |
| 434 |
oid notification_oid[] = { 1, 3, 6, 1, 6, 3, 1, 1, 5, 3 }; |
| 435 |
send_linkUpDownNotifications(notification_oid, OID_LENGTH(notification_oid), |
| 436 |
rowreq_ctx->tbl_idx.ifIndex, |
| 437 |
rowreq_ctx->data.ifAdminStatus, |
| 438 |
rowreq_ctx->data.ifOperStatus); |
| 439 |
} |
| 440 |
} |
| 441 |
#endif |
| 442 |
} else { |
| 443 |
rowreq_ctx->data.ifLastChange = lastchanged; |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
static void __check_interface_entry_for_updates(void *rowreq_ctx, void *cdc) |
| 448 |
{ |
| 449 |
_check_interface_entry_for_updates(rowreq_ctx, cdc); |
| 450 |
} |
| 451 |
|
| 452 |
/** |
| 453 |
* Remove all old interfaces with the same name as the newly added one. |
| 454 |
*/ |
| 455 |
static void |
| 456 |
_check_and_replace_old(netsnmp_interface_entry *ifentry, |
| 457 |
netsnmp_container *container) |
| 458 |
{ |
| 459 |
netsnmp_iterator *it; |
| 460 |
ifTable_rowreq_ctx * rowreq_ctx; |
| 461 |
netsnmp_container *to_delete; |
| 462 |
|
| 463 |
to_delete = netsnmp_container_find("ifTable_deleted:linked_list"); |
| 464 |
if (NULL == to_delete) { |
| 465 |
snmp_log(LOG_ERR, "couldn't create container for deleted interface\n"); |
| 466 |
return; |
| 467 |
} |
| 468 |
|
| 469 |
it = CONTAINER_ITERATOR(container); |
| 470 |
for (rowreq_ctx = ITERATOR_FIRST(it); rowreq_ctx; rowreq_ctx = ITERATOR_NEXT(it)) { |
| 471 |
if (strcmp(ifentry->name, rowreq_ctx->data.ifentry->name) == 0) { |
| 472 |
DEBUGMSGTL(("ifTable:access", |
| 473 |
"removing interface %ld due to new %s\n", |
| 474 |
(long) rowreq_ctx->data.ifentry->index, ifentry->name)); |
| 475 |
CONTAINER_INSERT(to_delete, rowreq_ctx); |
| 476 |
} |
| 477 |
} |
| 478 |
ITERATOR_RELEASE(it); |
| 479 |
|
| 480 |
CONTAINER_FOR_EACH(to_delete, __delete_missing_interface, container); |
| 481 |
CONTAINER_FREE(to_delete); |
| 482 |
} |
| 483 |
|
| 484 |
/** |
| 485 |
* add new entry |
| 486 |
*/ |
| 487 |
static void |
| 488 |
_add_new_interface(netsnmp_interface_entry *ifentry, |
| 489 |
netsnmp_container *container) |
| 490 |
{ |
| 491 |
ifTable_rowreq_ctx *rowreq_ctx; |
| 492 |
int rc; |
| 493 |
|
| 494 |
DEBUGMSGTL(("ifTable:access", "creating new entry\n")); |
| 495 |
|
| 496 |
/* |
| 497 |
* allocate an row context and set the index(es), then add it to |
| 498 |
* the container and set ifTableLastChanged. |
| 499 |
*/ |
| 500 |
rowreq_ctx = ifTable_allocate_rowreq_ctx(ifentry); |
| 501 |
if (!rowreq_ctx) { |
| 502 |
snmp_log(LOG_ERR, |
| 503 |
"memory allocation failed while loading ifTable cache.\n"); |
| 504 |
netsnmp_access_interface_entry_free(ifentry); |
| 505 |
return; |
| 506 |
} |
| 507 |
if (ifTable_indexes_set(rowreq_ctx, ifentry->index) != MFD_SUCCESS) { |
| 508 |
snmp_log(LOG_ERR, "error setting index while loading ifTable cache.\n"); |
| 509 |
ifTable_release_rowreq_ctx(rowreq_ctx); |
| 510 |
return; |
| 511 |
} |
| 512 |
|
| 513 |
if (replace_old) |
| 514 |
_check_and_replace_old(ifentry, container); |
| 515 |
|
| 516 |
rc = CONTAINER_INSERT(container, rowreq_ctx); |
| 517 |
netsnmp_assert(rc == 0); |
| 518 |
if (0 == _first_load) { |
| 519 |
rowreq_ctx->data.ifLastChange = netsnmp_get_agent_uptime(); |
| 520 |
ifTable_lastChange_set(rowreq_ctx->data.ifLastChange); |
| 521 |
} |
| 522 |
#ifdef USING_IP_MIB_IPV4INTERFACETABLE_IPV4INTERFACETABLE_MODULE |
| 523 |
/* |
| 524 |
* give ipv4If table a crack at the entry |
| 525 |
*/ |
| 526 |
ipv4InterfaceTable_check_entry_for_updates(rowreq_ctx, ifentry); |
| 527 |
#endif |
| 528 |
#ifdef USING_IP_MIB_IPV6INTERFACETABLE_IPV6INTERFACETABLE_MODULE |
| 529 |
/* |
| 530 |
* give ipv6If table a crack at the entry |
| 531 |
*/ |
| 532 |
ipv6InterfaceTable_check_entry_for_updates(rowreq_ctx, ifentry); |
| 533 |
#endif |
| 534 |
} |
| 535 |
|
| 536 |
static void __add_new_interface(void *ifentry, void *container) |
| 537 |
{ |
| 538 |
_add_new_interface(ifentry, container); |
| 539 |
} |
| 540 |
|
| 541 |
/** |
| 542 |
* add new entry |
| 543 |
*/ |
| 544 |
static void |
| 545 |
_delete_missing_interface(ifTable_rowreq_ctx *rowreq_ctx, |
| 546 |
netsnmp_container *container) |
| 547 |
{ |
| 548 |
DEBUGMSGTL(("ifTable:access", "removing missing entry %s\n", |
| 549 |
rowreq_ctx->data.ifName)); |
| 550 |
|
| 551 |
CONTAINER_REMOVE(container, rowreq_ctx); |
| 552 |
|
| 553 |
ifTable_release_rowreq_ctx(rowreq_ctx); |
| 554 |
} |
| 555 |
|
| 556 |
static void __delete_missing_interface(void *rowreq_ctx, void *container) |
| 557 |
{ |
| 558 |
_delete_missing_interface(rowreq_ctx, container); |
| 559 |
} |
| 560 |
|
| 561 |
/** |
| 562 |
* container shutdown |
| 563 |
* |
| 564 |
* @param container_ptr A pointer to the container. |
| 565 |
* |
| 566 |
* This function is called at shutdown to allow you to customize certain |
| 567 |
* aspects of the access method. For the most part, it is for advanced |
| 568 |
* users. The default code should suffice for most cases. |
| 569 |
* |
| 570 |
* This function is called before ifTable_container_free(). |
| 571 |
* |
| 572 |
* @remark |
| 573 |
* This would also be a good place to do any cleanup needed |
| 574 |
* for you data source. For example, closing a connection to another |
| 575 |
* process that supplied the data, closing a database, etc. |
| 576 |
*/ |
| 577 |
void |
| 578 |
ifTable_container_shutdown(netsnmp_container *container_ptr) |
| 579 |
{ |
| 580 |
DEBUGMSGTL(("verbose:ifTable:ifTable_container_shutdown", "called\n")); |
| 581 |
|
| 582 |
if (NULL == container_ptr) { |
| 583 |
snmp_log(LOG_ERR, "bad params to ifTable_container_shutdown\n"); |
| 584 |
return; |
| 585 |
} |
| 586 |
|
| 587 |
} /* ifTable_container_shutdown */ |
| 588 |
|
| 589 |
/** |
| 590 |
* Query the list of interfaces from the operating system and update ifTable. |
| 591 |
* |
| 592 |
* @param container container to which items should be inserted |
| 593 |
* |
| 594 |
* @retval MFD_SUCCESS : success. |
| 595 |
* @retval MFD_RESOURCE_UNAVAILABLE : Can't access data source |
| 596 |
* @retval MFD_ERROR : other error. |
| 597 |
* |
| 598 |
* This function is called to load the index(es) (and data, optionally) |
| 599 |
* for the every row in the data set. |
| 600 |
* |
| 601 |
* @remark |
| 602 |
* While loading the data, the only important thing is the indexes. |
| 603 |
* If access to your data is cheap/fast (e.g. you have a pointer to a |
| 604 |
* structure in memory), it would make sense to update the data here. |
| 605 |
* If, however, the accessing the data involves more work (e.g. parsing |
| 606 |
* some other existing data, or performing calculations to derive the data), |
| 607 |
* then you can limit yourself to setting the indexes and saving any |
| 608 |
* information you will need later. Then use the saved information in |
| 609 |
* ifTable_row_prep() for populating data. |
| 610 |
* |
| 611 |
* @note |
| 612 |
* If you need consistency between rows (like you want statistics |
| 613 |
* for each row to be from the same time frame), you should set all |
| 614 |
* data here. |
| 615 |
* |
| 616 |
*/ |
| 617 |
int |
| 618 |
ifTable_container_load(netsnmp_container *container) |
| 619 |
{ |
| 620 |
struct cd_container cdc; |
| 621 |
|
| 622 |
DEBUGMSGTL(("verbose:ifTable:ifTable_container_load", "called\n")); |
| 623 |
|
| 624 |
/* |
| 625 |
* TODO:351:M: |-> Load/update data in the ifTable container. |
| 626 |
* loop over your ifTable data, allocate a rowreq context, |
| 627 |
* set the index(es) [and data, optionally] and insert into |
| 628 |
* the container. |
| 629 |
*/ |
| 630 |
/* |
| 631 |
* ifTable gets its data from the netsnmp_interface API. |
| 632 |
*/ |
| 633 |
cdc.current = |
| 634 |
netsnmp_access_interface_container_load(NULL, |
| 635 |
NETSNMP_ACCESS_INTERFACE_INIT_NOFLAGS); |
| 636 |
if (NULL == cdc.current) |
| 637 |
return MFD_RESOURCE_UNAVAILABLE; /* msg already logged */ |
| 638 |
|
| 639 |
cdc.deleted = NULL; /* created as needed */ |
| 640 |
|
| 641 |
/* |
| 642 |
* we just got a fresh copy of interface data. compare it to |
| 643 |
* what we've already got, and make any adjustements... |
| 644 |
*/ |
| 645 |
CONTAINER_FOR_EACH(container, __check_interface_entry_for_updates, &cdc); |
| 646 |
|
| 647 |
/* |
| 648 |
* now remove any missing interfaces |
| 649 |
*/ |
| 650 |
if (NULL != cdc.deleted) { |
| 651 |
CONTAINER_FOR_EACH(cdc.deleted, __delete_missing_interface, container); |
| 652 |
CONTAINER_FREE(cdc.deleted); |
| 653 |
} |
| 654 |
|
| 655 |
/* |
| 656 |
* now add any new interfaces |
| 657 |
*/ |
| 658 |
CONTAINER_FOR_EACH(cdc.current, __add_new_interface, container); |
| 659 |
|
| 660 |
/* |
| 661 |
* free the container. we've either claimed each ifentry, or released it, |
| 662 |
* so the dal function doesn't need to clear the container. |
| 663 |
*/ |
| 664 |
netsnmp_access_interface_container_free(cdc.current, |
| 665 |
NETSNMP_ACCESS_INTERFACE_FREE_DONT_CLEAR); |
| 666 |
|
| 667 |
DEBUGMSGT(("verbose:ifTable:ifTable_cache_load", |
| 668 |
"%lu records\n", (unsigned long)CONTAINER_SIZE(container))); |
| 669 |
|
| 670 |
if (_first_load) |
| 671 |
_first_load = 0; |
| 672 |
|
| 673 |
return MFD_SUCCESS; |
| 674 |
} /* ifTable_container_load */ |
| 675 |
|
| 676 |
/** |
| 677 |
* container clean up |
| 678 |
* |
| 679 |
* @param container container with all current items |
| 680 |
* |
| 681 |
* This optional callback is called prior to all |
| 682 |
* item's being removed from the container. If you |
| 683 |
* need to do any processing before that, do it here. |
| 684 |
* |
| 685 |
* @note |
| 686 |
* The MFD helper will take care of releasing all the row contexts. |
| 687 |
* |
| 688 |
*/ |
| 689 |
void |
| 690 |
ifTable_container_free(netsnmp_container *container) |
| 691 |
{ |
| 692 |
DEBUGMSGTL(("verbose:ifTable:ifTable_container_free", "called\n")); |
| 693 |
|
| 694 |
/* |
| 695 |
* TODO:380:M: Free ifTable container data. |
| 696 |
*/ |
| 697 |
} /* ifTable_container_free */ |
| 698 |
|
| 699 |
/** |
| 700 |
* prepare row for processing. |
| 701 |
* |
| 702 |
* When the agent has located the row for a request, this function is |
| 703 |
* called to prepare the row for processing. If you fully populated |
| 704 |
* the data context during the index setup phase, you may not need to |
| 705 |
* do anything. |
| 706 |
* |
| 707 |
* @param rowreq_ctx pointer to a context. |
| 708 |
* |
| 709 |
* @retval MFD_SUCCESS : success. |
| 710 |
* @retval MFD_ERROR : other error. |
| 711 |
*/ |
| 712 |
int |
| 713 |
ifTable_row_prep(ifTable_rowreq_ctx * rowreq_ctx) |
| 714 |
{ |
| 715 |
DEBUGMSGTL(("verbose:ifTable:ifTable_row_prep", "called\n")); |
| 716 |
|
| 717 |
netsnmp_assert(NULL != rowreq_ctx); |
| 718 |
|
| 719 |
/* |
| 720 |
* TODO:390:o: Prepare row for request. |
| 721 |
* If populating row data was delayed, this is the place to |
| 722 |
* fill in the row for this request. |
| 723 |
*/ |
| 724 |
|
| 725 |
return MFD_SUCCESS; |
| 726 |
} /* ifTable_row_prep */ |
| 727 |
|
| 728 |
/** @} */ |