X-Git-Url: http://git.ipfire.org/?a=blobdiff_plain;f=repday.c;h=bed4b7cb89d77f683ec642089cdeab7f25793d93;hb=384139e1887e55e60d7e5b7852cb45a59b4d5367;hp=4a610f1943fd9b8582142a565d43ac6db8e65d98;hpb=324ba7f325ee78a8ab69da64c6f8c2c2967606d1;p=thirdparty%2Fsarg.git diff --git a/repday.c b/repday.c index 4a610f1..bed4b7c 100644 --- a/repday.c +++ b/repday.c @@ -1,6 +1,6 @@ /* * SARG Squid Analysis Report Generator http://sarg.sourceforge.net - * 1998, 2010 + * 1998, 2015 * * SARG donations: * please look at http://sarg.sourceforge.net/donations.php @@ -31,201 +31,225 @@ extern numlist hours; void report_day(const struct userinfostruct *uinfo) { - - FILE *fp_in, *fp_ou; - - char buf[200]; - char data[20]; - long long int hour; - const char *label; - char arqout[MAXLEN]; - char wdirname[2048]; - char colsep; - int ihour=0; - int day,month,year; - int daylist[MAX_DATETIME_DAYS]; - int daysort[MAX_DATETIME_DAYS]; - int ndaylist; - int daynum; - int dayidx; - long long int bytes; - long long int elap; - long long int tbytes[MAX_DATETIME_DAYS*24]; - long long int telap[MAX_DATETIME_DAYS*24]; - long long int tt; - long long int tttime[24]; - int i, j; - struct getwordstruct gwarea; - struct tm t; - - snprintf(wdirname,sizeof(wdirname),"%s/%s.day",tmp,uinfo->filename); - if(access(wdirname, R_OK) != 0) return; - - if (snprintf(arqout,sizeof(arqout),"%s/%s/d%s.html",outdirname,uinfo->filename,uinfo->filename)>=sizeof(arqout)) { - debuga(_("Output file name too long: %s/%s/d%s.html\n"),outdirname,uinfo->filename,uinfo->filename); - exit(EXIT_FAILURE); - } - - if((fp_in=fopen(wdirname,"r"))==NULL) { - debuga(_("(repday) Cannot open log file %s\n"),wdirname); - exit(EXIT_FAILURE); - } - - memset(tbytes,0,sizeof(tbytes)); - memset(telap,0,sizeof(telap)); - ndaylist=0; - - while(fgets(buf,sizeof(buf),fp_in)!=NULL) { - fixendofline(buf); - getword_start(&gwarea,buf); - if (getword(data,sizeof(data),&gwarea,'\t')<0) { - debuga(_("Invalid date in file %s\n"),wdirname); - exit(EXIT_FAILURE); - } - if (sscanf(data,"%d/%d/%d",&day,&month,&year)!=3) continue; - if (day<1 || day>31 || month<1 || month>12 || year>9999) continue; - daynum=(year*10000)+(month*100)+day; - for (dayidx=0 ; dayidx=ndaylist) { - if (dayidx>=sizeof(daylist)/sizeof(*daylist)) { - debuga(_("Too many different dates in %s\n"),wdirname); - exit(EXIT_FAILURE); - } - daylist[ndaylist++]=daynum; - } - - if (getword_atoll(&hour,&gwarea,'\t')<0) { - debuga(_("Invalid time in file %s\n"),wdirname); - exit(EXIT_FAILURE); - } - ihour=(int)hour; - i=dayidx*24+ihour; - - if ((datetimeby & DATETIME_BYTE)!=0) { - colsep=((datetimeby & DATETIME_ELAP)!=0) ? '\t' : '\0'; - if (getword_atoll(&bytes,&gwarea,colsep)<0) { - debuga(_("Invalid number of bytes in file %s\n"),wdirname); - exit(EXIT_FAILURE); - } - tbytes[i]+=bytes; - } - if ((datetimeby & DATETIME_ELAP)!=0) { - if (getword_atoll(&elap,&gwarea,'\0')<0) { - debuga(_("Invalid elapsed time in file %s\n"),wdirname); - exit(EXIT_FAILURE); - } - telap[i]+=elap; - } - } - fclose(fp_in); - - if((fp_ou=fopen(arqout,"w"))==NULL) { - debuga(_("(repday) Cannot open log file %s\n"),arqout); - exit(EXIT_FAILURE); - } - - write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Day report")); - fprintf(fp_ou,"%s: %s\n",_("Period"),period.html); - fprintf(fp_ou,"%s: %s\n",_("User"),uinfo->label); - close_html_header(fp_ou); - - dayidx=0; - for (i=0 ; i0 && daynum\n", fp_ou); - fputs("",fp_ou); - for( i = 0; i < hours.len; i++ ) - fprintf(fp_ou, "%02d%s
%s\n", hours.list[ i ], _("H"), label ); - fprintf(fp_ou, "%s
%s\n", _("TOTAL"), label ); - - memset(tttime,0,sizeof(tttime)); - for (dayidx=0 ; dayidx%s\n", data ); - tt=0; - for( i = 0; i < hours.len; i++ ) { - ihour=hours.list[i]; - if (tbytes[daynum*24+ihour]>0) { - fprintf(fp_ou, "%s\n",fixnum(tbytes[daynum*24+ihour],1)); - tt+=tbytes[daynum*24+ihour]; - tttime[ihour]+=tbytes[daynum*24+ihour]; - } else - fputs("\n",fp_ou); - } - fprintf(fp_ou, "%s\n",fixnum(tt,1)); - } - - fprintf(fp_ou, "%s\n", _("TOTAL") ); - tt=0; - for( i = 0; i < hours.len; i++ ) { - if (tttime[i]>0) { - fprintf(fp_ou, "%s\n",fixnum(tttime[i],1)); - tt+=tttime[i]; - } else - fputs("\n",fp_ou); - } - fprintf(fp_ou, "%s\n",fixnum(tt,1)); - fputs("\n",fp_ou); - } - - if((datetimeby & DATETIME_ELAP)!=0) { - label=_("H:M:S"); - fputs("\n", fp_ou); - fputs("",fp_ou); - for( i = 0; i < hours.len; i++ ) - fprintf(fp_ou, "\n", hours.list[ i ], _("H"), label ); - fprintf(fp_ou, "\n", _("TOTAL"), label ); - - memset(tttime,0,sizeof(tttime)); - for (dayidx=0 ; dayidx\n", data ); - tt=0; - for( i = 0; i < hours.len; i++ ) { - ihour=hours.list[i]; - if (telap[daynum*24+ihour]>0) { - fprintf(fp_ou, "\n",fixtime(telap[daynum*24+ihour])); - tt+=telap[daynum*24+ihour]; - tttime[ihour]+=telap[daynum*24+ihour]; - } else - fputs("\n",fp_ou); - } - fprintf(fp_ou, "\n",fixtime(tt)); - } - - fprintf(fp_ou, "\n", _("TOTAL") ); - tt=0; - for( i = 0; i < hours.len; i++ ) { - if (tttime[i]>0) { - fprintf(fp_ou, "\n",fixtime(tttime[i])); - tt+=tttime[i]; - } else - fputs("\n",fp_ou); - } - fprintf(fp_ou, "\n",fixtime(tt)); - fputs("
%02d%s
%s
%s
%s
%s%s%s
%s%s%s
\n",fp_ou); - } - - if (write_html_trailer(fp_ou)<0) - debuga(_("Write error in file %s\n"),arqout); - if (fclose(fp_ou)==EOF) - debuga(_("Failed to close file %s - %s\n"),arqout,strerror(errno)); - return; + FILE *fp_in, *fp_ou; + + char buf[200]; + char data[20]; + long long int hour; + const char *label; + char arqout[MAXLEN]; + char wdirname[2048]; + char colsep; + int ihour=0; + int day,month,year; + int daylist[MAX_DATETIME_DAYS]; + int daysort[MAX_DATETIME_DAYS]; + int ndaylist; + int daynum; + int dayidx; + long long int bytes; + long long int elap; + long long int tbytes[MAX_DATETIME_DAYS*24]; + long long int telap[MAX_DATETIME_DAYS*24]; + long long int tt; + long long int tttime[24]; + int i, j; + struct getwordstruct gwarea; + struct tm t; + + if (datetimeby==0) return; + snprintf(wdirname,sizeof(wdirname),"%s/%s.day",tmp,uinfo->filename); + if(access(wdirname, R_OK) != 0) return; + + if (snprintf(arqout,sizeof(arqout),"%s/%s/d%s.html",outdirname,uinfo->filename,uinfo->filename)>=sizeof(arqout)) { + debuga(__FILE__,__LINE__,_("Path too long: ")); + debuga_more("%s/%s/d%s.html\n",outdirname,uinfo->filename,uinfo->filename); + exit(EXIT_FAILURE); + } + + if((fp_in=fopen(wdirname,"r"))==NULL) { + debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),wdirname,strerror(errno)); + exit(EXIT_FAILURE); + } + + memset(tbytes,0,sizeof(tbytes)); + memset(telap,0,sizeof(telap)); + ndaylist=0; + + while(fgets(buf,sizeof(buf),fp_in)!=NULL) { + fixendofline(buf); + getword_start(&gwarea,buf); + if (getword(data,sizeof(data),&gwarea,'\t')<0) { + debuga(__FILE__,__LINE__,_("Invalid date in file \"%s\"\n"),wdirname); + exit(EXIT_FAILURE); + } + if (sscanf(data,"%d/%d/%d",&day,&month,&year)!=3) continue; + if (day<1 || day>31 || month<1 || month>12 || year>9999) continue; + daynum=(year*10000)+(month*100)+day; + for (dayidx=0 ; dayidx=ndaylist) { + if (dayidx>=sizeof(daylist)/sizeof(*daylist)) { + debuga(__FILE__,__LINE__,_("Too many different dates in file \"%s\"\n"),wdirname); + exit(EXIT_FAILURE); + } + daylist[ndaylist++]=daynum; + } + + if (getword_atoll(&hour,&gwarea,'\t')<0) { + debuga(__FILE__,__LINE__,_("Invalid time in file \"%s\"\n"),wdirname); + exit(EXIT_FAILURE); + } + ihour=(int)hour; + i=dayidx*24+ihour; + + if ((datetimeby & DATETIME_BYTE)!=0) { + colsep=((datetimeby & DATETIME_ELAP)!=0) ? '\t' : '\0'; + if (getword_atoll(&bytes,&gwarea,colsep)<0) { + debuga(__FILE__,__LINE__,_("Invalid number of bytes in file \"%s\"\n"),wdirname); + exit(EXIT_FAILURE); + } + tbytes[i]+=bytes; + } + if ((datetimeby & DATETIME_ELAP)!=0) { + if (getword_atoll(&elap,&gwarea,'\0')<0) { + debuga(__FILE__,__LINE__,_("Invalid elapsed time in file \"%s\"\n"),wdirname); + exit(EXIT_FAILURE); + } + telap[i]+=elap; + } + } + if (fclose(fp_in)==EOF) { + debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),wdirname,strerror(errno)); + exit(EXIT_FAILURE); + } + + if((fp_ou=fopen(arqout,"w"))==NULL) { + debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),arqout,strerror(errno)); + exit(EXIT_FAILURE); + } + + write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Day report"),HTML_JS_NONE); + fprintf(fp_ou,"%s: %s\n",_("Period"),period.html); + fprintf(fp_ou,"%s: %s\n",_("User"),uinfo->label); + close_html_header(fp_ou); + + dayidx=0; + for (i=0 ; i0 && daynum\n", fp_ou); + fputs("",fp_ou); + for( i = 0; i < hours.len; i++ ) { + /* TRANSLATORS: It is an hour in the hourly report */ + sprintf(data,_("%02dH"),hours.list[ i ]); + fprintf(fp_ou, "%s
%s\n", data, label ); + } + fprintf(fp_ou, "%s
%s\n", _("TOTAL"), label ); + + memset(tttime,0,sizeof(tttime)); + for (dayidx=0 ; dayidx%s\n", data ); + tt=0; + for( i = 0; i < hours.len; i++ ) { + ihour=hours.list[i]; + if (tbytes[daynum*24+ihour]>0) { + fprintf(fp_ou, "%s\n",fixnum(tbytes[daynum*24+ihour],1)); + tt+=tbytes[daynum*24+ihour]; + tttime[ihour]+=tbytes[daynum*24+ihour]; + } else + fputs("\n",fp_ou); + } + fprintf(fp_ou, "%s\n",fixnum(tt,1)); + } + + fprintf(fp_ou, "%s\n", _("TOTAL") ); + tt=0; + for( i = 0; i < hours.len; i++ ) { + if (tttime[i]>0) { + fprintf(fp_ou, "%s\n",fixnum(tttime[i],1)); + tt+=tttime[i]; + } else + fputs("\n",fp_ou); + } + fprintf(fp_ou, "%s\n",fixnum(tt,1)); + fputs("\n",fp_ou); +#ifdef ENABLE_DOUBLE_CHECK_DATA + if (tt!=uinfo->nbytes) { + debuga(__FILE__,__LINE__,_("Total downloaded bytes is %"PRIi64" instead of %"PRIi64" in the hourly report of user %s\n"), + (int64_t)tt,(int64_t)uinfo->nbytes,uinfo->label); + exit(EXIT_FAILURE); + } +#endif + } + + if((datetimeby & DATETIME_ELAP)!=0) { + label=_("H:M:S"); + fputs("\n", fp_ou); + fputs("",fp_ou); + for( i = 0; i < hours.len; i++ ) { + sprintf(data,_("%02dH"),hours.list[ i ]); + fprintf(fp_ou, "\n", data, label ); + } + fprintf(fp_ou, "\n", _("TOTAL"), label ); + + memset(tttime,0,sizeof(tttime)); + for (dayidx=0 ; dayidx\n", data ); + tt=0; + for( i = 0; i < hours.len; i++ ) { + ihour=hours.list[i]; + if (telap[daynum*24+ihour]>0) { + fprintf(fp_ou, "\n",fixtime(telap[daynum*24+ihour])); + tt+=telap[daynum*24+ihour]; + tttime[ihour]+=telap[daynum*24+ihour]; + } else + fputs("\n",fp_ou); + } + fprintf(fp_ou, "\n",fixtime(tt)); + } + + fprintf(fp_ou, "\n", _("TOTAL") ); + tt=0; + for( i = 0; i < hours.len; i++ ) { + if (tttime[i]>0) { + fprintf(fp_ou, "\n",fixtime(tttime[i])); + tt+=tttime[i]; + } else + fputs("\n",fp_ou); + } + fprintf(fp_ou, "\n",fixtime(tt)); + fputs("
%s
%s
%s
%s
%s%s%s
%s%s%s
\n",fp_ou); +#ifdef ENABLE_DOUBLE_CHECK_DATA + if (tt!=uinfo->elap) { + debuga(__FILE__,__LINE__,_("Total elapsed time is %"PRIi64" instead of %"PRIi64" in the hourly report of user %s\n"), + (int64_t)tt,(int64_t)uinfo->elap,uinfo->label); + exit(EXIT_FAILURE); + } +#endif + } + + write_html_trailer(fp_ou); + if (fclose(fp_ou)==EOF) { + debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),arqout,strerror(errno)); + exit(EXIT_FAILURE); + } + return; }