/*
- * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
- * 1998, 2008
* SARG Squid Analysis Report Generator http://sarg.sourceforge.net
+ * 1998, 2010
*
* SARG donations:
* please look at http://sarg.sourceforge.net/donations.php
+ * Support:
+ * http://sourceforge.net/projects/sarg/forums/forum/363374
* ---------------------------------------------------------------------
*
* This program is free software; you can redistribute it and/or modify
#include "include/conf.h"
#include "include/defs.h"
-void day_totalize(const char *tmp, const char *user, int indexonly)
+void day_totalize(const char *tmp, const struct userinfostruct *uinfo, int indexonly)
{
FILE *fp_in, *fp_ou;
- char data[20];
- char hora[20];
- char min[20];
- char elap[20];
- char odata[20];
- char ohora[20];
- char oelap[20];
- char hm[20];
- char ohm[20];
- char csort[255];
- char wdirname[MAXLEN];
- char sortout[MAXLEN];
- char arqout[MAXLEN];
- int regs=0;
- long long int telap=0;
- long long int tused=0;
- int cstatus;
+ char buf[200];
+ char date[20];
+ long long int hour;
+ long long int bytes;
+ long long int elap;
+ long long int tbytes[MAX_DATETIME_DAYS*24];
+ long long int telap[MAX_DATETIME_DAYS*24];
+ int day,month,year;
+ int daylist[MAX_DATETIME_DAYS];
+ int ndaylist;
+ int daynum;
+ int dayidx;
+ int i;
+ char wdirname[2048];
+ char arqout[2048];
+ char colsep;
+ struct getwordstruct gwarea;
if(indexonly) return;
- if(strstr(ReportType,"users_sites") == 0) return;
+ if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return;
+ if (datetimeby==0) return;
- sprintf(wdirname,"%s/%s.htmp",tmp,user);
- sprintf(arqout,"%s/%s.day",tmp,user);
- sprintf(sortout,"%s/%s.sort",tmp,user);
-
- sprintf(csort,"sort -k 1,1 -k 2,2 -o \"%s\" \"%s\"",sortout,wdirname);
- cstatus=system(csort);
- if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
- fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
- fprintf(stderr, "SARG: sort command: %s\n",csort);
- exit(1);
+ if (snprintf(wdirname,sizeof(wdirname),"%s/%s.htmp",tmp,uinfo->filename)>=sizeof(wdirname)) {
+ debuga(_("File name too long: %s/%s%s\n"),tmp,uinfo->filename,".htmp");
+ exit(EXIT_FAILURE);
}
- if((fp_in=fopen(sortout,"r"))==NULL) {
- fprintf(stderr, "SARG: (totday) %s: %s\n",text[8],sortout);
- fprintf(stderr, "SARG: sort command: %s\n",csort);
- exit(1);
+ if (snprintf(arqout,sizeof(arqout),"%s/%s.day",tmp,uinfo->filename)>=sizeof(arqout)) {
+ debuga(_("File name too long: %s/%s%s\n"),tmp,uinfo->filename,".day");
+ exit(EXIT_FAILURE);
}
- unlink(wdirname);
-
- if((fp_ou=fopen(arqout,"w"))==NULL) {
- fprintf(stderr, "SARG: (totday) %s: %s\n",text[8],arqout);
- exit(1);
+ if((fp_in=fopen(wdirname,"r"))==NULL) {
+ debuga(_("(totday) Cannot open log file %s\n"),wdirname);
+ exit(EXIT_FAILURE);
}
- while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
- if(strstr(buf,"\n") != 0)
- buf[strlen(buf)-1]='\0';
+ memset(tbytes,0,sizeof(tbytes));
+ memset(telap,0,sizeof(tbytes));
+ ndaylist=0;
- if (getword(data,sizeof(data),buf,'\t')<0 || getword(hora,sizeof(hora),buf,':')<0 ||
- getword(min,sizeof(min),buf,':')<0 || getword(elap,sizeof(elap),buf,'\t')<0 ||
- getword(elap,sizeof(elap),buf,0)<0) {
- printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",sortout);
- exit(1);
+ while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
+ fixendofline(buf);
+ getword_start(&gwarea,buf);
+ if (getword(date,sizeof(date),&gwarea,'\t')<0 || getword_atoll(&hour,&gwarea,'\t')<0) {
+ debuga(_("There is a broken record or garbage in file %s\n"),wdirname);
+ exit(EXIT_FAILURE);
}
- sprintf(hm,"%s%s",hora,min);
-
- if(!regs) {
- strcpy(odata,data);
- strcpy(ohora,hora);
- strcpy(oelap,elap);
- strcpy(ohm,hm);
- regs++;
+ if (sscanf(date,"%d/%d/%d",&day,&month,&year)!=3) continue;
+ if (day<1 || day>31 || month<1 || month>12 || year>9999) continue;
+ if (hour<0 || hour>=24) continue;
+ daynum=(year*10000)+(month*100)+day;
+ for (dayidx=0 ; dayidx<ndaylist && daynum!=daylist[dayidx] ; dayidx++);
+ if (dayidx>=ndaylist) {
+ if (dayidx>=sizeof(daylist)/sizeof(*daylist)) {
+ debuga(_("Too many different dates in %s\n"),wdirname);
+ exit(EXIT_FAILURE);
+ }
+ daylist[ndaylist++]=daynum;
}
-
- if(strcmp(hora,ohora) != 0 || strcmp(data,odata) != 0) {
- if(tused > telap)
- tused=telap;
-
- my_lltoa(telap,val1,15);
- fprintf(fp_ou,"%s\t%s\t%s\n",odata,ohora,val1);
- strcpy(odata,data);
- strcpy(ohora,hora);
- strcpy(ohm,hm);
- telap=0;
- tused=0;
+ i=dayidx*24+hour;
+ 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(strcmp(ohm,hm) != 0) {
- tused+=60000;
- strcpy(ohm,hm);
+ 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;
}
-
- telap+=my_atoll(elap);
-
}
+ fclose(fp_in);
- if(tused > telap)
- tused=telap;
-
- my_lltoa(telap,val1,15);
- fprintf(fp_ou,"%s\t%s\t%s\n",data,hora,val1);
+ if((fp_ou=fopen(arqout,"w"))==NULL) {
+ debuga(_("(totday) Cannot open log file %s\n"),arqout);
+ exit(EXIT_FAILURE);
+ }
- fclose(fp_in);
- fclose(fp_ou);
+ for (i=0 ; i<sizeof(tbytes)/sizeof(*tbytes) ; i++) {
+ if (tbytes[i]==0 && telap[i]==0) continue;
+ dayidx=i/24;
+ if (dayidx>sizeof(daylist)/sizeof(*daylist)) continue;
+ hour=i%24;
+ daynum=daylist[dayidx];
+ day=daynum%100;
+ month=(daynum/100)%100;
+ year=daynum/10000;
+ fprintf(fp_ou,"%d/%d/%d\t%lld",day,month,year,hour);
+ if ((datetimeby & DATETIME_BYTE)!=0) fprintf(fp_ou,"\t%lld",tbytes[i]);
+ if ((datetimeby & DATETIME_ELAP)!=0) fprintf(fp_ou,"\t%lld",telap[i]);
+ fputs("\n",fp_ou);
+ }
- unlink(sortout);
+ if (fclose(fp_ou)==EOF) {
+ debuga(_("Failed to close file %s - %s\n"),arqout,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ if (unlink(wdirname)==-1) {
+ debuga(_("Cannot delete temporary file %s - %s\n"),wdirname,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
return;
-
}