| 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 |
#ifdef HAVE_UNISTD_H |
| 15 |
#include <unistd.h> |
| 16 |
#endif |
| 17 |
|
| 18 |
/* |
| 19 |
* include our parent header |
| 20 |
*/ |
| 21 |
#include "etherStatsTable.h" |
| 22 |
#include "etherStatsTable_data_access.h" |
| 23 |
|
| 24 |
#if defined(linux) |
| 25 |
#include "ioctl_imp_common.h" |
| 26 |
#endif |
| 27 |
|
| 28 |
/** @ingroup interface |
| 29 |
* @addtogroup data_access data_access: Routines to access data |
| 30 |
* |
| 31 |
* These routines are used to locate the data used to satisfy |
| 32 |
* requests. |
| 33 |
* |
| 34 |
* @{ |
| 35 |
*/ |
| 36 |
/********************************************************************** |
| 37 |
********************************************************************** |
| 38 |
*** |
| 39 |
*** Table etherStatsTable |
| 40 |
*** |
| 41 |
********************************************************************** |
| 42 |
**********************************************************************/ |
| 43 |
/* |
| 44 |
* RMON-MIB::etherStatsTable is subid 1 of statistics. |
| 45 |
* Its status is Current. |
| 46 |
* OID: .1.3.6.1.2.1.16.1.1, length: 9 |
| 47 |
*/ |
| 48 |
|
| 49 |
/** |
| 50 |
* initialization for etherStatsTable data access |
| 51 |
* |
| 52 |
* This function is called during startup to allow you to |
| 53 |
* allocate any resources you need for the data table. |
| 54 |
* |
| 55 |
* @param etherStatsTable_reg |
| 56 |
* Pointer to etherStatsTable_registration |
| 57 |
* |
| 58 |
* @retval MFD_SUCCESS : success. |
| 59 |
* @retval MFD_ERROR : unrecoverable error. |
| 60 |
*/ |
| 61 |
int |
| 62 |
etherStatsTable_init_data(etherStatsTable_registration * |
| 63 |
etherStatsTable_reg) |
| 64 |
{ |
| 65 |
DEBUGMSGTL(("verbose:etherStatsTable:etherStatsTable_init_data", |
| 66 |
"called\n")); |
| 67 |
|
| 68 |
/* |
| 69 |
* TODO:303:o: Initialize etherStatsTable data. |
| 70 |
*/ |
| 71 |
|
| 72 |
return MFD_SUCCESS; |
| 73 |
} /* etherStatsTable_init_data */ |
| 74 |
|
| 75 |
/** |
| 76 |
* container overview |
| 77 |
* |
| 78 |
*/ |
| 79 |
|
| 80 |
/** |
| 81 |
* container initialization |
| 82 |
* |
| 83 |
* @param container_ptr_ptr A pointer to a container pointer. If you |
| 84 |
* create a custom container, use this parameter to return it |
| 85 |
* to the MFD helper. If set to NULL, the MFD helper will |
| 86 |
* allocate a container for you. |
| 87 |
* @param cache A pointer to a cache structure. You can set the timeout |
| 88 |
* and other cache flags using this pointer. |
| 89 |
* |
| 90 |
* This function is called at startup to allow you to customize certain |
| 91 |
* aspects of the access method. For the most part, it is for advanced |
| 92 |
* users. The default code should suffice for most cases. If no custom |
| 93 |
* container is allocated, the MFD code will create one for your. |
| 94 |
* |
| 95 |
* This is also the place to set up cache behavior. The default, to |
| 96 |
* simply set the cache timeout, will work well with the default |
| 97 |
* container. If you are using a custom container, you may want to |
| 98 |
* look at the cache helper documentation to see if there are any |
| 99 |
* flags you want to set. |
| 100 |
* |
| 101 |
* @remark |
| 102 |
* This would also be a good place to do any initialization needed |
| 103 |
* for you data source. For example, opening a connection to another |
| 104 |
* process that will supply the data, opening a database, etc. |
| 105 |
*/ |
| 106 |
void |
| 107 |
etherStatsTable_container_init(netsnmp_container ** container_ptr_ptr, |
| 108 |
netsnmp_cache * cache) |
| 109 |
{ |
| 110 |
DEBUGMSGTL(("verbose:etherStatsTable:etherStatsTable_container_init", |
| 111 |
"called\n")); |
| 112 |
|
| 113 |
if (NULL == container_ptr_ptr) { |
| 114 |
snmp_log(LOG_ERR, |
| 115 |
"bad container param to etherStatsTable_container_init\n"); |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
/* |
| 120 |
* For advanced users, you can use a custom container. If you |
| 121 |
* do not create one, one will be created for you. |
| 122 |
*/ |
| 123 |
*container_ptr_ptr = NULL; |
| 124 |
|
| 125 |
if (NULL == cache) { |
| 126 |
snmp_log(LOG_ERR, |
| 127 |
"bad cache param to etherStatsTable_container_init\n"); |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
/* |
| 132 |
* TODO:345:A: Set up etherStatsTable cache properties. |
| 133 |
* |
| 134 |
* Also for advanced users, you can set parameters for the |
| 135 |
* cache. Do not change the magic pointer, as it is used |
| 136 |
* by the MFD helper. To completely disable caching, set |
| 137 |
* cache->enabled to 0. |
| 138 |
*/ |
| 139 |
cache->timeout = ETHERSTATSTABLE_CACHE_TIMEOUT; /* seconds */ |
| 140 |
} /* etherStatsTable_container_init */ |
| 141 |
|
| 142 |
/** |
| 143 |
* container shutdown |
| 144 |
* |
| 145 |
* @param container_ptr A pointer to the container. |
| 146 |
* |
| 147 |
* This function is called at shutdown to allow you to customize certain |
| 148 |
* aspects of the access method. For the most part, it is for advanced |
| 149 |
* users. The default code should suffice for most cases. |
| 150 |
* |
| 151 |
* This function is called before etherStatsTable_container_free(). |
| 152 |
* |
| 153 |
* @remark |
| 154 |
* This would also be a good place to do any cleanup needed |
| 155 |
* for you data source. For example, closing a connection to another |
| 156 |
* process that supplied the data, closing a database, etc. |
| 157 |
*/ |
| 158 |
void |
| 159 |
etherStatsTable_container_shutdown(netsnmp_container * container_ptr) |
| 160 |
{ |
| 161 |
DEBUGMSGTL(("verbose:etherStatsTable:etherStatsTable_container_shutdown", "called\n")); |
| 162 |
|
| 163 |
if (NULL == container_ptr) { |
| 164 |
snmp_log(LOG_ERR, |
| 165 |
"bad params to etherStatsTable_container_shutdown\n"); |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
} /* etherStatsTable_container_shutdown */ |
| 170 |
|
| 171 |
/** |
| 172 |
* load initial data |
| 173 |
* |
| 174 |
* TODO:350:M: Implement etherStatsTable data load |
| 175 |
* This function will also be called by the cache helper to load |
| 176 |
* the container again (after the container free function has been |
| 177 |
* called to free the previous contents). |
| 178 |
* |
| 179 |
* @param container container to which items should be inserted |
| 180 |
* |
| 181 |
* @retval MFD_SUCCESS : success. |
| 182 |
* @retval MFD_RESOURCE_UNAVAILABLE : Can't access data source |
| 183 |
* @retval MFD_ERROR : other error. |
| 184 |
* |
| 185 |
* This function is called to load the index(es) (and data, optionally) |
| 186 |
* for the every row in the data set. |
| 187 |
* |
| 188 |
* @remark |
| 189 |
* While loading the data, the only important thing is the indexes. |
| 190 |
* If access to your data is cheap/fast (e.g. you have a pointer to a |
| 191 |
* structure in memory), it would make sense to update the data here. |
| 192 |
* If, however, the accessing the data involves more work (e.g. parsing |
| 193 |
* some other existing data, or performing calculations to derive the data), |
| 194 |
* then you can limit yourself to setting the indexes and saving any |
| 195 |
* information you will need later. Then use the saved information in |
| 196 |
* etherStatsTable_row_prep() for populating data. |
| 197 |
* |
| 198 |
* @note |
| 199 |
* If you need consistency between rows (like you want statistics |
| 200 |
* for each row to be from the same time frame), you should set all |
| 201 |
* data here. |
| 202 |
* |
| 203 |
*/ |
| 204 |
int |
| 205 |
etherStatsTable_container_load(netsnmp_container * container) |
| 206 |
{ |
| 207 |
size_t count = 0; |
| 208 |
|
| 209 |
/* |
| 210 |
* TODO:352:M: | |-> set indexes in new etherStatsTable rowreq context. |
| 211 |
* data context will be set from the param (unless NULL, |
| 212 |
* in which case a new data context will be allocated) |
| 213 |
*/ |
| 214 |
|
| 215 |
/* |
| 216 |
* temporary storage for index values |
| 217 |
*/ |
| 218 |
|
| 219 |
/* |
| 220 |
* etherStatsIndex(1)/INTEGER32/ASN_INTEGER/long(long)//l/A/w/e/R/d/h |
| 221 |
*/ |
| 222 |
|
| 223 |
int fd; |
| 224 |
|
| 225 |
#if defined(linux) |
| 226 |
struct ifname *list_head = NULL, *p = NULL; |
| 227 |
#endif |
| 228 |
|
| 229 |
DEBUGMSGTL(("verbose:etherStatsTable:etherStatsTable_container_load", |
| 230 |
"called\n")); |
| 231 |
|
| 232 |
|
| 233 |
/* |
| 234 |
* create socket for ioctls |
| 235 |
*/ |
| 236 |
|
| 237 |
fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 238 |
if(fd < 0) { |
| 239 |
snmp_log(LOG_ERR, "could not create socket\n"); |
| 240 |
return -2; |
| 241 |
} |
| 242 |
|
| 243 |
/* |
| 244 |
* get the interface names of the devices present in the system, in case of failure retval suggests the reason for failure |
| 245 |
* and list_head contains null |
| 246 |
*/ |
| 247 |
|
| 248 |
#if defined(linux) |
| 249 |
{ |
| 250 |
int retval; |
| 251 |
|
| 252 |
list_head = etherstats_interface_name_list_get(list_head, &retval); |
| 253 |
} |
| 254 |
|
| 255 |
if (!list_head) { |
| 256 |
snmp_log (LOG_ERR, "access:etherStatsTable, error getting the interface names present in the system\n"); |
| 257 |
DEBUGMSGTL(("access:etherStatsTable", "error getting the interface names present in the system")); |
| 258 |
close(fd); |
| 259 |
return MFD_ERROR; |
| 260 |
} |
| 261 |
|
| 262 |
/* |
| 263 |
* Walk over the list of interface names present in the system and retrieve the statistics |
| 264 |
*/ |
| 265 |
|
| 266 |
for (p = list_head; p; p = p->ifn_next) { |
| 267 |
long etherStatsIndex; |
| 268 |
etherStatsTable_rowreq_ctx *rowreq_ctx; |
| 269 |
|
| 270 |
DEBUGMSGTL(("access:etherStatsTable", "processing '%s'\n", p->name)); |
| 271 |
|
| 272 |
/* |
| 273 |
* get index via ioctl. |
| 274 |
*/ |
| 275 |
|
| 276 |
etherStatsIndex = (long) etherstats_interface_ioctl_ifindex_get(-1, p->name); |
| 277 |
|
| 278 |
/* |
| 279 |
* get the etherstats contents populated, if the device is not an ethernet device |
| 280 |
* the operation will not be supported and an error message will be logged |
| 281 |
*/ |
| 282 |
|
| 283 |
rowreq_ctx = etherStatsTable_allocate_rowreq_ctx(NULL); |
| 284 |
if (NULL == rowreq_ctx) { |
| 285 |
snmp_log(LOG_ERR, "memory allocation failed\n"); |
| 286 |
close(fd); |
| 287 |
etherstats_interface_name_list_free(list_head); |
| 288 |
return MFD_RESOURCE_UNAVAILABLE; |
| 289 |
} |
| 290 |
|
| 291 |
if (MFD_SUCCESS != |
| 292 |
etherStatsTable_indexes_set(rowreq_ctx, etherStatsIndex)) { |
| 293 |
snmp_log(LOG_ERR, |
| 294 |
"error setting index while loading " |
| 295 |
"etherStatsTable data.\n"); |
| 296 |
etherStatsTable_release_rowreq_ctx(rowreq_ctx); |
| 297 |
continue; |
| 298 |
} |
| 299 |
|
| 300 |
/* |
| 301 |
* TODO:352:r: | |-> populate etherStatsTable data context. |
| 302 |
* Populate data context here. (optionally, delay until row prep) |
| 303 |
*/ |
| 304 |
/* |
| 305 |
* non-TRANSIENT data: no need to copy. set pointer to data |
| 306 |
*/ |
| 307 |
|
| 308 |
memset (&rowreq_ctx->data, 0, sizeof (rowreq_ctx->data)); |
| 309 |
{ |
| 310 |
int rc; |
| 311 |
|
| 312 |
rc = interface_ioctl_etherstats_get (rowreq_ctx, fd, p->name); |
| 313 |
|
| 314 |
if (rc < 0) { |
| 315 |
DEBUGMSGTL(("access:etherStatsTable", "error getting the statistics for interface |%s| " |
| 316 |
"etherStatsTable data, operation might not be supported\n", p->name)); |
| 317 |
etherStatsTable_release_rowreq_ctx(rowreq_ctx); |
| 318 |
continue; |
| 319 |
} |
| 320 |
|
| 321 |
/* |
| 322 |
* insert into table container |
| 323 |
*/ |
| 324 |
rc = CONTAINER_INSERT(container, rowreq_ctx); |
| 325 |
if (rc < 0) { |
| 326 |
DEBUGMSGTL(("access:etherStatsTable", "error inserting |%s| ", p->name)); |
| 327 |
etherStatsTable_release_rowreq_ctx(rowreq_ctx); |
| 328 |
continue; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
++count; |
| 333 |
} |
| 334 |
|
| 335 |
close(fd); |
| 336 |
|
| 337 |
/* |
| 338 |
* free the interface names list |
| 339 |
*/ |
| 340 |
|
| 341 |
if ( (etherstats_interface_name_list_free(list_head)) < 0) { |
| 342 |
snmp_log(LOG_ERR, "access:etherStatsTable, error freeing the interface name list \n"); |
| 343 |
DEBUGMSGTL(("access:etherStatsTable", "error freeing the interface name list\n")); |
| 344 |
return MFD_ERROR; |
| 345 |
} |
| 346 |
#endif |
| 347 |
|
| 348 |
DEBUGMSGT(("verbose:etherStatsTable:etherStatsTable_container_load", |
| 349 |
"inserted %" NETSNMP_PRIz "d records\n", count)); |
| 350 |
|
| 351 |
return MFD_SUCCESS; |
| 352 |
} |
| 353 |
/* etherStatsTable_container_load */ |
| 354 |
|
| 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 |
etherStatsTable_container_free(netsnmp_container * container) |
| 371 |
{ |
| 372 |
DEBUGMSGTL(("verbose:etherStatsTable:etherStatsTable_container_free", |
| 373 |
"called\n")); |
| 374 |
|
| 375 |
/* |
| 376 |
* TODO:380:M: Free etherStatsTable container data. |
| 377 |
*/ |
| 378 |
} /* etherStatsTable_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 |
etherStatsTable_row_prep(etherStatsTable_rowreq_ctx * rowreq_ctx) |
| 395 |
{ |
| 396 |
DEBUGMSGTL(("verbose:etherStatsTable:etherStatsTable_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 |
} /* etherStatsTable_row_prep */ |
| 409 |
|
| 410 |
/* |
| 411 |
* TODO:420:r: Implement etherStatsTable index validation. |
| 412 |
*/ |
| 413 |
/*--------------------------------------------------------------------- |
| 414 |
* RMON-MIB::etherStatsEntry.etherStatsIndex |
| 415 |
* etherStatsIndex is subid 1 of etherStatsEntry. |
| 416 |
* Its status is Current, and its access level is ReadOnly. |
| 417 |
* OID: .1.3.6.1.2.1.16.1.1.1.1 |
| 418 |
* Description: |
| 419 |
The value of this object uniquely identifies this |
| 420 |
etherStats entry. |
| 421 |
* |
| 422 |
* Attributes: |
| 423 |
* accessible 1 isscalar 0 enums 0 hasdefval 0 |
| 424 |
* readable 1 iscolumn 1 ranges 1 hashint 0 |
| 425 |
* settable 0 |
| 426 |
* |
| 427 |
* Ranges: 1 - 65535; |
| 428 |
* |
| 429 |
* Its syntax is INTEGER32 (based on perltype INTEGER32) |
| 430 |
* The net-snmp type is ASN_INTEGER. The C type decl is long (long) |
| 431 |
*/ |
| 432 |
/** |
| 433 |
* check validity of etherStatsIndex index portion |
| 434 |
* |
| 435 |
* @retval MFD_SUCCESS : the incoming value is legal |
| 436 |
* @retval MFD_ERROR : the incoming value is NOT legal |
| 437 |
* |
| 438 |
* @note this is not the place to do any checks for the sanity |
| 439 |
* of multiple indexes. Those types of checks should be done in the |
| 440 |
* etherStatsTable_validate_index() function. |
| 441 |
* |
| 442 |
* @note Also keep in mind that if the index refers to a row in this or |
| 443 |
* some other table, you can't check for that row here to make |
| 444 |
* decisions, since that row might not be created yet, but may |
| 445 |
* be created during the processing this request. If you have |
| 446 |
* such checks, they should be done in the check_dependencies |
| 447 |
* function, because any new/deleted/changed rows should be |
| 448 |
* available then. |
| 449 |
* |
| 450 |
* The following checks have already been done for you: |
| 451 |
* The value is in (one of) the range set(s): 1 - 65535 |
| 452 |
* |
| 453 |
* If there a no other checks you need to do, simply return MFD_SUCCESS. |
| 454 |
*/ |
| 455 |
int |
| 456 |
etherStatsIndex_check_index(etherStatsTable_rowreq_ctx * rowreq_ctx) |
| 457 |
{ |
| 458 |
DEBUGMSGTL(("verbose:etherStatsTable:etherStatsIndex_check_index", |
| 459 |
"called\n")); |
| 460 |
|
| 461 |
netsnmp_assert(NULL != rowreq_ctx); |
| 462 |
|
| 463 |
/* |
| 464 |
* TODO:426:M: |-> Check etherStatsTable index etherStatsIndex. |
| 465 |
* check that index value in the table context is legal. |
| 466 |
* (rowreq_ctx->tbl_index.etherStatsIndex) |
| 467 |
*/ |
| 468 |
|
| 469 |
return MFD_SUCCESS; /* etherStatsIndex index ok */ |
| 470 |
} /* etherStatsIndex_check_index */ |
| 471 |
|
| 472 |
/** |
| 473 |
* verify specified index is valid. |
| 474 |
* |
| 475 |
* This check is independent of whether or not the values specified for |
| 476 |
* the columns of the new row are valid. Column values and row consistency |
| 477 |
* will be checked later. At this point, only the index values should be |
| 478 |
* checked. |
| 479 |
* |
| 480 |
* All of the individual index validation functions have been called, so this |
| 481 |
* is the place to make sure they are valid as a whole when combined. If |
| 482 |
* you only have one index, then you probably don't need to do anything else |
| 483 |
* here. |
| 484 |
* |
| 485 |
* @note Keep in mind that if the indexes refer to a row in this or |
| 486 |
* some other table, you can't check for that row here to make |
| 487 |
* decisions, since that row might not be created yet, but may |
| 488 |
* be created during the processing this request. If you have |
| 489 |
* such checks, they should be done in the check_dependencies |
| 490 |
* function, because any new/deleted/changed rows should be |
| 491 |
* available then. |
| 492 |
* |
| 493 |
* |
| 494 |
* @param etherStatsTable_reg |
| 495 |
* Pointer to the user registration data |
| 496 |
* @param etherStatsTable_rowreq_ctx |
| 497 |
* Pointer to the users context. |
| 498 |
* @retval MFD_SUCCESS : success |
| 499 |
* @retval MFD_CANNOT_CREATE_NOW : index not valid right now |
| 500 |
* @retval MFD_CANNOT_CREATE_EVER : index never valid |
| 501 |
*/ |
| 502 |
int |
| 503 |
etherStatsTable_validate_index(etherStatsTable_registration * |
| 504 |
etherStatsTable_reg, |
| 505 |
etherStatsTable_rowreq_ctx * rowreq_ctx) |
| 506 |
{ |
| 507 |
int rc = MFD_SUCCESS; |
| 508 |
|
| 509 |
DEBUGMSGTL(("verbose:etherStatsTable:etherStatsTable_validate_index", |
| 510 |
"called\n")); |
| 511 |
|
| 512 |
/** we should have a non-NULL pointer */ |
| 513 |
netsnmp_assert(NULL != rowreq_ctx); |
| 514 |
|
| 515 |
/* |
| 516 |
* TODO:430:M: |-> Validate potential etherStatsTable index. |
| 517 |
*/ |
| 518 |
if (1) { |
| 519 |
snmp_log(LOG_WARNING, "invalid index for a new row in the " |
| 520 |
"etherStatsTable table.\n"); |
| 521 |
/* |
| 522 |
* determine failure type. |
| 523 |
* |
| 524 |
* If the index could not ever be created, return MFD_NOT_EVER |
| 525 |
* If the index can not be created under the present circumstances |
| 526 |
* (even though it could be created under other circumstances), |
| 527 |
* return MFD_NOT_NOW. |
| 528 |
*/ |
| 529 |
if (0) { |
| 530 |
return MFD_CANNOT_CREATE_EVER; |
| 531 |
} else { |
| 532 |
return MFD_CANNOT_CREATE_NOW; |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
return rc; |
| 537 |
} /* etherStatsTable_validate_index */ |
| 538 |
|
| 539 |
/** @} */ |