datafile.c (sarg-2.3.11) | : | datafile.c (sarg-2.4.0) | ||
---|---|---|---|---|
/* | /* | |||
* SARG Squid Analysis Report Generator http://sarg.sourceforge.net | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net | |||
* 1998, 2013 | * 1998, 2015 | |||
* | * | |||
* SARG donations: | * SARG donations: | |||
* please look at http://sarg.sourceforge.net/donations.php | * please look at http://sarg.sourceforge.net/donations.php | |||
* Support: | * Support: | |||
* http://sourceforge.net/projects/sarg/forums/forum/363374 | * http://sourceforge.net/projects/sarg/forums/forum/363374 | |||
* --------------------------------------------------------------------- | * --------------------------------------------------------------------- | |||
* | * | |||
* This program is free software; you can redistribute it and/or modify | * This program is free software; you can redistribute it and/or modify | |||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | |||
* the Free Software Foundation; either version 2 of the License, or | * the Free Software Foundation; either version 2 of the License, or | |||
skipping to change at line 30 | skipping to change at line 30 | |||
* | * | |||
* You should have received a copy of the GNU General Public License | * You should have received a copy of the GNU General Public License | |||
* along with this program; if not, write to the Free Software | * along with this program; if not, write to the Free Software | |||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |||
* | * | |||
*/ | */ | |||
#include "include/conf.h" | #include "include/conf.h" | |||
#include "include/defs.h" | #include "include/defs.h" | |||
void saverecs(FILE *fp_ou, const struct userinfostruct *uinfo, long long int nac c, char *url, long long int nbytes, char *ip, char *hora, char *dia, long long i nt nelap, long long int incache, long long int oucache); | static void saverecs(FILE *fp_ou, const struct userinfostruct *uinfo, long long int nacc, const char *url, long long int nbytes, const char *ip, const char *hor a, const char *dia, long long int nelap, long long int incache, long long int ou cache); | |||
void data_file(char *tmp) | void data_file(char *tmp) | |||
{ | { | |||
FILE *fp_in, *fp_ou=NULL; | FileObject *fp_in; | |||
FILE *fp_ou=NULL; | ||||
char *buf; | char *buf; | |||
char accdia[11], acchora[9], accip[MAXLEN], *accurl; | char accdia[11], acchora[9], accip[MAXLEN], *accurl; | |||
char oldaccdia[11], oldacchora[9], oldaccip[MAXLEN]; | char oldaccdia[11], oldacchora[9], oldaccip[MAXLEN]; | |||
char *oldurl; | char *oldurl; | |||
char acccode[50], oldacccode[50]; | char acccode[50], oldacccode[50]; | |||
char ipantes[MAXLEN], nameantes[MAXLEN]; | char ipantes[MAXLEN], nameantes[MAXLEN]; | |||
char accsmart[MAXLEN]; | char accsmart[MAXLEN]; | |||
char crc2[50]; | char crc2[50]; | |||
char *str; | char *str; | |||
char tmp3[MAXLEN]; | char tmp3[MAXLEN]; | |||
char u2[MAX_USER_LEN]; | char u2[MAX_USER_LEN]; | |||
char userlabel[MAX_USER_LEN]; | ||||
userscan uscan; | userscan uscan; | |||
long long int nbytes=0; | long long int nbytes=0; | |||
long long int nelap=0; | long long int nelap=0; | |||
long long int nacc=0; | long long int nacc=0; | |||
int rtotal=0; | int rtotal=0; | |||
long long int incache=0; | long long int incache=0; | |||
long long int oucache=0; | long long int oucache=0; | |||
long long int accbytes; | long long int accbytes; | |||
long long int accelap; | long long int accelap; | |||
int new_user; | int new_user; | |||
int same_url; | int same_url; | |||
int url_len; | int url_len; | |||
int ourl_size; | int ourl_size; | |||
struct getwordstruct gwarea; | struct getwordstruct gwarea; | |||
struct userinfostruct *uinfo; | struct userinfostruct *uinfo; | |||
longline line; | longline line; | |||
init_usertab(UserTabFile); | ||||
ipantes[0]='\0'; | ipantes[0]='\0'; | |||
nameantes[0]='\0'; | nameantes[0]='\0'; | |||
oldurl=NULL; | oldurl=NULL; | |||
ourl_size=0; | ourl_size=0; | |||
uscan=userinfo_startscan(); | uscan=userinfo_startscan(); | |||
if (uscan == NULL) { | if (uscan == NULL) { | |||
debuga(_("Cannot enumerate the user list\n")); | debuga(__FILE__,__LINE__,_("Cannot enumerate the user list\n")); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
while ( (uinfo = userinfo_advancescan(uscan)) != NULL ) { | while ( (uinfo = userinfo_advancescan(uscan)) != NULL ) { | |||
strcpy(u2,uinfo->id); | strcpy(u2,uinfo->id); | |||
if(Ip2Name && uinfo->id_is_ip) { | if (Ip2Name && uinfo->id_is_ip) { | |||
strcpy(ipantes,u2); | strcpy(ipantes,u2); | |||
ip2name(u2,sizeof(u2)); | ip2name(u2,sizeof(u2)); | |||
strcpy(nameantes,u2); | strcpy(nameantes,u2); | |||
} | } | |||
user_find(uinfo->label,MAX_USER_LEN, u2); | user_find(userlabel,MAX_USER_LEN, u2); | |||
if(debug) debuga(_("Reading user file \"%s/%s\"\n"),tmp,uinfo->fi | userinfo_label(uinfo,userlabel); | |||
lename); | if (debug) debuga(__FILE__,__LINE__,_("Reading user file \"%s/%s\ | |||
"\n"),tmp,uinfo->filename); | ||||
sort_users_log(tmp,debug,uinfo); | sort_users_log(tmp,debug,uinfo); | |||
if (snprintf(tmp3,sizeof(tmp3),"%s/%s.user_log",tmp,uinfo->filena me)>=sizeof(tmp3)) { | if (snprintf(tmp3,sizeof(tmp3),"%s/%s.user_log",tmp,uinfo->filena me)>=sizeof(tmp3)) { | |||
debugapos("datafile",_("Path too long: ")); | debuga(__FILE__,__LINE__,_("Path too long: ")); | |||
debuga_more("%s/%s.user_log\n",tmp,uinfo->filename); | debuga_more("%s/%s.user_log\n",tmp,uinfo->filename); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
if((fp_in=MY_FOPEN(tmp3,"r"))==NULL){ | if ((fp_in=FileObject_Open(tmp3))==NULL){ | |||
debugapos("datafile",_("Cannot open file \"%s\": %s\n"),t | debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n | |||
mp3,strerror(errno)); | "),tmp3,FileObject_GetLastOpenError()); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
if ((line=longline_create())==NULL) { | if ((line=longline_create())==NULL) { | |||
debuga(_("Not enough memory to read file \"%s\"\n"),tmp3) ; | debuga(__FILE__,__LINE__,_("Not enough memory to read fil e \"%s\"\n"),tmp3); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
ttopen=0; | ttopen=0; | |||
new_user=1; | new_user=1; | |||
while((buf=longline_read(fp_in,line))!=NULL) { | while((buf=longline_read(fp_in,line))!=NULL) { | |||
getword_start(&gwarea,buf); | getword_start(&gwarea,buf); | |||
if (getword(accdia,sizeof(accdia),&gwarea,'\t')<0 || getw ord(acchora,sizeof(acchora),&gwarea,'\t')<0 || | if (getword(accdia,sizeof(accdia),&gwarea,'\t')<0 || getw ord(acchora,sizeof(acchora),&gwarea,'\t')<0 || | |||
getword(accip,sizeof(accip),&gwarea,'\t')<0 || | getword(accip,sizeof(accip),&gwarea,'\t')<0 || | |||
getword_ptr(buf,&accurl,&gwarea,'\t')<0 || getword_at oll(&accbytes,&gwarea,'\t')<0 || | getword_ptr(buf,&accurl,&gwarea,'\t')<0 || getword_at oll(&accbytes,&gwarea,'\t')<0 || | |||
getword(acccode,sizeof(acccode),&gwarea,'\t')<0 || ge tword_atoll(&accelap,&gwarea,'\t')<0) { | getword(acccode,sizeof(acccode),&gwarea,'\t')<0 || ge tword_atoll(&accelap,&gwarea,'\t')<0) { | |||
debuga(_("Invalid record in file \"%s\"\n"),tmp3) ; | debuga(__FILE__,__LINE__,_("Invalid record in fil e \"%s\"\n"),tmp3); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
if (getword_skip(20000,&gwarea,'"')<0 || getword(accsmart ,sizeof(accsmart),&gwarea,'"')<0) { | if (getword_skip(20000,&gwarea,'"')<0 || getword(accsmart ,sizeof(accsmart),&gwarea,'"')<0) { | |||
debuga(_("Invalid smart info in file \"%s\"\n"),t mp3); | debuga(__FILE__,__LINE__,_("Invalid smart info in file \"%s\"\n"),tmp3); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
if(Ip2Name) { | if (Ip2Name) { | |||
if(strcmp(accip,ipantes) != 0) { | if (strcmp(accip,ipantes) != 0) { | |||
strcpy(ipantes,accip); | strcpy(ipantes,accip); | |||
ip2name(accip,sizeof(accip)); | ip2name(accip,sizeof(accip)); | |||
strcpy(nameantes,accip); | strcpy(nameantes,accip); | |||
} | } | |||
else strcpy(accip,nameantes); | else strcpy(accip,nameantes); | |||
} | } | |||
if(!rtotal){ | if (!rtotal){ | |||
url_len=strlen(accurl); | url_len=strlen(accurl); | |||
if (!oldurl || url_len>=ourl_size) { | if (!oldurl || url_len>=ourl_size) { | |||
ourl_size=url_len+1; | ourl_size=url_len+1; | |||
oldurl=realloc(oldurl,ourl_size); | oldurl=realloc(oldurl,ourl_size); | |||
if (!oldurl) { | if (!oldurl) { | |||
debuga(_("Not enough memory to st ore the url\n")); | debuga(__FILE__,__LINE__,_("Not e nough memory to store the url\n")); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
} | } | |||
strcpy(oldurl,accurl); | strcpy(oldurl,accurl); | |||
strcpy(oldacccode,acccode); | strcpy(oldacccode,acccode); | |||
strcpy(oldaccip,accip); | strcpy(oldaccip,accip); | |||
strcpy(oldaccdia,accdia); | strcpy(oldaccdia,accdia); | |||
strcpy(oldacchora,acchora); | strcpy(oldacchora,acchora); | |||
new_user=0; | new_user=0; | |||
rtotal++; | rtotal++; | |||
} | } | |||
same_url=(strcmp(oldurl,accurl) == 0); | same_url=(strcmp(oldurl,accurl) == 0); | |||
if(!same_url || new_user){ | if (!same_url || new_user){ | |||
if(!fp_ou && (fp_ou=MY_FOPEN(DataFile,"w"))==NULL | if (!fp_ou && (fp_ou=MY_FOPEN(DataFile,"w"))==NUL | |||
){ | L){ | |||
debugapos("datafile",_("Cannot open file | debuga(__FILE__,__LINE__,_("Cannot open f | |||
\"%s\": %s\n"),DataFile,strerror(errno)); | ile \"%s\": %s\n"),DataFile,strerror(errno)); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
saverecs(fp_ou,uinfo,nacc,oldurl,nbytes,oldaccip, oldacchora,oldaccdia,nelap,incache,oucache); | saverecs(fp_ou,uinfo,nacc,oldurl,nbytes,oldaccip, oldacchora,oldaccdia,nelap,incache,oucache); | |||
nacc=0; | nacc=0; | |||
nbytes=0; | nbytes=0; | |||
nelap=0; | nelap=0; | |||
incache=0; | incache=0; | |||
oucache=0; | oucache=0; | |||
new_user=0; | new_user=0; | |||
} | } | |||
nacc++; | nacc++; | |||
nbytes+=accbytes; | nbytes+=accbytes; | |||
nelap+=accelap; | nelap+=accelap; | |||
strcpy(crc2,acccode); | strcpy(crc2,acccode); | |||
str=strchr(crc2,'/'); | str=strchr(crc2,'/'); | |||
if (str) *str='\0'; | if (str) *str='\0'; | |||
if(strstr(crc2,"MISS") != 0) oucache+=accbytes; | if (strstr(crc2,"MISS") != 0) oucache+=accbytes; | |||
else incache+=accbytes; | else incache+=accbytes; | |||
if (!same_url) { | if (!same_url) { | |||
url_len=strlen(accurl); | url_len=strlen(accurl); | |||
if (url_len>=ourl_size) { | if (url_len>=ourl_size) { | |||
ourl_size=url_len+1; | ourl_size=url_len+1; | |||
oldurl=realloc(oldurl,ourl_size); | oldurl=realloc(oldurl,ourl_size); | |||
if (!oldurl) { | if (!oldurl) { | |||
debuga(_("Not enough memory to st ore the url\n")); | debuga(__FILE__,__LINE__,_("Not e nough memory to store the url\n")); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
} | } | |||
strcpy(oldurl,accurl); | strcpy(oldurl,accurl); | |||
} | } | |||
new_user=0; | new_user=0; | |||
strcpy(oldacccode,acccode); | strcpy(oldacccode,acccode); | |||
strcpy(oldaccip,accip); | strcpy(oldaccip,accip); | |||
strcpy(oldaccdia,accdia); | strcpy(oldaccdia,accdia); | |||
strcpy(oldacchora,acchora); | strcpy(oldacchora,acchora); | |||
} | } | |||
fclose(fp_in); | if (FileObject_Close(fp_in)) { | |||
debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"), | ||||
tmp3,FileObject_GetLastCloseError()); | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
longline_destroy(&line); | longline_destroy(&line); | |||
} | } | |||
userinfo_stopscan(uscan); | userinfo_stopscan(uscan); | |||
if (oldurl) free(oldurl); | if (oldurl) free(oldurl); | |||
if (fp_ou) fclose(fp_ou); | if (fp_ou && fclose(fp_ou)==EOF) { | |||
debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),DataFil | ||||
e,strerror(errno)); | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
if(debug) | if (debug) | |||
debuga(_("Datafile \"%s\" written successfully\n"),DataFile); | debuga(__FILE__,__LINE__,_("Datafile \"%s\" written successfully\ | |||
n"),DataFile); | ||||
} | } | |||
void saverecs(FILE *fp_ou, const struct userinfostruct *uinfo, long long int nac c, char *url, long long int nbytes, char *ip, char *hora, char *dia, long long i nt nelap, long long int incache, long long int oucache) | static void saverecs(FILE *fp_ou, const struct userinfostruct *uinfo, long long int nacc, const char *url, long long int nbytes, const char *ip, const char *hor a, const char *dia, long long int nelap, long long int incache, long long int ou cache) | |||
{ | { | |||
char val[20]; | char val[20]; | |||
char name[512]; | char name[512]; | |||
if((DataFileFields & DATA_FIELD_USER) != 0) { | if ((DataFileFields & DATA_FIELD_USER) != 0) { | |||
fputs(uinfo->label,fp_ou); | fputs(uinfo->label,fp_ou); | |||
fputc(DataFileDelimiter[0],fp_ou); | fputc(DataFileDelimiter[0],fp_ou); | |||
} | } | |||
if((DataFileFields & DATA_FIELD_DATE) != 0) { | if ((DataFileFields & DATA_FIELD_DATE) != 0) { | |||
fputs(dia,fp_ou); | fputs(dia,fp_ou); | |||
fputc(DataFileDelimiter[0],fp_ou); | fputc(DataFileDelimiter[0],fp_ou); | |||
} | } | |||
if((DataFileFields & DATA_FIELD_TIME) != 0) { | if ((DataFileFields & DATA_FIELD_TIME) != 0) { | |||
fputs(hora,fp_ou); | fputs(hora,fp_ou); | |||
fputc(DataFileDelimiter[0],fp_ou); | fputc(DataFileDelimiter[0],fp_ou); | |||
} | } | |||
if((DataFileFields & DATA_FIELD_URL) != 0) { | if ((DataFileFields & DATA_FIELD_URL) != 0) { | |||
url_hostname(url,name,sizeof(name)); | url_hostname(url,name,sizeof(name)); | |||
if (DataFileUrl == DATAFILEURL_IP) name2ip(name,sizeof(name)); | if (DataFileUrl == DATAFILEURL_IP && name[0]!=ALIAS_PREFIX) name2 ip(name,sizeof(name)); | |||
fputs(name,fp_ou); | fputs(name,fp_ou); | |||
fputc(DataFileDelimiter[0],fp_ou); | fputc(DataFileDelimiter[0],fp_ou); | |||
} | } | |||
if((DataFileFields & DATA_FIELD_CONNECT) != 0) { | if ((DataFileFields & DATA_FIELD_CONNECT) != 0) { | |||
my_lltoa(nacc,val,sizeof(val),0); | my_lltoa(nacc,val,sizeof(val),0); | |||
fputs(val,fp_ou); | fputs(val,fp_ou); | |||
fputc(DataFileDelimiter[0],fp_ou); | fputc(DataFileDelimiter[0],fp_ou); | |||
} | } | |||
if((DataFileFields & DATA_FIELD_BYTES) != 0) { | if ((DataFileFields & DATA_FIELD_BYTES) != 0) { | |||
my_lltoa(nbytes,val,sizeof(val),0); | my_lltoa(nbytes,val,sizeof(val),0); | |||
fputs(val,fp_ou); | fputs(val,fp_ou); | |||
fputc(DataFileDelimiter[0],fp_ou); | fputc(DataFileDelimiter[0],fp_ou); | |||
} | } | |||
if((DataFileFields & DATA_FIELD_IN_CACHE) != 0) { | if ((DataFileFields & DATA_FIELD_IN_CACHE) != 0) { | |||
my_lltoa(incache,val,sizeof(val),0); | my_lltoa(incache,val,sizeof(val),0); | |||
fputs(val,fp_ou); | fputs(val,fp_ou); | |||
fputc(DataFileDelimiter[0],fp_ou); | fputc(DataFileDelimiter[0],fp_ou); | |||
} | } | |||
if((DataFileFields & DATA_FIELD_OUT_CACHE) != 0) { | if ((DataFileFields & DATA_FIELD_OUT_CACHE) != 0) { | |||
my_lltoa(oucache,val,sizeof(val),0); | my_lltoa(oucache,val,sizeof(val),0); | |||
fputs(val,fp_ou); | fputs(val,fp_ou); | |||
fputc(DataFileDelimiter[0],fp_ou); | fputc(DataFileDelimiter[0],fp_ou); | |||
} | } | |||
if((DataFileFields & DATA_FIELD_ELAPSED) != 0) { | if ((DataFileFields & DATA_FIELD_ELAPSED) != 0) { | |||
my_lltoa(nelap,val,sizeof(val),0); | my_lltoa(nelap,val,sizeof(val),0); | |||
fputs(val,fp_ou); | fputs(val,fp_ou); | |||
fputc(DataFileDelimiter[0],fp_ou); | fputc(DataFileDelimiter[0],fp_ou); | |||
} | } | |||
fputc('\n',fp_ou); | fputc('\n',fp_ou); | |||
} | } | |||
End of changes. 33 change blocks. | ||||
41 lines changed or deleted | 55 lines changed or added |