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"
30 #ifdef ENABLE_DOUBLE_CHECK_DATA
31 extern struct globalstatstruct globstat
;
36 FILE *fp_in
= NULL
, *fp_ou
= NULL
, *fp_top1
= NULL
, *fp_top2
= NULL
, *fp_top3
= NULL
;
37 long long int ttnbytes
=0, ttnacc
=0, tnacc
=0;
38 long long int tnbytes
=0, ttnelap
=0, tnelap
=0;
39 long long int tnincache
=0, tnoucache
=0, ttnincache
=0, ttnoucache
=0;
42 long long int elap
, incac
, oucac
;
45 double inperc
=0.00, ouperc
=0.00;
47 char olduser
[MAX_USER_LEN
], csort
[MAXLEN
];
48 char wger
[MAXLEN
], top1
[MAXLEN
], top2
[MAXLEN
], top3
[MAXLEN
];
49 char user
[MAX_USER_LEN
], tusr
[MAXLEN
];
50 char ipantes
[MAXLEN
], nameantes
[MAXLEN
];
51 const char *sfield
="-n -k 2,2";
53 const char *sort_field
;
54 const char *sort_order
;
60 struct getwordstruct gwarea
;
62 struct generalitemstruct item
;
63 const struct userinfostruct
*uinfo
;
69 snprintf(wger
,sizeof(wger
),"%s/sarg-general",outdirname
);
70 if((fp_in
=fopen(wger
,"r"))==NULL
) {
71 debuga(_("(topuser) Cannot open file %s\n"),wger
);
75 snprintf(top2
,sizeof(top2
),"%s/top.tmp",outdirname
);
76 if((fp_top2
=fopen(top2
,"w"))==NULL
) {
77 debuga(_("(topuser) Cannot open file %s\n"),top2
);
84 if ((line
=longline_create())==NULL
) {
85 debuga(_("Not enough memory to read the file %s\n"),wger
);
89 while((warea
=longline_read(fp_in
,line
))!=NULL
) {
90 ger_read(warea
,&item
,wger
);
91 if(item
.total
) continue;
92 if(strcmp(olduser
,item
.user
) != 0) {
95 if (olduser
[0] != '\0') {
97 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
98 to print a long long int unless it is exactly 64-bits long.
100 fprintf(fp_top2
,"%s\t%"PRIu64
"\t%"PRIu64
"\t%"PRIu64
"\t%"PRIu64
"\t%"PRIu64
"\n",olduser
,(uint64_t)tnbytes
,(uint64_t)tnacc
,(uint64_t)tnelap
,(uint64_t)tnincache
,(uint64_t)tnoucache
);
105 ttnincache
+=tnincache
;
106 ttnoucache
+=tnoucache
;
108 strcpy(olduser
,item
.user
);
116 tnbytes
+=item
.nbytes
;
119 tnincache
+=item
.incache
;
120 tnoucache
+=item
.oucache
;
123 longline_destroy(&line
);
125 if (olduser
[0] != '\0') {
127 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
128 to print a long long int unless it is exactly 64-bits long.
130 fprintf(fp_top2
,"%s\t%"PRIu64
"\t%"PRIu64
"\t%"PRIu64
"\t%"PRIu64
"\t%"PRIu64
"\n",olduser
,(uint64_t)tnbytes
,(uint64_t)tnacc
,(uint64_t)tnelap
,(uint64_t)tnincache
,(uint64_t)tnoucache
);
135 ttnincache
+=tnincache
;
136 ttnoucache
+=tnoucache
;
140 #ifdef ENABLE_DOUBLE_CHECK_DATA
141 if (ttnacc
!=globstat
.nacc
|| ttnbytes
!=globstat
.nbytes
|| ttnelap
!=globstat
.elap
||
142 ttnincache
!=globstat
.incache
|| ttnoucache
!=globstat
.oucache
) {
143 debuga(_("Total statistics mismatch when reading %s to produce the top users\n"),wger
);
148 if((TopuserSort
& TOPUSER_SORT_USER
) != 0) {
150 sort_field
=_("user");
151 } else if((TopuserSort
& TOPUSER_SORT_CONNECT
) != 0) {
153 sort_field
=_("connect");
154 } else if((TopuserSort
& TOPUSER_SORT_TIME
) != 0) {
156 sort_field
=_("time");
158 sort_field
=_("bytes");
161 if((TopuserSort
& TOPUSER_SORT_REVERSE
) == 0) {
163 sort_order
=_("normal");
166 sort_order
=_("reverse");
169 snprintf(top1
,sizeof(top1
),"%s/top",outdirname
);
170 sprintf(csort
,"sort -T \"%s\" -t \"\t\" %s %s -o \"%s\" \"%s\"", tmp
, order
, sfield
, top1
, top2
);
171 cstatus
=system(csort
);
172 if (!WIFEXITED(cstatus
) || WEXITSTATUS(cstatus
)) {
173 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus
));
174 debuga(_("sort command: %s\n"),csort
);
178 if((fp_top1
=fopen(top1
,"r"))==NULL
) {
179 debuga(_("(topuser) Cannot open file %s\n"),top1
);
184 debuga(_("Cannot delete %s - %s\n"),top2
,strerror(errno
));
188 snprintf(top3
,sizeof(top3
),"%s/index.html",outdirname
);
189 if((fp_top3
=fopen(top3
,"w"))==NULL
) {
190 debuga(_("(topuser) Cannot open file %s\n"),top3
);
194 snprintf(title
,sizeof(title
),_("SARG report for %s"),period
.text
);
195 write_html_header(fp_top3
,(IndexTree
== INDEX_TREE_DATE
) ? 3 : 1,title
,HTML_JS_SORTTABLE
);
196 fputs("<tr><td class=\"header_c\">",fp_top3
);
197 fprintf(fp_top3
,_("Period: %s"),period
.html
);
198 fputs("</td></tr>\n",fp_top3
);
199 fputs("<tr><td class=\"header_c\">",fp_top3
);
200 fprintf(fp_top3
,_("Sort: %s, %s"),sort_field
,sort_order
);
201 fputs("</td></tr>\n",fp_top3
);
202 fprintf(fp_top3
,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Top users"));
203 close_html_header(fp_top3
);
206 fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_top3
);
207 if((ReportType
& REPORT_TYPE_TOPSITES
) != 0 && !Privacy
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"0\"><a href=\"topsites.html\">%s</a></td></tr>\n",_("Top sites"));
208 if((ReportType
& REPORT_TYPE_SITES_USERS
) != 0 && !Privacy
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"0\"><a href=\"siteuser.html\">%s</a></td></tr>\n",_("Sites & Users"));
209 if(dansguardian_count
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"0\"><a href=\"dansguardian.html\">%s</a></td></tr>\n",_("DansGuardian"));
210 if(squidguard_count
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"0\"><a href=\"redirector.html\">%s</a></td></tr>\n",_("Redirector"));
211 if ((ReportType
& REPORT_TYPE_DOWNLOADS
) != 0 && download_count
&& !Privacy
&& ndownload
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"0\"><a href=\"download.html\">%s</a></td></tr>\n",_("Downloads"));
212 if ((ReportType
& REPORT_TYPE_DENIED
) != 0 && denied_count
&& !Privacy
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"0\"><a href=\"denied.html\">%s</a></td></tr>\n",_("Denied accesses"));
213 if ((ReportType
& REPORT_TYPE_AUTH_FAILURES
) != 0 && authfail_count
&& !Privacy
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"0\"><a href=\"authfail.html\">%s</a></td></tr>\n",_("Authentication Failures"));
214 if(smartfilter
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"0\"><a href=\"smartfilter.html\">%s</a></td></tr>\n",_("SmartFilter"));
215 if(UserAgentLog
[0] != '\0' && useragent_count
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"0\"><a href=\"useragent.html\">%s</a></td></tr>\n",_("Useragent"));
216 fputs("<tr><td></td></tr>\n</table></div>\n",fp_top3
);
219 if ((ReportType
& REPORT_TYPE_TOPUSERS
) == 0) {
220 fputs("</body>\n</html>\n",fp_top3
);
225 fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\"",fp_top3
);
227 fputs(" class=\"sortable\"",fp_top3
);
228 fputs(">\n<thead><tr>",fp_top3
);
230 if((TopUserFields
& TOPUSERFIELDS_NUM
) != 0)
231 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("NUM"));
232 if((TopUserFields
& TOPUSERFIELDS_DATE_TIME
) !=0 && (ReportType
& REPORT_TYPE_DATE_TIME
) != 0 && !indexonly
) {
233 fputs("<th class=\"header_l",fp_top3
);
234 if (SortTableJs
[0]) fputs(" sorttable_nosort",fp_top3
);
235 fputs("\"></th>",fp_top3
);
237 if((TopUserFields
& TOPUSERFIELDS_USERID
) != 0) {
238 fputs("<th class=\"header_l",fp_top3
);
239 if (SortTableJs
[0]) fputs(" sorttable_alpha",fp_top3
);
240 fprintf(fp_top3
,"\">%s</th>",_("USERID"));
242 if((TopUserFields
& TOPUSERFIELDS_CONNECT
) != 0)
243 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("CONNECT"));
244 if((TopUserFields
& TOPUSERFIELDS_BYTES
) != 0)
245 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("BYTES"));
246 if((TopUserFields
& TOPUSERFIELDS_SETYB
) != 0)
247 fprintf(fp_top3
,"<th class=\"header_l\">%%%s</th>",_("BYTES"));
248 if((TopUserFields
& TOPUSERFIELDS_IN_CACHE_OUT
) != 0)
249 fprintf(fp_top3
,"<th class=\"header_c\" colspan=\"2\">%s</th><th style=\"display:none;\"></th>",_("IN-CACHE-OUT"));
250 if((TopUserFields
& TOPUSERFIELDS_USED_TIME
) != 0)
251 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("ELAPSED TIME"));
252 if((TopUserFields
& TOPUSERFIELDS_MILISEC
) != 0)
253 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("MILLISEC"));
254 if((TopUserFields
& TOPUSERFIELDS_PTIME
) != 0)
255 fprintf(fp_top3
,"<th class=\"header_l\">%%%s</th>",_("TIME"));
257 fputs("</tr></thead>\n",fp_top3
);
261 if ((line
=longline_create())==NULL
) {
262 debuga(_("Not enough memory to read the downloaded files\n"));
266 while((warea
=longline_read(fp_top1
,line
))!=NULL
) {
267 getword_start(&gwarea
,warea
);
268 if (getword(user
,sizeof(user
),&gwarea
,'\t')<0) {
269 debuga(_("There is a broken user in file %s\n"),top1
);
272 if (getword_atoll(&nbytes
,&gwarea
,'\t')<0) {
273 debuga(_("There is a broken number of bytes in file %s\n"),top1
);
276 if (getword_atoll(&nacc
,&gwarea
,'\t')<0) {
277 debuga(_("There is a broken number of access in file %s\n"),top1
);
280 if (getword_atoll(&elap
,&gwarea
,'\t')<0) {
281 debuga(_("There is a broken elpased time in file %s\n"),top1
);
284 if (getword_atoll(&incac
,&gwarea
,'\t')<0) {
285 debuga(_("There is a broken in-cache size in file %s\n"),top1
);
288 if (getword_atoll(&oucac
,&gwarea
,'\n')<0) {
289 debuga(_("There is a broken out-of-cache size in file %s\n"),top1
);
295 if(TopUsersNum
> 0 && topcount
>= TopUsersNum
) break;
299 uinfo
=userinfo_find_from_id(user
);
301 debuga(_("Unknown user ID %s in file %s\n"),user
,top1
);
305 fputs("<tr>",fp_top3
);
308 if((TopUserFields
& TOPUSERFIELDS_NUM
) != 0)
309 fprintf(fp_top3
,"<td class=\"data\">%d</td>",posicao
);
312 if((TopUserFields
& TOPUSERFIELDS_DATE_TIME
) !=0 && (ReportType
& REPORT_TYPE_DATE_TIME
) != 0) {
313 fputs("<td class=\"data2\">",fp_top3
);
315 if(Graphs
&& GraphFont
[0]!='\0') {
316 //fprintf(fp_top3,"<a href=\"%s/graph_day.png\"><img src=\"%s/graph.png\" title=\"%s\" alt=\"G\"></a> ",uinfo->filename,ImageFile,_("Graphic"));
317 fprintf(fp_top3
,"<a href=\"%s/graph.html\"><img src=\"%s/graph.png\" title=\"%s\" alt=\"G\"></a> ",uinfo
->filename
,ImageFile
,_("Graphic"));
320 fprintf(fp_top3
,"<a href=\"%s/d%s.html\"><img src=\"%s/datetime.png\" title=\"%s\" alt=\"T\"></a></td>",uinfo
->filename
,uinfo
->filename
,ImageFile
,_("date/time report"));
322 sprintf(val1
,"%s/d%s.html",outdirname
,uinfo
->filename
);
324 debuga(_("Cannot delete %s - %s\n"),val1
,strerror(errno
));
329 if((TopUserFields
& TOPUSERFIELDS_USERID
) != 0) {
330 if((ReportType
& REPORT_TYPE_USERS_SITES
) == 0 || indexonly
)
331 fprintf(fp_top3
,"<td class=\"data2\">%s</td>",uinfo
->label
);
333 fprintf(fp_top3
,"<td class=\"data2\"><a href=\"%s/%s.html\">%s</a></td>",uinfo
->filename
,uinfo
->filename
,uinfo
->label
);
335 if((TopUserFields
& TOPUSERFIELDS_CONNECT
) != 0) {
336 fputs("<td class=\"data\"",fp_top3
);
337 if (SortTableJs
[0]) fprintf(fp_top3
," sorttable_customkey=\"%"PRId64
"\"",(int64_t)nacc
);
338 fprintf(fp_top3
,">%s</td>",fixnum(nacc
,1));
340 if((TopUserFields
& TOPUSERFIELDS_BYTES
) != 0) {
341 fputs("<td class=\"data\"",fp_top3
);
342 if (SortTableJs
[0]) fprintf(fp_top3
," sorttable_customkey=\"%"PRId64
"\"",(int64_t)tnbytes
);
343 fprintf(fp_top3
,">%s</td>",fixnum(tnbytes
,1));
345 if((TopUserFields
& TOPUSERFIELDS_SETYB
) != 0) {
346 perc
=(ttnbytes
) ? tnbytes
* 100. / ttnbytes
: 0.;
347 fprintf(fp_top3
,"<td class=\"data\">%3.2lf%%</td>",perc
);
349 if((TopUserFields
& TOPUSERFIELDS_IN_CACHE_OUT
) != 0) {
350 inperc
=(tnbytes
) ? incac
* 100. / tnbytes
: 0.;
351 ouperc
=(tnbytes
) ? oucac
* 100. / tnbytes
: 0.;
352 fprintf(fp_top3
,"<td class=\"data\">%3.2lf%%</td><td class=\"data\">%3.2lf%%</td>",inperc
,ouperc
);
353 #ifdef ENABLE_DOUBLE_CHECK_DATA
354 if (fabs(inperc
+ouperc
-100.)>=0.01) {
355 debuga(_("The total of the in-cache and cache-miss is not 100%% at position %d (user %s)\n"),posicao
,uinfo
->label
);
359 if((TopUserFields
& TOPUSERFIELDS_USED_TIME
) != 0) {
360 fputs("<td class=\"data\"",fp_top3
);
361 if (SortTableJs
[0]) fprintf(fp_top3
," sorttable_customkey=\"%"PRId64
"\"",(int64_t)tnelap
);
362 fprintf(fp_top3
,">%s</td>",buildtime(tnelap
));
364 if((TopUserFields
& TOPUSERFIELDS_MILISEC
) != 0) {
365 fputs("<td class=\"data\"",fp_top3
);
366 if (SortTableJs
[0]) fprintf(fp_top3
," sorttable_customkey=\"%"PRId64
"\"",(int64_t)tnelap
);
367 fprintf(fp_top3
,">%s</td>",fixnum2(tnelap
,1));
369 if((TopUserFields
& TOPUSERFIELDS_PTIME
) != 0) {
370 perc2
=(ttnelap
) ? elap
* 100. / ttnelap
: 0.;
371 fprintf(fp_top3
,"<td class=\"data\">%3.2lf%%</td>",perc2
);
374 fputs("</tr>\n",fp_top3
);
380 debuga(_("Cannot delete %s - %s\n"),top1
,strerror(errno
));
383 longline_destroy(&line
);
385 if((TopUserFields
& TOPUSERFIELDS_TOTAL
) != 0) {
386 fputs("<tfoot><tr>",fp_top3
);
387 if((TopUserFields
& TOPUSERFIELDS_NUM
) != 0)
388 fputs("<td></td>",fp_top3
);
389 if((TopUserFields
& TOPUSERFIELDS_DATE_TIME
) !=0 && (ReportType
& REPORT_TYPE_DATE_TIME
) != 0 && !indexonly
)
390 fputs("<td></td>",fp_top3
);
391 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("TOTAL"));
393 if((TopUserFields
& TOPUSERFIELDS_CONNECT
) != 0)
394 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",fixnum(ttnacc
,1));
395 if((TopUserFields
& TOPUSERFIELDS_BYTES
) != 0)
396 fprintf(fp_top3
,"<th class=\"header_r\">%15s</th>",fixnum(ttnbytes
,1));
397 if((TopUserFields
& TOPUSERFIELDS_SETYB
) != 0)
398 fputs("<td></td>",fp_top3
);
399 if((TopUserFields
& TOPUSERFIELDS_IN_CACHE_OUT
) != 0)
401 inperc
=(ttnbytes
) ? ttnincache
* 100. / ttnbytes
: 0.;
402 ouperc
=(ttnbytes
) ? ttnoucache
*100. / ttnbytes
: 0.;
403 fprintf(fp_top3
,"<th class=\"header_r\">%3.2lf%%</th><th class=\"header_r\">%3.2lf%%</th>",inperc
,ouperc
);
404 #ifdef ENABLE_DOUBLE_CHECK_DATA
405 if (fabs(inperc
+ouperc
-100.)>=0.01) {
406 debuga(_("The total of the in-cache and cache-miss is not 100%%\n"));
410 if((TopUserFields
& TOPUSERFIELDS_USED_TIME
) != 0)
411 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",buildtime(ttnelap
));
412 if((TopUserFields
& TOPUSERFIELDS_MILISEC
) != 0)
413 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",fixnum2(ttnelap
,1));
415 fputs("</tr>\n",fp_top3
);
418 if(ntopuser
&& (TopUserFields
& TOPUSERFIELDS_AVERAGE
) != 0) {
419 fputs("<tr>",fp_top3
);
420 if((TopUserFields
& TOPUSERFIELDS_NUM
) != 0)
421 fputs("<td></td>",fp_top3
);
422 if((TopUserFields
& TOPUSERFIELDS_DATE_TIME
) !=0 && (ReportType
& REPORT_TYPE_DATE_TIME
) != 0 && !indexonly
)
423 fputs("<td></td>",fp_top3
);
424 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("AVERAGE"));
426 if((TopUserFields
& TOPUSERFIELDS_CONNECT
) != 0)
427 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",fixnum(ttnacc
/totuser
,1));
428 if((TopUserFields
& TOPUSERFIELDS_BYTES
) != 0) {
429 tnbytes
=(totuser
) ? ttnbytes
/ totuser
: 0;
430 fprintf(fp_top3
,"<th class=\"header_r\">%15s</th>",fixnum(tnbytes
,1));
432 if((TopUserFields
& TOPUSERFIELDS_SETYB
) != 0)
433 fputs("<td></td>",fp_top3
);
434 if((TopUserFields
& TOPUSERFIELDS_IN_CACHE_OUT
) != 0)
435 fputs("<td></td><td></td>",fp_top3
);
436 if((TopUserFields
& TOPUSERFIELDS_USED_TIME
) != 0)
437 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",buildtime(ttnelap
/totuser
));
438 if((TopUserFields
& TOPUSERFIELDS_MILISEC
) != 0)
439 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",fixnum2(ttnelap
/totuser
,1));
440 fputs("</tr></tfoot>\n",fp_top3
);
443 fputs("</table></div>\n",fp_top3
);
444 if (write_html_trailer(fp_top3
)<0)
445 debuga(_("Write error in top user list %s\n"),top3
);
446 if (fclose(fp_top3
)==EOF
)
447 debuga(_("Failed to close the top user list %s - %s\n"),top3
,strerror(errno
));
449 snprintf(tusr
,sizeof(tusr
),"%s/sarg-users",outdirname
);
450 if((fp_ou
=fopen(tusr
,"w"))==NULL
) {
451 debuga(_("(topuser) Cannot open file %s\n"),tusr
);
454 fprintf(fp_ou
,"%d\n",totuser
);
455 if (fclose(fp_ou
)==EOF
)
456 debuga(_("Failed to close file %s - %s\n"),tusr
,strerror(errno
));