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
= NULL
, *fp_ou
= NULL
, *fp_top1
= NULL
, *fp_top2
= NULL
, *fp_top3
= NULL
;
34 long long int ttnbytes
=0, ttnacc
=0, tnacc
=0;
35 long long int tnbytes
=0, ttnelap
=0, tnelap
=0;
36 long long int tnincache
=0, tnoucache
=0, ttnincache
=0, ttnoucache
=0;
39 long long int elap
, incac
, oucac
;
42 double inperc
=0.00, ouperc
=0.00;
44 char olduser
[MAX_USER_LEN
], csort
[MAXLEN
];
45 char wger
[MAXLEN
], top1
[MAXLEN
], top2
[MAXLEN
], top3
[MAXLEN
];
46 char user
[MAX_USER_LEN
], tusr
[MAXLEN
];
47 char ipantes
[MAXLEN
], nameantes
[MAXLEN
];
48 const char *sfield
="-n -k 2,2";
50 const char *sort_field
;
51 const char *sort_order
;
57 struct getwordstruct gwarea
;
59 struct generalitemstruct item
;
60 const struct userinfostruct
*uinfo
;
66 snprintf(wger
,sizeof(wger
),"%s/sarg-general",outdirname
);
67 if((fp_in
=fopen(wger
,"r"))==NULL
) {
68 debuga(_("(topuser) Cannot open file %s\n"),wger
);
72 snprintf(top2
,sizeof(top2
),"%s/top.tmp",outdirname
);
73 if((fp_top2
=fopen(top2
,"w"))==NULL
) {
74 debuga(_("(topuser) Cannot open file %s\n"),top2
);
81 if ((line
=longline_create())==NULL
) {
82 debuga(_("Not enough memory to read the file %s\n"),wger
);
86 while((warea
=longline_read(fp_in
,line
))!=NULL
) {
87 ger_read(warea
,&item
,wger
);
88 if(item
.total
) continue;
89 if(strcmp(olduser
,item
.user
) != 0) {
92 if (olduser
[0] != '\0') {
94 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
95 to print a long long int unless it is exactly 64-bits long.
97 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
);
102 ttnincache
+=tnincache
;
103 ttnoucache
+=tnoucache
;
105 strcpy(olduser
,item
.user
);
113 tnbytes
+=item
.nbytes
;
116 tnincache
+=item
.incache
;
117 tnoucache
+=item
.oucache
;
120 longline_destroy(&line
);
122 if (olduser
[0] != '\0') {
124 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
125 to print a long long int unless it is exactly 64-bits long.
127 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
);
132 ttnincache
+=tnincache
;
133 ttnoucache
+=tnoucache
;
137 if((TopuserSort
& TOPUSER_SORT_USER
) != 0) {
139 sort_field
=_("user");
140 } else if((TopuserSort
& TOPUSER_SORT_CONNECT
) != 0) {
142 sort_field
=_("connect");
143 } else if((TopuserSort
& TOPUSER_SORT_TIME
) != 0) {
145 sort_field
=_("time");
147 sort_field
=_("bytes");
150 if((TopuserSort
& TOPUSER_SORT_REVERSE
) == 0) {
152 sort_order
=_("normal");
155 sort_order
=_("reverse");
158 snprintf(top1
,sizeof(top1
),"%s/top",outdirname
);
159 sprintf(csort
,"sort -T \"%s\" %s %s -o \"%s\" \"%s\"", tmp
, order
, sfield
, top1
, top2
);
160 cstatus
=system(csort
);
161 if (!WIFEXITED(cstatus
) || WEXITSTATUS(cstatus
)) {
162 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus
));
163 debuga(_("sort command: %s\n"),csort
);
167 if((fp_top1
=fopen(top1
,"r"))==NULL
) {
168 debuga(_("(topuser) Cannot open file %s\n"),top1
);
174 snprintf(top3
,sizeof(top3
),"%s/index.html",outdirname
);
175 if((fp_top3
=fopen(top3
,"w"))==NULL
) {
176 debuga(_("(topuser) Cannot open file %s\n"),top3
);
180 snprintf(title
,sizeof(title
),_("SARG report for %s"),period
.text
);
181 write_html_header(fp_top3
,(IndexTree
== INDEX_TREE_DATE
) ? 3 : 1,title
,HTML_JS_SORTTABLE
);
182 fputs("<tr><td class=\"header_c\">",fp_top3
);
183 fprintf(fp_top3
,_("Period: %s"),period
.html
);
184 fputs("</td></tr>\n",fp_top3
);
185 fputs("<tr><td class=\"header_c\">",fp_top3
);
186 fprintf(fp_top3
,_("Sort: %s, %s"),sort_field
,sort_order
);
187 fputs("</td></tr>\n",fp_top3
);
188 fprintf(fp_top3
,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Top users"));
189 close_html_header(fp_top3
);
191 fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_top3
);
193 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"));
194 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"));
195 if(dansguardian_count
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"0\"><a href=\"dansguardian.html\">%s</a></td></tr>\n",_("DansGuardian"));
196 if(squidguard_count
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"0\"><a href=\"redirector.html\">%s</a></td></tr>\n",_("Redirector"));
197 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"));
198 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"));
199 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"));
200 if(smartfilter
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"0\"><a href=\"smartfilter.html\">%s</a></td></tr>\n",_("SmartFilter"));
201 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"));
202 fputs("<tr><td></td></tr>\n</table></div>\n",fp_top3
);
204 if ((ReportType
& REPORT_TYPE_TOPUSERS
) == 0) {
205 fputs("</body>\n</html>\n",fp_top3
);
210 fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\"",fp_top3
);
212 fputs(" class=\"sortable\"",fp_top3
);
213 fputs(">\n<thead><tr>",fp_top3
);
215 if((TopUserFields
& TOPUSERFIELDS_NUM
) != 0)
216 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("NUM"));
217 if((TopUserFields
& TOPUSERFIELDS_DATE_TIME
) !=0 && (ReportType
& REPORT_TYPE_DATE_TIME
) != 0) {
218 fputs("<th class=\"header_l",fp_top3
);
219 if (SortTableJs
[0]) fputs(" sorttable_nosort",fp_top3
);
220 fputs("\"></th>",fp_top3
);
222 if((TopUserFields
& TOPUSERFIELDS_USERID
) != 0) {
223 fputs("<th class=\"header_l",fp_top3
);
224 if (SortTableJs
[0]) fputs(" sorttable_alpha",fp_top3
);
225 fprintf(fp_top3
,"\">%s</th>",_("USERID"));
227 if((TopUserFields
& TOPUSERFIELDS_CONNECT
) != 0)
228 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("CONNECT"));
229 if((TopUserFields
& TOPUSERFIELDS_BYTES
) != 0)
230 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("BYTES"));
231 if((TopUserFields
& TOPUSERFIELDS_SETYB
) != 0)
232 fprintf(fp_top3
,"<th class=\"header_l\">%%%s</th>",_("BYTES"));
233 if((TopUserFields
& TOPUSERFIELDS_IN_CACHE_OUT
) != 0)
234 fprintf(fp_top3
,"<th class=\"header_c\" colspan=\"2\">%s</th><th style=\"display:none;\"></th>",_("IN-CACHE-OUT"));
235 if((TopUserFields
& TOPUSERFIELDS_USED_TIME
) != 0)
236 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("ELAPSED TIME"));
237 if((TopUserFields
& TOPUSERFIELDS_MILISEC
) != 0)
238 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("MILLISEC"));
239 if((TopUserFields
& TOPUSERFIELDS_PTIME
) != 0)
240 fprintf(fp_top3
,"<th class=\"header_l\">%%%s</th>",_("TIME"));
242 fputs("</tr></thead>\n",fp_top3
);
246 if ((line
=longline_create())==NULL
) {
247 debuga(_("Not enough memory to read the downloaded files\n"));
251 while((warea
=longline_read(fp_top1
,line
))!=NULL
) {
252 getword_start(&gwarea
,warea
);
253 if (getword(user
,sizeof(user
),&gwarea
,'\t')<0) {
254 debuga(_("There is a broken user in file %s\n"),top1
);
257 if (getword_atoll(&nbytes
,&gwarea
,'\t')<0) {
258 debuga(_("There is a broken number of bytes in file %s\n"),top1
);
261 if (getword_atoll(&nacc
,&gwarea
,'\t')<0) {
262 debuga(_("There is a broken number of access in file %s\n"),top1
);
265 if (getword_atoll(&elap
,&gwarea
,'\t')<0) {
266 debuga(_("There is a broken elpased time in file %s\n"),top1
);
269 if (getword_atoll(&incac
,&gwarea
,'\t')<0) {
270 debuga(_("There is a broken in-cache size in file %s\n"),top1
);
273 if (getword_atoll(&oucac
,&gwarea
,'\n')<0) {
274 debuga(_("There is a broken out-of-cache size in file %s\n"),top1
);
280 if(TopUsersNum
> 0 && topcount
>= TopUsersNum
) break;
284 uinfo
=userinfo_find_from_id(user
);
286 debuga(_("Unknown user ID %s in file %s\n"),user
,top1
);
290 fputs("<tr>",fp_top3
);
293 if((TopUserFields
& TOPUSERFIELDS_NUM
) != 0)
294 fprintf(fp_top3
,"<td class=\"data\">%d</td>",posicao
);
296 if((TopUserFields
& TOPUSERFIELDS_DATE_TIME
) !=0 && (ReportType
& REPORT_TYPE_DATE_TIME
) != 0) {
297 fputs("<td class=\"data2\">",fp_top3
);
299 if(Graphs
&& GraphFont
[0]!='\0') {
300 //fprintf(fp_top3,"<a href=\"%s/graph_day.png\"><img src=\"%s/graph.png\" title=\"%s\" alt=\"G\"></a> ",uinfo->filename,ImageFile,_("Graphic"));
301 fprintf(fp_top3
,"<a href=\"%s/graph.html\"><img src=\"%s/graph.png\" title=\"%s\" alt=\"G\"></a> ",uinfo
->filename
,ImageFile
,_("Graphic"));
304 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"));
306 sprintf(val1
,"%s/d%s.html",outdirname
,uinfo
->filename
);
309 if((TopUserFields
& TOPUSERFIELDS_USERID
) != 0) {
310 if((ReportType
& REPORT_TYPE_USERS_SITES
) == 0)
311 fprintf(fp_top3
,"<td class=\"data2\">%s</td>",name
);
313 fprintf(fp_top3
,"<td class=\"data2\"><a href=\"%s/%s.html\">%s</a></td>",uinfo
->filename
,uinfo
->filename
,uinfo
->label
);
315 if((TopUserFields
& TOPUSERFIELDS_CONNECT
) != 0) {
316 fputs("<td class=\"data\"",fp_top3
);
317 if (SortTableJs
[0]) fprintf(fp_top3
," sorttable_customkey=\"%"PRId64
"\"",(int64_t)nacc
);
318 fprintf(fp_top3
,">%s</td>",fixnum(nacc
,1));
320 if((TopUserFields
& TOPUSERFIELDS_BYTES
) != 0) {
321 fputs("<td class=\"data\"",fp_top3
);
322 if (SortTableJs
[0]) fprintf(fp_top3
," sorttable_customkey=\"%"PRId64
"\"",(int64_t)tnbytes
);
323 fprintf(fp_top3
,">%s</td>",fixnum(tnbytes
,1));
325 if((TopUserFields
& TOPUSERFIELDS_SETYB
) != 0) {
326 perc
=(ttnbytes
) ? tnbytes
* 100. / ttnbytes
: 0.;
327 fprintf(fp_top3
,"<td class=\"data\">%3.2lf%%</td>",perc
);
329 if((TopUserFields
& TOPUSERFIELDS_IN_CACHE_OUT
) != 0) {
330 inperc
=(tnbytes
) ? incac
* 100. / tnbytes
: 0.;
331 ouperc
=(tnbytes
) ? oucac
* 100. / tnbytes
: 0.;
332 fprintf(fp_top3
,"<td class=\"data\">%3.2lf%%</td><td class=\"data\">%3.2lf%%</td>",inperc
,ouperc
);
333 #ifdef ENABLE_DOUBLE_CHECK_DATA
334 if (fabs(inperc
+ouperc
-100.)>=0.01) {
335 debuga(_("The total of the in-cache and cache-miss is not 100%% at position %d (user %s)\n"),posicao
,uinfo
->label
);
339 if((TopUserFields
& TOPUSERFIELDS_USED_TIME
) != 0) {
340 fputs("<td class=\"data\"",fp_top3
);
341 if (SortTableJs
[0]) fprintf(fp_top3
," sorttable_customkey=\"%"PRId64
"\"",(int64_t)tnelap
);
342 fprintf(fp_top3
,">%s</td>",buildtime(tnelap
));
344 if((TopUserFields
& TOPUSERFIELDS_MILISEC
) != 0) {
345 fputs("<td class=\"data\"",fp_top3
);
346 if (SortTableJs
[0]) fprintf(fp_top3
," sorttable_customkey=\"%"PRId64
"\"",(int64_t)tnelap
);
347 fprintf(fp_top3
,">%s</td>",fixnum2(tnelap
,1));
349 if((TopUserFields
& TOPUSERFIELDS_PTIME
) != 0) {
350 perc2
=(ttnelap
) ? elap
* 100. / ttnelap
: 0.;
351 fprintf(fp_top3
,"<td class=\"data\">%3.2lf%%</td>",perc2
);
354 fputs("</tr>\n",fp_top3
);
360 longline_destroy(&line
);
362 if((TopUserFields
& TOPUSERFIELDS_TOTAL
) != 0) {
363 fputs("<tfoot><tr>",fp_top3
);
364 if((TopUserFields
& TOPUSERFIELDS_NUM
) != 0)
365 fputs("<td></td>",fp_top3
);
366 if((TopUserFields
& TOPUSERFIELDS_DATE_TIME
) !=0 && (ReportType
& REPORT_TYPE_DATE_TIME
) != 0)
367 fputs("<td></td>",fp_top3
);
368 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("TOTAL"));
370 if((TopUserFields
& TOPUSERFIELDS_CONNECT
) != 0)
371 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",fixnum(ttnacc
,1));
372 if((TopUserFields
& TOPUSERFIELDS_BYTES
) != 0)
373 fprintf(fp_top3
,"<th class=\"header_r\">%15s</th>",fixnum(ttnbytes
,1));
374 if((TopUserFields
& TOPUSERFIELDS_SETYB
) != 0)
375 fputs("<td></td>",fp_top3
);
376 if((TopUserFields
& TOPUSERFIELDS_IN_CACHE_OUT
) != 0)
378 inperc
=(ttnbytes
) ? ttnincache
* 100. / ttnbytes
: 0.;
379 ouperc
=(ttnbytes
) ? ttnoucache
*100. / ttnbytes
: 0.;
380 fprintf(fp_top3
,"<th class=\"header_r\">%3.2lf%%</th><th class=\"header_r\">%3.2lf%%</th>",inperc
,ouperc
);
381 #ifdef ENABLE_DOUBLE_CHECK_DATA
382 if (fabs(inperc
+ouperc
-100.)>=0.01) {
383 debuga(_("The total of the in-cache and cache-miss is not 100%%\n"));
387 if((TopUserFields
& TOPUSERFIELDS_USED_TIME
) != 0)
388 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",buildtime(ttnelap
));
389 if((TopUserFields
& TOPUSERFIELDS_MILISEC
) != 0)
390 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",fixnum2(ttnelap
,1));
392 fputs("</tr>\n",fp_top3
);
395 if(ntopuser
&& (TopUserFields
& TOPUSERFIELDS_AVERAGE
) != 0) {
396 fputs("<tr>",fp_top3
);
397 if((TopUserFields
& TOPUSERFIELDS_NUM
) != 0)
398 fputs("<td></td>",fp_top3
);
399 if((TopUserFields
& TOPUSERFIELDS_DATE_TIME
) !=0 && (ReportType
& REPORT_TYPE_DATE_TIME
) != 0)
400 fputs("<td></td>",fp_top3
);
401 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("AVERAGE"));
403 if((TopUserFields
& TOPUSERFIELDS_CONNECT
) != 0)
404 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",fixnum(ttnacc
/totuser
,1));
405 if((TopUserFields
& TOPUSERFIELDS_BYTES
) != 0) {
406 tnbytes
=(totuser
) ? ttnbytes
/ totuser
: 0;
407 fprintf(fp_top3
,"<th class=\"header_r\">%15s</th>",fixnum(tnbytes
,1));
409 if((TopUserFields
& TOPUSERFIELDS_SETYB
) != 0)
410 fputs("<td></td>",fp_top3
);
411 if((TopUserFields
& TOPUSERFIELDS_IN_CACHE_OUT
) != 0)
412 fputs("<td></td><td></td>",fp_top3
);
413 if((TopUserFields
& TOPUSERFIELDS_USED_TIME
) != 0)
414 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",buildtime(ttnelap
/totuser
));
415 if((TopUserFields
& TOPUSERFIELDS_MILISEC
) != 0)
416 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",fixnum2(ttnelap
/totuser
,1));
417 fputs("</tr></tfoot>\n",fp_top3
);
420 fputs("</table></div>\n",fp_top3
);
421 if (write_html_trailer(fp_top3
)<0)
422 debuga(_("Write error in top user list %s\n"),top3
);
423 if (fclose(fp_top3
)==EOF
)
424 debuga(_("Failed to close the top user list %s - %s\n"),top3
,strerror(errno
));
426 snprintf(tusr
,sizeof(tusr
),"%s/sarg-users",outdirname
);
427 if((fp_ou
=fopen(tusr
,"w"))==NULL
) {
428 debuga(_("(topuser) Cannot open file %s\n"),tusr
);
431 fprintf(fp_ou
,"%d\n",totuser
);
432 if (fclose(fp_ou
)==EOF
)
433 debuga(_("Failed to close file %s - %s\n"),tusr
,strerror(errno
));