2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
6 * please look at http://sarg.sourceforge.net/donations.php
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
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.
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.
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.
27 #include "include/conf.h"
28 #include "include/defs.h"
33 FILE *fp_in
, *fp_ou
, *fp_ip
, *fp_ip2
, *fp_usr
;
35 struct dirent
*direntp
;
36 long long int nnbytes
=0, unbytes
=0, tnbytes
=0, totbytes
=0, totbytes2
=0;
37 long long int totelap
=0, totelap2
=0, nnelap
=0, unelap
=0, tnelap
=0;
38 long long int incache
=0, oucache
=0, tnincache
=0, tnoucache
=0, twork
=0;
40 long long int ntotuser
;
41 long long int userbytes
, userelap
;
43 char arqin
[MAXLEN
], arqou
[MAXLEN
], arqper
[MAXLEN
], arqip
[MAXLEN
];
44 char *url
, tmsg
[50], csort
[MAXLEN
];
45 char user
[MAXLEN
], duser
[MAXLEN
];
46 char userhora
[9], userdia
[9];
47 char user_ip
[MAXLEN
], olduserip
[MAXLEN
], tmp2
[MAXLEN
], tmp3
[MAXLEN
];
48 char denied_report
[255];
54 long long int tnacc
=0, ttnacc
=0, unacc
=0;
55 double perc
=0, perc2
=0, ouperc
=0, inperc
=0;
59 const char txtext
[]=".txt";
61 char siteind
[MAX_TRUNCATED_URL
];
62 struct getwordstruct gwarea
;
64 struct generalitemstruct item
;
65 const struct userinfostruct
*uinfo
;
67 if((ReportType
& REPORT_TYPE_USERS_SITES
) == 0) return;
70 strcat(tmp2
,"/sargtmp.unsort");
73 strcat(tmp3
,"/sargtmp.log");
75 snprintf(arqper
,sizeof(arqper
),"%s/sarg-general",outdirname
);
76 if ((fp_in
= fopen(arqper
, "r")) == 0){
77 debuga(_("(html2) Cannot open file %s\n"),arqper
);
80 if ((line
=longline_create())==NULL
) {
81 debuga(_("Not enough memory to read file %s\n"),arqper
);
87 while((buf
=longline_read(fp_in
,line
))!=NULL
) {
88 ger_read(buf
,&item
,arqper
);
91 totbytes
+=item
.nbytes
;
96 longline_destroy(&line
);
98 snprintf(arqper
,sizeof(arqper
),"%s/sarg-users",outdirname
);
99 if ((fp_in
= fopen(arqper
, "r")) == 0){
100 debuga(_("(html11) Cannot open file %s\n"),arqper
);
103 if (!fgets(totuser
,sizeof(totuser
),fp_in
)) {
104 debuga(_("(html11) read error in %s\n"),arqper
);
108 ntotuser
=my_atoll(totuser
);
109 if (ntotuser
<=0) ntotuser
=1;
111 if ((dirp
= opendir(tmp
)) == NULL
) {
112 debuga(_("Failed to open directory %s - %s\n"),tmp
,strerror(errno
));
115 while ( (direntp
= readdir( dirp
)) != NULL
) {
116 dlen
=strlen(direntp
->d_name
)-(sizeof(txtext
)-1);
117 if (dlen
<0) continue;
118 if(strcmp(direntp
->d_name
+dlen
,txtext
) != 0)
123 if (dlen
>=sizeof(user
)) continue;
124 strncpy(user
,direntp
->d_name
,dlen
);
130 uinfo
=userinfo_find_from_file(user
);
132 debuga(_("Unknown user ID %s in directory %s\n"),user
,tmp
);
135 str
=strrchr(user
,'.');
137 strcpy(denied_report
,str
+1);
139 strcpy(denied_report
,user
);
141 if (snprintf(warea
,sizeof(warea
),"%s/%s",outdirname
,user
)>=sizeof(warea
)) {
142 debuga(_("Destination directory too long: %s/%s\n"),outdirname
,user
);
150 if (snprintf(arqin
,sizeof(arqin
),"%s/%s",tmp
,direntp
->d_name
)>=sizeof(arqin
)) {
151 debuga(_("Input file name too long: %s/%s\n"),tmp
,direntp
->d_name
);
154 if (snprintf(arqou
,sizeof(arqou
),"%s/%s/%s.html",outdirname
,uinfo
->filename
,uinfo
->filename
)>=sizeof(arqou
)) {
155 debuga(_("Output file name too long: %s/%s/%s.html\n"),outdirname
,uinfo
->filename
,uinfo
->filename
);
158 if (snprintf(duser
,sizeof(duser
),"%s/%s/denied_%s.html",tmp
,direntp
->d_name
,denied_report
)>=sizeof(duser
)) {
159 debuga(_("File name too long: %s/%s/denied_%s.html\n"),tmp
,direntp
->d_name
,denied_report
);
162 if(access(duser
, R_OK
) != 0)
163 denied_report
[0]='\0';
165 if ((fp_in
= fopen(arqin
, "r")) == 0){
166 debuga(_("(html3) Cannot open file %s\n"),arqin
);
170 if ((line
=longline_create())==NULL
) {
171 debuga(_("Not enough memory to read file %s\n"),arqin
);
180 while((buf
=longline_read(fp_in
,line
))!=NULL
) {
181 getword_start(&gwarea
,buf
);
182 if (getword_atoll(<emp
,&gwarea
,'\t')<0) {
183 debuga(_("There is a broken number of access in file %s\n"),arqin
);
187 if (getword_atoll(<emp
,&gwarea
,'\t')<0) {
188 debuga(_("There is a broken downloaded size in file %s\n"),arqin
);
192 if (getword_ptr(NULL
,NULL
,&gwarea
,'\t')<0) {
193 debuga(_("There is a broken url in file %s\n"),arqin
);
196 if (getword_skip(MAXLEN
,&gwarea
,'\t')<0) {
197 debuga(_("There is a broken access code in file %s\n"),arqin
);
200 if (getword_atoll(<emp
,&gwarea
,'\t')<0) {
201 debuga(_("There is a broken elapsed time in file %s\n"),arqin
);
205 if (getword_atoll(<emp
,&gwarea
,'\t')<0) {
206 debuga(_("There is a broken in-cache volume in file %s\n"),arqin
);
210 if (getword_atoll(<emp
,&gwarea
,'\n')<0) {
211 debuga(_("There is a broken out-cache volume in file %s\n"),arqin
);
219 if ((fp_ou
= fopen(arqou
, "w")) == 0){
220 debuga(_("(html5) Cannot open file %s\n"),arqou
);
224 write_html_header(fp_ou
,(IndexTree
== INDEX_TREE_DATE
) ? 4 : 2,_("User report"));
225 fprintf(fp_ou
,"<tr><td class=\"header_c\">%s: %s</td></tr>\n",_("Period"),period
.html
);
226 fprintf(fp_ou
,"<tr><td class=\"header_c\">%s: %s</td></tr>\n",_("User"),uinfo
->label
);
227 fprintf(fp_ou
,"<tr><td class=\"header_c\">%s: %s, %s</td></tr>\n",_("Sort"),UserSortField
,UserSortOrder
);
228 fprintf(fp_ou
,"<tr><th class=\"header_c\">%s</th></tr>\n",_("User report"));
229 close_html_header(fp_ou
);
231 fputs("<div class=\"report\"><table cellpadding=\"2\" cellspacing=\"1\">\n",fp_ou
);
233 if(denied_report
[0]!='\0') {
234 fprintf(fp_ou
,"<tr><td class=\"header_l\" colspan=\"11\"><a href=\"denied_%s.html\">%s</a> %s</td></tr>\n",denied_report
,_("SmartFilter"),_("Report"));
237 fprintf(fp_ou
,"<tr><th></th><th class=\"header_l\">%s</th>",_("ACCESSED SITE"));
239 if((UserReportFields
& USERREPORTFIELDS_CONNECT
) != 0)
240 fprintf(fp_ou
,"<th class=\"header_l\">%s</th>",_("CONNECT"));
241 if((UserReportFields
& USERREPORTFIELDS_BYTES
) != 0)
242 fprintf(fp_ou
,"<th class=\"header_l\">%s</th>",_("BYTES"));
243 if((UserReportFields
& USERREPORTFIELDS_SETYB
) != 0)
244 fprintf(fp_ou
,"<th class=\"header_l\">%%%s</th>",_("BYTES"));
245 if((UserReportFields
& USERREPORTFIELDS_IN_CACHE_OUT
) != 0)
246 fprintf(fp_ou
,"<th class=\"header_c\" colspan=\"2\">%s</th>",_("IN-CACHE-OUT"));
247 if((UserReportFields
& USERREPORTFIELDS_USED_TIME
) != 0)
248 fprintf(fp_ou
,"<th class=\"header_l\">%s</th>",_("ELAPSED TIME"));
249 if((UserReportFields
& USERREPORTFIELDS_MILISEC
) != 0)
250 fprintf(fp_ou
,"<th class=\"header_l\">%s</th>",_("MILLISEC"));
251 if((UserReportFields
& USERREPORTFIELDS_PTIME
) != 0)
252 fprintf(fp_ou
,"<th class=\"header_l\">%%%s</th>",_("TIME"));
254 fputs("</tr>\n",fp_ou
);
257 debuga(_("Making report: %s\n"),uinfo
->id
);
260 while((buf
=longline_read(fp_in
,line
))!=NULL
) {
261 getword_start(&gwarea
,buf
);
262 if (getword_atoll(&twork
,&gwarea
,'\t')<0) {
263 debuga(_("There is a broken number of access in file %s\n"),arqin
);
266 if (getword_atoll(&nnbytes
,&gwarea
,'\t')<0) {
267 debuga(_("There is a broken number of bytes in file %s\n"),arqin
);
270 if (getword_ptr(buf
,&url
,&gwarea
,'\t')<0) {
271 debuga(_("There is a broken url in file %s\n"),arqin
);
274 if (getword(tmsg
,sizeof(tmsg
),&gwarea
,'\t')<0) {
275 debuga(_("There is a broken access code in file %s\n"),arqin
);
278 if (getword_atoll(&nnelap
,&gwarea
,'\t')<0) {
279 debuga(_("There is a broken elapsed time in file %s\n"),arqin
);
282 if (getword_atoll(&incache
,&gwarea
,'\t')<0) {
283 debuga(_("There is a broken in cache column in file %s\n"),arqin
);
286 if (getword_atoll(&oucache
,&gwarea
,'\n')<0) {
287 debuga(_("There is a broken out of cache column in file %s (%d)\n"),arqin
,__LINE__
);
291 if(UserReportLimit
<=0 || count
<=UserReportLimit
) {
294 if(IndexTree
== INDEX_TREE_DATE
)
295 sprintf(tmp6
,"../%s",ImageFile
);
297 strcpy(tmp6
,"../../images");
299 if((ReportType
& REPORT_TYPE_SITE_USER_TIME_DATE
) != 0) {
301 for(z1
=0 ; z1
<sizeof(ltext110
)-1 && s
[z1
] ; z1
++)
302 ltext110
[z1
]=tolower(s
[z1
]);
304 url_to_file(url
,siteind
,sizeof(siteind
));
305 fprintf(fp_ou
,"<td class=\"data\"><a href=\"tt%s-%s.html\"><img src=\"%s/datetime.png\" title=\"%s %s\" alt=\"T\"></a></td>",uinfo
->filename
,siteind
,tmp6
,ltext110
,_("Report"));
307 fprintf(fp_ou
,"<td class=\"data\"></td>");
311 fprintf(fp_ou
,"<td class=\"data2\"><span style=\"color:%s;\">%s</span></td>",PrivacyStringColor
,PrivacyString
);
313 fputs("<td class=\"data2\">",fp_ou
);
314 if(BlockIt
[0]!='\0') {
315 fprintf(fp_ou
,"<a href=\"%s%s?url=",wwwDocumentRoot
,BlockIt
);
316 output_html_url(fp_ou
,url
);
317 fprintf(fp_ou
,"\"><img src=\"%s/sarg-squidguard-block.png\"></a> ",tmp6
);
319 fputs("<a href=\"http://",fp_ou
);
320 output_html_url(fp_ou
,url
);
322 output_html_string(fp_ou
,url
,100);
323 fputs("</a></td>",fp_ou
);
326 if((UserReportFields
& USERREPORTFIELDS_CONNECT
) != 0)
327 fprintf(fp_ou
,"<td class=\"data\">%s</td>",fixnum(twork
,1));
328 if((UserReportFields
& USERREPORTFIELDS_BYTES
) != 0)
329 fprintf(fp_ou
,"<td class=\"data\">%s</td>",fixnum(nnbytes
,1));
330 if((UserReportFields
& USERREPORTFIELDS_SETYB
) != 0) {
331 perc
=(tnbytes
) ? nnbytes
* 100. / tnbytes
: 0.;
332 fprintf(fp_ou
,"<td class=\"data\">%3.2lf%%</td>",perc
);
334 if((UserReportFields
& USERREPORTFIELDS_IN_CACHE_OUT
) != 0) {
335 inperc
=(nnbytes
) ? incache
* 100. / nnbytes
: 0.;
336 ouperc
=(nnbytes
) ? oucache
* 100. / nnbytes
: 0.;
337 fprintf(fp_ou
,"<td class=\"data\">%3.2lf%%</td><td class=\"data\">%3.2lf%%</td>",inperc
,ouperc
);
339 if((UserReportFields
& USERREPORTFIELDS_USED_TIME
) != 0)
340 fprintf(fp_ou
,"<td class=\"data\">%s</td>",buildtime(nnelap
));
341 if((UserReportFields
& USERREPORTFIELDS_MILISEC
) != 0)
342 fprintf(fp_ou
,"<td class=\"data\">%s</td>",fixnum2(nnelap
,1));
343 if((UserReportFields
& USERREPORTFIELDS_PTIME
) != 0) {
344 perc2
=(tnelap
) ? nnelap
* 100. / tnelap
: 0.;
345 fprintf(fp_ou
,"<td class=\"data\">%3.2lf%%</td>",perc2
);
348 if(strncmp(tmsg
,"OK",2) != 0)
349 fprintf(fp_ou
,"<td class=\"data\">%s</td>",_("DENIED"));
351 fputs("</tr>\n",fp_ou
);
356 if (snprintf(arqip
,sizeof(arqip
),"%s/%s.ip",tmp
,uinfo
->filename
)>=sizeof(arqip
)) {
357 debuga(_("File name too long: %s/%s.ip\n"),tmp
,uinfo
->filename
);
361 if ((fp_ip
= fopen(arqip
, "r")) == 0){
362 debuga(_("(html6) Cannot open file %s\n"),arqip
);
366 if ((fp_ip2
= MY_FOPEN(tmp2
, "a")) == 0){
367 debuga(_("(html7) Cannot open file %s\n"),tmp2
);
371 if ((line1
=longline_create())==NULL
) {
372 debuga(_("Not enough memory to read file %s\n"),arqip
);
375 while((buf
=longline_read(fp_ip
,line1
))!=NULL
) {
376 if(strstr(buf
,url
) != 0)
379 longline_destroy(&line1
);
384 sprintf(csort
,"sort -n -T \"%s\" -k 1,1 -k 5,5 -o \"%s\" \"%s\"",TempDir
,tmp3
,tmp2
);
385 cstatus
=system(csort
);
386 if (!WIFEXITED(cstatus
) || WEXITSTATUS(cstatus
)) {
387 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus
));
388 debuga(_("sort command: %s\n"),csort
);
392 if ((fp_ip
= MY_FOPEN(tmp3
, "r")) == 0) {
393 debuga(_("(html8) Cannot open file %s\n"),tmp3
);
399 if ((line1
=longline_create())==NULL
) {
400 debuga(_("Not enough memory to read file %s\n"),arqip
);
403 while((buf
=longline_read(fp_ip
,line1
))!=NULL
) {
404 getword_start(&gwarea
,buf
);
405 if (getword(user_ip
,sizeof(user_ip
),&gwarea
,'\t')<0) {
406 debuga(_("Maybe you have a broken user IP in your %s file\n"),tmp3
);
409 if (getword_ptr(NULL
,NULL
,&gwarea
,'\t')<0) {
410 debuga(_("Maybe you have a broken url in your %s file\n"),tmp3
);
413 if (getword(userdia
,sizeof(userdia
),&gwarea
,'\t')<0) {
414 debuga(_("Maybe you have a broken day in your %s file\n"),tmp3
);
417 if (getword(userhora
,sizeof(userhora
),&gwarea
,'\t')<0) {
418 debuga(_("Maybe you have a broken time in your %s file\n"),tmp3
);
421 if (getword_atoll(&userbytes
,&gwarea
,'\t')<0) {
422 debuga(_("Maybe you have a broken size in your %s file\n"),tmp3
);
425 if (getword_atoll(&userelap
,&gwarea
,'\t')<0) {
426 debuga(_("Maybe you have a broken elapsed time in your %s file\n"),tmp3
);
429 if(strcmp(user_ip
,olduserip
) != 0) {
430 sprintf(wwork1
,"%s",fixnum(unbytes
,1));
431 fprintf(fp_ou
,"<tr><td></td><td class=\"data\">%s</td><td></td><td class=\"data\">%s</td><td></td><td></td><td></td><td class=\"data\">%s</td><td class=\"data\">%lld</td></tr>\n",olduserip
,wwork1
,buildtime(unelap
),unelap
);
433 strcpy(olduserip
,user_ip
);
439 unbytes
=unbytes
+userbytes
;
440 unelap
=unelap
+userelap
;
444 longline_destroy(&line1
);
449 sprintf(wwork1
,"%s",fixnum(unbytes
,1));
450 fprintf(fp_ou
,"<tr><td></td><td class=\"data\">%s</td><td></td><td class=\"data\">%s</td><td></td><td></td><td></td><td class=\"data\">%s</td><td class=\"data\">%lld</td></tr>\n",olduserip
,wwork1
,buildtime(unelap
),unelap
);
459 longline_destroy(&line
);
465 if((UserReportFields
& USERREPORTFIELDS_TOTAL
) != 0) {
466 fprintf(fp_ou
,"<tr><th></th><th class=\"header_l\">%s</th>",_("TOTAL"));
467 if((UserReportFields
& USERREPORTFIELDS_CONNECT
) != 0)
468 fprintf(fp_ou
,"<th class=\"header_r\">%s</th>",fixnum(tnacc
,1));
469 if((UserReportFields
& USERREPORTFIELDS_BYTES
) != 0)
470 fprintf(fp_ou
,"<th class=\"header_r\">%s</th>",fixnum(tnbytes
,1));
471 if((UserReportFields
& USERREPORTFIELDS_SETYB
) != 0) {
472 perc
=(totbytes
) ? tnbytes
*100. / totbytes
:0.;
473 fprintf(fp_ou
,"<th class=\"header_r\">%3.2lf%%</th>",perc
);
475 if((UserReportFields
& USERREPORTFIELDS_IN_CACHE_OUT
) != 0) {
476 inperc
=(tnbytes
) ? tnincache
* 100. / tnbytes
: 0.;
477 ouperc
=(tnbytes
) ? tnoucache
* 100. / tnbytes
: 0.;
478 fprintf(fp_ou
,"<th class=\"header_r\">%3.2lf%%</th><th class=\"header_r\">%3.2lf%%</th>",inperc
,ouperc
);
480 if((UserReportFields
& USERREPORTFIELDS_USED_TIME
) != 0)
481 fprintf(fp_ou
,"<th class=\"header_r\">%s</th>",buildtime(tnelap
));
482 if((UserReportFields
& USERREPORTFIELDS_MILISEC
) != 0)
483 fprintf(fp_ou
,"<th class=\"header_r\">%s</th>",fixnum2(tnelap
,1));
484 if((UserReportFields
& USERREPORTFIELDS_PTIME
) != 0) {
485 perc2
=(totelap
) ? tnelap
* 100. / totelap
: 0.;
486 fprintf(fp_ou
,"<th class=\"header_r\">%3.2lf%%</th>",perc2
);
488 fputs("</tr>\n",fp_ou
);
491 if(PerUserLimit
> 0) {
492 if(tnbytes
> (PerUserLimit
*1000000)) {
494 if(access(PerUserLimitFile
, R_OK
) == 0) {
495 if((fp_usr
= fopen(PerUserLimitFile
, "r")) == 0) {
496 debuga(_("(html9) Cannot open file %s\n"),PerUserLimitFile
);
499 while(fgets(tmp6
,sizeof(tmp6
),fp_usr
)!=NULL
) {
501 if(strcmp(tmp6
,uinfo
->label
) == 0) {
510 if((fp_usr
= fopen(PerUserLimitFile
, "a")) == 0) {
511 debuga(_("(html10) Cannot open file %s\n"),PerUserLimitFile
);
514 fprintf(fp_usr
,"%s\n",uinfo
->label
);
518 debuga(_("User %s limit exceeded (%d MB). Added to file %s\n"),uinfo
->label
,PerUserLimit
,PerUserLimitFile
);
528 if ((ReportType
& REPORT_TYPE_TOPUSERS
) != 0 && (UserReportFields
& USERREPORTFIELDS_AVERAGE
) != 0) {
529 totbytes2
=totbytes
/ntotuser
;
530 totelap2
=totelap
/ntotuser
;
532 fprintf(fp_ou
,"<tr><th></th><th class=\"header_l\">%s</th>",_("AVERAGE"));
533 if((UserReportFields
& USERREPORTFIELDS_CONNECT
) != 0)
534 fprintf(fp_ou
,"<th class=\"header_r\">%s</th>",fixnum(ttnacc
/ntotuser
,1));
535 if((UserReportFields
& USERREPORTFIELDS_BYTES
) != 0)
536 fprintf(fp_ou
,"<th class=\"header_r\">%s</th>",fixnum(totbytes2
,1));
537 fprintf(fp_ou
,"<th></th><th></th><th></th>");
538 if((UserReportFields
& USERREPORTFIELDS_USED_TIME
) != 0)
539 fprintf(fp_ou
,"<th class=\"header_r\">%s</th>",buildtime(totelap2
));
540 if((UserReportFields
& USERREPORTFIELDS_MILISEC
) != 0)
541 fprintf(fp_ou
,"<th class=\"header_r\">%s</th>",fixnum2(totelap2
,1));
542 if((UserReportFields
& USERREPORTFIELDS_PTIME
) != 0) {
543 perc2
= (totelap
) ? totelap2
* 100. / totelap
: 0.;
544 fprintf(fp_ou
,"<th class=\"header_r\">%3.2lf%%</th>",perc2
);
546 fputs("</tr>\n",fp_ou
);
549 fputs("</table></div>\n",fp_ou
);
550 if (write_html_trailer(fp_ou
)<0)
551 debuga(_("Write error in file %s\n"),arqou
);
552 if (fclose(fp_ou
)==EOF
)
553 debuga(_("Failed to close file %s - %s\n"),arqou
,strerror(errno
));
558 (void)closedir(dirp
);