]> git.ipfire.org Git - thirdparty/sarg.git/blob - report.c
Merge squidguard_log.c and squidguard_report.c into one file
[thirdparty/sarg.git] / report.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2011
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
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"
28 #include "include/defs.h"
29
30 //! The global statistics of the whole log read.
31 struct globalstatstruct globstat;
32
33 static FILE *fp_tt=NULL;
34
35 static void maketmp(const char *user, const char *dirname, int debug);
36 static void maketmp_hour(const char *user, const char *dirname);
37 static void gravatmp_hora(const char *dirname, const struct userinfostruct *uinfo, const char *data, const char *hora, long long int elap, long long int accbytes);
38 static void gravatmp(const struct userinfostruct *uinfo, const char *oldurl, long long int nacc, long long int nbytes, const char *oldmsg, long long int nelap, long long int incache, long long int oucache);
39 static void gravaporuser(const struct userinfostruct *uinfo, const char *dirname, const char *url, const char *ip, const char *data, const char *hora, long long int tam, long long int elap);
40 static void gravager(FILE *fp_gen,const char *filename, const struct userinfostruct *uinfo, long long int nacc, const char *url, long long int nbytes, const char *ip, const char *hora, const char *dia, long long int nelap, long long int incache, long long int oucache);
41 static void grava_SmartFilter(const char *dirname, const char *user, const char *ip, const char *data, const char *hora, const char *url, const char *smart);
42
43 void gerarel(void)
44 {
45 FILE *fp_in;
46 FILE *fp_gen;
47
48 char *buf;
49 char accdia[11], acchora[9], accip[MAXLEN], *accurl;
50 char oldaccdia[11], oldacchora[9], oldaccip[MAXLEN];
51 char wdirname[MAXLEN];
52 char *oldurl=NULL;
53 char oldmsg[50], acccode[MAXLEN/2 - 1], oldacccode[MAXLEN/2 - 1];
54 char ipantes[MAXLEN], nameantes[MAXLEN];
55 char accsmart[MAXLEN];
56 char crc2[MAXLEN/2 -1];
57 char siteind[MAX_TRUNCATED_URL];
58 char arqtt[256];
59 char *oldurltt=NULL;
60 char oldaccdiatt[11],oldacchoratt[9];
61 char tmp3[MAXLEN];
62 char tmp4[5];
63 char u2[MAX_USER_LEN];
64 long long int nbytes=0;
65 long long int nelap=0;
66 long long int nacc=0;
67 long long int rtotal=0;
68 long long int incache=0;
69 long long int oucache=0;
70 long long int accbytes, accelap;
71 char *str;
72 userscan uscan;
73 const char *sort_field;
74 const char *sort_order;
75 const char *user;
76 int url_len;
77 int ourl_size=0;
78 int ourltt_size=0;
79 int same_url;
80 int new_user;
81 struct getwordstruct gwarea;
82 longline line;
83 struct userinfostruct *uinfo,*puinfo;
84
85 ipantes[0]='\0';
86 smartfilter=0;
87 memset(&globstat,0,sizeof(globstat));
88
89 if (vrfydir(&period, addr, site, us, email)<0) {
90 debuga(_("Cannot create the output directory name containing the period as part of the name\n"));
91 exit(EXIT_FAILURE);
92 }
93
94 if(debugz){
95 debugaz("outdirname",outdirname);
96 }
97
98 if(UserAgentLog[0] != '\0' && email[0] == '\0') useragent();
99
100 snprintf(wdirname,sizeof(wdirname),"%s/sarg-general",outdirname);
101 if((fp_gen=MY_FOPEN(wdirname,"w"))==NULL){
102 debuga(_("(report) Cannot open file %s\n"),wdirname);
103 exit(EXIT_FAILURE);
104 }
105
106 puinfo=NULL;
107 fp_tt=NULL;
108 sort_labels(&sort_field,&sort_order);
109
110 uscan=userinfo_startscan();
111 if (uscan == NULL) {
112 debuga(_("Cannot enumerate the user list\n"));
113 exit(EXIT_FAILURE);
114 }
115 while ((uinfo = userinfo_advancescan(uscan)) != NULL ) {
116 if (snprintf(tmp3,sizeof(tmp3),"%s/%s.log",tmp,uinfo->filename)>=sizeof(tmp3)) {
117 debuga(_("(report) directory entry too long: %s/%s.log\n"),tmp,uinfo->filename);
118 exit(EXIT_FAILURE);
119 }
120 if((fp_in=MY_FOPEN(tmp3,"r"))==NULL){
121 debuga(_("(report) Cannot open file %s\n"),tmp);
122 exit(EXIT_FAILURE);
123 }
124 user=uinfo->filename;
125
126 strcpy(u2,uinfo->id);
127 if(Ip2Name && uinfo->id_is_ip) {
128 strcpy(ipantes,u2);
129 ip2name(u2,sizeof(u2));
130 strcpy(nameantes,u2);
131 }
132 user_find(uinfo->label,MAX_USER_LEN, u2);
133
134 if (!indexonly) {
135 maketmp(user,tmp,debug);
136 maketmp_hour(user,tmp);
137 }
138
139 ttopen=0;
140 oldurltt=NULL;
141 ourltt_size=0;
142 memset(oldaccdiatt,0,sizeof(oldaccdiatt));
143 memset(oldacchoratt,0,sizeof(oldacchoratt));
144 new_user=1;
145
146 if ((line=longline_create())==NULL) {
147 debuga(_("Not enough memory to read the downloaded files\n"));
148 exit(EXIT_FAILURE);
149 }
150
151 while((buf=longline_read(fp_in,line))!=NULL) {
152 getword_start(&gwarea,buf);
153 if (getword(accdia,sizeof(accdia),&gwarea,'\t')<0 || getword(acchora,sizeof(acchora),&gwarea,'\t')<0 ||
154 getword(accip,sizeof(accip),&gwarea,'\t')<0 ||
155 getword_ptr(buf,&accurl,&gwarea,'\t')<0 || getword_atoll(&accbytes,&gwarea,'\t')<0 ||
156 getword(acccode,sizeof(acccode),&gwarea,'\t')<0) {
157 debuga(_("There is a broken record or garbage in file %s\n"),tmp3);
158 exit(EXIT_FAILURE);
159 }
160 if(strncmp(acccode,"TCP_DENIED/407",14) == 0) continue;
161 if (getword_atoll(&accelap,&gwarea,'\t')<0) {
162 debuga(_("There is a broken elapsed time in file %s\n"),tmp3);
163 exit(EXIT_FAILURE);
164 }
165 if (getword_skip(20000,&gwarea,'"')<0 || getword(accsmart,sizeof(accsmart),&gwarea,'"')<0) {
166 debuga(_("There is a broken smart info in file %s\n"),tmp3);
167 exit(EXIT_FAILURE);
168 }
169
170 if(accsmart[0] != '\0') {
171 smartfilter++;
172 grava_SmartFilter(outdirname,uinfo->id,accip,accdia,acchora,accurl,accsmart);
173 }
174
175 if(Ip2Name) {
176 if(strcmp(accip,ipantes) != 0) {
177 strcpy(ipantes,accip);
178 ip2name(accip,sizeof(accip));
179 strcpy(nameantes,accip);
180 } else strcpy(accip,nameantes);
181 }
182
183 if (!indexonly) {
184 gravatmp_hora(outdirname,uinfo,accdia,acchora,accelap,accbytes);
185
186 if(iprel) gravaporuser(uinfo,outdirname,accurl,accip,accdia,acchora,accbytes,accelap);
187 }
188
189 if(!rtotal){
190 url_len=strlen(accurl);
191 if (!oldurl || url_len>=ourl_size) {
192 ourl_size=url_len+1;
193 oldurl=realloc(oldurl,ourl_size);
194 if (!oldurl) {
195 debuga(_("Not enough memory to store the url\n"));
196 exit(EXIT_FAILURE);
197 }
198 }
199 strcpy(oldurl,accurl);
200 strcpy(oldacccode,acccode);
201 puinfo=uinfo;
202 strcpy(oldaccip,accip);
203 strcpy(oldaccdia,accdia);
204 strcpy(oldacchora,acchora);
205 new_user=0;
206 rtotal++;
207 }
208 same_url=(strcmp(oldurl,accurl) == 0);
209
210 if(site[0] != '\0') {
211 if(new_user){
212 if(strstr(oldacccode,"DENIED") != 0)
213 strcpy(oldmsg,"DENIED");
214 else
215 strcpy(oldmsg,"OK");
216 if (!indexonly) gravatmp(puinfo,oldurl,nacc,nbytes,oldmsg,nelap,incache,oucache);
217 gravager(fp_gen,wdirname,puinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
218 nacc=0;
219 nbytes=0;
220 nelap=0;
221 incache=0;
222 oucache=0;
223 }
224 } else {
225 if(!same_url || new_user){
226 if(strstr(oldacccode,"DENIED") != 0)
227 strcpy(oldmsg,"DENIED");
228 else
229 strcpy(oldmsg,"OK");
230 if (!indexonly) gravatmp(puinfo,oldurl,nacc,nbytes,oldmsg,nelap,incache,oucache);
231 gravager(fp_gen,wdirname,puinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
232 nacc=0;
233 nbytes=0;
234 nelap=0;
235 incache=0;
236 oucache=0;
237 }
238 }
239 nacc++;
240 nbytes+=accbytes;
241 nelap+=accelap;
242
243 if ((ReportType & REPORT_TYPE_SITE_USER_TIME_DATE) != 0 && !indexonly &&
244 (!oldurltt || strcmp(oldurltt,accurl) || strcmp(oldaccdiatt,accdia) || strcmp(oldacchoratt,acchora))) {
245
246 if(!ttopen) {
247 snprintf(arqtt,sizeof(arqtt),"%s/%s",outdirname,uinfo->filename);
248 if(access(arqtt, R_OK) != 0)
249 my_mkdir(arqtt);
250 url_to_file(accurl,siteind,sizeof(siteind));
251 snprintf(arqtt,sizeof(arqtt),"%s/%s/tt%s-%s.html",outdirname,uinfo->filename,uinfo->filename,siteind);
252 if ((fp_tt = fopen(arqtt, "w")) == 0) {
253 debuga(_("(report) Cannot open file %s\n"),arqtt);
254 exit(EXIT_FAILURE);
255 }
256 ttopen=1;
257
258 /*
259 if(Privacy)
260 sprintf(httplink,"<font size=%s color=%s><href=http://%s>%s",FontSize,PrivacyStringColor,PrivacyString,PrivacyString);
261 else
262 sprintf(httplink,"<font size=%s><a href=\"http://%s\">%s</a>",FontSize,accurl,accurl);
263 */
264
265 write_html_header(fp_tt,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Site access report"),HTML_JS_NONE);
266 fprintf(fp_tt,"<tr><td class=\"header_c\">%s:&nbsp;%s</td></tr>\n",_("Period"),period.html);
267 fprintf(fp_tt,"<tr><td class=\"header_c\">%s:&nbsp;%s</td></tr>\n",_("User"),uinfo->label);
268 fputs("<tr><td class=\"header_c\">",fp_tt);
269 fprintf(fp_tt,_("Sort:&nbsp;%s, %s"),sort_field,sort_order);
270 fputs("</td></tr>\n",fp_tt);
271 fprintf(fp_tt,"<tr><th class=\"header_c\">%s</th></tr>\n",_("User"));
272 close_html_header(fp_tt);
273
274 fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_tt);
275 strncpy(tmp4,_("DATE/TIME"),4);
276 tmp4[4]='\0';
277 fprintf(fp_tt,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",_("ACCESSED SITE"),tmp4,_("DATE/TIME")+5);
278 }
279
280 fputs("<tr><td class=\"data\">",fp_tt);
281 output_html_string(fp_tt,accurl,100);
282 fprintf(fp_tt,"</td><td class=\"data\">%s</td><td class=\"data\">%s</td></tr>\n",accdia,acchora);
283
284 url_len=strlen(accurl);
285 if (!oldurltt || url_len>=ourltt_size) {
286 ourltt_size=url_len+1;
287 oldurltt=realloc(oldurltt,ourltt_size);
288 if (!oldurltt) {
289 debuga(_("Not enough memory to store the url\n"));
290 exit(EXIT_FAILURE);
291 }
292 }
293 strcpy(oldurltt,accurl);
294 strcpy(oldaccdiatt,accdia);
295 strcpy(oldacchoratt,acchora);
296 }
297
298 strcpy(crc2,acccode);
299 str=strchr(crc2,'/');
300 if (str) *str='\0';
301 if(strstr(crc2,"MISS") != 0)
302 oucache+=accbytes;
303 else incache+=accbytes;
304
305 if(new_user) {
306 new_user=0;
307 if (!indexonly) day_totalize(tmp,puinfo);
308 }
309
310 puinfo=uinfo;
311 strcpy(oldacccode,acccode);
312 strcpy(oldaccip,accip);
313 if (!same_url) {
314 url_len=strlen(accurl);
315 if (url_len>=ourl_size) {
316 ourl_size=url_len+1;
317 oldurl=realloc(oldurl,ourl_size);
318 if (!oldurl) {
319 debuga(_("Not enough memory to store the url\n"));
320 exit(EXIT_FAILURE);
321 }
322 }
323 strcpy(oldurl,accurl);
324 }
325 strcpy(oldaccdia,accdia);
326 strcpy(oldacchora,acchora);
327
328 }
329 fclose(fp_in);
330 longline_destroy(&line);
331 if (oldurltt) free(oldurltt);
332 if (unlink(tmp3)) {
333 debuga(_("Cannot delete %s - %s\n"),tmp3,strerror(errno));
334 exit(EXIT_FAILURE);
335 }
336 }
337 userinfo_stopscan(uscan);
338
339 if (oldurl) {
340 if(strstr(oldacccode,"DENIED") != 0)
341 strcpy(oldmsg,"DENIED");
342 else
343 strcpy(oldmsg,"OK");
344 if (!indexonly) gravatmp(puinfo,oldurl,nacc,nbytes,oldmsg,nelap,incache,oucache);
345 gravager(fp_gen,wdirname,puinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
346 free(oldurl);
347 }
348 totalger(fp_gen,wdirname);
349 fclose(fp_gen);
350 if (puinfo && !indexonly) day_totalize(tmp,puinfo);
351
352 if (!indexonly) tmpsort();
353
354 if(email[0] == '\0') {
355 if (!indexonly) {
356 if((ReportType & REPORT_TYPE_DOWNLOADS) != 0) download_report();
357
358 if(DansGuardianConf[0] != '\0') {
359 dansguardian_log();
360 }
361
362 redirector_log();
363 }
364
365 topuser();
366
367 if (!indexonly) {
368 if((ReportType & REPORT_TYPE_TOPSITES) != 0) topsites();
369
370 if((ReportType & REPORT_TYPE_SITES_USERS) != 0) siteuser();
371 gen_denied_report();
372
373 authfail_report();
374
375 if(smartfilter) smartfilter_report();
376
377 if(DansGuardianConf[0] != '\0') dansguardian_report();
378
379 redirector_report();
380
381 if((ReportType & REPORT_TYPE_USERS_SITES) != 0) htmlrel();
382 }
383
384 make_index();
385
386 if(SuccessfulMsg) debuga(_("Successful report generated on %s\n"),outdirname);
387 } else {
388 geramail(outdirname, debug, outdir, email, tmp);
389
390 if((strcmp(email,"stdout") != 0) && SuccessfulMsg)
391 debuga(_("Successful report generated and sent to %s\n"),email);
392 }
393
394 if(indexonly) index_only(outdirname, debug);
395
396 removetmp(outdirname);
397 return;
398 }
399
400 static void maketmp(const char *user, const char *dirname, int debug)
401 {
402 FILE *fp_ou;
403 char wdirname[MAXLEN];
404
405 if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return;
406
407 if(debug) debuga(_("Making file: %s/%s\n"),tmp,user);
408 if (snprintf(wdirname,sizeof(wdirname),"%s/%s.utmp",tmp,user)>=sizeof(wdirname)) {
409 debuga(_("Temporary file name too long: %s/%s.utmp\n"),tmp,user);
410 exit(EXIT_FAILURE);
411 }
412
413 if((fp_ou=fopen(wdirname,"w"))==NULL){
414 debuga(_("(report) Cannot open file %s\n"),wdirname);
415 exit(EXIT_FAILURE);
416 }
417
418 fclose(fp_ou);
419 return;
420 }
421
422
423 static void maketmp_hour(const char *user, const char *dirname)
424 {
425 FILE *fp_ou;
426 char wdirname[MAXLEN];
427
428 if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return;
429 if(datetimeby==0) return;
430
431 if (snprintf(wdirname,sizeof(wdirname),"%s/%s.htmp",tmp,user)>=sizeof(wdirname)) {
432 debuga(_("Temporary file name too long: %s/%s.htmp\n"),tmp,user);
433 exit(EXIT_FAILURE);
434 }
435
436 if((fp_ou=fopen(wdirname,"w"))==NULL){
437 debuga(_("(report-1) Cannot open file %s - %s\n"),wdirname,strerror(errno));
438 exit(EXIT_FAILURE);
439 }
440
441 fclose(fp_ou);
442 return;
443 }
444
445
446 static void gravatmp(const struct userinfostruct *uinfo, const char *oldurl, long long int nacc, long long int nbytes, const char *oldmsg, long long int nelap, long long int incache, long long int oucache)
447 {
448 FILE *fp_ou;
449 char wdirname[MAXLEN];
450
451 if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return;
452
453 if (snprintf(wdirname,sizeof(wdirname),"%s/%s.utmp",tmp,uinfo->filename)>=sizeof(wdirname)) {
454 debuga(_("Temporary file name too long: %s/%s.utmp\n"),tmp,uinfo->filename);
455 exit(EXIT_FAILURE);
456 }
457
458 if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){
459 debuga(_("(report) Cannot open file %s\n"),wdirname);
460 exit(EXIT_FAILURE);
461 }
462
463 /*
464 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
465 to print a long long int unless it is exactly 64-bits long.
466 */
467 fprintf(fp_ou,"%"PRIu64"\t%"PRIu64"\t%s\t%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",(uint64_t)nacc,(uint64_t)nbytes,oldurl,oldmsg,(uint64_t)nelap,(uint64_t)incache,(uint64_t)oucache);
468
469 if (fclose(fp_ou)==EOF) {
470 debuga(_("Failed to close file %s - %s\n"),wdirname,strerror(errno));
471 exit(EXIT_FAILURE);
472 }
473 ttopen=0;
474
475 if(fp_tt) {
476 fputs("</table>\n</div>\n",fp_tt);
477 fputs("</body>\n</html>\n",fp_tt);
478 fclose(fp_tt);
479 fp_tt=NULL;
480 }
481
482 return;
483 }
484
485 static void gravatmp_hora(const char *dirname, const struct userinfostruct *uinfo, const char *data, const char *hora, long long int elap, long long int bytes)
486 {
487 FILE *fp_ou;
488 char wdirname[MAXLEN];
489 int hour;
490
491 if(((ReportType & REPORT_TYPE_USERS_SITES) == 0) || datetimeby==0) return;
492
493 if (snprintf(wdirname,sizeof(wdirname),"%s/%s.htmp",tmp,uinfo->filename)>=sizeof(wdirname)) {
494 debuga(_("Path too long %s/%s.htmp\n"),tmp,uinfo->filename);
495 exit(EXIT_FAILURE);
496 }
497
498 if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){
499 debuga(_("(report-2) Cannot open file %s - %s\n"),wdirname,strerror(errno));
500 exit(EXIT_FAILURE);
501 }
502
503 hour=atoi(hora);
504 fprintf(fp_ou,"%s\t%d",data,hour);
505 if((datetimeby & DATETIME_BYTE)!=0) fprintf(fp_ou,"\t%"PRIu64,(uint64_t)bytes);
506 if((datetimeby & DATETIME_ELAP)!=0) fprintf(fp_ou,"\t%"PRIu64,(uint64_t)elap);
507 fputs("\n",fp_ou);
508
509 if (fclose(fp_ou)==EOF) {
510 debuga(_("Failed to close file %s - %s\n"),wdirname,strerror(errno));
511 exit(EXIT_FAILURE);
512 }
513
514 return;
515 }
516
517
518 static void gravaporuser(const struct userinfostruct *uinfo, const char *dirname, const char *url, const char *ip, const char *data, const char *hora, long long int tam, long long int elap)
519 {
520 FILE *fp_ou;
521 char wdirname[MAXLEN];
522
523 if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return;
524
525 if (snprintf(wdirname,sizeof(wdirname),"%s/%s.ip",tmp,uinfo->filename)>=sizeof(wdirname)) {
526 debuga(_("Path too long %s/%s.ip\n"),tmp,uinfo->filename);
527 exit(EXIT_FAILURE);
528 }
529
530 if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){
531 debuga(_("(report) Cannot open file %s\n"),wdirname);
532 exit(EXIT_FAILURE);
533 }
534
535 /*
536 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
537 to print a long long int unless it is exactly 64-bits long.
538 */
539 fprintf(fp_ou,"%s\t%s\t%s\t%s\t%"PRIu64"\t%"PRIu64"\n",ip,url,data,hora,(uint64_t)tam,(uint64_t)elap);
540
541 fclose(fp_ou);
542
543 return;
544 }
545
546
547 static void gravager(FILE *fp_gen,const char *filename, const struct userinfostruct *uinfo, long long int nacc, const char *url, long long int nbytes, const char *ip, const char *hora, const char *dia, long long int nelap, long long int incache, long long int oucache)
548 {
549 /*
550 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
551 to print a long long int unless it is exactly 64-bits long.
552 */
553 if (fprintf(fp_gen,"%s\t%"PRIu64"\t%"PRIu64"\t%s\t%s\t%s\t%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",uinfo->id,(uint64_t)nacc,(uint64_t)nbytes,url,ip,hora,dia,(uint64_t)nelap,(uint64_t)incache,(uint64_t)oucache)<0) {
554 debuga(_("Failed to write a line in %s\n"),filename);
555 exit(EXIT_FAILURE);
556 }
557
558 globstat.nacc+=nacc;
559 globstat.nbytes+=nbytes;
560 globstat.elap+=nelap;
561 globstat.incache+=incache;
562 globstat.oucache+=oucache;
563 return;
564 }
565
566 /*!
567 Write the total line at the end of the general file.
568 */
569 void totalger(FILE *fp_gen,const char *filename)
570 {
571 /*
572 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
573 to print a long long int unless it is exactly 64-bits long.
574 */
575 if (fprintf(fp_gen,"TOTAL\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",(uint64_t)globstat.nacc,(uint64_t)globstat.nbytes,(uint64_t)globstat.elap,(uint64_t)globstat.incache,(uint64_t)globstat.oucache)<0) {
576 debuga(_("Failed to write the total line in %s\n"),filename);
577 exit(EXIT_FAILURE);
578 }
579 }
580
581 int ger_read(char *buffer,struct generalitemstruct *item,const char *filename)
582 {
583 int i;
584 int sign;
585 long long int number;
586
587 if (strncmp(buffer,"TOTAL\t",6)==0) {
588 item->total=1;
589 buffer+=6;
590
591 sign=+1;
592 if (*buffer == '-') {
593 buffer++;
594 sign=-1;
595 } else if (*buffer == '+') {
596 buffer++;
597 }
598 number=0LL;
599 while (isdigit(*buffer))
600 number=(number * 10) + (*buffer++)-'0';
601 if (*buffer!='\t') {
602 debuga(_("Invalid total number of accesses in %s\n"),filename);
603 exit(EXIT_FAILURE);
604 }
605 buffer++;
606 item->nacc=number*sign;
607
608 sign=+1;
609 if (*buffer == '-') {
610 buffer++;
611 sign=-1;
612 } else if (*buffer == '+') {
613 buffer++;
614 }
615 number=0LL;
616 while (isdigit(*buffer))
617 number=(number * 10) + (*buffer++)-'0';
618 if (*buffer!='\t') {
619 debuga(_("Invalid total size in %s\n"),filename);
620 exit(EXIT_FAILURE);
621 }
622 buffer++;
623 item->nbytes=number*sign;
624
625 sign=+1;
626 if (*buffer == '-') {
627 buffer++;
628 sign=-1;
629 } else if (*buffer == '+') {
630 buffer++;
631 }
632 number=0LL;
633 while (isdigit(*buffer))
634 number=(number * 10) + (*buffer++)-'0';
635 if (*buffer!='\t') {
636 debuga(_("Invalid total elapsed time in %s\n"),filename);
637 exit(EXIT_FAILURE);
638 }
639 buffer++;
640 item->nelap=number*sign;
641
642 sign=+1;
643 if (*buffer == '-') {
644 buffer++;
645 sign=-1;
646 } else if (*buffer == '+') {
647 buffer++;
648 }
649 number=0LL;
650 while (isdigit(*buffer))
651 number=(number * 10) + (*buffer++)-'0';
652 if (*buffer!='\t') {
653 debuga(_("Invalid total cache hit in %s\n"),filename);
654 exit(EXIT_FAILURE);
655 }
656 buffer++;
657 item->incache=number*sign;
658
659 sign=+1;
660 if (*buffer == '-') {
661 buffer++;
662 sign=-1;
663 } else if (*buffer == '+') {
664 buffer++;
665 }
666 number=0LL;
667 while (isdigit(*buffer))
668 number=(number * 10) + (*buffer++)-'0';
669 if (*buffer!='\0') {
670 debuga(_("Invalid total cache miss in %s\n"),filename);
671 exit(EXIT_FAILURE);
672 }
673 item->oucache=number*sign;
674 } else {
675 item->total=0;
676
677 item->user=buffer;
678 for (i=0 ; i<MAX_USER_LEN-1 && (unsigned char)*buffer>=' ' ; i++) buffer++;
679 if (*buffer!='\t') {
680 debuga(_("User name too long or invalid in %s\n"),filename);
681 exit(EXIT_FAILURE);
682 }
683 *buffer++='\0';
684
685 sign=+1;
686 if (*buffer == '-') {
687 buffer++;
688 sign=-1;
689 } else if (*buffer == '+') {
690 buffer++;
691 }
692 number=0LL;
693 while (isdigit(*buffer))
694 number=(number * 10) + (*buffer++)-'0';
695 if (*buffer!='\t') {
696 debuga(_("Invalid number of accesses in %s\n"),filename);
697 exit(EXIT_FAILURE);
698 }
699 buffer++;
700 item->nacc=number*sign;
701
702 sign=+1;
703 if (*buffer == '-') {
704 buffer++;
705 sign=-1;
706 } else if (*buffer == '+') {
707 buffer++;
708 }
709 number=0LL;
710 while (isdigit(*buffer))
711 number=(number * 10) + (*buffer++)-'0';
712 if (*buffer!='\t') {
713 debuga(_("Invalid number of bytes in %s\n"),filename);
714 exit(EXIT_FAILURE);
715 }
716 buffer++;
717 item->nbytes=number*sign;
718
719 item->url=buffer;
720 while ((unsigned char)*buffer>=' ') buffer++;
721 if (*buffer!='\t') {
722 debuga(_("URL too long or invalid in %s\n"),filename);
723 exit(EXIT_FAILURE);
724 }
725 *buffer++='\0';
726
727 item->ip=buffer;
728 for (i=0 ; i<MAX_IP_LEN-1 && (unsigned char)*buffer>=' ' ; i++) buffer++;
729 if (*buffer!='\t') {
730 debuga(_("IP address too long or invalid in %s\n"),filename);
731 exit(EXIT_FAILURE);
732 }
733 *buffer++='\0';
734
735 item->time=buffer;
736 for (i=0 ; i<MAX_DATETIME_LEN-1 && (unsigned char)*buffer>=' ' ; i++) buffer++;
737 if (*buffer!='\t') {
738 debuga(_("Time too long or invalid in %s\n"),filename);
739 exit(EXIT_FAILURE);
740 }
741 *buffer++='\0';
742
743 item->date=buffer;
744 for (i=0 ; i<MAX_DATETIME_LEN-1 && (unsigned char)*buffer>=' ' ; i++) buffer++;
745 if (*buffer!='\t') {
746 debuga(_("Date too long or invalid in %s\n"),filename);
747 exit(EXIT_FAILURE);
748 }
749 *buffer++='\0';
750
751 sign=+1;
752 if (*buffer == '-') {
753 buffer++;
754 sign=-1;
755 } else if (*buffer == '+') {
756 buffer++;
757 }
758 number=0LL;
759 while (isdigit(*buffer))
760 number=(number * 10) + (*buffer++)-'0';
761 if (*buffer!='\t') {
762 debuga(_("Invalid elapsed time in %s\n"),filename);
763 exit(EXIT_FAILURE);
764 }
765 buffer++;
766 item->nelap=number*sign;
767
768 sign=+1;
769 if (*buffer == '-') {
770 buffer++;
771 sign=-1;
772 } else if (*buffer == '+') {
773 buffer++;
774 }
775 number=0LL;
776 while (isdigit(*buffer))
777 number=(number * 10) + (*buffer++)-'0';
778 if (*buffer!='\t') {
779 debuga(_("Invalid cache hit size in %s\n"),filename);
780 exit(EXIT_FAILURE);
781 }
782 buffer++;
783 item->incache=number*sign;
784
785 sign=+1;
786 if (*buffer == '-') {
787 buffer++;
788 sign=-1;
789 } else if (*buffer == '+') {
790 buffer++;
791 }
792 number=0LL;
793 while (isdigit(*buffer))
794 number=(number * 10) + (*buffer++)-'0';
795 if (*buffer!='\0') {
796 debuga(_("Invalid cache miss size in %s\n"),filename);
797 exit(EXIT_FAILURE);
798 }
799 item->oucache=number*sign;
800 }
801 return(0);
802 }
803
804 static void grava_SmartFilter(const char *dirname, const char *user, const char *ip, const char *data, const char *hora, const char *url, const char *smart)
805 {
806 FILE *fp_ou;
807 char wdirname[MAXLEN];
808
809 sprintf(wdirname,"%s/smartfilter.unsort",dirname);
810
811 if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){
812 debuga(_("(report) Cannot open file %s\n"),wdirname);
813 exit(EXIT_FAILURE);
814 }
815
816 fprintf(fp_ou,"%s\t%s\t%s\t%s\t%s\t%s\n",user,data,hora,ip,url,smart);
817 fputs("</body>\n</html>\n",fp_tt);
818
819 fclose(fp_ou);
820
821 return;
822 }