📄 iftable.c
字号:
/* * Interface MIB ifTable (and ifXTable) implementation - ifTable.c * */#include <net-snmp/net-snmp-config.h>#include "mibII_common.h"#include <net-snmp/net-snmp-includes.h>#include <net-snmp/agent/net-snmp-agent-includes.h>#include <net-snmp/agent/auto_nlist.h>#include <net-snmp/agent/table_container.h>#include <net-snmp/agent/cache_handler.h>#include "ifTable.h"#include "ifTable_columns.h"#include "ifXTable_columns.h"/* * prototypes for architecture specific routines */void ifTable_free(netsnmp_cache * cache, void *magic);int ifTable_ifentry_info_init(netsnmp_ifentry * entry);unsigned long long get_ifspeed(netsnmp_ifentry * entry);int get_iftype(netsnmp_ifentry * entry);static int ifTable_ifentry_compare_name(const void *lhs, const void *rhs);/* * local statics, or useful utility routines? */netsnmp_ifentry *ifTable_ifentry_get_by_index(netsnmp_cache * cache, int index);netsnmp_ifentry *ifTable_ifentry_get_by_name(netsnmp_cache * cache, char *name, int create); /* * * Initialization and handler routines are common to all architectures * */#ifndef MIB_STATS_CACHE_TIMEOUT#define MIB_STATS_CACHE_TIMEOUT 5#endif#ifndef IF_STATS_CACHE_TIMEOUT#define IF_STATS_CACHE_TIMEOUT MIB_STATS_CACHE_TIMEOUT#endifoid ifTable_oid[] = { SNMP_OID_MIB2, 2, 2 };oid ifXTable_oid[] = { SNMP_OID_MIB2, 31, 1, 1 };voidinit_ifTable(void){ netsnmp_table_registration_info *table_info; netsnmp_handler_registration *reginfo; netsnmp_container *container; netsnmp_cache *cache; DEBUGMSGTL(("mibII/ifTable", "Initialising Interface Table\n")); /* * get ifcontainer */ container = netsnmp_dal_ifcontainer_init(0); /* * Create the table data structure, and define the indexing.... */ table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info); if (!table_info) { return; } netsnmp_table_helper_add_indexes(table_info, ASN_INTEGER, 0); table_info->min_column = COLUMN_IFINDEX; table_info->max_column = COLUMN_IFSPECIFIC; /* * .... and register the table with the agent. */ reginfo = netsnmp_create_handler_registration("ifTable", ifTable_handler, ifTable_oid, OID_LENGTH(ifTable_oid), HANDLER_CAN_RONLY); netsnmp_container_table_register(reginfo, table_info, container1, TABLE_CONTAINER_KEY_NETSNMP_INDEX); /* * .... with a local cache * (except for Solaris, which uses a different approach) */ cache = netsnmp_cache_create(IF_STATS_CACHE_TIMEOUT, ifTable_load, ifTable_free, ifTable_oid, OID_LENGTH(ifTable_oid)); cache->magic = container1; netsnmp_inject_handler(reginfo, netsnmp_cache_handler_get(cache));#define ENTENDED_IF_TABLE#ifdef ENTENDED_IF_TABLE /* * Now do exactly the same thing with the extension table */ DEBUGMSGTL(("mibII/ifTable", "Initialising Interface Extension Table\n")); table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info); if (!table_info) { return; } netsnmp_table_helper_add_indexes(table_info, ASN_INTEGER, 0); table_info->min_column = COLUMN_IFNAME; table_info->max_column = COLUMN_IFCOUNTERDISCONTINUITYTIME; reginfo = netsnmp_create_handler_registration("ifXTable", ifXTable_handler, ifXTable_oid, OID_LENGTH(ifXTable_oid), HANDLER_CAN_RONLY), netsnmp_container_table_register(reginfo, table_info, container1, TABLE_CONTAINER_KEY_NETSNMP_INDEX); netsnmp_inject_handler(reginfo, netsnmp_cache_handler_get(cache));#endif /* ENTENDED_IF_TABLE */}NETSNMP_STATIC_INLINE netsnmp_ifentry *ifTable_ifentry_extract(netsnmp_request_info * request){ return (netsnmp_ifentry *) netsnmp_container_table_extract_context(request);}static intifTable_ifentry_compare_name(const void *lhs, const void *rhs){ return strcmp(((const netsnmp_ifentry *) lhs)->if_name, ((const netsnmp_ifentry *) rhs)->if_name);}static voidifTable_ifentry_release(netsnmp_ifentry * entry, void *context){ if (NULL == entry) return; if (NULL != entry->if_name) free(entry->if_name); if (NULL != entry->if_descr) free(entry->if_descr); free(entry);} /* * Don't actually release the list * (since it contains static information) * Just mark the entries as inactive. */voidifTable_free(netsnmp_cache * cache, void *magic){ netsnmp_container *container; if ((NULL == cache) || (NULL == cache->magic)) { snmp_log(LOG_ERR, "invalid cache for ifTable\n"); return; } DEBUGMSGTL(("ifTable/cache", "ifTable_free %p/%p\n", cache, cache->magic)); container = (netsnmp_container *) cache->magic; /* * free all items. inefficient, but easy. */ CONTAINER_CLEAR(container, ifTable_ifentry_release, NULL);} /************ * * Handler for the original ifTable * ************/intifTable_handler(netsnmp_mib_handler * handler, netsnmp_handler_registration * reginfo, netsnmp_agent_request_info * reqinfo, netsnmp_request_info * requests){ netsnmp_request_info *request; netsnmp_variable_list *requestvb; netsnmp_table_request_info *table_info; netsnmp_ifentry *entry; oid subid; long val; DEBUGMSGTL(("mibII/ifTable", "Handler - mode %s\n", se_find_label_in_slist("agent_mode", reqinfo->mode))); switch (reqinfo->mode) { case MODE_GET: for (request = requests; request; request = request->next) { requestvb = request->requestvb; DEBUGMSGTL(("mibII/ifTable", "oid: ")); DEBUGMSGOID(("mibII/ifTable", requestvb->name, requestvb->name_length)); DEBUGMSG(("mibII/ifTable", "\n")); entry = ifTable_ifentry_extract(request); if (!entry) continue; table_info = netsnmp_extract_table_info(request); subid = table_info->colnum; switch (subid) { case COLUMN_IFINDEX: snmp_set_var_typed_value(requestvb, ASN_INTEGER, (u_char *) & entry->index, sizeof(entry->index)); break; case COLUMN_IFDESCR: snmp_set_var_typed_value(requestvb, ASN_OCTET_STR, (u_char *) entry->if_descr, (entry->if_descr ? strlen(entry->if_descr) : 0)); break; case COLUMN_IFTYPE: snmp_set_var_typed_value(requestvb, ASN_INTEGER, (u_char *) & entry->if_type, sizeof(entry->if_type)); break; case COLUMN_IFMTU: snmp_set_var_typed_value(requestvb, ASN_INTEGER, (u_char *) & entry->if_mtu, sizeof(entry->if_mtu)); break; case COLUMN_IFSPEED: if (entry->flags & NETSNMP_IF_FLAGS_DYNAMIC_SPEED) val = get_ifspeed(entry) & 0xffffffff; else val = entry->if_speed; snmp_set_var_typed_value(requestvb, ASN_GAUGE, (u_char *) & val, sizeof(val)); break; case COLUMN_IFPHYSADDRESS: snmp_set_var_typed_value(requestvb, ASN_OCTET_STR, (u_char *) entry->if_paddr, entry->if_paddr_len); break; case COLUMN_IFADMINSTATUS: case COLUMN_IFOPERSTATUS: /* * XXX */ netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT); break; case COLUMN_IFLASTCHANGE: if (entry->flags & NETSNMP_IF_FLAGS_HAS_LASTCHANGE) snmp_set_var_typed_value(requestvb, ASN_INTEGER, (u_char *) & entry-> if_lastchange, sizeof(entry->if_lastchange)); else netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHINSTANCE); break; case COLUMN_IFINOCTETS: if (entry->flags & NETSNMP_IF_FLAGS_HAS_BYTES) snmp_set_var_typed_value(requestvb, ASN_COUNTER, (u_char *) & entry->if_ibytes. low, sizeof(entry->if_ibytes.low)); else netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHINSTANCE); break; case COLUMN_IFINUCASTPKTS: snmp_set_var_typed_value(requestvb, ASN_COUNTER, (u_char *) & entry->if_iucast.low, sizeof(entry->if_iucast.low)); break; case COLUMN_IFINNUCASTPKTS: /* * Deprecated object */ val = entry->if_imcast.low + entry->if_ibcast.low; snmp_set_var_typed_value(requestvb, ASN_COUNTER, (u_char *) & val, sizeof(val)); break; case COLUMN_IFINDISCARDS: snmp_set_var_typed_value(requestvb, ASN_COUNTER, (u_char *) & entry->if_idiscards, sizeof(entry->if_idiscards));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -