From: Frédéric Marchal Date: Tue, 3 Jul 2012 15:07:10 +0000 (+0200) Subject: Store the elapsed time and the number of bytes into the structure X-Git-Url: http://git.ipfire.org/?p=thirdparty%2Fsarg.git;a=commitdiff_plain;h=2c4bc22b7686fcd8a2d394d9a06beb46ff4906f6 Store the elapsed time and the number of bytes into the structure Two more variables are included in the structure to be shared among modules. --- diff --git a/include/defs.h b/include/defs.h index 25838e5..506781f 100755 --- a/include/defs.h +++ b/include/defs.h @@ -273,6 +273,8 @@ __attribute__((warn_unused_result)) int getword_multisep(/*@out@*/char *word, in __attribute__((warn_unused_result)) int getword_skip(int limit, struct getwordstruct *gwarea, char stop); __attribute__((warn_unused_result)) int getword_atoll(/*@out@*/long long int *number, struct getwordstruct *gwarea, char stop); __attribute__((warn_unused_result)) int getword_atoi(/*@out@*/int *number, struct getwordstruct *gwarea, char stop); +__attribute__((warn_unused_result)) int getword_atol(long int *number, struct getwordstruct *gwarea, char stop); +__attribute__((warn_unused_result)) int getword_atolu(unsigned long int *number, struct getwordstruct *gwarea, char stop); __attribute__((warn_unused_result)) int getword_ptr(char *orig_line,/*@out@*/char **word, struct getwordstruct *gwarea, char stop); long long int my_atoll (const char *nptr); int is_absolute(const char *path); diff --git a/include/readlog.h b/include/readlog.h index a6bf38e..2909a49 100644 --- a/include/readlog.h +++ b/include/readlog.h @@ -28,9 +28,9 @@ struct ReadLogStruct //! The URL of the visited site. char *Url; //! Time necessary to process the user's request. - unsigned int ElapsedTime; + long int ElapsedTime; //! Number of transfered bytes. - unsigned long int DataSize; + long long int DataSize; //! HTTP code returned to the user for the entry. char *HttpCode; }; diff --git a/readlog.c b/readlog.c index 79a3e93..4d380e5 100644 --- a/readlog.c +++ b/readlog.c @@ -85,7 +85,6 @@ int ReadLogFile(struct ReadLogDataStruct *Filter) char user[MAX_USER_LEN]; char data[255]; char ip[60]; - char tam[255]; char hora[30]; char mes[30]; char tbuf2[128]; @@ -116,8 +115,6 @@ int ReadLogFile(struct ReadLogDataStruct *Filter) long int totregsl=0; long int totregsg=0; long int totregsx=0; - long int nbytes; - long int elap_time; long long int iyear, imonth, iday; FILE *fp_in=NULL; FILE *fp_log=NULL; @@ -313,7 +310,6 @@ int ReadLogFile(struct ReadLogDataStruct *Filter) if((str=(char *) strchr(data, '.')) != (char *) NULL && (str=(char *) strchr(str+1, '.')) != (char *) NULL ) { strcpy(ip,data); log_entry.Ip=ip; - strcpy(elap,"0"); if(squid24) { if (getword(user,sizeof(user),&gwarea,' ')<0 || getword_skip(255,&gwarea,' ')<0) { debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq); @@ -343,7 +339,7 @@ int ReadLogFile(struct ReadLogDataStruct *Filter) debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq); exit(EXIT_FAILURE); } - if (getword(tam,sizeof(tam),&gwarea,' ')<0) { + if (getword_atoll(&log_entry.DataSize,&gwarea,' ')<0) { debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq); exit(EXIT_FAILURE); } @@ -362,9 +358,6 @@ int ReadLogFile(struct ReadLogDataStruct *Filter) if ((str = strchr(code, ':')) != NULL) *str = '/'; - if(strcmp(tam,"\0") == 0) - strcpy(tam,"0"); - ilf=ILF_Common; ilf_count[ilf]++; @@ -413,6 +406,7 @@ int ReadLogFile(struct ReadLogDataStruct *Filter) exit(EXIT_FAILURE); } if(strlen(elap) < 1) continue; + log_entry.ElapsedTime=atol(elap); if (getword(ip,sizeof(ip),&gwarea,' ')<0){ debuga(_("Maybe you have a broken client IP address in your %s file\n"),arq); exit(EXIT_FAILURE); @@ -422,7 +416,7 @@ int ReadLogFile(struct ReadLogDataStruct *Filter) debuga(_("Maybe you have a broken result code in your %s file\n"),arq); exit(EXIT_FAILURE); } - if (getword(tam,sizeof(tam),&gwarea,' ')<0){ + if (getword_atoll(&log_entry.DataSize,&gwarea,' ')<0){ debuga(_("Maybe you have a broken amount of data in your %s file\n"),arq); exit(EXIT_FAILURE); } @@ -478,7 +472,7 @@ int ReadLogFile(struct ReadLogDataStruct *Filter) debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq); exit(EXIT_FAILURE); } - if (getword(tam,sizeof(tam),&gwarea,'\t')<0){ + if (getword_atoll(&log_entry.DataSize,&gwarea,'\t')<0){ debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq); exit(EXIT_FAILURE); } @@ -486,7 +480,7 @@ int ReadLogFile(struct ReadLogDataStruct *Filter) debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq); exit(EXIT_FAILURE); } - if (getword(elap,sizeof(elap),&gwarea,'\t')<0){ + if (getword_atol(&log_entry.ElapsedTime,&gwarea,'\t')<0){ debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq); exit(EXIT_FAILURE); } @@ -585,17 +579,9 @@ int ReadLogFile(struct ReadLogDataStruct *Filter) } strcpy(hora,str); } else if (x==isa_cols[ISACOL_TimeTaken]) { - if (strlen(str)>=sizeof(elap)) { - debuga(_("Maybe you have a broken download duration in your %s file\n"),arq); - exit(EXIT_FAILURE); - } - strcpy(elap,str); + log_entry.ElapsedTime=atol(str); } else if (x==isa_cols[ISACOL_Bytes]) { - if (strlen(str)>=sizeof(tam)) { - debuga(_("Maybe you have a broken download size in your %s file\n"),arq); - exit(EXIT_FAILURE); - } - strcpy(tam,str); + log_entry.DataSize=atoll(str); } else if (x==isa_cols[ISACOL_Uri]) { full_url=str; } else if (x==isa_cols[ISACOL_Status]) { @@ -777,13 +763,11 @@ int ReadLogFile(struct ReadLogDataStruct *Filter) strcmp(log_entry.User,"") ==0 || strcmp(log_entry.User,":") ==0) continue; - nbytes=atol(tam); - if (nbytes<0) nbytes=0; + if (log_entry.DataSize<0) log_entry.DataSize=0; - elap_time=atol(elap); - if (elap_time<0) elap_time=0; - if (Filter->max_elapsed>0 && elap_time>Filter->max_elapsed) { - elap_time=0; + if (log_entry.ElapsedTime<0) log_entry.ElapsedTime=0; + if (Filter->max_elapsed>0 && log_entry.ElapsedTime>Filter->max_elapsed) { + log_entry.ElapsedTime=0; } if((str=(char *) strstr(linebuf, "[SmartFilter:")) != (char *) NULL ) { @@ -817,8 +801,8 @@ int ReadLogFile(struct ReadLogDataStruct *Filter) } } #ifdef ENABLE_DOUBLE_CHECK_DATA - ufile->user->nbytes+=nbytes; - ufile->user->elap+=elap_time; + ufile->user->nbytes+=log_entry.DataSize; + ufile->user->elap+=log_entry.ElapsedTime; #endif if (ufile->file==NULL) { @@ -850,13 +834,13 @@ int ReadLogFile(struct ReadLogDataStruct *Filter) strftime(dia, sizeof(dia), "%d/%m/%Y", log_entry.EntryTime); strftime(hora,sizeof(hora),"%H:%M:%S",log_entry.EntryTime); - if (fprintf(ufile->file, "%s\t%s\t%s\t%s\t%ld\t%s\t%ld\t%s\n",dia,hora,log_entry.Ip,url,nbytes,code,elap_time,smartfilter)<=0) { + if (fprintf(ufile->file, "%s\t%s\t%s\t%s\t%"PRIu64"\t%s\t%ld\t%s\n",dia,hora,log_entry.Ip,url,(uint64_t)log_entry.DataSize,code,log_entry.ElapsedTime,smartfilter)<=0) { debuga(_("Write error in the log file of user %s\n"),log_entry.User); exit(EXIT_FAILURE); } if(fp_log && ilf!=ILF_Sarg) - fprintf(fp_log, "%s\t%s\t%s\t%s\t%s\t%ld\t%s\t%ld\t%s\n",dia,hora,log_entry.User,log_entry.Ip,url,nbytes,code,elap_time,smartfilter); + fprintf(fp_log, "%s\t%s\t%s\t%s\t%s\t%"PRIu64"\t%s\t%ld\t%s\n",dia,hora,log_entry.User,log_entry.Ip,url,(uint64_t)log_entry.DataSize,code,log_entry.ElapsedTime,smartfilter); totregsg++; @@ -899,13 +883,13 @@ int ReadLogFile(struct ReadLogDataStruct *Filter) if(debugm){ printf("IP=\t%s\n",log_entry.Ip); printf("USER=\t%s\n",log_entry.User); - printf("ELAP=\t%ld\n",elap_time); + printf("ELAP=\t%ld\n",log_entry.ElapsedTime); printf("DATE=\t%s\n",dia); printf("TIME=\t%s\n",hora); printf("FUNC=\t%s\n",fun); printf("URL=\t%s\n",url); printf("CODE=\t%s\n",code); - printf("LEN=\t%ld\n",nbytes); + printf("LEN=\t%"PRIu64"\n",(uint64_t)log_entry.DataSize); } } diff --git a/util.c b/util.c index 419c3e6..59056ca 100644 --- a/util.c +++ b/util.c @@ -246,6 +246,85 @@ int getword_atoi(int *number, struct getwordstruct *gwarea, char stop) return(0); } +int getword_atol(long int *number, struct getwordstruct *gwarea, char stop) +{ + long int x; + long int sign=+1; + int digit; + + if (gwarea->current[0] == '-') { + gwarea->current++; + sign=-1; + } else if (gwarea->current[0] == '+') { + gwarea->current++; + } + *number=0; + for(x=0;isdigit(gwarea->current[x]);x++) { + digit=gwarea->current[x]-'0'; + if (*number > (LONG_MAX-digit)/10) { + debuga(_("Integer overflow detected in getword_atol in line %s\n"),gwarea->beginning); + return(-1); + } + *number=(*number * 10) + digit; + } + if(gwarea->current[x] && gwarea->current[x]!=stop) { + printf("SARG: getword_atol loop detected after %ld bytes.\n",x); + printf("SARG: Line=\"%s\"\n",gwarea->beginning); + printf("SARG: Record=\"%s\"\n",gwarea->current); + printf("SARG: searching for \'x%x\'\n",stop); + //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n"); +#if USE_GETWORD_BACKTRACE + getword_backtrace(); +#endif + return(-1); + } + *number*=sign; + + if (gwarea->current[x]) ++x; + gwarea->current+=x; + return(0); +} + +int getword_atolu(unsigned long int *number, struct getwordstruct *gwarea, char stop) +{ + unsigned long int x; + int digit; + + if (gwarea->current[0] == '-') { + printf("SARG: getword_atolu got a negative number.\n"); + printf("SARG: Line=\"%s\"\n",gwarea->beginning); + printf("SARG: Record=\"%s\"\n",gwarea->current); + return(-1); + } + if (gwarea->current[0] == '+') { + gwarea->current++; + } + *number=0; + for(x=0;isdigit(gwarea->current[x]);x++) { + digit=gwarea->current[x]-'0'; + if (*number > (ULONG_MAX-digit)/10) { + debuga(_("Integer overflow detected in getword_atolu in line %s\n"),gwarea->beginning); + return(-1); + } + *number=(*number * 10) + digit; + } + if(gwarea->current[x] && gwarea->current[x]!=stop) { + printf("SARG: getword_atolu loop detected after %ld bytes.\n",x); + printf("SARG: Line=\"%s\"\n",gwarea->beginning); + printf("SARG: Record=\"%s\"\n",gwarea->current); + printf("SARG: searching for \'x%x\'\n",stop); + //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n"); +#if USE_GETWORD_BACKTRACE + getword_backtrace(); +#endif + return(-1); + } + + if (gwarea->current[x]) ++x; + gwarea->current+=x; + return(0); +} + int getword_ptr(char *orig_line,char **word, struct getwordstruct *gwarea, char stop) {