]> git.ipfire.org Git - thirdparty/sarg.git/blame - datafile.c
Update the po files
[thirdparty/sarg.git] / datafile.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
1164c474 3 * 1998, 2010
25697a35
GS
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
1164c474
FM
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
25697a35
GS
9 * ---------------------------------------------------------------------
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
24 *
25 */
26
27#include "include/conf.h"
5f3cfd1d 28#include "include/defs.h"
25697a35 29
f84a35a3 30void saverecs(FILE *fp_ou, const struct userinfostruct *uinfo, long long int nacc, char *url, long long int nbytes, char *ip, char *hora, char *dia, long long int nelap, long long int incache, long long int oucache);
25697a35
GS
31
32void data_file(char *tmp)
33{
34
f84a35a3 35 FILE *fp_in, *fp_ou=NULL;
25697a35 36
f84a35a3
FM
37 char *buf;
38 char accdia[11], acchora[9], accip[MAXLEN], *accurl;
d6e703cc 39 char oldaccdia[11], oldacchora[9], oldaccip[MAXLEN];
f84a35a3 40 char *oldurl;
94558017 41 char olduser[MAXLEN], acccode[50], oldacccode[50];
5542cb59 42 char ipantes[MAXLEN], nameantes[MAXLEN];
25697a35 43 char accsmart[MAXLEN];
25697a35 44 char crc2[50];
9c7c6346 45 char *str;
2240dcea 46 char tmp3[MAXLEN];
f2ec8c75
FM
47 char user[MAX_USER_LEN];
48 char u2[MAX_USER_LEN];
25697a35
GS
49 DIR *dirp;
50 struct dirent *direntp;
5542cb59
FM
51 long long int nbytes=0;
52 long long int nelap=0;
25697a35 53 long long int nacc=0;
f84a35a3 54 int rtotal=0;
25697a35
GS
55 long long int incache=0;
56 long long int oucache=0;
6e792ade
FM
57 long long int accbytes;
58 long long int accelap;
c0ec9cc7 59 int dlen;
f2ec8c75 60 int new_user;
f84a35a3
FM
61 int same_url;
62 int url_len;
63 int ourl_size;
9c7c6346 64 struct getwordstruct gwarea;
f2ec8c75 65 struct userinfostruct *uinfo;
afaa3b67 66 longline line;
c0ec9cc7 67 const char logext[]=".log";
25697a35
GS
68
69 ipantes[0]='\0';
70 nameantes[0]='\0';
71
72 olduser[0]='\0';
2240dcea 73 strcat(tmp,"/sarg");
f84a35a3
FM
74 oldurl=NULL;
75 ourl_size=0;
25697a35 76
e21b6c02
FM
77 if ((dirp = opendir(tmp)) == NULL) {
78 debuga(_("Failed to open directory %s - %s\n"),tmp,strerror(errno));
79 exit(EXIT_FAILURE);
80 }
25697a35 81 while ( (direntp = readdir( dirp )) != NULL ) {
c0ec9cc7
FM
82 dlen=strlen(direntp->d_name)-(sizeof(logext)-1);
83 if (dlen<=0) continue;
84 if(strcmp(direntp->d_name+dlen,logext) != 0)
25697a35 85 continue;
c0ec9cc7 86
f2ec8c75
FM
87 if (dlen>0) {
88 if (dlen>=sizeof(user)) continue;
89 strncpy(user,direntp->d_name,dlen);
90 user[dlen]=0;
91 } else {
92 user[0]='\0';
93 }
94
95 uinfo=userinfo_find_from_file(user);
96 if (!uinfo) {
f84a35a3 97 debuga(_("Ignoring unknown user file %s\n"),user);
f2ec8c75
FM
98 continue;
99 }
100 strcpy(u2,uinfo->id);
f84a35a3
FM
101 if(Ip2Name && uinfo->id_is_ip) {
102 strcpy(ipantes,u2);
103 ip2name(u2,sizeof(u2));
104 strcpy(nameantes,u2);
105 }
f2ec8c75
FM
106 user_find(uinfo->label,MAX_USER_LEN, u2);
107
9c7c6346 108 if (snprintf(tmp3,sizeof(tmp3),"%s/%s",tmp,direntp->d_name)>=sizeof(tmp3)) {
f84a35a3 109 debuga(_("(datafile) directory path too long: %s/%s\n"),tmp,direntp->d_name);
06b39c87 110 exit(EXIT_FAILURE);
9c7c6346 111 }
25697a35 112
936c9905 113 if((fp_in=MY_FOPEN(tmp3,"r"))==NULL){
9f70c14e 114 debuga(_("(datafile) Cannot open file %s\n"),tmp3);
06b39c87 115 exit(EXIT_FAILURE);
f84a35a3
FM
116 }
117
afaa3b67 118 if ((line=longline_create())==NULL) {
10210234 119 debuga(_("Not enough memory to read the downloaded files.\n"));
06b39c87 120 exit(EXIT_FAILURE);
25697a35 121 }
25697a35
GS
122
123 ttopen=0;
f2ec8c75 124 new_user=1;
afaa3b67 125 while((buf=longline_read(fp_in,line))!=NULL) {
9c7c6346 126 getword_start(&gwarea,buf);
f84a35a3
FM
127 if (getword(accdia,sizeof(accdia),&gwarea,'\t')<0 || getword(acchora,sizeof(acchora),&gwarea,'\t')<0 ||
128 getword(accip,sizeof(accip),&gwarea,'\t')<0 ||
129 getword_ptr(buf,&accurl,&gwarea,'\t')<0 || getword_atoll(&accbytes,&gwarea,'\t')<0 ||
130 getword(acccode,sizeof(acccode),&gwarea,'\t')<0 || getword_atoll(&accelap,&gwarea,'\t')<0) {
131 debuga(_("There is a broken record or garbage in file %s\n"),tmp3);
06b39c87 132 exit(EXIT_FAILURE);
f84a35a3
FM
133 }
134 if (getword_skip(20000,&gwarea,'"')<0 || getword(accsmart,sizeof(accsmart),&gwarea,'"')<0) {
135 debuga(_("There is an invalid smart info in file %s\n"),tmp3);
06b39c87 136 exit(EXIT_FAILURE);
4bcb77cf 137 }
5542cb59 138
246c8489 139 if(Ip2Name) {
25697a35
GS
140 if(strcmp(accip,ipantes) != 0) {
141 strcpy(ipantes,accip);
a1c55d8c 142 ip2name(accip,sizeof(accip));
25697a35 143 strcpy(nameantes,accip);
5542cb59
FM
144 }
145 else strcpy(accip,nameantes);
25697a35 146 }
5542cb59 147
25697a35 148 if(!rtotal){
f84a35a3
FM
149 url_len=strlen(accurl);
150 if (!oldurl || url_len>=ourl_size) {
151 ourl_size=url_len+1;
152 oldurl=realloc(oldurl,ourl_size);
153 if (!oldurl) {
154 debuga(_("Not enough memory to store the url\n"));
06b39c87 155 exit(EXIT_FAILURE);
f84a35a3
FM
156 }
157 }
25697a35
GS
158 strcpy(oldurl,accurl);
159 strcpy(oldacccode,acccode);
25697a35
GS
160 strcpy(oldaccip,accip);
161 strcpy(oldaccdia,accdia);
162 strcpy(oldacchora,acchora);
f84a35a3 163 new_user=0;
25697a35
GS
164 rtotal++;
165 }
f84a35a3 166 same_url=(strcmp(oldurl,accurl) == 0);
5542cb59 167
f84a35a3 168 if(!same_url || new_user){
f84a35a3 169 if(!fp_ou && (fp_ou=MY_FOPEN(DataFile,"w"))==NULL){
9f70c14e 170 debuga(_("(datafile) Cannot open file %s\n"),DataFile);
06b39c87 171 exit(EXIT_FAILURE);
f84a35a3
FM
172 }
173 saverecs(fp_ou,uinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
25697a35
GS
174 nacc=0;
175 nbytes=0;
176 nelap=0;
177 incache=0;
178 oucache=0;
f84a35a3 179 new_user=0;
25697a35
GS
180 }
181
182 nacc++;
6e792ade
FM
183 nbytes+=accbytes;
184 nelap+=accelap;
5542cb59 185
9c7c6346
FM
186 strcpy(crc2,acccode);
187 str=strchr(crc2,'/');
188 if (str) *str='\0';
25697a35 189
6e792ade
FM
190 if(strstr(crc2,"MISS") != 0) oucache+=accbytes;
191 else incache+=accbytes;
5542cb59 192
f84a35a3
FM
193 if (!same_url) {
194 url_len=strlen(accurl);
195 if (url_len>=ourl_size) {
196 ourl_size=url_len+1;
197 oldurl=realloc(oldurl,ourl_size);
198 if (!oldurl) {
199 debuga(_("Not enough memory to store the url\n"));
06b39c87 200 exit(EXIT_FAILURE);
f84a35a3
FM
201 }
202 }
203 strcpy(oldurl,accurl);
204 }
f2ec8c75 205 new_user=0;
25697a35 206 strcpy(oldacccode,acccode);
25697a35
GS
207 strcpy(oldaccip,accip);
208 strcpy(oldaccdia,accdia);
209 strcpy(oldacchora,acchora);
210 }
5542cb59 211
25697a35 212 fclose(fp_in);
afaa3b67 213 longline_destroy(&line);
c0ec9cc7 214 }
25697a35 215 (void)closedir( dirp );
f84a35a3
FM
216 if (oldurl) free(oldurl);
217 if (fp_ou) fclose(fp_ou);
218
d2fe0c32 219 if(debug)
fcdc0918 220 debuga(_("Datafile %s written successfully\n"),DataFile);
25697a35
GS
221}
222
f84a35a3 223void saverecs(FILE *fp_ou, const struct userinfostruct *uinfo, long long int nacc, char *url, long long int nbytes, char *ip, char *hora, char *dia, long long int nelap, long long int incache, long long int oucache)
25697a35 224{
e5b2c6f0 225 char val[20];
f84a35a3 226 char name[512];
25697a35 227
085c1e1f 228 if((DataFileFields & DATA_FIELD_USER) != 0) {
f2ec8c75
FM
229 fputs(uinfo->label,fp_ou);
230 fputc(DataFileDelimiter[0],fp_ou);
25697a35 231 }
085c1e1f 232 if((DataFileFields & DATA_FIELD_DATE) != 0) {
f2ec8c75
FM
233 fputs(dia,fp_ou);
234 fputc(DataFileDelimiter[0],fp_ou);
25697a35 235 }
085c1e1f 236 if((DataFileFields & DATA_FIELD_TIME) != 0) {
f2ec8c75
FM
237 fputs(hora,fp_ou);
238 fputc(DataFileDelimiter[0],fp_ou);
25697a35 239 }
085c1e1f 240 if((DataFileFields & DATA_FIELD_URL) != 0) {
f84a35a3
FM
241 url_hostname(url,name,sizeof(name));
242 if (DataFileUrl == DATAFILEURL_IP) name2ip(name);
f2ec8c75
FM
243 fputs(name,fp_ou);
244 fputc(DataFileDelimiter[0],fp_ou);
25697a35 245 }
085c1e1f 246 if((DataFileFields & DATA_FIELD_CONNECT) != 0) {
e5b2c6f0 247 my_lltoa(nacc,val,sizeof(val),0);
f2ec8c75
FM
248 fputs(val,fp_ou);
249 fputc(DataFileDelimiter[0],fp_ou);
25697a35 250 }
085c1e1f 251 if((DataFileFields & DATA_FIELD_BYTES) != 0) {
e5b2c6f0 252 my_lltoa(nbytes,val,sizeof(val),0);
f2ec8c75
FM
253 fputs(val,fp_ou);
254 fputc(DataFileDelimiter[0],fp_ou);
25697a35 255 }
085c1e1f 256 if((DataFileFields & DATA_FIELD_IN_CACHE) != 0) {
e5b2c6f0 257 my_lltoa(incache,val,sizeof(val),0);
f2ec8c75
FM
258 fputs(val,fp_ou);
259 fputc(DataFileDelimiter[0],fp_ou);
25697a35 260 }
085c1e1f 261 if((DataFileFields & DATA_FIELD_OUT_CACHE) != 0) {
e5b2c6f0 262 my_lltoa(oucache,val,sizeof(val),0);
f2ec8c75
FM
263 fputs(val,fp_ou);
264 fputc(DataFileDelimiter[0],fp_ou);
25697a35 265 }
085c1e1f 266 if((DataFileFields & DATA_FIELD_ELAPSED) != 0) {
e5b2c6f0 267 my_lltoa(nelap,val,sizeof(val),0);
f2ec8c75
FM
268 fputs(val,fp_ou);
269 fputc(DataFileDelimiter[0],fp_ou);
25697a35
GS
270 }
271
f2ec8c75 272 fputc('\n',fp_ou);
25697a35 273}