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') {
91 fprintf(fp_top2
,"%s\t%lld\t%lld\t%lld\t%lld\t%lld\n",olduser
,tnbytes
,tnacc
,tnelap
,tnincache
,tnoucache
);
96 ttnincache
+=tnincache
;
97 ttnoucache
+=tnoucache
;
99 strcpy(olduser
,item
.user
);
107 tnbytes
+=item
.nbytes
;
110 tnincache
+=item
.incache
;
111 tnoucache
+=item
.oucache
;
114 longline_destroy(&line
);
116 if (olduser
[0] != '\0') {
117 fprintf(fp_top2
,"%s\t%lld\t%lld\t%lld\t%lld\t%lld\n",olduser
,tnbytes
,tnacc
,tnelap
,tnincache
,tnoucache
);
122 ttnincache
+=tnincache
;
123 ttnoucache
+=tnoucache
;
127 strup(TopuserSortField
);
128 strlow(TopuserSortOrder
);
130 if(strcmp(TopuserSortField
,"USER") == 0)
133 if(strcmp(TopuserSortField
,"CONNECT") == 0)
136 if(strcmp(TopuserSortField
,"TIME") == 0)
139 if(strcmp(TopuserSortOrder
,"normal") == 0)
142 snprintf(top1
,sizeof(top1
),"%s/top",outdirname
);
143 sprintf(csort
,"sort -T \"%s\" %s %s -o \"%s\" \"%s\"", TempDir
, order
, sfield
, top1
, top2
);
144 cstatus
=system(csort
);
145 if (!WIFEXITED(cstatus
) || WEXITSTATUS(cstatus
)) {
146 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus
));
147 debuga(_("sort command: %s\n"),csort
);
151 if((fp_top1
=fopen(top1
,"r"))==NULL
) {
152 debuga(_("(topuser) Cannot open file %s\n"),top1
);
158 snprintf(top3
,sizeof(top3
),"%s/index.html",outdirname
);
159 if((fp_top3
=fopen(top3
,"w"))==NULL
) {
160 debuga(_("(topuser) Cannot open file %s\n"),top3
);
164 snprintf(title
,sizeof(title
),_("SARG report for %s"),period
.text
);
165 write_html_header(fp_top3
,(IndexTree
== INDEX_TREE_DATE
) ? 3 : 1,title
);
166 fputs("<tr><td class=\"header_c\">",fp_top3
);
167 fprintf(fp_top3
,_("Period: %s"),period
.html
);
168 fputs("</td></tr>\n",fp_top3
);
169 fprintf(fp_top3
,"<tr><td class=\"header_c\">%s: %s, %s</td></tr>\n",_("Sort"),TopuserSortField
,TopuserSortOrder
);
170 fprintf(fp_top3
,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Top users"));
171 close_html_header(fp_top3
);
173 fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_top3
);
175 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"));
176 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"));
177 if(dansguardian_count
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"0\"><a href=\"dansguardian.html\">%s</a></td></tr>\n",_("DansGuardian"));
178 if(squidguard_count
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"0\"><a href=\"redirector.html\">%s</a></td></tr>\n",_("Redirector"));
179 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"));
180 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"));
181 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"));
182 if(smartfilter
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"0\"><a href=\"smartfilter.html\">%s</a></td></tr>\n",_("SmartFilter"));
183 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"));
184 fputs("<tr><td></td></tr>\n</table></div>\n",fp_top3
);
186 if ((ReportType
& REPORT_TYPE_TOPUSERS
) == 0) {
187 fputs("</body>\n</html>\n",fp_top3
);
192 fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_top3
);
193 fputs("<tr>",fp_top3
);
195 if((TopUserFields
& TOPUSERFIELDS_NUM
) != 0)
196 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("NUM"));
197 if((TopUserFields
& TOPUSERFIELDS_DATE_TIME
) !=0 && (ReportType
& REPORT_TYPE_DATE_TIME
) != 0)
198 fputs("<th class=\"header_l\"></th>",fp_top3
);
199 if((TopUserFields
& TOPUSERFIELDS_USERID
) != 0)
200 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("USERID"));
201 if((TopUserFields
& TOPUSERFIELDS_CONNECT
) != 0)
202 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("CONNECT"));
203 if((TopUserFields
& TOPUSERFIELDS_BYTES
) != 0)
204 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("BYTES"));
205 if((TopUserFields
& TOPUSERFIELDS_SETYB
) != 0)
206 fprintf(fp_top3
,"<th class=\"header_l\">%%%s</th>",_("BYTES"));
207 if((TopUserFields
& TOPUSERFIELDS_IN_CACHE_OUT
) != 0)
208 fprintf(fp_top3
,"<th class=\"header_c\" colspan=\"2\">%s</th>",_("IN-CACHE-OUT"));
209 if((TopUserFields
& TOPUSERFIELDS_USED_TIME
) != 0)
210 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("ELAPSED TIME"));
211 if((TopUserFields
& TOPUSERFIELDS_MILISEC
) != 0)
212 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("MILLISEC"));
213 if((TopUserFields
& TOPUSERFIELDS_PTIME
) != 0)
214 fprintf(fp_top3
,"<th class=\"header_l\">%%%s</th>",_("TIME"));
216 fputs("</tr>\n",fp_top3
);
220 if ((line
=longline_create())==NULL
) {
221 debuga(_("Not enough memory to read the downloaded files\n"));
225 while((warea
=longline_read(fp_top1
,line
))!=NULL
) {
226 getword_start(&gwarea
,warea
);
227 if (getword(user
,sizeof(user
),&gwarea
,'\t')<0) {
228 debuga(_("There is a broken user in file %s\n"),top1
);
231 if (getword_atoll(&nbytes
,&gwarea
,'\t')<0) {
232 debuga(_("There is a broken number of bytes in file %s\n"),top1
);
235 if (getword_atoll(&nacc
,&gwarea
,'\t')<0) {
236 debuga(_("There is a broken number of access in file %s\n"),top1
);
239 if (getword_atoll(&elap
,&gwarea
,'\t')<0) {
240 debuga(_("There is a broken elpased time in file %s\n"),top1
);
243 if (getword_atoll(&incac
,&gwarea
,'\t')<0) {
244 debuga(_("There is a broken in-cache size in file %s\n"),top1
);
247 if (getword_atoll(&oucac
,&gwarea
,'\n')<0) {
248 debuga(_("There is a broken out-of-cache size in file %s\n"),top1
);
254 if(TopUsersNum
> 0 && topcount
>= TopUsersNum
) break;
258 uinfo
=userinfo_find_from_id(user
);
260 debuga(_("Unknown user ID %s in file %s\n"),user
,top1
);
264 fputs("<tr>",fp_top3
);
267 if((TopUserFields
& TOPUSERFIELDS_NUM
) != 0)
268 fprintf(fp_top3
,"<td class=\"data\">%d</td>",posicao
);
270 if((TopUserFields
& TOPUSERFIELDS_DATE_TIME
) !=0 && (ReportType
& REPORT_TYPE_DATE_TIME
) != 0) {
271 fputs("<td class=\"data2\">",fp_top3
);
273 if(Graphs
&& GraphFont
[0]!='\0') {
274 fprintf(fp_top3
,"<a href=\"%s/graph_day.png\"><img src=\"%s/graph.png\" title=\"%s\" alt=\"G\"></a> ",uinfo
->filename
,ImageFile
,_("Graphic"));
277 fprintf(fp_top3
,"<a href=\"%s/d%s.html\"><img src=\"%s/datetime.png\" title=\"%s",uinfo
->filename
,uinfo
->filename
,ImageFile
,_("date/time"));
279 fprintf(fp_top3
," %s",_("Report"));
281 fputs("\" alt=\"T\"></a></td>",fp_top3
);
283 sprintf(val1
,"%s/d%s.html",outdirname
,uinfo
->filename
);
286 if((TopUserFields
& TOPUSERFIELDS_USERID
) != 0) {
287 if((ReportType
& REPORT_TYPE_USERS_SITES
) == 0)
288 fprintf(fp_top3
,"<td class=\"data2\">%s</td>",name
);
290 fprintf(fp_top3
,"<td class=\"data2\"><a href=\"%s/%s.html\">%s</a></td>",uinfo
->filename
,uinfo
->filename
,uinfo
->label
);
292 if((TopUserFields
& TOPUSERFIELDS_CONNECT
) != 0)
293 fprintf(fp_top3
,"<td class=\"data\">%s</td>",fixnum(nacc
,1));
294 if((TopUserFields
& TOPUSERFIELDS_BYTES
) != 0)
295 fprintf(fp_top3
,"<td class=\"data\">%s</td>",fixnum(tnbytes
,1));
296 if((TopUserFields
& TOPUSERFIELDS_SETYB
) != 0) {
297 perc
=(ttnbytes
) ? tnbytes
* 100. / ttnbytes
: 0.;
298 fprintf(fp_top3
,"<td class=\"data\">%3.2lf%%</td>",perc
);
300 if((TopUserFields
& TOPUSERFIELDS_IN_CACHE_OUT
) != 0) {
301 inperc
=(tnbytes
) ? incac
* 100. / tnbytes
: 0.;
302 ouperc
=(tnbytes
) ? oucac
* 100. / tnbytes
: 0.;
303 fprintf(fp_top3
,"<td class=\"data\">%3.2lf%%</td><td class=\"data\">%3.2lf%%</td>",inperc
,ouperc
);
305 if((TopUserFields
& TOPUSERFIELDS_USED_TIME
) != 0)
306 fprintf(fp_top3
,"<td class=\"data\">%s</td>",buildtime(tnelap
));
307 if((TopUserFields
& TOPUSERFIELDS_MILISEC
) != 0)
308 fprintf(fp_top3
,"<td class=\"data\">%s</td>",fixnum2(tnelap
,1));
309 if((TopUserFields
& TOPUSERFIELDS_PTIME
) != 0) {
310 perc2
=(ttnelap
) ? elap
* 100. / ttnelap
: 0.;
311 fprintf(fp_top3
,"<td class=\"data\">%3.2lf%%</td>",perc2
);
314 fputs("</tr>\n",fp_top3
);
320 longline_destroy(&line
);
322 if((TopUserFields
& TOPUSERFIELDS_TOTAL
) != 0) {
323 fputs("<tr>",fp_top3
);
324 if((TopUserFields
& TOPUSERFIELDS_NUM
) != 0)
325 fputs("<td></td>",fp_top3
);
326 if((TopUserFields
& TOPUSERFIELDS_DATE_TIME
) !=0 && (ReportType
& REPORT_TYPE_DATE_TIME
) != 0)
327 fputs("<td></td>",fp_top3
);
328 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("TOTAL"));
330 if((TopUserFields
& TOPUSERFIELDS_CONNECT
) != 0)
331 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",fixnum(ttnacc
,1));
332 if((TopUserFields
& TOPUSERFIELDS_BYTES
) != 0)
333 fprintf(fp_top3
,"<th class=\"header_r\">%15s</th>",fixnum(ttnbytes
,1));
334 if((TopUserFields
& TOPUSERFIELDS_SETYB
) != 0)
335 fputs("<td></td>",fp_top3
);
336 if((TopUserFields
& TOPUSERFIELDS_IN_CACHE_OUT
) != 0)
338 inperc
=(ttnbytes
) ? ttnincache
* 100. / ttnbytes
: 0.;
339 ouperc
=(ttnbytes
) ? ttnoucache
*100. / ttnbytes
: 0.;
340 fprintf(fp_top3
,"<th class=\"header_r\">%3.2lf%%</th><th class=\"header_r\">%3.2lf%%</th>",inperc
,ouperc
);
342 if((TopUserFields
& TOPUSERFIELDS_USED_TIME
) != 0)
343 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",buildtime(ttnelap
));
344 if((TopUserFields
& TOPUSERFIELDS_MILISEC
) != 0)
345 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",fixnum2(ttnelap
,1));
347 fputs("</tr>\n",fp_top3
);
350 if(ntopuser
&& (TopUserFields
& TOPUSERFIELDS_AVERAGE
) != 0) {
351 fputs("<tr>",fp_top3
);
352 if((TopUserFields
& TOPUSERFIELDS_NUM
) != 0)
353 fputs("<td></td>",fp_top3
);
354 if((TopUserFields
& TOPUSERFIELDS_DATE_TIME
) !=0 && (ReportType
& REPORT_TYPE_DATE_TIME
) != 0)
355 fputs("<td></td>",fp_top3
);
356 fprintf(fp_top3
,"<th class=\"header_l\">%s</th>",_("AVERAGE"));
358 if((TopUserFields
& TOPUSERFIELDS_CONNECT
) != 0)
359 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",fixnum(ttnacc
/totuser
,1));
360 if((TopUserFields
& TOPUSERFIELDS_BYTES
) != 0) {
361 tnbytes
=(totuser
) ? ttnbytes
/ totuser
: 0;
362 fprintf(fp_top3
,"<th class=\"header_r\">%15s</th>",fixnum(tnbytes
,1));
364 if((TopUserFields
& TOPUSERFIELDS_SETYB
) != 0)
365 fputs("<td></td>",fp_top3
);
366 if((TopUserFields
& TOPUSERFIELDS_IN_CACHE_OUT
) != 0)
367 fputs("<td></td><td></td>",fp_top3
);
368 if((TopUserFields
& TOPUSERFIELDS_USED_TIME
) != 0)
369 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",buildtime(ttnelap
/totuser
));
370 if((TopUserFields
& TOPUSERFIELDS_MILISEC
) != 0)
371 fprintf(fp_top3
,"<th class=\"header_r\">%s</th>",fixnum2(ttnelap
/totuser
,1));
372 fputs("</tr>\n",fp_top3
);
375 fputs("</table></div>\n",fp_top3
);
376 if (write_html_trailer(fp_top3
)<0)
377 debuga(_("Write error in top user list %s\n"),top3
);
378 if (fclose(fp_top3
)==EOF
)
379 debuga(_("Failed to close the top user list %s - %s\n"),top3
,strerror(errno
));
381 snprintf(tusr
,sizeof(tusr
),"%s/sarg-users",outdirname
);
382 if((fp_ou
=fopen(tusr
,"w"))==NULL
) {
383 debuga(_("(topuser) Cannot open file %s\n"),tusr
);
386 fprintf(fp_ou
,"%d\n",totuser
);
387 if (fclose(fp_ou
)==EOF
)
388 debuga(_("Failed to close file %s - %s\n"),tusr
,strerror(errno
));