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 #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
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
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
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
00186 strncpy(dir, sfPerf->file, PATH_MAX-1);
00187 ptr = strrchr(dir, '/');
00188 if ( ptr != NULL )
00189 *ptr = '\0';
00190
00191
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
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
00241
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
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
00307 ProcessBaseStats(&(sfPerf->sfBase),
00308 sfPerf->iPerfFlags & SFPERF_CONSOLE,
00309 sfPerf->iPerfFlags & SFPERF_FILE,
00310 sfPerf->fh );
00311 }
00312
00313
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