00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include <time.h>
00043 #ifndef WIN32
00044 #include <sys/time.h>
00045 #include <sys/resource.h>
00046 #endif
00047 #include <stdlib.h>
00048 #include <stdio.h>
00049 #include <sys/types.h>
00050
00051 #include "snort.h"
00052 #include "util.h"
00053 #include "mpse.h"
00054
00055 #ifndef UINT32_MAX
00056 #define UINT32_MAX (4294967295U)
00057 #endif
00058
00059 int GetPktDropStats(SFBASE *sfBase, SFBASE_STATS *sfBaseStats);
00060 int DisplayBasePerfStatsConsole(SFBASE_STATS *sfBaseStats, int iFlags);
00061 int CalculateBasePerfStats(SFBASE *sfPerf, SFBASE_STATS *sfBaseStats);
00062 int LogBasePerfStats(SFBASE_STATS *sfBaseStats, FILE * fh );
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 int InitBaseStats(SFBASE *sfBase)
00078 {
00079 int rusageRet = -1;
00080 int todRet = -1;
00081 struct timeval tvTime;
00082
00083 #ifndef WIN32
00084 #ifndef LINUX_SMP
00085 struct rusage rusage;
00086 #endif
00087
00088 #ifdef LINUX_SMP
00089 static int first_time = 0;
00090
00091 if(!first_time)
00092 {
00093 sfInitProcPidStats(&(sfBase->sfProcPidStats));
00094 first_time = 1;
00095 }
00096 todRet = gettimeofday(&tvTime, NULL);
00097 #else
00098
00099 rusageRet = getrusage(RUSAGE_SELF, &rusage);
00100 todRet = gettimeofday(&tvTime, NULL);
00101
00102 if (rusageRet >= 0)
00103 {
00104 sfBase->usertime_sec = (double)rusage.ru_utime.tv_sec +
00105 ((double)rusage.ru_utime.tv_usec * 1.0e-6);
00106 sfBase->systemtime_sec = (double)rusage.ru_stime.tv_sec +
00107 ((double)rusage.ru_stime.tv_usec * 1.0e-6);
00108 }
00109 else
00110 {
00111 sfBase->usertime_sec = 0;
00112 sfBase->systemtime_sec = 0;
00113 }
00114
00115 #endif
00116 #else
00117 sfBase->usertime_sec = 0;
00118 sfBase->systemtime_sec = 0;
00119 todRet = gettimeofday(&tvTime, NULL);
00120 #endif
00121
00122 if(todRet >= 0)
00123 {
00124 sfBase->realtime_sec = (double)tvTime.tv_sec +
00125 ((double)tvTime.tv_usec * 1.0e-6);
00126 }
00127 else
00128 {
00129 sfBase->realtime_sec = 0;
00130 }
00131
00132 sfBase->total_wire_packets = 0;
00133 sfBase->total_ipfragmented_packets = 0;
00134 sfBase->total_ipreassembled_packets = 0;
00135 sfBase->total_packets = 0;
00136 sfBase->total_rebuilt_packets = 0;
00137 sfBase->total_wire_bytes = 0;
00138 sfBase->total_ipfragmented_bytes = 0;
00139 sfBase->total_ipreassembled_bytes = 0;
00140 sfBase->total_bytes = 0;
00141 sfBase->total_rebuilt_bytes = 0;
00142
00143 sfBase->iNewSessions = 0;
00144 sfBase->iDeletedSessions = 0;
00145
00146 sfBase->iStreamFlushes = 0;
00147 sfBase->iStreamFaults = 0;
00148 sfBase->iStreamTimeouts = 0;
00149
00150 sfBase->iFragCreates = 0;
00151 sfBase->iFragCompletes = 0;
00152 sfBase->iFragInserts = 0;
00153 sfBase->iFragDeletes = 0;
00154 sfBase->iFragAutoFrees = 0;
00155 sfBase->iFragFlushes = 0;
00156 sfBase->iFragTimeouts = 0;
00157 sfBase->iFragFaults = 0;
00158
00159 return 0;
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 int UpdateBaseStats(SFBASE *sfBase, int len, int iRebuiltPkt)
00211 {
00212
00213 if(iRebuiltPkt)
00214 {
00215 sfBase->total_rebuilt_bytes += len;
00216 sfBase->total_rebuilt_packets++;
00217 }
00218 else
00219 {
00220 len += 4;
00221 }
00222
00223
00224
00225
00226 sfBase->total_packets++;
00227
00228 sfBase->total_bytes += len;
00229
00230 return 0;
00231 }
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 void UpdateWireStats(SFBASE *sfBase, int len)
00248 {
00249 sfBase->total_wire_packets++;
00250
00251 len += 4;
00252 sfBase->total_wire_bytes += len;
00253 }
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269 void UpdateIPFragStats(SFBASE *sfBase, int len)
00270 {
00271 sfBase->total_ipfragmented_packets++;
00272
00273 len += 4;
00274 sfBase->total_wire_bytes += len;
00275 sfBase->total_ipfragmented_bytes += len;
00276 }
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292 void UpdateIPReassStats(SFBASE *sfBase, int len)
00293 {
00294 sfBase->total_ipreassembled_packets++;
00295
00296 len += 4;
00297 sfBase->total_wire_bytes += len;
00298 sfBase->total_ipreassembled_bytes += len;
00299 }
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315 int AddStreamSession(SFBASE *sfBase)
00316 {
00317 sfBase->iTotalSessions++;
00318 sfBase->iNewSessions++;
00319
00320 if(sfBase->iTotalSessions > sfBase->iMaxSessions)
00321 sfBase->iMaxSessions = sfBase->iTotalSessions;
00322
00323 return 0;
00324 }
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340 int RemoveStreamSession(SFBASE *sfBase)
00341 {
00342 sfBase->iTotalSessions--;
00343 sfBase->iDeletedSessions++;
00344 return 0;
00345 }
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362 int ProcessBaseStats(SFBASE *sfBase, int console, int file, FILE * fh)
00363 {
00364 SFBASE_STATS sfBaseStats;
00365
00366 if( console || file )
00367 {
00368 if(CalculateBasePerfStats(sfBase, &sfBaseStats))
00369 return -1;
00370 }
00371
00372
00373 if( console )
00374 DisplayBasePerfStatsConsole(&sfBaseStats, sfBase->iFlags);
00375
00376 if( file )
00377 LogBasePerfStats(&sfBaseStats, fh );
00378
00379 return 0;
00380 }
00381
00382 int GetProcessingTime(SYSTIMES *Systimes, SFBASE *sfBase)
00383 {
00384 int rusageRet = -1;
00385 int todRet = -1;
00386 struct timeval tvTime;
00387 #ifdef LINUX_SMP
00388
00389 if(sfProcessProcPidStats(&(sfBase->sfProcPidStats)))
00390 return -1;
00391 todRet = gettimeofday(&tvTime, NULL);
00392 #else
00393 struct rusage rusage;
00394 #ifndef WIN32
00395 rusageRet = getrusage(RUSAGE_SELF, &rusage);
00396 #endif
00397 todRet = gettimeofday(&tvTime, NULL);
00398
00399 if (rusageRet < 0)
00400 {
00401 rusage.ru_utime.tv_sec = 0;
00402 rusage.ru_utime.tv_usec = 0;
00403 rusage.ru_stime.tv_sec = 0;
00404 rusage.ru_stime.tv_usec = 0;
00405 }
00406 Systimes->usertime = ((double)rusage.ru_utime.tv_sec +
00407 ((double)rusage.ru_utime.tv_usec * 1.0e-6)) -
00408 sfBase->usertime_sec;
00409 Systimes->systemtime = ((double)rusage.ru_stime.tv_sec +
00410 ((double)rusage.ru_stime.tv_usec * 1.0e-6)) -
00411 sfBase->systemtime_sec;
00412 Systimes->totaltime = Systimes->usertime + Systimes->systemtime;
00413 #endif
00414
00415 if (todRet < 0)
00416 {
00417 return todRet;
00418 }
00419
00420 Systimes->realtime = ((double)tvTime.tv_sec +
00421 ((double)tvTime.tv_usec * 1.0e-6)) -
00422 sfBase->realtime_sec;
00423 return 0;
00424 }
00425
00426 int GetEventsPerSecond(SFBASE *sfBase, SFBASE_STATS *sfBaseStats,
00427 SYSTIMES *Systimes)
00428 {
00429 sfBaseStats->alerts_per_second =
00430 (double)(pc.alert_pkts - sfBase->iAlerts) / Systimes->realtime;
00431
00432 sfBase->iAlerts = pc.alert_pkts;
00433
00434 sfBaseStats->total_sessions = sfBase->iTotalSessions;
00435 sfBaseStats->max_sessions = sfBase->iMaxSessions;
00436
00437 sfBaseStats->syns_per_second =
00438 (double)(sfBase->iSyns) / Systimes->realtime;
00439
00440 sfBaseStats->synacks_per_second =
00441 (double)(sfBase->iSynAcks) / Systimes->realtime;
00442
00443 sfBaseStats->deleted_sessions_per_second =
00444 (double)(sfBase->iDeletedSessions) / Systimes->realtime;
00445
00446 sfBaseStats->new_sessions_per_second =
00447 (double)(sfBase->iNewSessions) / Systimes->realtime;
00448
00449
00450 sfBaseStats->stream_flushes_per_second =
00451 (double)sfBase->iStreamFlushes / Systimes->realtime;
00452
00453 sfBaseStats->stream_faults = sfBase->iStreamFaults;
00454 sfBaseStats->stream_timeouts = sfBase->iStreamTimeouts;
00455
00456 sfBaseStats->frag_creates_per_second =
00457 (double)sfBase->iFragCreates / Systimes->realtime;
00458
00459 sfBaseStats->frag_completes_per_second =
00460 (double)sfBase->iFragCompletes / Systimes->realtime;
00461
00462 sfBaseStats->frag_inserts_per_second =
00463 (double)sfBase->iFragInserts / Systimes->realtime;
00464
00465 sfBaseStats->frag_deletes_per_second =
00466 (double)sfBase->iFragDeletes / Systimes->realtime;
00467
00468 sfBaseStats->frag_autofrees_per_second =
00469 (double)sfBase->iFragAutoFrees / Systimes->realtime;
00470
00471 sfBaseStats->frag_flushes_per_second =
00472 (double)sfBase->iFragFlushes / Systimes->realtime;
00473
00474 sfBaseStats->max_frags = sfBase->iMaxFrags;
00475 sfBaseStats->current_frags = sfBase->iCurrentFrags;
00476 sfBaseStats->frag_timeouts = sfBase->iFragTimeouts;
00477 sfBaseStats->frag_faults = sfBase->iFragFaults;
00478
00479 sfBase->iSyns = 0;
00480 sfBase->iSynAcks = 0;
00481 sfBase->iNewSessions = 0;
00482 sfBase->iDeletedSessions = 0;
00483
00484 sfBase->iStreamFlushes = 0;
00485 sfBase->iStreamFaults = 0;
00486 sfBase->iStreamTimeouts = 0;
00487
00488 sfBase->iFragCreates = 0;
00489 sfBase->iFragCompletes = 0;
00490 sfBase->iFragInserts = 0;
00491 sfBase->iFragDeletes = 0;
00492 sfBase->iFragAutoFrees = 0;
00493 sfBase->iFragFlushes = 0;
00494 sfBase->iFragTimeouts = 0;
00495 sfBase->iFragFaults = 0;
00496
00497
00498 return 0;
00499 }
00500
00501 int GetPacketsPerSecond(SFBASE *sfBase, SFBASE_STATS *sfBaseStats,
00502 SYSTIMES *Systimes)
00503 {
00504 sfBaseStats->kpackets_per_sec.realtime =
00505 (double)((double)sfBase->total_packets / 1000) / Systimes->realtime;
00506
00507 if(sfBase->iFlags & MAX_PERF_STATS)
00508 {
00509 sfBaseStats->kpackets_per_sec.usertime =
00510 (double)((double)sfBase->total_packets / 1000) /
00511 Systimes->usertime;
00512 sfBaseStats->kpackets_per_sec.systemtime =
00513 (double)((double)sfBase->total_packets / 1000) /
00514 Systimes->systemtime;
00515 sfBaseStats->kpackets_per_sec.totaltime =
00516 (double)((double)sfBase->total_packets / 1000) /
00517 Systimes->totaltime;
00518 }
00519
00520 sfBaseStats->kpackets_wire_per_sec.realtime =
00521 (double)((double)sfBase->total_wire_packets / 1000) / Systimes->realtime;
00522
00523 if(sfBase->iFlags & MAX_PERF_STATS)
00524 {
00525 sfBaseStats->kpackets_wire_per_sec.usertime =
00526 (double)((double)sfBase->total_wire_packets / 1000) /
00527 Systimes->usertime;
00528 sfBaseStats->kpackets_wire_per_sec.systemtime =
00529 (double)((double)sfBase->total_wire_packets / 1000) /
00530 Systimes->systemtime;
00531 sfBaseStats->kpackets_wire_per_sec.totaltime =
00532 (double)((double)sfBase->total_wire_packets / 1000) /
00533 Systimes->totaltime;
00534 }
00535
00536 sfBaseStats->kpackets_ipfrag_per_sec.realtime =
00537 (double)((double)sfBase->total_ipfragmented_packets / 1000) / Systimes->realtime;
00538
00539 if(sfBase->iFlags & MAX_PERF_STATS)
00540 {
00541 sfBaseStats->kpackets_ipfrag_per_sec.usertime =
00542 (double)((double)sfBase->total_ipfragmented_packets / 1000) /
00543 Systimes->usertime;
00544 sfBaseStats->kpackets_ipfrag_per_sec.systemtime =
00545 (double)((double)sfBase->total_ipfragmented_packets / 1000) /
00546 Systimes->systemtime;
00547 sfBaseStats->kpackets_ipfrag_per_sec.totaltime =
00548 (double)((double)sfBase->total_ipfragmented_packets / 1000) /
00549 Systimes->totaltime;
00550 }
00551
00552 sfBaseStats->kpackets_ipreass_per_sec.realtime =
00553 (double)((double)sfBase->total_ipreassembled_packets / 1000) / Systimes->realtime;
00554
00555 if(sfBase->iFlags & MAX_PERF_STATS)
00556 {
00557 sfBaseStats->kpackets_ipreass_per_sec.usertime =
00558 (double)((double)sfBase->total_ipreassembled_packets / 1000) /
00559 Systimes->usertime;
00560 sfBaseStats->kpackets_ipreass_per_sec.systemtime =
00561 (double)((double)sfBase->total_ipreassembled_packets / 1000) /
00562 Systimes->systemtime;
00563 sfBaseStats->kpackets_ipreass_per_sec.totaltime =
00564 (double)((double)sfBase->total_ipreassembled_packets / 1000) /
00565 Systimes->totaltime;
00566 }
00567
00568 sfBaseStats->kpackets_rebuilt_per_sec.realtime =
00569 (double)((double)sfBase->total_rebuilt_packets / 1000) / Systimes->realtime;
00570
00571 if(sfBase->iFlags & MAX_PERF_STATS)
00572 {
00573 sfBaseStats->kpackets_rebuilt_per_sec.usertime =
00574 (double)((double)sfBase->total_rebuilt_packets / 1000) /
00575 Systimes->usertime;
00576 sfBaseStats->kpackets_rebuilt_per_sec.systemtime =
00577 (double)((double)sfBase->total_rebuilt_packets / 1000) /
00578 Systimes->systemtime;
00579 sfBaseStats->kpackets_rebuilt_per_sec.totaltime =
00580 (double)((double)sfBase->total_rebuilt_packets / 1000) /
00581 Systimes->totaltime;
00582 }
00583
00584
00585 return 0;
00586 }
00587
00588 int GetuSecondsPerPacket(SFBASE *sfBase, SFBASE_STATS *sfBaseStats,
00589 SYSTIMES *Systimes)
00590 {
00591 sfBaseStats->usecs_per_packet.usertime = (Systimes->usertime * 1.0e6) /
00592 (double)sfBase->total_packets;
00593 sfBaseStats->usecs_per_packet.systemtime = (Systimes->systemtime * 1.0e6) /
00594 (double)sfBase->total_packets;
00595 sfBaseStats->usecs_per_packet.totaltime = (Systimes->totaltime * 1.0e6) /
00596 (double)sfBase->total_packets;
00597 sfBaseStats->usecs_per_packet.realtime = (Systimes->realtime * 1.0e6) /
00598 (double)sfBase->total_packets;
00599
00600 return 0;
00601 }
00602
00603 int GetMbitsPerSecond(SFBASE *sfBase, SFBASE_STATS *sfBaseStats,
00604 SYSTIMES *Systimes)
00605 {
00606
00607
00608
00609
00610
00611 if(sfBase->iFlags & MAX_PERF_STATS)
00612 {
00613 sfBaseStats->mbits_per_sec.usertime = ((double)
00614 (sfBase->total_bytes<<3) *
00615 1.0e-6) /
00616 Systimes->usertime;
00617 sfBaseStats->mbits_per_sec.systemtime = ((double)
00618 (sfBase->total_bytes<<3) *
00619 1.0e-6) /
00620 Systimes->systemtime;
00621 sfBaseStats->mbits_per_sec.totaltime = ((double)
00622 (sfBase->total_bytes<<3) *
00623 1.0e-6) /
00624 Systimes->totaltime;
00625 }
00626
00627 sfBaseStats->mbits_per_sec.realtime = ((double)(sfBase->total_bytes<<3) *
00628 1.0e-6) /
00629 Systimes->realtime;
00630 sfBaseStats->wire_mbits_per_sec.realtime =
00631 ((double)(sfBase->total_wire_bytes<<3) *
00632 1.0e-6) /
00633 Systimes->realtime;
00634 sfBaseStats->rebuilt_mbits_per_sec.realtime =
00635 ((double)(sfBase->total_rebuilt_bytes<<3) *
00636 1.0e-6) /
00637 Systimes->realtime;
00638
00639 sfBaseStats->ipfrag_mbits_per_sec.realtime =
00640 ((double)(sfBase->total_ipfragmented_bytes<<3) *
00641 1.0e-6) /
00642 Systimes->realtime;
00643
00644 sfBaseStats->ipreass_mbits_per_sec.realtime =
00645 ((double)(sfBase->total_ipreassembled_bytes<<3) *
00646 1.0e-6) /
00647 Systimes->realtime;
00648
00649 return 0;
00650 }
00651
00652 int GetCPUTime(SFBASE *sfBase, SFBASE_STATS *sfBaseStats, SYSTIMES *Systimes)
00653 {
00654 #ifndef LINUX_SMP
00655 unsigned char needToNormalize = 0;
00656 sfBaseStats->user_cpu_time = (Systimes->usertime /
00657 Systimes->realtime) * 100;
00658 sfBaseStats->system_cpu_time = (Systimes->systemtime /
00659 Systimes->realtime) * 100;
00660 sfBaseStats->idle_cpu_time = ((Systimes->realtime -
00661 Systimes->totaltime) /
00662 Systimes->realtime) * 100;
00663
00664
00665
00666
00667
00668
00669 if (sfBaseStats->user_cpu_time < 0)
00670 {
00671 sfBaseStats->user_cpu_time = 0;
00672 needToNormalize = 1;
00673 }
00674 if (sfBaseStats->system_cpu_time < 0)
00675 {
00676 sfBaseStats->system_cpu_time = 0;
00677 needToNormalize = 1;
00678 }
00679 if (sfBaseStats->idle_cpu_time < 0)
00680 {
00681 sfBaseStats->idle_cpu_time = 0;
00682 needToNormalize = 1;
00683 }
00684
00685 if (needToNormalize)
00686 {
00687 double totalPercent = sfBaseStats->user_cpu_time +
00688 sfBaseStats->system_cpu_time +
00689 sfBaseStats->idle_cpu_time;
00690
00691
00692 sfBaseStats->user_cpu_time = (sfBaseStats->user_cpu_time /
00693 totalPercent) * 100;
00694 sfBaseStats->system_cpu_time = ( sfBaseStats->system_cpu_time /
00695 totalPercent) * 100;
00696 sfBaseStats->idle_cpu_time = ( sfBaseStats->idle_cpu_time /
00697 totalPercent) * 100;
00698
00699 }
00700 #endif
00701 return 0;
00702 }
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731 int CalculateBasePerfStats(SFBASE *sfBase, SFBASE_STATS *sfBaseStats)
00732 {
00733 SYSTIMES Systimes;
00734 time_t clock;
00735
00736 #ifdef LINUX_SMP
00737
00738
00739
00740
00741
00742
00743 sfBaseStats->sfProcPidStats = &(sfBase->sfProcPidStats);
00744
00745 #endif
00746 if(GetProcessingTime(&Systimes, sfBase))
00747 return -1;
00748
00749
00750
00751
00752 if (sfBase->total_packets > 0)
00753 sfBaseStats->avg_bytes_per_packet =
00754 (int)((double)(sfBase->total_bytes) /
00755 (double)(sfBase->total_packets));
00756 else
00757 sfBaseStats->avg_bytes_per_packet = 0;
00758
00759 if (sfBase->total_wire_packets > 0)
00760 sfBaseStats->avg_bytes_per_wire_packet =
00761 (int)((double)(sfBase->total_wire_bytes) /
00762 (double)(sfBase->total_wire_packets));
00763 else
00764 sfBaseStats->avg_bytes_per_wire_packet = 0;
00765
00766 if (sfBase->total_ipfragmented_packets > 0)
00767 sfBaseStats->avg_bytes_per_ipfrag_packet =
00768 (int)((double)(sfBase->total_ipfragmented_bytes) /
00769 (double)(sfBase->total_ipfragmented_packets));
00770 else
00771 sfBaseStats->avg_bytes_per_ipfrag_packet = 0;
00772
00773 if (sfBase->total_ipreassembled_packets > 0)
00774 sfBaseStats->avg_bytes_per_ipreass_packet =
00775 (int)((double)(sfBase->total_ipreassembled_bytes) /
00776 (double)(sfBase->total_ipreassembled_packets));
00777 else
00778 sfBaseStats->avg_bytes_per_ipreass_packet = 0;
00779
00780 if (sfBase->total_rebuilt_packets > 0)
00781 sfBaseStats->avg_bytes_per_rebuilt_packet =
00782 (int)((double)(sfBase->total_rebuilt_bytes) /
00783 (double)(sfBase->total_rebuilt_packets));
00784 else
00785 sfBaseStats->avg_bytes_per_rebuilt_packet = 0;
00786
00787
00788
00789
00790 GetCPUTime(sfBase, sfBaseStats, &Systimes);
00791
00792
00793
00794
00795 GetPktDropStats(sfBase, sfBaseStats);
00796
00797
00798
00799
00800 sfBaseStats->total_packets = sfBase->total_wire_packets;
00801
00802
00803
00804
00805 sfBaseStats->patmatch_percent = 100.0 * mpseGetPatByteCount() /
00806 sfBase->total_wire_bytes;
00807
00808 mpseResetByteCount();
00809
00810 if(sfBase->iFlags & MAX_PERF_STATS)
00811 {
00812
00813
00814
00815
00816 GetuSecondsPerPacket(sfBase, sfBaseStats, &Systimes);
00817 }
00818
00819
00820
00821
00822
00823 GetMbitsPerSecond(sfBase, sfBaseStats, &Systimes);
00824
00825
00826
00827
00828
00829
00830 GetEventsPerSecond(sfBase, sfBaseStats, &Systimes);
00831
00832
00833
00834
00835
00836 GetPacketsPerSecond(sfBase, sfBaseStats, &Systimes);
00837
00838
00839
00840
00841 time(&clock);
00842 sfBaseStats->time = clock;
00843
00844 return 0;
00845 }
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863 int GetPktDropStats(SFBASE *sfBase, SFBASE_STATS *sfBaseStats)
00864 {
00865
00866
00867
00868
00869 extern pcap_t *pd;
00870 struct pcap_stat pcapStats;
00871
00872 if((!pd)
00873 #ifdef WIN32
00874 || (pv.readmode_flag)
00875 #endif
00876 )
00877 {
00878 sfBaseStats->pkt_stats.pkts_recv = sfBaseStats->total_packets;
00879 sfBaseStats->pkt_stats.pkts_drop = 0;
00880 sfBaseStats->pkt_drop_percent = 0.0;
00881 return 0;
00882 }
00883
00884 if(pcap_stats(pd, &pcapStats) < 0)
00885 {
00886 sfBaseStats->pkt_stats.pkts_recv = sfBaseStats->total_packets;
00887 sfBaseStats->pkt_stats.pkts_drop = 0;
00888 sfBaseStats->pkt_drop_percent = 0.0;
00889 }
00890 else
00891 {
00892 if( sfBase->iReset == 0 )
00893 {
00894 if (pcapStats.ps_recv < (u_int32_t)sfBase->pkt_stats.pkts_recv)
00895 {
00896
00897 sfBaseStats->pkt_stats.pkts_recv = pcapStats.ps_recv +
00898 (UINT32_MAX - (u_int32_t)sfBase->pkt_stats.pkts_recv);
00899 }
00900 else
00901 {
00902 sfBaseStats->pkt_stats.pkts_recv = pcapStats.ps_recv -
00903 (u_int32_t)sfBase->pkt_stats.pkts_recv;
00904 }
00905 if (pcapStats.ps_drop < (u_int32_t)sfBase->pkt_stats.pkts_drop)
00906 {
00907
00908 sfBaseStats->pkt_stats.pkts_drop = pcapStats.ps_drop +
00909 (UINT32_MAX - (u_int32_t)sfBase->pkt_stats.pkts_drop);
00910 }
00911 else
00912 {
00913 sfBaseStats->pkt_stats.pkts_drop = pcapStats.ps_drop -
00914 (u_int32_t)sfBase->pkt_stats.pkts_drop;
00915 }
00916 }
00917 else
00918 {
00919 sfBaseStats->pkt_stats.pkts_recv = pcapStats.ps_recv;
00920 sfBaseStats->pkt_stats.pkts_drop = pcapStats.ps_drop;
00921 }
00922
00923 sfBaseStats->pkt_drop_percent =
00924 ((double)sfBaseStats->pkt_stats.pkts_drop /
00925 (double)sfBaseStats->pkt_stats.pkts_recv) * 100;
00926
00927
00928
00929
00930 sfBase->pkt_stats.pkts_recv = pcapStats.ps_recv;
00931 sfBase->pkt_stats.pkts_drop = pcapStats.ps_drop;
00932 }
00933
00934 return 0;
00935 }
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987 int LogBasePerfStats(SFBASE_STATS *sfBaseStats, FILE * fh )
00988 {
00989 double sys=0.0,usr=0.0,idle=0.0;
00990
00991 #ifdef LINUX_SMP
00992 int iCtr;
00993 #endif
00994 if( ! fh ) return 0;
00995
00996 fprintf(fh,"%lu,%.3f,%.1f,%.1f,%.1f,%d,%.2f,",
00997 (unsigned long)sfBaseStats->time,
00998 sfBaseStats->pkt_drop_percent,
00999 sfBaseStats->wire_mbits_per_sec.realtime,
01000 sfBaseStats->alerts_per_second,
01001 sfBaseStats->kpackets_per_sec.realtime,
01002 sfBaseStats->avg_bytes_per_packet,
01003 sfBaseStats->patmatch_percent);
01004
01005
01006
01007 fprintf(fh,
01008 #ifdef WIN32
01009 "%.1f,%.1f,%.1f,%.1f,%I64i,%I64i,",
01010 #else
01011 "%.1f,%.1f,%.1f,%.1f,%llu,%llu,",
01012 #endif
01013 sfBaseStats->syns_per_second,
01014 sfBaseStats->synacks_per_second,
01015 sfBaseStats->new_sessions_per_second,
01016 sfBaseStats->deleted_sessions_per_second,
01017 sfBaseStats->total_sessions,
01018 sfBaseStats->max_sessions);
01019
01020
01021 fprintf(fh,
01022 #ifdef WIN32
01023 "%.1f,%I64i,%I64i,",
01024 #else
01025 "%.1f,%llu,%llu,",
01026 #endif
01027 sfBaseStats->stream_flushes_per_second,
01028 sfBaseStats->stream_faults,
01029 sfBaseStats->stream_timeouts);
01030
01031 fprintf(fh,
01032 #ifdef WIN32
01033 "%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,%I64i,%I64i,%I64i,%I64i,",
01034 #else
01035 "%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,%llu,%llu,%llu,%llu,",
01036 #endif
01037 sfBaseStats->frag_creates_per_second,
01038 sfBaseStats->frag_completes_per_second,
01039 sfBaseStats->frag_inserts_per_second,
01040 sfBaseStats->frag_deletes_per_second,
01041 sfBaseStats->frag_autofrees_per_second,
01042 sfBaseStats->frag_flushes_per_second,
01043 sfBaseStats->current_frags,
01044 sfBaseStats->max_frags,
01045 sfBaseStats->frag_timeouts,
01046 sfBaseStats->frag_faults);
01047
01048
01049 #ifdef LINUX_SMP
01050
01051 fprintf(fh, "%d,", sfBaseStats->sfProcPidStats->iCPUs);
01052
01053
01054 for(iCtr = 0; iCtr < sfBaseStats->sfProcPidStats->iCPUs; iCtr++)
01055 {
01056 usr= sfBaseStats->sfProcPidStats->SysCPUs[iCtr].user;
01057 sys= sfBaseStats->sfProcPidStats->SysCPUs[iCtr].sys;
01058 idle= sfBaseStats->sfProcPidStats->SysCPUs[iCtr].idle;
01059
01060 fprintf(fh,"%.1f,%.1f,%.1f,",usr,sys,idle);
01061 }
01062
01063 #else
01064
01065 usr=sfBaseStats->user_cpu_time;
01066 sys=sfBaseStats->system_cpu_time;
01067 idle=sfBaseStats->idle_cpu_time;
01068
01069
01070 fprintf(fh,"1,%.1f,%.1f,%.1f,",usr,sys,idle);
01071
01072 #endif
01073
01074
01075
01076
01077 fprintf(fh,"%.2f,%.2f,%.2f,%.2f,%.2f,",
01078 sfBaseStats->wire_mbits_per_sec.realtime,
01079 sfBaseStats->ipfrag_mbits_per_sec.realtime,
01080 sfBaseStats->ipreass_mbits_per_sec.realtime,
01081 sfBaseStats->rebuilt_mbits_per_sec.realtime,
01082 sfBaseStats->mbits_per_sec.realtime);
01083
01084 fprintf(fh,"%d,%d,%d,%d,%d,",
01085 sfBaseStats->avg_bytes_per_wire_packet,
01086 sfBaseStats->avg_bytes_per_ipfrag_packet,
01087 sfBaseStats->avg_bytes_per_ipreass_packet,
01088 sfBaseStats->avg_bytes_per_rebuilt_packet,
01089 sfBaseStats->avg_bytes_per_packet);
01090
01091 fprintf(fh,"%.2f,%.2f,%.2f,%.2f,%.2f,",
01092 sfBaseStats->kpackets_wire_per_sec.realtime,
01093 sfBaseStats->kpackets_ipfrag_per_sec.realtime,
01094 sfBaseStats->kpackets_ipreass_per_sec.realtime,
01095 sfBaseStats->kpackets_rebuilt_per_sec.realtime,
01096 sfBaseStats->kpackets_per_sec.realtime);
01097
01098 fprintf(fh,"%llu,",sfBaseStats->pkt_stats.pkts_recv);
01099 fprintf(fh,"%llu", sfBaseStats->pkt_stats.pkts_drop);
01100
01101
01102 fprintf(fh,"\n");
01103
01104 fflush(fh);
01105
01106 #ifdef LINUX
01107
01108 #endif
01109
01110 return 0;
01111 }
01112
01113
01114
01115
01116
01117
01118
01119
01120
01121
01122
01123
01124
01125
01126
01127
01128
01129
01130
01131 int DisplayBasePerfStatsConsole(SFBASE_STATS *sfBaseStats, int iFlags)
01132 {
01133 #ifdef LINUX_SMP
01134 int iCtr;
01135 #endif
01136
01137 LogMessage("\n\nSnort Realtime Performance : %s--------------------------\n",
01138 ctime(&sfBaseStats->time));
01139
01140 LogMessage("Pkts Recv: %llu\n", sfBaseStats->pkt_stats.pkts_recv);
01141 LogMessage("Pkts Drop: %llu\n", sfBaseStats->pkt_stats.pkts_drop);
01142 LogMessage("%% Dropped: %.2f%%\n\n",sfBaseStats->pkt_drop_percent);
01143
01144 LogMessage("Mbits/Sec: %.2f (wire)\n",
01145 sfBaseStats->wire_mbits_per_sec.realtime);
01146 LogMessage("Mbits/Sec: %.2f (ip fragmented)\n",
01147 sfBaseStats->ipfrag_mbits_per_sec.realtime);
01148 LogMessage("Mbits/Sec: %.2f (ip reassembled)\n",
01149 sfBaseStats->ipreass_mbits_per_sec.realtime);
01150 LogMessage("Mbits/Sec: %.2f (tcp rebuilt)\n",
01151 sfBaseStats->rebuilt_mbits_per_sec.realtime);
01152 LogMessage("Mbits/Sec: %.2f (app layer)\n\n",
01153 sfBaseStats->mbits_per_sec.realtime);
01154
01155 LogMessage("Bytes/Pkt: %d (wire)\n",
01156 sfBaseStats->avg_bytes_per_wire_packet);
01157 LogMessage("Bytes/Pkt: %d (ip fragmented)\n",
01158 sfBaseStats->avg_bytes_per_ipfrag_packet);
01159 LogMessage("Bytes/Pkt: %d (ip reassembled)\n",
01160 sfBaseStats->avg_bytes_per_ipreass_packet);
01161 LogMessage("Bytes/Pkt: %d (tcp rebuilt)\n",
01162 sfBaseStats->avg_bytes_per_rebuilt_packet);
01163 LogMessage("Bytes/Pkt: %d (app layer)\n\n",
01164 sfBaseStats->avg_bytes_per_packet);
01165
01166 LogMessage("KPkts/Sec: %.2f (wire)\n",
01167 sfBaseStats->kpackets_wire_per_sec.realtime);
01168 LogMessage("KPkts/Sec: %.2f (ip fragmented)\n",
01169 sfBaseStats->kpackets_ipfrag_per_sec.realtime);
01170 LogMessage("KPkts/Sec: %.2f (ip reassembled)\n",
01171 sfBaseStats->kpackets_ipreass_per_sec.realtime);
01172 LogMessage("KPkts/Sec: %.2f (tcp rebuilt)\n",
01173 sfBaseStats->kpackets_rebuilt_per_sec.realtime);
01174 LogMessage("KPkts/Sec: %.2f (app layer)\n\n",
01175 sfBaseStats->kpackets_per_sec.realtime);
01176
01177 LogMessage("PatMatch: %.2f%%\n\n", sfBaseStats->patmatch_percent);
01178
01179
01180
01181
01182
01183
01184
01185
01186 #ifdef LINUX_SMP
01187
01188 for(iCtr = 0; iCtr < sfBaseStats->sfProcPidStats->iCPUs; iCtr++)
01189 {
01190 LogMessage("CPU%d Usage: %.2f%% (user) %.2f%% (sys) %.2f%% (idle)\n",
01191 iCtr,
01192 sfBaseStats->sfProcPidStats->SysCPUs[iCtr].user,
01193 sfBaseStats->sfProcPidStats->SysCPUs[iCtr].sys,
01194 sfBaseStats->sfProcPidStats->SysCPUs[iCtr].idle);
01195 }
01196 printf("\n");
01197
01198 #else
01199
01200 LogMessage("CPU Usage: %.2f%% (user) %.2f%% (sys) %.2f%% (idle)\n\n",
01201 sfBaseStats->user_cpu_time,
01202 sfBaseStats->system_cpu_time,
01203 sfBaseStats->idle_cpu_time);
01204
01205 #endif
01206
01207
01208
01209
01210 LogMessage("Alerts/Sec : %.1f\n", sfBaseStats->alerts_per_second);
01211
01212
01213 LogMessage("Syns/Sec : %.1f\n", sfBaseStats->syns_per_second);
01214 LogMessage("Syn-Acks/Sec : %.1f\n", sfBaseStats->synacks_per_second);
01215 LogMessage("New Cached Sessions/Sec: %.1f\n", sfBaseStats->new_sessions_per_second);
01216 LogMessage("Cached Sessions Del/Sec: %.1f\n", sfBaseStats->deleted_sessions_per_second);
01217 LogMessage("Current Cached Sessions: %llu\n", sfBaseStats->total_sessions);
01218 LogMessage("Max Cached Sessions : %llu\n", sfBaseStats->max_sessions);
01219
01220
01221 LogMessage("Stream Flushes/Sec : %.1f\n", sfBaseStats->stream_flushes_per_second);
01222 LogMessage("Stream Cache Faults/Sec: %llu\n", sfBaseStats->stream_faults);
01223 LogMessage("Stream Cache Timeouts : %llu\n", sfBaseStats->stream_timeouts);
01224
01225 LogMessage("Frag Creates()s/Sec : %.1f\n", sfBaseStats->frag_creates_per_second);
01226 LogMessage("Frag Completes()s/Sec : %.1f\n", sfBaseStats->frag_completes_per_second);
01227 LogMessage("Frag Inserts()s/Sec : %.1f\n", sfBaseStats->frag_inserts_per_second);
01228 LogMessage("Frag Deletes/Sec : %.1f\n", sfBaseStats->frag_deletes_per_second);
01229 LogMessage("Frag AutoFrees/Sec : %.1f\n", sfBaseStats->frag_autofrees_per_second);
01230 LogMessage("Frag Flushes/Sec : %.1f\n", sfBaseStats->frag_flushes_per_second);
01231 LogMessage("Current Cached Frags : %llu\n", sfBaseStats->current_frags);
01232 LogMessage("Max Cached Frags : %llu\n", sfBaseStats->max_frags);
01233 LogMessage("Frag Timeouts : %llu\n", sfBaseStats->frag_timeouts);
01234 LogMessage("Frag Faults : %llu\n\n", sfBaseStats->frag_faults);
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245 if(iFlags & MAX_PERF_STATS)
01246 {
01247
01248 LogMessage("Snort Maximum Performance\n");
01249 LogMessage("-------------------------\n\n");
01250
01251 LogMessage("Mbits/Second\n");
01252 LogMessage("----------------\n");
01253 LogMessage("Snort: %.2f\n",sfBaseStats->mbits_per_sec.usertime);
01254 LogMessage("Sniffing: %.2f\n",sfBaseStats->mbits_per_sec.systemtime);
01255 LogMessage("Combined: %.2f\n\n",sfBaseStats->mbits_per_sec.totaltime);
01256
01257
01258 LogMessage("uSeconds/Pkt\n");
01259 LogMessage("----------------\n");
01260 LogMessage("Snort: %.2f\n",sfBaseStats->usecs_per_packet.usertime);
01261 LogMessage("Sniffing: %.2f\n",sfBaseStats->usecs_per_packet.systemtime);
01262 LogMessage("Combined: %.2f\n\n",sfBaseStats->usecs_per_packet.totaltime);
01263
01264 LogMessage("KPkts/Second\n");
01265 LogMessage("------------------\n");
01266 LogMessage("Snort: %.2f\n",sfBaseStats->kpackets_per_sec.usertime);
01267 LogMessage("Sniffing: %.2f\n",sfBaseStats->kpackets_per_sec.systemtime);
01268 LogMessage("Combined: %.2f\n\n",sfBaseStats->kpackets_per_sec.totaltime);
01269 }
01270
01271 return 0;
01272 }
01273