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 extern struct globalstatstruct globstat
;
31 extern bool smartfilter
;
34 Save the total number of users. The number is written in sarg-users and set
35 in a global variable for further reference.
37 \param totuser The total number of users.
39 static void set_total_users(int totuser
)
44 snprintf(tusr
,sizeof(tusr
),"%s/sarg-users",outdirname
);
45 if((fp_ou
=fopen(tusr
,"w"))==NULL
) {
46 debuga(_("(topuser) Cannot open file %s: %s\n"),tusr
,strerror(errno
));
49 fprintf(fp_ou
,"%d\n",totuser
);
50 if (fclose(fp_ou
)==EOF
) {
51 debuga(_("Write error in %s: %s\n"),tusr
,strerror(errno
));
54 globstat
.totuser
=totuser
;
59 FILE *fp_in
= NULL
, *fp_top1
= NULL
, *fp_top2
= NULL
, *fp_top3
= NULL
;
60 long long int ttnbytes
=0, ttnacc
=0, tnacc
=0;
61 long long int tnbytes
=0, ttnelap
=0, tnelap
=0;
62 long long int tnincache
=0, tnoucache
=0, ttnincache
=0, ttnoucache
=0;
65 long long int elap
, incac
, oucac
;
68 double inperc
=0.00, ouperc
=0.00;
70 char olduser
[MAX_USER_LEN
], csort
[MAXLEN
];
71 char wger
[MAXLEN
], top1
[MAXLEN
], top2
[MAXLEN
], top3
[MAXLEN
];
72 char user
[MAX_USER_LEN
];
73 const char *sfield
="-n -k 2,2";
75 const char *sort_field
;
76 const char *sort_order
;
82 struct getwordstruct gwarea
;
84 struct generalitemstruct item
;
85 struct userinfostruct
*uinfo
;
88 snprintf(wger
,sizeof(wger
),"%s/sarg-general",outdirname
);
89 if((fp_in
=fopen(wger
,"r"))==NULL
) {
90 debuga(_("(topuser) Cannot open file %s: %s\n"),wger
,strerror(errno
));
94 snprintf(top2
,sizeof(top2
),"%s/top.tmp",outdirname
);
95 if((fp_top2
=fopen(top2
,"w"))==NULL
) {
96 debuga(_("(topuser) Cannot open file %s: %s\n"),top2
,strerror(errno
));
103 if ((line
=longline_create())==NULL
) {
104 debuga(_("Not enough memory to read the file %s\n"),wger
);
108 while((warea
=longline_read(fp_in
,line
))!=NULL
) {
109 ger_read(warea
,&item
,wger
);
110 if(item
.total
) continue;
111 if(strcmp(olduser
,item
.user
) != 0) {
114 if (olduser
[0] != '\0') {
116 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
117 to print a long long int unless it is exactly 64-bits long.
119 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
);
124 ttnincache
+=tnincache
;
125 ttnoucache
+=tnoucache
;
127 strcpy(olduser
,item
.user
);
135 tnbytes
+=item
.nbytes
;
138 tnincache
+=item
.incache
;
139 tnoucache
+=item
.oucache
;
142 longline_destroy(&line
);
144 if (olduser
[0] != '\0') {
146 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
147 to print a long long int unless it is exactly 64-bits long.
149 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
);
154 ttnincache
+=tnincache
;
155 ttnoucache
+=tnoucache
;
157 if (fclose(fp_top2
)==EOF
) {
158 debuga(_("Write error in %s: %s\n"),top2
,strerror(errno
));
162 #ifdef ENABLE_DOUBLE_CHECK_DATA
163 if (ttnacc
!=globstat
.nacc
|| ttnbytes
!=globstat
.nbytes
|| ttnelap
!=globstat
.elap
||
164 ttnincache
!=globstat
.incache
|| ttnoucache
!=globstat
.oucache
) {
165 debuga(_("Total statistics mismatch when reading %s to produce the top users\n"),wger
);
170 set_total_users(totuser
);
172 if((TopuserSort
& TOPUSER_SORT_USER
) != 0) {
174 sort_field
=_("user");
175 } else if((TopuserSort
& TOPUSER_SORT_CONNECT
) != 0) {
177 sort_field
=_("connect");
178 } else if((TopuserSort
& TOPUSER_SORT_TIME
) != 0) {
180 sort_field
=pgettext("duration","time");
182 sort_field
=_("bytes");
185 if((TopuserSort
& TOPUSER_SORT_REVERSE
) == 0) {
187 sort_order
=_("normal");
190 sort_order
=_("reverse");
193 snprintf(top1
,sizeof(top1
),"%s/top",outdirname
);
194 if (snprintf(csort
,sizeof(csort
),"sort -T \"%s\" -t \"\t\" %s %s -o \"%s\" \"%s\"", tmp
, order
, sfield
, top1
, top2
)>=sizeof(csort
)) {
195 debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),top2
,top1
);
198 cstatus
=system(csort
);
199 if (!WIFEXITED(cstatus
) || WEXITSTATUS(cstatus
)) {
200 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus
));
201 debuga(_("sort command: %s\n"),csort
);
205 if((fp_top1
=fopen(top1
,"r"))==NULL
) {
206 debuga(_("(topuser) Cannot open file %s: %s\n"),top1
,strerror(errno
));
210 if (!KeepTempLog
&& unlink(top2
)) {
211 debuga(_("Cannot delete \"%s\": %s\n"),top2
,strerror(errno
));
215 snprintf(top3
,sizeof(top3
),"%s/index.html",outdirname
);
216 if((fp_top3
=fopen(top3
,"w"))==NULL
) {
217 debuga(_("(topuser) Cannot open file %s: %s\n"),top3
,strerror(errno
));
221 snprintf(title
,sizeof(title
),_("SARG report for %s"),period
.text
);
222 write_html_header(fp_top3
,(IndexTree
== INDEX_TREE_DATE
) ? 3 : 1,title
,HTML_JS_SORTTABLE
);
223 fputs("<tr><td class=\"header_c\">",fp_top3
);
224 fprintf(fp_top3
,_("Period: %s"),period
.html
);
225 fputs("</td></tr>\n",fp_top3
);
226 if ((ReportType
& REPORT_TYPE_TOPUSERS
) != 0) {
227 fputs("<tr><td class=\"header_c\">",fp_top3
);
228 fprintf(fp_top3
,_("Sort: %s, %s"),sort_field
,sort_order
);
229 fputs("</td></tr>\n",fp_top3
);
230 fprintf(fp_top3
,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Top users"));
232 /* TRANSLATORS: This is the title of the main report page when no
233 * top users list are requested.
235 fprintf(fp_top3
,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Table of content"));
237 close_html_header(fp_top3
);
240 fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_top3
);
241 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"));
242 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"));
243 if(dansguardian_count
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"0\"><a href=\"dansguardian.html\">%s</a></td></tr>\n",_("DansGuardian"));
244 if(redirector_count
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"0\"><a href=\"redirector.html\">%s</a></td></tr>\n",_("Redirector"));
245 if (is_download()) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"0\"><a href=\"download.html\">%s</a></td></tr>\n",_("Downloads"));
246 if (is_denied()) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"0\"><a href=\"denied.html\">%s</a></td></tr>\n",_("Denied accesses"));
247 if (is_authfail()) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"0\"><a href=\"authfail.html\">%s</a></td></tr>\n",_("Authentication Failures"));
248 if(smartfilter
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"0\"><a href=\"smartfilter.html\">%s</a></td></tr>\n",_("SmartFilter"));
249 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"));
250 fputs("<tr><td></td></tr>\n</table></div>\n",fp_top3
);
253 if ((ReportType
& REPORT_TYPE_TOPUSERS
) == 0) {
254 fputs("</body>\n</html>\n",fp_top3
);
255 if (fclose (fp_top3
)==EOF
) {
256 debuga(_("Write error in %s: %s\n"),top3
,strerror(errno
));
259 if (debugz
) debugaz(_("No top users report because it is not configured in report_type\n"));
263 fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\"",fp_top3
);
265 fputs(" class=\"sortable\"",fp_top3
);
266 fputs(">\n<thead><tr>",fp_top3
);
268 if((TopUserFields
& TOPUSERFIELDS_NUM
) != 0)
269 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("NUM"));
270 if((TopUserFields
& TOPUSERFIELDS_DATE_TIME
) !=0 && (ReportType
& REPORT_TYPE_DATE_TIME
) != 0 && !indexonly
) {
271 fputs("<th class=\"header_l",fp_top3
);
272 if (SortTableJs
[0]) fputs(" sorttable_nosort",fp_top3
);
273 fputs("\"></th>",fp_top3
);
275 if((TopUserFields
& TOPUSERFIELDS_USERID
) != 0) {
276 fputs("<th class=\"header_l",fp_top3
);
277 if (SortTableJs
[0]) fputs(" sorttable_alpha",fp_top3
);
278 fprintf(fp_top3
,"\">%s</th>",_("USERID"));
280 if((TopUserFields
& TOPUSERFIELDS_CONNECT
) != 0)
281 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("CONNECT"));
282 if((TopUserFields
& TOPUSERFIELDS_BYTES
) != 0)
283 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("BYTES"));
284 if((TopUserFields
& TOPUSERFIELDS_SETYB
) != 0)
285 fprintf(fp_top3
,"<th class=\"header_l\">%%%s</th>",_("BYTES"));
286 if((TopUserFields
& TOPUSERFIELDS_IN_CACHE_OUT
) != 0)
287 fprintf(fp_top3
,"<th class=\"header_c\" colspan=\"2\">%s</th><th style=\"display:none;\"></th>",_("IN-CACHE-OUT"));
288 if((TopUserFields
& TOPUSERFIELDS_USED_TIME
) != 0)
289 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("ELAPSED TIME"));
290 if((TopUserFields
& TOPUSERFIELDS_MILISEC
) != 0)
291 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("MILLISEC"));
292 if((TopUserFields
& TOPUSERFIELDS_PTIME
) != 0)
293 fprintf(fp_top3
,"<th class=\"header_l\">%%%s</th>",pgettext("duration","TIME"));
295 fputs("</tr></thead>\n",fp_top3
);
301 if ((line
=longline_create())==NULL
) {
302 debuga(_("Not enough memory to read the downloaded files\n"));
306 while((warea
=longline_read(fp_top1
,line
))!=NULL
) {
307 getword_start(&gwarea
,warea
);
308 if (getword(user
,sizeof(user
),&gwarea
,'\t')<0) {
309 debuga(_("There is a broken user in file %s\n"),top1
);
312 if (getword_atoll(&nbytes
,&gwarea
,'\t')<0) {
313 debuga(_("There is a broken number of bytes in file %s\n"),top1
);
316 if (getword_atoll(&nacc
,&gwarea
,'\t')<0) {
317 debuga(_("There is a broken number of access in file %s\n"),top1
);
320 if (getword_atoll(&elap
,&gwarea
,'\t')<0) {
321 debuga(_("There is a broken elpased time in file %s\n"),top1
);
324 if (getword_atoll(&incac
,&gwarea
,'\t')<0) {
325 debuga(_("There is a broken in-cache size in file %s\n"),top1
);
328 if (getword_atoll(&oucac
,&gwarea
,'\n')<0) {
329 debuga(_("There is a broken out-of-cache size in file %s\n"),top1
);
335 if(TopUsersNum
> 0 && topcount
>= TopUsersNum
) break;
339 uinfo
=userinfo_find_from_id(user
);
341 debuga(_("Unknown user ID %s in file %s\n"),user
,top1
);
346 fputs("<tr>",fp_top3
);
349 if((TopUserFields
& TOPUSERFIELDS_NUM
) != 0)
350 fprintf(fp_top3
,"<td class=\"data\">%d</td>",posicao
);
353 if((TopUserFields
& TOPUSERFIELDS_DATE_TIME
) !=0 && (ReportType
& REPORT_TYPE_DATE_TIME
) != 0) {
354 fputs("<td class=\"data2\">",fp_top3
);
356 if(Graphs
&& GraphFont
[0]!='\0') {
358 //fprintf(fp_top3,"<a href=\"%s/graph_day.png\"><img src=\"%s/graph.png\" title=\"%s\" alt=\"G\"></a> ",uinfo->filename,ImageFile,_("Graphic"));
359 fprintf(fp_top3
,"<a href=\"%s/graph.html\"><img src=\"%s/graph.png\" title=\"%s\" alt=\"G\"></a> ",uinfo
->filename
,ImageFile
,_("Graphic"));
363 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"));
364 day_deletefile(uinfo
);
367 if((TopUserFields
& TOPUSERFIELDS_USERID
) != 0) {
368 if((ReportType
& REPORT_TYPE_USERS_SITES
) == 0 || indexonly
)
369 fprintf(fp_top3
,"<td class=\"data2\">%s</td>",uinfo
->label
);
371 fprintf(fp_top3
,"<td class=\"data2\"><a href=\"%s/%s.html\">%s</a></td>",uinfo
->filename
,uinfo
->filename
,uinfo
->label
);
373 if((TopUserFields
& TOPUSERFIELDS_CONNECT
) != 0) {
374 fputs("<td class=\"data\"",fp_top3
);
375 if (SortTableJs
[0]) fprintf(fp_top3
," sorttable_customkey=\"%"PRId64
"\"",(int64_t)nacc
);
376 fprintf(fp_top3
,">%s</td>",fixnum(nacc
,1));
378 if((TopUserFields
& TOPUSERFIELDS_BYTES
) != 0) {
379 fputs("<td class=\"data\"",fp_top3
);
380 if (SortTableJs
[0]) fprintf(fp_top3
," sorttable_customkey=\"%"PRId64
"\"",(int64_t)tnbytes
);
381 fprintf(fp_top3
,">%s</td>",fixnum(tnbytes
,1));
383 if((TopUserFields
& TOPUSERFIELDS_SETYB
) != 0) {
384 perc
=(ttnbytes
) ? tnbytes
* 100. / ttnbytes
: 0.;
385 fprintf(fp_top3
,"<td class=\"data\">%3.2lf%%</td>",perc
);
387 if((TopUserFields
& TOPUSERFIELDS_IN_CACHE_OUT
) != 0) {
388 inperc
=(tnbytes
) ? incac
* 100. / tnbytes
: 0.;
389 ouperc
=(tnbytes
) ? oucac
* 100. / tnbytes
: 0.;
390 fprintf(fp_top3
,"<td class=\"data\">%3.2lf%%</td><td class=\"data\">%3.2lf%%</td>",inperc
,ouperc
);
391 #ifdef ENABLE_DOUBLE_CHECK_DATA
392 if ((inperc
!=0. || ouperc
!=0.) && fabs(inperc
+ouperc
-100.)>=0.01) {
393 debuga(_("The total of the in-cache and cache-miss is not 100%% at position %d (user %s)\n"),posicao
,uinfo
->label
);
397 if((TopUserFields
& TOPUSERFIELDS_USED_TIME
) != 0) {
398 fputs("<td class=\"data\"",fp_top3
);
399 if (SortTableJs
[0]) fprintf(fp_top3
," sorttable_customkey=\"%"PRId64
"\"",(int64_t)tnelap
);
400 fprintf(fp_top3
,">%s</td>",buildtime(tnelap
));
402 if((TopUserFields
& TOPUSERFIELDS_MILISEC
) != 0) {
403 fputs("<td class=\"data\"",fp_top3
);
404 if (SortTableJs
[0]) fprintf(fp_top3
," sorttable_customkey=\"%"PRId64
"\"",(int64_t)tnelap
);
405 fprintf(fp_top3
,">%s</td>",fixnum2(tnelap
,1));
407 if((TopUserFields
& TOPUSERFIELDS_PTIME
) != 0) {
408 perc2
=(ttnelap
) ? elap
* 100. / ttnelap
: 0.;
409 fprintf(fp_top3
,"<td class=\"data\">%3.2lf%%</td>",perc2
);
412 fputs("</tr>\n",fp_top3
);
417 if (!KeepTempLog
&& unlink(top1
)) {
418 debuga(_("Cannot delete \"%s\": %s\n"),top1
,strerror(errno
));
421 longline_destroy(&line
);
423 if((TopUserFields
& TOPUSERFIELDS_TOTAL
) != 0) {
424 fputs("<tfoot><tr>",fp_top3
);
425 if((TopUserFields
& TOPUSERFIELDS_NUM
) != 0)
426 fputs("<td></td>",fp_top3
);
427 if((TopUserFields
& TOPUSERFIELDS_DATE_TIME
) !=0 && (ReportType
& REPORT_TYPE_DATE_TIME
) != 0 && !indexonly
)
428 fputs("<td></td>",fp_top3
);
429 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("TOTAL"));
431 if((TopUserFields
& TOPUSERFIELDS_CONNECT
) != 0)
432 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",fixnum(ttnacc
,1));
433 if((TopUserFields
& TOPUSERFIELDS_BYTES
) != 0)
434 fprintf(fp_top3
,"<th class=\"header_r\">%15s</th>",fixnum(ttnbytes
,1));
435 if((TopUserFields
& TOPUSERFIELDS_SETYB
) != 0)
436 fputs("<td></td>",fp_top3
);
437 if((TopUserFields
& TOPUSERFIELDS_IN_CACHE_OUT
) != 0)
439 inperc
=(ttnbytes
) ? ttnincache
* 100. / ttnbytes
: 0.;
440 ouperc
=(ttnbytes
) ? ttnoucache
*100. / ttnbytes
: 0.;
441 fprintf(fp_top3
,"<th class=\"header_r\">%3.2lf%%</th><th class=\"header_r\">%3.2lf%%</th>",inperc
,ouperc
);
442 #ifdef ENABLE_DOUBLE_CHECK_DATA
443 if (fabs(inperc
+ouperc
-100.)>=0.01) {
444 debuga(_("The total of the in-cache and cache-miss is not 100%%\n"));
448 if((TopUserFields
& TOPUSERFIELDS_USED_TIME
) != 0)
449 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",buildtime(ttnelap
));
450 if((TopUserFields
& TOPUSERFIELDS_MILISEC
) != 0)
451 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",fixnum2(ttnelap
,1));
453 fputs("</tr>\n",fp_top3
);
457 if(ntopuser
&& (TopUserFields
& TOPUSERFIELDS_AVERAGE
) != 0) {
458 fputs("<tr>",fp_top3
);
459 if((TopUserFields
& TOPUSERFIELDS_NUM
) != 0)
460 fputs("<td></td>",fp_top3
);
461 if((TopUserFields
& TOPUSERFIELDS_DATE_TIME
) !=0 && (ReportType
& REPORT_TYPE_DATE_TIME
) != 0 && !indexonly
)
462 fputs("<td></td>",fp_top3
);
463 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("AVERAGE"));
465 if((TopUserFields
& TOPUSERFIELDS_CONNECT
) != 0)
466 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",fixnum(ttnacc
/totuser
,1));
467 if((TopUserFields
& TOPUSERFIELDS_BYTES
) != 0) {
468 tnbytes
=(totuser
) ? ttnbytes
/ totuser
: 0;
469 fprintf(fp_top3
,"<th class=\"header_r\">%15s</th>",fixnum(tnbytes
,1));
471 if((TopUserFields
& TOPUSERFIELDS_SETYB
) != 0)
472 fputs("<td></td>",fp_top3
);
473 if((TopUserFields
& TOPUSERFIELDS_IN_CACHE_OUT
) != 0)
474 fputs("<td></td><td></td>",fp_top3
);
475 if((TopUserFields
& TOPUSERFIELDS_USED_TIME
) != 0)
476 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",buildtime(ttnelap
/totuser
));
477 if((TopUserFields
& TOPUSERFIELDS_MILISEC
) != 0)
478 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",fixnum2(ttnelap
/totuser
,1));
479 fputs("</tr></tfoot>\n",fp_top3
);
482 fputs("</table></div>\n",fp_top3
);
483 if (write_html_trailer(fp_top3
)<0)
484 debuga(_("Write error in top user list %s\n"),top3
);
485 if (fclose(fp_top3
)==EOF
) {
486 debuga(_("Write error in %s: %s\n"),top3
,strerror(errno
));