⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 statbufc.c

📁 嵌入式RMON,RMON为Remote monitor的缩写,基于SNMP为网络提供主动监控及错误告警,智能交换路由必备协议
💻 C
字号:
/* Beholder RMON ethernet network monitor,Copyright (C) 1993 DNPAP group *//* See file COPYING 'GNU General Public Licence' for copyright details   */#include <math.h>#include <config.h>#include <memory.h>#include <maxmem.h>#include <mibrqs.h>#include <mibsup.h>#include <sysmib.h>#include <dmath.h>#include "statbufd.h"#include "statbufe.h"#include "statbufc.h"IMPORT BOOLEAN BeholderStrictRMON;SHORT StatBufMaxNrEntries = 100;static VOID StatBufCallback(TIMER_DESCR *timer, ULONG now, VOID *param);static WORD StatBufSample(LONG *id, WORD idlen, LONG *value, SNMP_OBJECT *obj);static BOOLEAN StatBufAddValue(StatBufControl *statbufcontrol, LONG value, ULONG now);static BOOLEAN StatBufCalcStats(StatBufControl *statbufcontrol);BOOLEAN StatBufConfigInit(VOID){	ConfigGetShort("beholder.statbuf.maxnrentries", &StatBufMaxNrEntries);	if (StatBufMaxNrEntries < 2)    {        DnpapMessage(DMC_WARNING, STATBUF_MAX, "statbufcontrol: beholder.statbuf.maxnrentries < 2, setting it to 2");        StatBufMaxNrEntries = 2;    }	return TRUE;}BOOLEAN StatBufCInit(StatBufControl *statbufcontrol){    statbufcontrol->Variable[0] = 0;    statbufcontrol->Variable[1] = 0;    statbufcontrol->VariableLen = 2;    statbufcontrol->RequestedSize = 10;    statbufcontrol->GrantedSize = 0;    statbufcontrol->Interval = 100;    statbufcontrol->SampleType = STATBUF_ABSOLUTE;    statbufcontrol->FullStatus = STATBUF_SPACE;    statbufcontrol->FullAction = STATBUF_LOCK;    statbufcontrol->Average = 0;    statbufcontrol->Deviation = 0;    statbufcontrol->Owner[0] = '\0';    statbufcontrol->OwnerLen = 0;    statbufcontrol->Status = SNMP_INVALID;    statbufcontrol->Buffer = NULL;    statbufcontrol->BufferAllocated = 0;    statbufcontrol->BufferPos = 0;    statbufcontrol->BufferWrapped = FALSE;    statbufcontrol->Timer = NULL;    statbufcontrol->GrantedSize = LongMin(MemoryGetAvail(), MemoryGetMaxChunk())/sizeof(StatBuf);    if (statbufcontrol->GrantedSize > LongMin(statbufcontrol->RequestedSize, StatBufMaxNrEntries))        statbufcontrol->GrantedSize = LongMin(statbufcontrol->RequestedSize, StatBufMaxNrEntries);    if ((statbufcontrol->Buffer = NewStatBuf(statbufcontrol->GrantedSize)) == NULL)    {        statbufcontrol->GrantedSize = 0;        statbufcontrol->BufferAllocated = 0;        statbufcontrol->BufferPos = 0;        statbufcontrol->BufferWrapped = FALSE;    }    else    {        statbufcontrol->BufferAllocated = statbufcontrol->GrantedSize;        statbufcontrol->BufferPos = 0;        statbufcontrol->BufferWrapped = FALSE;    }	BooleanSetAllTrue(statbufcontrol->ObjectSet);	BooleanSetFalse(statbufcontrol->ObjectSet, STATBUF_BOOLEAN_VARIABLE);	BooleanSetFalse(statbufcontrol->ObjectSet, STATBUF_BOOLEAN_OWNER);    return TRUE;}BOOLEAN StatBufCStart(StatBufControl *statbufcontrol){LONG value;SNMP_OBJECT obj = { SNMP_PDU_GET, { 0, 0 }, 2, SNMP_INTEGER };	if (BeholderStrictRMON && !BooleanCheckAllTrue(statbufcontrol->ObjectSet))		return FALSE;    if (statbufcontrol->SampleType == STATBUF_DELTA)    {        if (StatBufSample(statbufcontrol->Variable, statbufcontrol->VariableLen, &value, &obj) == SNMP_NOERROR)        {            statbufcontrol->PrevValue = value;        }    }    if ((statbufcontrol->Timer = TimerRegister(StatBufCallback, (VOID*)statbufcontrol, statbufcontrol->Interval*10, TIMER_FOREVER, TIMER_TYPE_SKIP)) == NULL)        return FALSE;    return TRUE;}BOOLEAN StatBufCStop(StatBufControl *statbufcontrol){    DelStatBuf(statbufcontrol->Buffer);    TimerRemove(statbufcontrol->Timer);    return TRUE;}VOID StatBufCallback(TIMER_DESCR *timer, ULONG now, VOID *param){SNMP_OBJECT obj = { SNMP_PDU_GET, { 0, 0 }, 2, SNMP_INTEGER };LONG value = 0, v = 0;StatBufControl *statbufcontrol = param;    if (StatBufSample(statbufcontrol->Variable, statbufcontrol->VariableLen, &value, &obj) == SNMP_NOERROR)    {        if (statbufcontrol->SampleType == STATBUF_DELTA)        {            v = value-statbufcontrol->PrevValue;            statbufcontrol->PrevValue = value;        }        else        if (statbufcontrol->SampleType == STATBUF_ABSOLUTE)        {            v = value;        }        if (StatBufAddValue(statbufcontrol, v, now) == TRUE)            StatBufCalcStats(statbufcontrol);    }}WORD StatBufSample(LONG *id, WORD idlen, LONG *value, SNMP_OBJECT *obj){    obj->Request = SNMP_PDU_GET;	memcpy(obj->Id, id, (obj->IdLen = idlen)*sizeof(id[0]));	if (MibRequest(obj) != SNMP_NOERROR)        return SNMP_NOSUCHNAME;		switch (obj->Type)	{	case SNMP_INTEGER:	case SNMP_COUNTER:	case SNMP_GAUGE:	case SNMP_TIMETICKS:		*value = obj->Syntax.LngInt;		return SNMP_NOERROR;	default:		return SNMP_BADVALUE;	}}BOOLEAN StatBufAddValue(StatBufControl *statbufcontrol, LONG value, ULONG now){StatBuf *entries, *entry;    if (statbufcontrol->BufferWrapped == FALSE &&        statbufcontrol->GrantedSize < statbufcontrol->RequestedSize &&        statbufcontrol->GrantedSize < StatBufMaxNrEntries)    {        if ((entries = ReNewStatBuf(statbufcontrol->Buffer, statbufcontrol->GrantedSize+1, statbufcontrol->BufferAllocated, StatBufMaxNrEntries)) != NULL)        {            statbufcontrol->FullStatus = STATBUF_SPACE;            statbufcontrol->Buffer = entries;            statbufcontrol->BufferAllocated = ++statbufcontrol->GrantedSize;        }    }    if (statbufcontrol->BufferAllocated == 0)    {        DnpapMessage(DMC_ERROR, STATBUF_NOENTRIES, "history: could not allocate any entries");        return FALSE;    }    if (statbufcontrol->BufferPos == statbufcontrol->GrantedSize)    {        statbufcontrol->FullStatus = STATBUF_FULL;        if (statbufcontrol->FullAction == STATBUF_LOCK)            return FALSE;        statbufcontrol->BufferPos = 0;        statbufcontrol->BufferWrapped = TRUE;    }    if (statbufcontrol->FullStatus == STATBUF_FULL && statbufcontrol->FullAction == STATBUF_LOCK)        return FALSE;    entry = &statbufcontrol->Buffer[statbufcontrol->BufferPos++];    entry->Time = now;    entry->Value = value;    return TRUE;}BOOLEAN StatBufCalcStats(StatBufControl *statbufcontrol){LONG i, n;LONG value;FLOAT average = 0, deviation = 0;    if (statbufcontrol->GrantedSize > 0)    {        if (statbufcontrol->BufferWrapped == FALSE)            n = statbufcontrol->BufferPos;        else            n = statbufcontrol->GrantedSize;        for (i = 0; i < n; i++)        {            value = statbufcontrol->Buffer[i].Value;            average += value;            deviation += value*value;        }        average /= statbufcontrol->GrantedSize;        deviation /= statbufcontrol->GrantedSize;        deviation -= average*average;        deviation = (FLOAT)sqrt(deviation);    }    statbufcontrol->Average = (LONG)(average*1000);    statbufcontrol->Deviation = (LONG)(deviation*1000);    return TRUE;}StatBuf* StatBufSearch(StatBufControl *statbufcontrol, SNMP_OBJECT *obj, WORD idlen){    if (obj->Id[idlen+1] > statbufcontrol->GrantedSize ||        (statbufcontrol->BufferWrapped == FALSE && obj->Id[idlen+1] > statbufcontrol->BufferPos))        return NULL;    else        if (statbufcontrol->BufferWrapped == FALSE)            return &statbufcontrol->Buffer[obj->Id[idlen+1]-1];        else            return &statbufcontrol->Buffer[(statbufcontrol->BufferPos+obj->Id[idlen+1]-1) % statbufcontrol->GrantedSize];    return NULL;}StatBuf* NewStatBuf(LONG n){	return DnpapMalloc(n*sizeof(StatBuf));}StatBuf* ReNewStatBuf(StatBuf *p, LONG n, LONG oldn, LONG maxn){LONG m, mold;	if (n > maxn || ((LONG)n*sizeof(StatBuf) > MemoryGetMaxChunk()))		return NULL;    m = ((n-1)/MINALLOCENTRIES)*MINALLOCENTRIES+MINALLOCENTRIES;    mold = ((oldn-1)/MINALLOCENTRIES)*MINALLOCENTRIES+MINALLOCENTRIES;    if (p == NULL || LongAbs(m-mold) >= MINALLOCENTRIES)    {        return DnpapRealloc(p, LongMin(m, maxn)*sizeof(StatBuf));    }            else        return p;}VOID DelStatBuf(StatBuf *p){    DnpapFree(p);}StatBufControl* NewStatBufControl(LONG n){	return DnpapMalloc(n*sizeof(StatBufControl));}VOID DelStatBufControl(StatBufControl *p){    DnpapFree(p);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -