Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

perf.c

Go to the documentation of this file.
00001 /*
00002 **  $Id$
00003 **
00004 **  perf.c
00005 **
00006 ** Copyright (C) 2002 Sourcefire,Inc
00007 ** Dan Roelker <droelker@sourcefire.com>
00008 **
00009 ** This program is free software; you can redistribute it and/or modify
00010 ** it under the terms of the GNU General Public License as published by
00011 ** the Free Software Foundation; either version 2 of the License, or
00012 ** (at your option) any later version.
00013 **
00014 ** This program is distributed in the hope that it will be useful,
00015 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 ** GNU General Public License for more details.
00018 **
00019 ** You should have received a copy of the GNU General Public License
00020 ** along with this program; if not, write to the Free Software
00021 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00022 **
00023 **
00024 **  DESCRIPTION
00025 **    These are the basic functions that are needed to call performance
00026 **    functions.
00027 **
00028 */
00029 
00030 #include <stdlib.h>
00031 #include <stdio.h>
00032 #include <string.h>
00033 #include <errno.h>
00034 #ifndef WIN32
00035 #include <time.h>
00036 #include <sys/types.h>
00037 #include <sys/stat.h>
00038 #endif /* WIN32 */
00039 
00040 #include "util.h"
00041 #include "perf.h"
00042 
00043 int InitPerfStats(SFPERF *sfPerf);
00044 int UpdatePerfStats(SFPERF *sfPerf, unsigned char *pucPacket, int len,
00045         int iRebuiltPkt);
00046 int ProcessPerfStats(SFPERF *sfPerf);
00047 
00048 
00049 int sfInitPerformanceStatistics(SFPERF *sfPerf)
00050 {
00051     memset(sfPerf, 0x00, sizeof(SFPERF));
00052     sfSetPerformanceSampleTime(sfPerf, 0);
00053     sfSetPerformanceStatistics(sfPerf, 0);
00054 
00055     return 0;
00056 }
00057 
00058 int sfSetPerformanceSampleTime(SFPERF *sfPerf, int iSeconds)
00059 {
00060     sfPerf->sample_time = 0;
00061     
00062     if(iSeconds < 0)
00063     {
00064         iSeconds = 0;
00065     }
00066 
00067     sfPerf->sample_interval = iSeconds;
00068 
00069     return 0;
00070 }
00071 
00072 
00073 int sfSetPerformanceAccounting(SFPERF *sfPerf, int iReset)
00074 {
00075     sfPerf->sfBase.iReset = iReset;
00076     
00077     return 0;
00078 }
00079 
00080 
00081 int sfSetPerformanceStatistics(SFPERF *sfPerf, int iFlag)
00082 {
00083     if(iFlag & SFPERF_BASE)
00084     {
00085         sfPerf->iPerfFlags = sfPerf->iPerfFlags | SFPERF_BASE;
00086     }
00087 
00088 #ifndef LINUX_SMP
00089 
00090     if(iFlag & SFPERF_BASE_MAX)
00091     {
00092         sfPerf->sfBase.iFlags |= MAX_PERF_STATS;
00093     }
00094 
00095 #endif
00096 
00097     if(iFlag & SFPERF_FLOW)
00098     {
00099         sfPerf->iPerfFlags = sfPerf->iPerfFlags | SFPERF_FLOW;
00100     }
00101     if(iFlag & SFPERF_EVENT)
00102     {
00103         sfPerf->iPerfFlags = sfPerf->iPerfFlags | SFPERF_EVENT;
00104     }
00105     if(iFlag & SFPERF_CONSOLE)
00106     {
00107         sfPerf->iPerfFlags = sfPerf->iPerfFlags | SFPERF_CONSOLE;
00108     }
00109     
00110     return 0;
00111 }
00112 int sfSetPerformanceStatisticsEx(SFPERF *sfPerf, int iFlag, void * p)
00113 {
00114 #ifndef WIN32    
00115     mode_t old_umask;
00116 #endif 
00117     
00118     if(iFlag & SFPERF_FILE)
00119     {
00120         sfPerf->iPerfFlags = sfPerf->iPerfFlags | SFPERF_FILE;
00121         
00122             strncpy( sfPerf->file, p, sizeof(sfPerf->file)-1 );
00123         sfPerf->file[sizeof(sfPerf->file)-1] = 0x00;
00124 
00125         /* this file needs to be readable by everyone */
00126 #ifndef WIN32
00127         old_umask = umask(022);
00128 #endif         
00129 
00130             sfPerf->fh = fopen(sfPerf->file,"at");
00131 
00132 #ifndef WIN32
00133         umask(old_umask);
00134 #endif
00135         
00136             if( !sfPerf->fh )
00137             return -1;
00138     }
00139     else if(iFlag & SFPERF_PKTCNT)
00140     {
00141         sfPerf->iPktCnt = *(int*)p;
00142     }
00143     else if (iFlag & SFPERF_SUMMARY)
00144     {
00145         sfPerf->iPerfFlags |= SFPERF_SUMMARY;
00146     }
00147     return 0;
00148 }
00149 
00150 
00151 #ifndef PATH_MAX
00152 #define PATH_MAX 1024
00153 #endif
00154 
00155 #ifndef FILE_MAX
00156 #define FILE_MAX  (PATH_MAX + 256)
00157 #endif
00158 
00159 int sfRotatePerformanceStatisticsFile(SFPERF *sfPerf)
00160 {
00161     int        ret;
00162     time_t     t;
00163     struct tm *tm;
00164     char       newfile[FILE_MAX];
00165     char       dir[PATH_MAX];
00166     char      *ptr;
00167 
00168     /* Close current stats file - if it is open already */
00169     if(!sfPerf->fh)
00170     {
00171         LogMessage("Performance log file '%s' not open",
00172                         sfPerf->file);
00173 
00174         return(1);
00175     }
00176     
00177     ret = fclose(sfPerf->fh);
00178     
00179     if ( ret != 0 )
00180     {
00181         FatalError("Cannot close performance log file '%s': %s\n",
00182                                     sfPerf->file, strerror(errno));
00183     }
00184     
00185     /* Rename current stats file with yesterday's date */
00186     strncpy(dir, sfPerf->file, PATH_MAX-1);
00187     ptr = strrchr(dir, '/');
00188     if ( ptr != NULL )
00189         *ptr = '\0';
00190 
00191     /* Get current time, then subtract one day to get yesterday */
00192     t = time(&t);
00193     t -= (24*60*60);
00194     tm = localtime(&t);
00195     snprintf(newfile, FILE_MAX - 1, "%s/%d-%02d-%02d",
00196                         dir, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
00197 
00198     /* Checking return code from rename */
00199     if(rename(sfPerf->file, newfile) != 0)
00200     {
00201         LogMessage("Cannot move performance log file '%s' to '%s': %s\n",
00202                         sfPerf->file, newfile,strerror(errno));
00203     }
00204 
00205     ret = sfSetPerformanceStatisticsEx(sfPerf, SFPERF_FILE, sfPerf->file);
00206 
00207     if( ret != 0 )
00208     {
00209         FatalError("Cannot open performance log file '%s': %s\n",
00210                                     sfPerf->file, strerror(errno));
00211     }
00212 
00213     return 0;
00214 }
00215 
00216 int sfPerformanceStats(SFPERF *sfPerf, unsigned char *pucPacket, int len,
00217                        int iRebuiltPkt)
00218 {
00219     static unsigned int cnt=0;
00220 
00221     if (( cnt==0 || cnt >= sfPerf->iPktCnt ) &&
00222         !(sfPerf->iPerfFlags & SFPERF_SUMMARY))
00223     {
00224        cnt=1;
00225        CheckSampleInterval(time(NULL), sfPerf);
00226     }
00227 
00228     cnt++;
00229 
00230     UpdatePerfStats(sfPerf, pucPacket, len, iRebuiltPkt);
00231 
00232     return 0;
00233 }
00234 
00235 int CheckSampleInterval(time_t curr_time, SFPERF *sfPerf)
00236 {
00237     time_t prev_time = sfPerf->sample_time;
00238 
00239     /*
00240     *  This is for when sfBasePerformance is
00241     *  starting up.
00242     */
00243     if(prev_time == 0)
00244     {
00245         InitPerfStats(sfPerf);
00246     }
00247     else if((curr_time - prev_time) >= sfPerf->sample_interval)
00248     {
00249         ProcessPerfStats(sfPerf);
00250         InitPerfStats(sfPerf);
00251     }
00252 
00253     return 0;
00254 }
00255 
00256 int InitPerfStats(SFPERF *sfPerf)
00257 {
00258     static int first = 1;
00259     /*
00260     *  Reset sample time for next sampling
00261     */
00262     sfPerf->sample_time = time(NULL);
00263 
00264     if(sfPerf->iPerfFlags & SFPERF_BASE)
00265     {  
00266         if(InitBaseStats(&(sfPerf->sfBase)))
00267             return -1;
00268     }
00269     if(sfPerf->iPerfFlags & SFPERF_FLOW)
00270     {  
00271         if(first) InitFlowStats(&(sfPerf->sfFlow));
00272         first = 0;
00273     }
00274     if(sfPerf->iPerfFlags & SFPERF_EVENT)
00275     {  
00276         InitEventStats(&(sfPerf->sfEvent));
00277     }
00278 
00279     return 0;
00280 }
00281 
00282 int UpdatePerfStats(SFPERF *sfPerf, unsigned char *pucPacket, int len,
00283                     int iRebuiltPkt)
00284 {
00285     if(sfPerf->iPerfFlags & SFPERF_BASE)
00286     {
00287         UpdateBaseStats(&(sfPerf->sfBase), len, iRebuiltPkt);
00288     }
00289     if(sfPerf->iPerfFlags & SFPERF_FLOW)
00290     {
00291         UpdateFlowStats(&(sfPerf->sfFlow), pucPacket, len, iRebuiltPkt);
00292     }
00293 
00294     return 0;
00295 }
00296 
00297 int sfProcessPerfStats(SFPERF *sfPerf)
00298 {
00299     return ProcessPerfStats(sfPerf);
00300 }
00301 
00302 int ProcessPerfStats(SFPERF *sfPerf)
00303 {
00304     if(sfPerf->iPerfFlags & SFPERF_BASE)
00305     {
00306         /* Allow this to go out to console and/or a file */
00307         ProcessBaseStats(&(sfPerf->sfBase),
00308                          sfPerf->iPerfFlags & SFPERF_CONSOLE,
00309                          sfPerf->iPerfFlags & SFPERF_FILE,
00310                          sfPerf->fh );
00311     }
00312     
00313     /* Always goes to the console */
00314     if(sfPerf->iPerfFlags & SFPERF_FLOW)
00315     {
00316         if( sfPerf->iPerfFlags & SFPERF_CONSOLE )
00317             ProcessFlowStats(&(sfPerf->sfFlow));
00318     }
00319    
00320     if(sfPerf->iPerfFlags & SFPERF_EVENT)
00321     {
00322         if( sfPerf->iPerfFlags & SFPERF_CONSOLE )
00323             ProcessEventStats(&(sfPerf->sfEvent));
00324     }
00325 
00326     return 0;
00327 }
00328     

Generated on Sun May 14 14:51:15 2006 by  doxygen 1.4.2