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";
49 const char *order
="-r";
55 struct getwordstruct gwarea
;
57 struct generalitemstruct item
;
58 const struct userinfostruct
*uinfo
;
64 snprintf(wger
,sizeof(wger
),"%s/sarg-general",outdirname
);
65 if((fp_in
=fopen(wger
,"r"))==NULL
) {
66 debuga(_("(topuser) Cannot open file %s\n"),wger
);
70 snprintf(top2
,sizeof(top2
),"%s/top.tmp",outdirname
);
71 if((fp_top2
=fopen(top2
,"w"))==NULL
) {
72 debuga(_("(topuser) Cannot open file %s\n"),top2
);
79 if ((line
=longline_create())==NULL
) {
80 debuga(_("Not enough memory to read the file %s\n"),wger
);
84 while((warea
=longline_read(fp_in
,line
))!=NULL
) {
85 ger_read(warea
,&item
,wger
);
86 if(item
.total
) continue;
87 if(strcmp(olduser
,item
.user
) != 0) {
90 if (olduser
[0] != '\0') {
92 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
93 to print a long long int unless it is exactly 64-bits long.
95 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
);
100 ttnincache
+=tnincache
;
101 ttnoucache
+=tnoucache
;
103 strcpy(olduser
,item
.user
);
111 tnbytes
+=item
.nbytes
;
114 tnincache
+=item
.incache
;
115 tnoucache
+=item
.oucache
;
118 longline_destroy(&line
);
120 if (olduser
[0] != '\0') {
122 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
123 to print a long long int unless it is exactly 64-bits long.
125 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
);
130 ttnincache
+=tnincache
;
131 ttnoucache
+=tnoucache
;
135 strup(TopuserSortField
);
136 strlow(TopuserSortOrder
);
138 if(strcmp(TopuserSortField
,"USER") == 0)
141 if(strcmp(TopuserSortField
,"CONNECT") == 0)
144 if(strcmp(TopuserSortField
,"TIME") == 0)
147 if(strcmp(TopuserSortOrder
,"normal") == 0)
150 snprintf(top1
,sizeof(top1
),"%s/top",outdirname
);
151 sprintf(csort
,"sort -T \"%s\" %s %s -o \"%s\" \"%s\"", tmp
, order
, sfield
, top1
, top2
);
152 cstatus
=system(csort
);
153 if (!WIFEXITED(cstatus
) || WEXITSTATUS(cstatus
)) {
154 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus
));
155 debuga(_("sort command: %s\n"),csort
);
159 if((fp_top1
=fopen(top1
,"r"))==NULL
) {
160 debuga(_("(topuser) Cannot open file %s\n"),top1
);
166 snprintf(top3
,sizeof(top3
),"%s/index.html",outdirname
);
167 if((fp_top3
=fopen(top3
,"w"))==NULL
) {
168 debuga(_("(topuser) Cannot open file %s\n"),top3
);
172 snprintf(title
,sizeof(title
),_("SARG report for %s"),period
.text
);
173 write_html_header(fp_top3
,(IndexTree
== INDEX_TREE_DATE
) ? 3 : 1,title
);
174 fputs("<tr><td class=\"header_c\">",fp_top3
);
175 fprintf(fp_top3
,_("Period: %s"),period
.html
);
176 fputs("</td></tr>\n",fp_top3
);
177 fprintf(fp_top3
,"<tr><td class=\"header_c\">%s: %s, %s</td></tr>\n",_("Sort"),TopuserSortField
,TopuserSortOrder
);
178 fprintf(fp_top3
,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Top users"));
179 close_html_header(fp_top3
);
181 fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_top3
);
183 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"));
184 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"));
185 if(dansguardian_count
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"0\"><a href=\"dansguardian.html\">%s</a></td></tr>\n",_("DansGuardian"));
186 if(squidguard_count
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"0\"><a href=\"redirector.html\">%s</a></td></tr>\n",_("Redirector"));
187 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"));
188 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"));
189 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"));
190 if(smartfilter
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"0\"><a href=\"smartfilter.html\">%s</a></td></tr>\n",_("SmartFilter"));
191 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"));
192 fputs("<tr><td></td></tr>\n</table></div>\n",fp_top3
);
194 if ((ReportType
& REPORT_TYPE_TOPUSERS
) == 0) {
195 fputs("</body>\n</html>\n",fp_top3
);
200 fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_top3
);
201 fputs("<tr>",fp_top3
);
203 if((TopUserFields
& TOPUSERFIELDS_NUM
) != 0)
204 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("NUM"));
205 if((TopUserFields
& TOPUSERFIELDS_DATE_TIME
) !=0 && (ReportType
& REPORT_TYPE_DATE_TIME
) != 0)
206 fputs("<th class=\"header_l\"></th>",fp_top3
);
207 if((TopUserFields
& TOPUSERFIELDS_USERID
) != 0)
208 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("USERID"));
209 if((TopUserFields
& TOPUSERFIELDS_CONNECT
) != 0)
210 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("CONNECT"));
211 if((TopUserFields
& TOPUSERFIELDS_BYTES
) != 0)
212 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("BYTES"));
213 if((TopUserFields
& TOPUSERFIELDS_SETYB
) != 0)
214 fprintf(fp_top3
,"<th class=\"header_l\">%%%s</th>",_("BYTES"));
215 if((TopUserFields
& TOPUSERFIELDS_IN_CACHE_OUT
) != 0)
216 fprintf(fp_top3
,"<th class=\"header_c\" colspan=\"2\">%s</th>",_("IN-CACHE-OUT"));
217 if((TopUserFields
& TOPUSERFIELDS_USED_TIME
) != 0)
218 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("ELAPSED TIME"));
219 if((TopUserFields
& TOPUSERFIELDS_MILISEC
) != 0)
220 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("MILLISEC"));
221 if((TopUserFields
& TOPUSERFIELDS_PTIME
) != 0)
222 fprintf(fp_top3
,"<th class=\"header_l\">%%%s</th>",_("TIME"));
224 fputs("</tr>\n",fp_top3
);
228 if ((line
=longline_create())==NULL
) {
229 debuga(_("Not enough memory to read the downloaded files\n"));
233 while((warea
=longline_read(fp_top1
,line
))!=NULL
) {
234 getword_start(&gwarea
,warea
);
235 if (getword(user
,sizeof(user
),&gwarea
,'\t')<0) {
236 debuga(_("There is a broken user in file %s\n"),top1
);
239 if (getword_atoll(&nbytes
,&gwarea
,'\t')<0) {
240 debuga(_("There is a broken number of bytes in file %s\n"),top1
);
243 if (getword_atoll(&nacc
,&gwarea
,'\t')<0) {
244 debuga(_("There is a broken number of access in file %s\n"),top1
);
247 if (getword_atoll(&elap
,&gwarea
,'\t')<0) {
248 debuga(_("There is a broken elpased time in file %s\n"),top1
);
251 if (getword_atoll(&incac
,&gwarea
,'\t')<0) {
252 debuga(_("There is a broken in-cache size in file %s\n"),top1
);
255 if (getword_atoll(&oucac
,&gwarea
,'\n')<0) {
256 debuga(_("There is a broken out-of-cache size in file %s\n"),top1
);
262 if(TopUsersNum
> 0 && topcount
>= TopUsersNum
) break;
266 uinfo
=userinfo_find_from_id(user
);
268 debuga(_("Unknown user ID %s in file %s\n"),user
,top1
);
272 fputs("<tr>",fp_top3
);
275 if((TopUserFields
& TOPUSERFIELDS_NUM
) != 0)
276 fprintf(fp_top3
,"<td class=\"data\">%d</td>",posicao
);
278 if((TopUserFields
& TOPUSERFIELDS_DATE_TIME
) !=0 && (ReportType
& REPORT_TYPE_DATE_TIME
) != 0) {
279 fputs("<td class=\"data2\">",fp_top3
);
281 if(Graphs
&& GraphFont
[0]!='\0') {
282 //fprintf(fp_top3,"<a href=\"%s/graph_day.png\"><img src=\"%s/graph.png\" title=\"%s\" alt=\"G\"></a> ",uinfo->filename,ImageFile,_("Graphic"));
283 fprintf(fp_top3
,"<a href=\"%s/graph.html\"><img src=\"%s/graph.png\" title=\"%s\" alt=\"G\"></a> ",uinfo
->filename
,ImageFile
,_("Graphic"));
286 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"));
288 sprintf(val1
,"%s/d%s.html",outdirname
,uinfo
->filename
);
291 if((TopUserFields
& TOPUSERFIELDS_USERID
) != 0) {
292 if((ReportType
& REPORT_TYPE_USERS_SITES
) == 0)
293 fprintf(fp_top3
,"<td class=\"data2\">%s</td>",name
);
295 fprintf(fp_top3
,"<td class=\"data2\"><a href=\"%s/%s.html\">%s</a></td>",uinfo
->filename
,uinfo
->filename
,uinfo
->label
);
297 if((TopUserFields
& TOPUSERFIELDS_CONNECT
) != 0)
298 fprintf(fp_top3
,"<td class=\"data\">%s</td>",fixnum(nacc
,1));
299 if((TopUserFields
& TOPUSERFIELDS_BYTES
) != 0)
300 fprintf(fp_top3
,"<td class=\"data\">%s</td>",fixnum(tnbytes
,1));
301 if((TopUserFields
& TOPUSERFIELDS_SETYB
) != 0) {
302 perc
=(ttnbytes
) ? tnbytes
* 100. / ttnbytes
: 0.;
303 fprintf(fp_top3
,"<td class=\"data\">%3.2lf%%</td>",perc
);
305 if((TopUserFields
& TOPUSERFIELDS_IN_CACHE_OUT
) != 0) {
306 inperc
=(tnbytes
) ? incac
* 100. / tnbytes
: 0.;
307 ouperc
=(tnbytes
) ? oucac
* 100. / tnbytes
: 0.;
308 fprintf(fp_top3
,"<td class=\"data\">%3.2lf%%</td><td class=\"data\">%3.2lf%%</td>",inperc
,ouperc
);
310 if((TopUserFields
& TOPUSERFIELDS_USED_TIME
) != 0)
311 fprintf(fp_top3
,"<td class=\"data\">%s</td>",buildtime(tnelap
));
312 if((TopUserFields
& TOPUSERFIELDS_MILISEC
) != 0)
313 fprintf(fp_top3
,"<td class=\"data\">%s</td>",fixnum2(tnelap
,1));
314 if((TopUserFields
& TOPUSERFIELDS_PTIME
) != 0) {
315 perc2
=(ttnelap
) ? elap
* 100. / ttnelap
: 0.;
316 fprintf(fp_top3
,"<td class=\"data\">%3.2lf%%</td>",perc2
);
319 fputs("</tr>\n",fp_top3
);
325 longline_destroy(&line
);
327 if((TopUserFields
& TOPUSERFIELDS_TOTAL
) != 0) {
328 fputs("<tr>",fp_top3
);
329 if((TopUserFields
& TOPUSERFIELDS_NUM
) != 0)
330 fputs("<td></td>",fp_top3
);
331 if((TopUserFields
& TOPUSERFIELDS_DATE_TIME
) !=0 && (ReportType
& REPORT_TYPE_DATE_TIME
) != 0)
332 fputs("<td></td>",fp_top3
);
333 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("TOTAL"));
335 if((TopUserFields
& TOPUSERFIELDS_CONNECT
) != 0)
336 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",fixnum(ttnacc
,1));
337 if((TopUserFields
& TOPUSERFIELDS_BYTES
) != 0)
338 fprintf(fp_top3
,"<th class=\"header_r\">%15s</th>",fixnum(ttnbytes
,1));
339 if((TopUserFields
& TOPUSERFIELDS_SETYB
) != 0)
340 fputs("<td></td>",fp_top3
);
341 if((TopUserFields
& TOPUSERFIELDS_IN_CACHE_OUT
) != 0)
343 inperc
=(ttnbytes
) ? ttnincache
* 100. / ttnbytes
: 0.;
344 ouperc
=(ttnbytes
) ? ttnoucache
*100. / ttnbytes
: 0.;
345 fprintf(fp_top3
,"<th class=\"header_r\">%3.2lf%%</th><th class=\"header_r\">%3.2lf%%</th>",inperc
,ouperc
);
347 if((TopUserFields
& TOPUSERFIELDS_USED_TIME
) != 0)
348 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",buildtime(ttnelap
));
349 if((TopUserFields
& TOPUSERFIELDS_MILISEC
) != 0)
350 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",fixnum2(ttnelap
,1));
352 fputs("</tr>\n",fp_top3
);
355 if(ntopuser
&& (TopUserFields
& TOPUSERFIELDS_AVERAGE
) != 0) {
356 fputs("<tr>",fp_top3
);
357 if((TopUserFields
& TOPUSERFIELDS_NUM
) != 0)
358 fputs("<td></td>",fp_top3
);
359 if((TopUserFields
& TOPUSERFIELDS_DATE_TIME
) !=0 && (ReportType
& REPORT_TYPE_DATE_TIME
) != 0)
360 fputs("<td></td>",fp_top3
);
361 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("AVERAGE"));
363 if((TopUserFields
& TOPUSERFIELDS_CONNECT
) != 0)
364 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",fixnum(ttnacc
/totuser
,1));
365 if((TopUserFields
& TOPUSERFIELDS_BYTES
) != 0) {
366 tnbytes
=(totuser
) ? ttnbytes
/ totuser
: 0;
367 fprintf(fp_top3
,"<th class=\"header_r\">%15s</th>",fixnum(tnbytes
,1));
369 if((TopUserFields
& TOPUSERFIELDS_SETYB
) != 0)
370 fputs("<td></td>",fp_top3
);
371 if((TopUserFields
& TOPUSERFIELDS_IN_CACHE_OUT
) != 0)
372 fputs("<td></td><td></td>",fp_top3
);
373 if((TopUserFields
& TOPUSERFIELDS_USED_TIME
) != 0)
374 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",buildtime(ttnelap
/totuser
));
375 if((TopUserFields
& TOPUSERFIELDS_MILISEC
) != 0)
376 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",fixnum2(ttnelap
/totuser
,1));
377 fputs("</tr>\n",fp_top3
);
380 fputs("</table></div>\n",fp_top3
);
381 if (write_html_trailer(fp_top3
)<0)
382 debuga(_("Write error in top user list %s\n"),top3
);
383 if (fclose(fp_top3
)==EOF
)
384 debuga(_("Failed to close the top user list %s - %s\n"),top3
,strerror(errno
));
386 snprintf(tusr
,sizeof(tusr
),"%s/sarg-users",outdirname
);
387 if((fp_ou
=fopen(tusr
,"w"))==NULL
) {
388 debuga(_("(topuser) Cannot open file %s\n"),tusr
);
391 fprintf(fp_ou
,"%d\n",totuser
);
392 if (fclose(fp_ou
)==EOF
)
393 debuga(_("Failed to close file %s - %s\n"),tusr
,strerror(errno
));