📄 perf-base.c
字号:
int GetEventsPerSecond(SFBASE *sfBase, SFBASE_STATS *sfBaseStats, SYSTIMES *Systimes){ sfBaseStats->alerts_per_second = (double)(pc.alert_pkts - sfBase->iAlerts) / Systimes->realtime; sfBase->iAlerts = pc.alert_pkts; sfBaseStats->total_sessions = sfBase->iTotalSessions; sfBaseStats->max_sessions = sfBase->iMaxSessions; sfBaseStats->syns_per_second = (double)(sfBase->iSyns) / Systimes->realtime; sfBaseStats->synacks_per_second = (double)(sfBase->iSynAcks) / Systimes->realtime; sfBaseStats->deleted_sessions_per_second = (double)(sfBase->iDeletedSessions) / Systimes->realtime; sfBaseStats->new_sessions_per_second = (double)(sfBase->iNewSessions) / Systimes->realtime; sfBaseStats->stream_flushes_per_second = (double)sfBase->iStreamFlushes / Systimes->realtime; sfBaseStats->stream_faults = sfBase->iStreamFaults; sfBaseStats->stream_timeouts = sfBase->iStreamTimeouts; sfBaseStats->frag_creates_per_second = (double)sfBase->iFragCreates / Systimes->realtime; sfBaseStats->frag_completes_per_second = (double)sfBase->iFragCompletes / Systimes->realtime; sfBaseStats->frag_inserts_per_second = (double)sfBase->iFragInserts / Systimes->realtime; sfBaseStats->frag_deletes_per_second = (double)sfBase->iFragDeletes / Systimes->realtime; sfBaseStats->frag_autofrees_per_second = (double)sfBase->iFragAutoFrees / Systimes->realtime; sfBaseStats->frag_flushes_per_second = (double)sfBase->iFragFlushes / Systimes->realtime; sfBaseStats->max_frags = sfBase->iMaxFrags; sfBaseStats->current_frags = sfBase->iCurrentFrags; sfBaseStats->frag_timeouts = sfBase->iFragTimeouts; sfBaseStats->frag_faults = sfBase->iFragFaults; sfBase->iSyns = 0; sfBase->iSynAcks = 0; sfBase->iNewSessions = 0; sfBase->iDeletedSessions = 0; sfBase->iStreamFlushes = 0; sfBase->iStreamFaults = 0; sfBase->iStreamTimeouts = 0; sfBase->iFragCreates = 0; sfBase->iFragCompletes = 0; sfBase->iFragInserts = 0; sfBase->iFragDeletes = 0; sfBase->iFragAutoFrees = 0; sfBase->iFragFlushes = 0; sfBase->iFragTimeouts = 0; sfBase->iFragFaults = 0; return 0;} int GetPacketsPerSecond(SFBASE *sfBase, SFBASE_STATS *sfBaseStats, SYSTIMES *Systimes){ sfBaseStats->kpackets_per_sec.realtime = (double)((double)sfBase->total_packets / 1000) / Systimes->realtime; if(sfBase->iFlags & MAX_PERF_STATS) { sfBaseStats->kpackets_per_sec.usertime = (double)((double)sfBase->total_packets / 1000) / Systimes->usertime; sfBaseStats->kpackets_per_sec.systemtime = (double)((double)sfBase->total_packets / 1000) / Systimes->systemtime; sfBaseStats->kpackets_per_sec.totaltime = (double)((double)sfBase->total_packets / 1000) / Systimes->totaltime; } sfBaseStats->kpackets_wire_per_sec.realtime = (double)((double)sfBase->total_wire_packets / 1000) / Systimes->realtime; if(sfBase->iFlags & MAX_PERF_STATS) { sfBaseStats->kpackets_wire_per_sec.usertime = (double)((double)sfBase->total_wire_packets / 1000) / Systimes->usertime; sfBaseStats->kpackets_wire_per_sec.systemtime = (double)((double)sfBase->total_wire_packets / 1000) / Systimes->systemtime; sfBaseStats->kpackets_wire_per_sec.totaltime = (double)((double)sfBase->total_wire_packets / 1000) / Systimes->totaltime; } sfBaseStats->kpackets_ipfrag_per_sec.realtime = (double)((double)sfBase->total_ipfragmented_packets / 1000) / Systimes->realtime; if(sfBase->iFlags & MAX_PERF_STATS) { sfBaseStats->kpackets_ipfrag_per_sec.usertime = (double)((double)sfBase->total_ipfragmented_packets / 1000) / Systimes->usertime; sfBaseStats->kpackets_ipfrag_per_sec.systemtime = (double)((double)sfBase->total_ipfragmented_packets / 1000) / Systimes->systemtime; sfBaseStats->kpackets_ipfrag_per_sec.totaltime = (double)((double)sfBase->total_ipfragmented_packets / 1000) / Systimes->totaltime; } sfBaseStats->kpackets_ipreass_per_sec.realtime = (double)((double)sfBase->total_ipreassembled_packets / 1000) / Systimes->realtime; if(sfBase->iFlags & MAX_PERF_STATS) { sfBaseStats->kpackets_ipreass_per_sec.usertime = (double)((double)sfBase->total_ipreassembled_packets / 1000) / Systimes->usertime; sfBaseStats->kpackets_ipreass_per_sec.systemtime = (double)((double)sfBase->total_ipreassembled_packets / 1000) / Systimes->systemtime; sfBaseStats->kpackets_ipreass_per_sec.totaltime = (double)((double)sfBase->total_ipreassembled_packets / 1000) / Systimes->totaltime; } sfBaseStats->kpackets_rebuilt_per_sec.realtime = (double)((double)sfBase->total_rebuilt_packets / 1000) / Systimes->realtime; if(sfBase->iFlags & MAX_PERF_STATS) { sfBaseStats->kpackets_rebuilt_per_sec.usertime = (double)((double)sfBase->total_rebuilt_packets / 1000) / Systimes->usertime; sfBaseStats->kpackets_rebuilt_per_sec.systemtime = (double)((double)sfBase->total_rebuilt_packets / 1000) / Systimes->systemtime; sfBaseStats->kpackets_rebuilt_per_sec.totaltime = (double)((double)sfBase->total_rebuilt_packets / 1000) / Systimes->totaltime; } return 0;}int GetuSecondsPerPacket(SFBASE *sfBase, SFBASE_STATS *sfBaseStats, SYSTIMES *Systimes){ sfBaseStats->usecs_per_packet.usertime = (Systimes->usertime * 1.0e6) / (double)sfBase->total_packets; sfBaseStats->usecs_per_packet.systemtime = (Systimes->systemtime * 1.0e6) / (double)sfBase->total_packets; sfBaseStats->usecs_per_packet.totaltime = (Systimes->totaltime * 1.0e6) / (double)sfBase->total_packets; sfBaseStats->usecs_per_packet.realtime = (Systimes->realtime * 1.0e6) / (double)sfBase->total_packets; return 0;}int GetMbitsPerSecond(SFBASE *sfBase, SFBASE_STATS *sfBaseStats, SYSTIMES *Systimes){ /* ** These Mbits stats are for the Snort Maximum Performance stats ** that can't reliably be gotten from Linux SMP kernels. So ** we don't do them. */ if(sfBase->iFlags & MAX_PERF_STATS) { sfBaseStats->mbits_per_sec.usertime = ((double) (sfBase->total_bytes<<3) * 1.0e-6) / Systimes->usertime; sfBaseStats->mbits_per_sec.systemtime = ((double) (sfBase->total_bytes<<3) * 1.0e-6) / Systimes->systemtime; sfBaseStats->mbits_per_sec.totaltime = ((double) (sfBase->total_bytes<<3) * 1.0e-6) / Systimes->totaltime; } sfBaseStats->mbits_per_sec.realtime = ((double)(sfBase->total_bytes<<3) * 1.0e-6) / Systimes->realtime; sfBaseStats->wire_mbits_per_sec.realtime = ((double)(sfBase->total_wire_bytes<<3) * 1.0e-6) / Systimes->realtime; sfBaseStats->rebuilt_mbits_per_sec.realtime = ((double)(sfBase->total_rebuilt_bytes<<3) * 1.0e-6) / Systimes->realtime; sfBaseStats->ipfrag_mbits_per_sec.realtime = ((double)(sfBase->total_ipfragmented_bytes<<3) * 1.0e-6) / Systimes->realtime; sfBaseStats->ipreass_mbits_per_sec.realtime = ((double)(sfBase->total_ipreassembled_bytes<<3) * 1.0e-6) / Systimes->realtime; return 0;}int GetCPUTime(SFBASE *sfBase, SFBASE_STATS *sfBaseStats, SYSTIMES *Systimes){#ifndef LINUX_SMP unsigned char needToNormalize = 0; sfBaseStats->user_cpu_time = (Systimes->usertime / Systimes->realtime) * 100; sfBaseStats->system_cpu_time = (Systimes->systemtime / Systimes->realtime) * 100; sfBaseStats->idle_cpu_time = ((Systimes->realtime - Systimes->totaltime) / Systimes->realtime) * 100; /* percentages can be < 0 because of a small variance between * when the snapshot is taken of the CPU times and snapshot of * the real time. So these are just a safe-guard to normalize * the data so we see positive values. */ if (sfBaseStats->user_cpu_time < 0) { sfBaseStats->user_cpu_time = 0; needToNormalize = 1; } if (sfBaseStats->system_cpu_time < 0) { sfBaseStats->system_cpu_time = 0; needToNormalize = 1; } if (sfBaseStats->idle_cpu_time < 0) { sfBaseStats->idle_cpu_time = 0; needToNormalize = 1; } if (needToNormalize) { double totalPercent = sfBaseStats->user_cpu_time + sfBaseStats->system_cpu_time + sfBaseStats->idle_cpu_time; sfBaseStats->user_cpu_time = (sfBaseStats->user_cpu_time / totalPercent) * 100; sfBaseStats->system_cpu_time = ( sfBaseStats->system_cpu_time / totalPercent) * 100; sfBaseStats->idle_cpu_time = ( sfBaseStats->idle_cpu_time / totalPercent) * 100; }#endif return 0;}/*** NAME** CalculateBasePerfStats**** DESCRIPTION** This is the main function that calculates the stats. Stats ** that we caculate are:** *uSecs per Packet** *Packets per Second** *Mbits per Second** *Average bytes per Packet** *CPU Time** *Dropped Packets** These statistics are processed and then stored in the** SFBASE_STATS structure. This allows output functions to** be easily formed and inserted.** NOTE: We can break up these statistics into functions for easier** reading.**** FORMAL INPUTS** SFBASE * - ptr to performance struct** SFBASE_STATS * - ptr to struct to fill in performance stats**** FORMAL OUTPUTS** int - 0 is successful*/int CalculateBasePerfStats(SFBASE *sfBase, SFBASE_STATS *sfBaseStats){ SYSTIMES Systimes; time_t clock;#ifdef LINUX_SMP /* ** We also give sfBaseStats access to the CPU usage ** contained in sfProcPidStats. This way we don't need ** to complicate sfBaseStats further. */ sfBaseStats->sfProcPidStats = &(sfBase->sfProcPidStats);#endif if(GetProcessingTime(&Systimes, sfBase)) return -1; /* ** Avg. bytes per Packet */ if (sfBase->total_packets > 0) sfBaseStats->avg_bytes_per_packet = (int)((double)(sfBase->total_bytes) / (double)(sfBase->total_packets)); else sfBaseStats->avg_bytes_per_packet = 0; if (sfBase->total_wire_packets > 0) sfBaseStats->avg_bytes_per_wire_packet = (int)((double)(sfBase->total_wire_bytes) / (double)(sfBase->total_wire_packets)); else sfBaseStats->avg_bytes_per_wire_packet = 0; if (sfBase->total_ipfragmented_packets > 0) sfBaseStats->avg_bytes_per_ipfrag_packet = (int)((double)(sfBase->total_ipfragmented_bytes) / (double)(sfBase->total_ipfragmented_packets)); else sfBaseStats->avg_bytes_per_ipfrag_packet = 0; if (sfBase->total_ipreassembled_packets > 0) sfBaseStats->avg_bytes_per_ipreass_packet = (int)((double)(sfBase->total_ipreassembled_bytes) / (double)(sfBase->total_ipreassembled_packets)); else sfBaseStats->avg_bytes_per_ipreass_packet = 0; if (sfBase->total_rebuilt_packets > 0) sfBaseStats->avg_bytes_per_rebuilt_packet = (int)((double)(sfBase->total_rebuilt_bytes) / (double)(sfBase->total_rebuilt_packets)); else sfBaseStats->avg_bytes_per_rebuilt_packet = 0; /* ** CPU time */ GetCPUTime(sfBase, sfBaseStats, &Systimes); /* ** Get Dropped Packets */ GetPktDropStats(sfBase, sfBaseStats); /* ** Total packets */ sfBaseStats->total_packets = sfBase->total_wire_packets; /* * Pattern Matching Performance in Real and User time */ sfBaseStats->patmatch_percent = 100.0 * mpseGetPatByteCount() / sfBase->total_wire_bytes; mpseResetByteCount(); if(sfBase->iFlags & MAX_PERF_STATS) { /* ** uSeconds per Packet ** user, system, total time */ GetuSecondsPerPacket(sfBase, sfBaseStats, &Systimes); } /* ** Mbits per sec ** user, system, total time */ GetMbitsPerSecond(sfBase, sfBaseStats, &Systimes); /* ** EventsPerSecond ** We get the information from the global variable ** PacketCount. */ GetEventsPerSecond(sfBase, sfBaseStats, &Systimes); /* ** Packets per seconds ** user, system, total time */ GetPacketsPerSecond(sfBase, sfBaseStats, &Systimes); /* ** Set the date string for print out */ time(&clock); sfBaseStats->time = clock; return 0;}/*** NAME** GetPktDropStats**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -