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

📄 hist_c.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 <stdlib.h>#include <time.h>#ifdef UNIX#include <sys/time.h>#endif#include <dnpap.h>#include <dtime.h>#include <memory.h>#include <maxmem.h>#include <config.h>#include <mac.h>#include <sysmib.h>#include <dmath.h>#include "hist_e.h"#include "hist_c.h"  IMPORT BOOLEAN BeholderStrictRMON;LONG HistoryMaxNrBuckets = 2000;static VOID HistoryTimerCallback(TIMER_DESCR *timer, ULONG now, VOID *param);BOOLEAN HistoryConfigInit(VOID){	ConfigGetLong("beholder.history.maxnrbuckets", &HistoryMaxNrBuckets);	if (HistoryMaxNrBuckets < 2)    {        DnpapMessage(DMC_WARNING, HISTORY_MAX, "historycontrol: beholder.history.maxnrbuckets < 2, setting it to 2");        HistoryMaxNrBuckets = 2;    }	return TRUE;}BOOLEAN HistoryCInit(HISTORY_CONTROL *historycontrol){LONG source[] = {1,3,6,1,2,1,2,2,1,1,1};    historycontrol->Index = 0;    memcpy(historycontrol->Source, source, sizeof(source));    historycontrol->SourceLen = sizeof(source)/sizeof(source[0]);    historycontrol->BucketsRequested = 50;    historycontrol->BucketsGranted = 0;    historycontrol->Interval = 1800;    historycontrol->Owner[0] = '\0';    historycontrol->OwnerLen = 0;    historycontrol->Status = SNMP_INVALID;    historycontrol->Timer = NULL;    historycontrol->FirstCall = TRUE;    if ((historycontrol->Iface =        MacIfaceGet((WORD) historycontrol->Source[historycontrol->SourceLen-1])) == NULL)    {        DnpapMessage(DMC_ERROR, HISTORY_NETINIT, "historycontrol: network initialisation failed");        return (FALSE);    }    historycontrol->BucketsGranted = LongMin(MemoryGetAvail(), MemoryGetMaxChunk())/sizeof(ETHER_HISTORY);    if (historycontrol->BucketsGranted > LongMin(historycontrol->BucketsRequested, HistoryMaxNrBuckets))        historycontrol->BucketsGranted = LongMin(historycontrol->BucketsRequested, HistoryMaxNrBuckets);    if ((historycontrol->EtherBuckets = NewEtherHistory(historycontrol->BucketsGranted)) == NULL)    {        historycontrol->BucketsGranted = 0;        historycontrol->BucketsAllocated = 0;    }    else    {        historycontrol->BucketsAllocated = historycontrol->BucketsGranted;    }    historycontrol->BucketsPos = 0;    historycontrol->BucketsWrapped = FALSE;    historycontrol->BucketsDeleted = 0;	BooleanSetAllTrue(historycontrol->ObjectSet);	BooleanSetFalse(historycontrol->ObjectSet, HISTORY_BOOLEAN_DATASOURCE);	BooleanSetFalse(historycontrol->ObjectSet, HISTORY_BOOLEAN_OWNER);	return TRUE;}BOOLEAN HistoryCStart(HISTORY_CONTROL *historycontrol){ULONG now, waittime, nexthour;                   	if (BeholderStrictRMON && !BooleanCheckAllTrue(historycontrol->ObjectSet))		return FALSE;    now = (ULONG)difftime(0, time(NULL));    nexthour = (now/3600+1)*3600;    waittime = (nexthour-now) - ((nexthour-now)/historycontrol->Interval)*historycontrol->Interval;        if ((historycontrol->Timer = TimerRegister(HistoryTimerCallback, historycontrol, waittime, TIMER_FOREVER, TIMER_TYPE_SKIP)) == NULL)    {        DnpapMessage(DMC_ERROR, HISTORY_TIMER, "history: can not register a timer");        return FALSE;    }                            historycontrol->FirstCall = TRUE;        return TRUE;}BOOLEAN HistoryCStop(HISTORY_CONTROL *historycontrol){    TimerRemove(historycontrol->Timer);    DelEtherHistory(historycontrol->EtherBuckets);    return TRUE;}VOID HistoryTimerCallback(TIMER_DESCR *timer, ULONG now, VOID *param){ULONG dtime;MAC_STAT netstat;ETHER_HISTORY *etherhistory, *etherentries;HISTORY_CONTROL *historycontrol = param;                      /*  check if first call     *  if so, initialize and return immediately     */            if (historycontrol->FirstCall == TRUE)    {        historycontrol->PrvTime = now;            MacStatistics(historycontrol->Iface, &netstat);            historycontrol->EtherStats.DropEvents = netstat.LostPkts + netstat.DiscardedPkts;        historycontrol->EtherStats.Octets = netstat.Octets;        historycontrol->EtherStats.Pkts = netstat.Pkts;        historycontrol->EtherStats.BroadcastPkts = netstat.BroadcastPkts;         historycontrol->EtherStats.MulticastPkts = netstat.MulticastPkts;        historycontrol->EtherStats.CRCAlignErrors = netstat.CRCAlignErrors;        historycontrol->EtherStats.UndersizePkts = netstat.UndersizePkts;         historycontrol->EtherStats.OversizePkts = netstat.OversizePkts;          historycontrol->EtherStats.Fragments = netstat.Fragments;             historycontrol->EtherStats.Jabbers = netstat.Jabbers;               historycontrol->EtherStats.Collisions = netstat.Collisions;            TimerChange(timer, historycontrol->Interval*1000);                    historycontrol->FirstCall = FALSE;                return;    }    if (historycontrol->BucketsWrapped == FALSE &&        historycontrol->BucketsGranted < historycontrol->BucketsRequested &&        historycontrol->BucketsGranted < HistoryMaxNrBuckets)    {        if ((etherentries = ReNewEtherHistory(historycontrol->EtherBuckets, historycontrol->BucketsGranted+1, historycontrol->BucketsAllocated, HistoryMaxNrBuckets)) != NULL)        {            historycontrol->EtherBuckets = etherentries;            historycontrol->BucketsAllocated = ++historycontrol->BucketsGranted;        }    }    if (historycontrol->BucketsAllocated == 0)    {        DnpapMessage(DMC_ERROR, HISTORY_NOENTRIES, "history: could not allocate any entries");        return;    }    if (historycontrol->BucketsPos == historycontrol->BucketsGranted)    {        historycontrol->BucketsPos = 0;        historycontrol->BucketsWrapped = TRUE;    }    etherhistory = &historycontrol->EtherBuckets[historycontrol->BucketsPos++];    dtime = now - historycontrol->PrvTime;    historycontrol->PrvTime = now;    MacStatistics(historycontrol->Iface, &netstat);    etherhistory->IntervalStart = now;    etherhistory->DropEvents = netstat.LostPkts + netstat.DiscardedPkts - historycontrol->EtherStats.DropEvents;    etherhistory->Octets = netstat.Octets - historycontrol->EtherStats.Octets;    etherhistory->Pkts = netstat.Pkts - historycontrol->EtherStats.Pkts;    etherhistory->BroadcastPkts = netstat.BroadcastPkts - historycontrol->EtherStats.BroadcastPkts;     etherhistory->MulticastPkts = netstat.MulticastPkts - historycontrol->EtherStats.MulticastPkts;    etherhistory->CRCAlignErrors = netstat.CRCAlignErrors - historycontrol->EtherStats.CRCAlignErrors;    etherhistory->UndersizePkts = netstat.UndersizePkts - historycontrol->EtherStats.UndersizePkts;     etherhistory->OversizePkts = netstat.OversizePkts - historycontrol->EtherStats.OversizePkts;      etherhistory->Fragments = netstat.Fragments - historycontrol->EtherStats.Fragments;         etherhistory->Jabbers = netstat.Jabbers - historycontrol->EtherStats.Jabbers;        etherhistory->Collisions = netstat.Collisions - historycontrol->EtherStats.Collisions;    etherhistory->Utilization = (etherhistory->Octets + etherhistory->Pkts*24)*8/dtime;    historycontrol->EtherStats.DropEvents = netstat.LostPkts + netstat.DiscardedPkts;    historycontrol->EtherStats.Octets = netstat.Octets;    historycontrol->EtherStats.Pkts = netstat.Pkts;    historycontrol->EtherStats.BroadcastPkts = netstat.BroadcastPkts;     historycontrol->EtherStats.MulticastPkts = netstat.MulticastPkts;    historycontrol->EtherStats.CRCAlignErrors = netstat.CRCAlignErrors;    historycontrol->EtherStats.UndersizePkts = netstat.UndersizePkts;     historycontrol->EtherStats.OversizePkts = netstat.OversizePkts;      historycontrol->EtherStats.Fragments = netstat.Fragments;         historycontrol->EtherStats.Jabbers = netstat.Jabbers;           historycontrol->EtherStats.Collisions = netstat.Collisions;    if (historycontrol->BucketsWrapped == TRUE)    {        historycontrol->BucketsDeleted++;    }}ETHER_HISTORY *EtherHistorySearch(HISTORY_CONTROL *historycontrol, SNMP_OBJECT *obj, WORD idlen){LONG index;    if ((index = obj->Id[idlen+1] - historycontrol->BucketsDeleted) < 1)        return NULL;    if (index > historycontrol->BucketsGranted ||        (historycontrol->BucketsWrapped == FALSE && index > historycontrol->BucketsPos))        return NULL;    else        if (historycontrol->BucketsWrapped == FALSE)            return &historycontrol->EtherBuckets[index-1];        else            return &historycontrol->EtherBuckets[(historycontrol->BucketsPos+index-1) % historycontrol->BucketsGranted];}ETHER_HISTORY *NewEtherHistory(LONG nrentries){    return ReNewEtherHistory(NULL, nrentries, 0, HistoryMaxNrBuckets);}ETHER_HISTORY *ReNewEtherHistory(ETHER_HISTORY* ptr, LONG nrentries, LONG oldnrentries, LONG maxnrentries){           LONG n, nold;	if (nrentries > maxnrentries || nrentries*sizeof(ETHER_HISTORY) > MemoryGetMaxChunk())		return NULL;    n = ((nrentries-1)/MINALLOCENTRIES)*MINALLOCENTRIES+MINALLOCENTRIES;    nold = ((oldnrentries-1)/MINALLOCENTRIES)*MINALLOCENTRIES+MINALLOCENTRIES;    if (ptr == NULL || LongAbs(n-nold) >= MINALLOCENTRIES)      {        return DnpapRealloc(ptr, LongMin(n, maxnrentries)*sizeof(ETHER_HISTORY));    }            else        return ptr;}VOID DelEtherHistory(ETHER_HISTORY* ptr){    DnpapFree(ptr);}

⌨️ 快捷键说明

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