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
;
43 char general2
[MAXLEN
];
44 char general3
[MAXLEN
];
47 char ouser
[MAX_USER_LEN
]="";
53 long long int tnacc
=0;
54 long long int tnbytes
=0;
55 long long int tntime
=0;
56 long long int twork1
=0, twork2
=0, twork3
=0;
57 #ifdef ENABLE_DOUBLE_CHECK_DATA
58 long long int ttnacc
=0;
59 long long int ttnbytes
=0;
60 long long int ttntime
=0;
67 struct getwordstruct gwarea
;
69 struct generalitemstruct item
;
72 if (debugz
) debugaz(_("Top sites report not produced because privacy option is on\n"));
76 sprintf(general
,"%s/sarg-general",outdirname
);
77 sprintf(sites
,"%s/sarg-sites",outdirname
);
78 sprintf(general2
,"%s/sarg-general2",outdirname
);
79 sprintf(general3
,"%s/sarg-general3",outdirname
);
81 sprintf(report
,"%s/topsites.html",outdirname
);
83 if (snprintf(csort
,sizeof(csort
),"sort -t \"\t\" -k 4,4 -k 1,1 -o \"%s\" \"%s\"",general2
,general
)>=sizeof(csort
)) {
84 debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),general
,general2
);
87 cstatus
=system(csort
);
88 if (!WIFEXITED(cstatus
) || WEXITSTATUS(cstatus
)) {
89 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus
));
90 debuga(_("sort command: %s\n"),csort
);
94 if((fp_in
=fopen(general2
,"r"))==NULL
) {
95 debuga(_("(topsites) Cannot open log file %s\n"),general2
);
96 debuga(_("sort command: %s\n"),csort
);
100 if((fp_ou
=fopen(general3
,"w"))==NULL
) {
101 debuga(_("(topsites) Cannot open log file %s\n"),general3
);
105 if ((line
=longline_create())==NULL
) {
106 debuga(_("Not enough memory to read file %s\n"),general2
);
110 while((buf
=longline_read(fp_in
,line
))!=NULL
) {
111 ger_read(buf
,&item
,general2
);
112 if(item
.total
) continue;
115 url_len
=strlen(item
.url
);
116 if (!ourl
|| url_len
>=ourl_size
) {
118 ourl
=realloc(ourl
,ourl_size
);
120 debuga(_("Not enough memory to store the url\n"));
124 strcpy(ourl
,item
.url
);
128 if(strcmp(item
.url
,ourl
) != 0) {
130 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
131 to print a long long int unless it is exactly 64-bits long.
133 fprintf(fp_ou
,"%"PRIu64
"\t%"PRIu64
"\t%"PRIu64
"\t%d\t%s\n",(uint64_t)tnacc
,(uint64_t)tnbytes
,(uint64_t)tntime
,nusers
,ourl
);
134 url_len
=strlen(item
.url
);
135 if (url_len
>=ourl_size
) {
137 ourl
=realloc(ourl
,ourl_size
);
139 debuga(_("Not enough memory to store the url\n"));
143 strcpy(ourl
,item
.url
);
144 strcpy(ouser
,item
.user
);
149 } else if (strcmp(item
.user
,ouser
)!=0) {
150 strcpy(ouser
,item
.user
);
155 tnbytes
+=item
.nbytes
;
157 #ifdef ENABLE_DOUBLE_CHECK_DATA
159 ttnbytes
+=item
.nbytes
;
164 longline_destroy(&line
);
168 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
169 to print a long long int unless it is exactly 64-bits long.
171 fprintf(fp_ou
,"%"PRIu64
"\t%"PRIu64
"\t%"PRIu64
"\t%d\t%s\n",(uint64_t)tnacc
,(uint64_t)tnbytes
,(uint64_t)tntime
,nusers
,ourl
);
177 #ifdef ENABLE_DOUBLE_CHECK_DATA
178 if (ttnacc
!=globstat
.nacc
|| ttnbytes
!=globstat
.nbytes
|| ttntime
!=globstat
.elap
) {
179 debuga(_("Total statistics mismatch when reading %s to produce the top sites\n"),general2
);
184 if (!KeepTempLog
&& unlink(general2
)) {
185 debuga(_("Cannot delete \"%s\": %s\n"),general2
,strerror(errno
));
189 if((TopsitesSort
& TOPSITE_SORT_CONNECT
) != 0) {
190 sortf
="-k 1,1 -k 2,2";
191 } else if((TopsitesSort
& TOPSITE_SORT_BYTES
) != 0) {
192 sortf
="-k 2,2 -k 1,1";
193 } else if((TopsitesSort
& TOPSITE_SORT_TIME
) != 0) {
195 } else if((TopsitesSort
& TOPSITE_SORT_USER
) != 0) {
196 sortf
="-k 4,4 -k 1,1 -k 2,2";
198 sortf
="-k 2,2 -k 1,1"; //default is BYTES
200 if((TopsitesSort
& TOPSITE_SORT_REVERSE
) != 0) {
206 if (snprintf(csort
,sizeof(csort
),"sort -t \"\t\" %s -n %s -o \"%s\" \"%s\"",sortt
,sortf
,sites
,general3
)>=sizeof(csort
)) {
207 debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),general3
,sites
);
210 cstatus
=system(csort
);
211 if (!WIFEXITED(cstatus
) || WEXITSTATUS(cstatus
)) {
212 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus
));
213 debuga(_("sort command: %s\n"),csort
);
216 if((fp_in
=fopen(sites
,"r"))==NULL
) {
217 debuga(_("(topsites) Cannot open log file %s\n"),sites
);
218 debuga(_("sort command: %s\n"),csort
);
222 if (!KeepTempLog
&& unlink(general3
)) {
223 debuga(_("Cannot delete \"%s\": %s\n"),general3
,strerror(errno
));
227 if((fp_ou
=fopen(report
,"w"))==NULL
) {
228 debuga(_("(topsites) Cannot open log file %s\n"),report
);
232 write_html_header(fp_ou
,(IndexTree
== INDEX_TREE_DATE
) ? 3 : 1,_("Top sites"),HTML_JS_SORTTABLE
);
233 fputs("<tr><td class=\"header_c\">",fp_ou
);
234 fprintf(fp_ou
,_("Period: %s"),period
.html
);
235 fputs("</td></tr>\n",fp_ou
);
236 fputs("<tr><th class=\"header_c\">",fp_ou
);
237 fprintf(fp_ou
,_("Top %d sites"),TopSitesNum
);
238 fputs("</th></tr>\n",fp_ou
);
239 close_html_header(fp_ou
);
241 fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\"",fp_ou
);
242 if (SortTableJs
[0]) fputs(" class=\"sortable\"",fp_ou
);
244 fprintf(fp_ou
,"<thead><tr><th class=\"header_l\">%s</th><th class=\"header_l",
245 /* TRANSLATORS: This is a column header showing the position of the entry in the sorted list. */
247 if (SortTableJs
[0]) fputs(" sorttable_alpha",fp_ou
);
248 fprintf(fp_ou
,"\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr></thead>\n",
249 /* TRANSLATORS: This is a column header showing the URL of the visited sites. */
251 /* TRANSLATORS: This is a column header showing the number of connections to a visited site. */
253 /* TRANSLATORS: This is a column header showing the number of transfered bytes. */
255 /* TRANSLATORS: This is a column header showing the time spent by the proxy processing the requests. */
257 /* TRANSLATORS: This is a column header showing the number of users who visited a sites. */
263 if ((line
=longline_create())==NULL
) {
264 debuga(_("Not enough memory to read file %s\n"),sites
);
268 while(regs
<TopSitesNum
&& (buf
=longline_read(fp_in
,line
))!=NULL
) {
269 getword_start(&gwarea
,buf
);
270 if (getword_atoll(&nacc
,&gwarea
,'\t')<0) {
271 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),sites
);
274 if (nacc
== 0) continue;
275 if (getword_atoll(&nbytes
,&gwarea
,'\t')<0 || getword_atoll(&ntime
,&gwarea
,'\t')<0) {
276 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),sites
);
279 if (getword_atoi(&nusers
,&gwarea
,'\t')<0) {
280 debuga(_("The number of users is invalid in file %s\n"),sites
);
283 if (getword_ptr(buf
,&url
,&gwarea
,'\t')<0) {
284 debuga(_("The url is invalid in file %s\n"),sites
);
292 fprintf(fp_ou
,"<tr><td class=\"data\">%d</td><td class=\"data2\">",++regs
);
294 if(BlockIt
[0] != '\0' && url
[0]!=ALIAS_PREFIX
) {
295 fprintf(fp_ou
,"<a href=\"%s%s?url=\"",wwwDocumentRoot
,BlockIt
);
296 output_html_url(fp_ou
,url
);
297 fputs("\"><img src=\"../images/sarg-squidguard-block.png\"></a> ",fp_ou
);
300 output_html_link(fp_ou
,url
,100);
301 fputs("</td><td class=\"data\"",fp_ou
);
302 if (SortTableJs
[0]) fprintf(fp_ou
," sorttable_customkey=\"%"PRId64
"\"",(uint64_t)twork1
);
303 fprintf(fp_ou
,">%s</td>",fixnum(twork1
,1));
304 fputs("<td class=\"data\"",fp_ou
);
305 if (SortTableJs
[0]) fprintf(fp_ou
," sorttable_customkey=\"%"PRId64
"\"",(uint64_t)twork2
);
306 fprintf(fp_ou
,">%s</td>",fixnum(twork2
,1));
307 fputs("<td class=\"data\"",fp_ou
);
308 if (SortTableJs
[0]) fprintf(fp_ou
," sorttable_customkey=\"%"PRId64
"\"",(uint64_t)twork3
);
309 fprintf(fp_ou
,">%s</td>",fixtime(twork3
));
310 fputs("<td class=\"data\"",fp_ou
);
311 if (SortTableJs
[0]) fprintf(fp_ou
," sorttable_customkey=\"%d\"",nusers
);
312 fprintf(fp_ou
,">%s</td></tr>\n",fixnum(nusers
,1));
315 longline_destroy(&line
);
317 fputs("</table></div>\n",fp_ou
);
318 if (write_html_trailer(fp_ou
)<0)
319 debuga(_("Write error in file %s\n"),report
);
320 if (fclose(fp_ou
)==EOF
)
321 debuga(_("Failed to close file %s - %s\n"),report
,strerror(errno
));