📄 statistics.c
字号:
/************************************************************** * Copyright (C) 2001 Tali Rozin, Optical Access * * All Rights Reserved * * Permission to use, copy, modify and distribute this software and its * documentation for any purpose and without fee is hereby granted, * provided that the above copyright notice appear in all copies and that * both that copyright notice and this permission notice appear in * supporting documentation. * * TALI ROZIN DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL * ALEX ROZIN BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS * SOFTWARE. ******************************************************************/#include <net-snmp/net-snmp-config.h>#if HAVE_STDLIB#include <stdlib.h>#endif#if TIME_WITH_SYS_TIME# ifdef WIN32# include <sys/timeb.h># else# include <sys/time.h># endif# include <time.h>#else# if HAVE_SYS_TIME_H# include <sys/time.h># else# include <time.h># endif#endif#if HAVE_UNISTD_H#include <unistd.h>#endif#include <net-snmp/net-snmp-includes.h>#include <net-snmp/agent/net-snmp-agent-includes.h>#include "util_funcs.h"#include "statistics.h" /* * Implementation headers */#include "agutil_api.h"#include "row_api.h" /* * File scope definitions section */ /* * from MIB compilation */#define MIB_DESCR "EthStat"#define etherStatsEntryFirstIndexBegin 11#define IDetherStatsDroppedFrames 1#define IDetherStatsCreateTime 2#define IDetherStatsIndex 3#define IDetherStatsDataSource 4#define IDetherStatsDropEvents 5#define IDetherStatsOctets 6#define IDetherStatsPkts 7#define IDetherStatsBroadcastPkts 8#define IDetherStatsMulticastPkts 9#define IDetherStatsCRCAlignErrors 10#define IDetherStatsUndersizePkts 11#define IDetherStatsOversizePkts 12#define IDetherStatsFragments 13#define IDetherStatsJabbers 14#define IDetherStatsCollisions 15#define IDetherStatsPkts64Octets 16#define IDetherStatsPkts65to127Octets 17#define IDetherStatsPkts128to255Octets 18#define IDetherStatsPkts256to511Octets 19#define IDetherStatsPkts512to1023Octets 20#define IDetherStatsPkts1024to1518Octets 21#define IDetherStatsOwner 22#define IDetherStatsStatus 23#define Leaf_etherStatsDataSource 2#define Leaf_etherStatsOwner 20#define Leaf_etherStatsStatus 21#define MIN_etherStatsIndex 1#define MAX_etherStatsIndex 65535 typedef struct { VAR_OID_T data_source; u_long etherStatsCreateTime; ETH_STATS_T eth; } CRTL_ENTRY_T;/* * Main section */ static TABLE_DEFINTION_T StatCtrlTable; static TABLE_DEFINTION_T * table_ptr = & StatCtrlTable;/* * Control Table RowApi Callbacks */ int stat_Create(RMON_ENTRY_T * eptr){ /* create the body: alloc it and set defaults */ CRTL_ENTRY_T *body; static VAR_OID_T data_src_if_index_1 = { 11, {1, 3, 6, 1, 2, 1, 2, 2, 1, 1, 1} }; eptr->body = AGMALLOC(sizeof(CRTL_ENTRY_T)); if (!eptr->body) return -3; body = (CRTL_ENTRY_T *) eptr->body; /* * set defaults */ memcpy(&body->data_source, &data_src_if_index_1, sizeof(VAR_OID_T)); body->data_source.objid[body->data_source.length - 1] = eptr->ctrl_index; eptr->owner = AGSTRDUP("Startup Mgmt"); memset(&body->eth, 0, sizeof(ETH_STATS_T)); return 0;}intstat_Validate(RMON_ENTRY_T * eptr){ /* * T.B.D. (system dependent) check valid inteface in body->data_source; */ return 0;}intstat_Activate(RMON_ENTRY_T * eptr){ CRTL_ENTRY_T *body = (CRTL_ENTRY_T *) eptr->body; body->etherStatsCreateTime = AGUTIL_sys_up_time(); return 0;}intstat_Copy(RMON_ENTRY_T * eptr){ CRTL_ENTRY_T *body = (CRTL_ENTRY_T *) eptr->body; CRTL_ENTRY_T *clone = (CRTL_ENTRY_T *) eptr->tmp; if (snmp_oid_compare (clone->data_source.objid, clone->data_source.length, body->data_source.objid, body->data_source.length)) { memcpy(&body->data_source, &clone->data_source, sizeof(VAR_OID_T)); } return 0;}intstat_Deactivate(RMON_ENTRY_T * eptr){ CRTL_ENTRY_T *body = (CRTL_ENTRY_T *) eptr->body; memset(&body->eth, 0, sizeof(ETH_STATS_T)); return 0;}/*************************************************** * Function:var_etherStats2Entry * Purpose: Handles the request for etherStats2Entry variable instances ***************************************************/u_char *var_etherStats2Entry(struct variable * vp, oid * name, size_t * length, int exact, size_t * var_len, WriteMethod ** write_method){ static long long_return; static CRTL_ENTRY_T theEntry; RMON_ENTRY_T *hdr; *write_method = NULL; hdr = ROWAPI_header_ControlEntry(vp, name, length, exact, var_len, table_ptr, &theEntry, sizeof(CRTL_ENTRY_T)); if (!hdr) return NULL; *var_len = sizeof(long); /* default */ switch (vp->magic) { case IDetherStatsDroppedFrames: long_return = 0; return (u_char *) & long_return; case IDetherStatsCreateTime: long_return = theEntry.etherStatsCreateTime; return (u_char *) & long_return; default: ag_trace("%s: unknown vp->magic=%d", table_ptr->name, (int) vp->magic); ERROR_MSG(""); }; /* of switch by 'vp->magic' */ return NULL;}/*************************************************** * Function:write_etherStatsEntry ***************************************************/static intwrite_etherStatsEntry(int action, u_char * var_val, u_char var_val_type, size_t var_val_len, u_char * statP, oid * name, size_t name_len){ long long_temp; int leaf_id, snmp_status; static int prev_action = COMMIT; RMON_ENTRY_T *hdr; CRTL_ENTRY_T *cloned_body; CRTL_ENTRY_T *body; switch (action) { case RESERVE1: case FREE: case UNDO: case ACTION: case COMMIT: default: snmp_status = ROWAPI_do_another_action(name, etherStatsEntryFirstIndexBegin, action, &prev_action, table_ptr, sizeof(CRTL_ENTRY_T)); if (SNMP_ERR_NOERROR != snmp_status) { ag_trace("failed action %d with %d", action, snmp_status); } break; case RESERVE2: /* * get values from PDU, check them and save them in the cloned entry */ long_temp = name[etherStatsEntryFirstIndexBegin]; leaf_id = (int) name[etherStatsEntryFirstIndexBegin - 1]; hdr = ROWAPI_find(table_ptr, long_temp); /* it MUST be OK */ cloned_body = (CRTL_ENTRY_T *) hdr->tmp; body = (CRTL_ENTRY_T *) hdr->body; switch (leaf_id) { case Leaf_etherStatsDataSource: snmp_status = AGUTIL_get_oid_value(var_val, var_val_type, var_val_len, &cloned_body->data_source); if (SNMP_ERR_NOERROR != snmp_status) { return snmp_status; } if (RMON1_ENTRY_UNDER_CREATION != hdr->status && snmp_oid_compare(cloned_body->data_source.objid, cloned_body->data_source.length, body->data_source.objid, body->data_source.length)) return SNMP_ERR_BADVALUE; break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -