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
];
52 long long int tnacc
=0;
53 long long int tnbytes
=0;
54 long long int tntime
=0;
55 long long int twork1
=0, twork2
=0, twork3
=0;
56 #ifdef ENABLE_DOUBLE_CHECK_DATA
57 long long int ttnacc
=0;
58 long long int ttnbytes
=0;
59 long long int ttntime
=0;
65 struct getwordstruct gwarea
;
67 struct generalitemstruct item
;
72 sprintf(general
,"%s/sarg-general",outdirname
);
73 sprintf(sites
,"%s/sarg-sites",outdirname
);
74 sprintf(general2
,"%s/sarg-general2",outdirname
);
75 sprintf(general3
,"%s/sarg-general3",outdirname
);
77 if ((ReportType
& REPORT_TYPE_TOPUSERS
) == 0)
78 sprintf(report
,"%s/index.html",outdirname
);
80 sprintf(report
,"%s/topsites.html",outdirname
);
82 if (snprintf(csort
,sizeof(csort
),"sort -t \"\t\" -k 4,4 -o \"%s\" \"%s\"",general2
,general
)>=sizeof(csort
)) {
83 debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),general
,general2
);
86 cstatus
=system(csort
);
87 if (!WIFEXITED(cstatus
) || WEXITSTATUS(cstatus
)) {
88 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus
));
89 debuga(_("sort command: %s\n"),csort
);
93 if((fp_in
=fopen(general2
,"r"))==NULL
) {
94 debuga(_("(topsites) Cannot open log file %s\n"),general2
);
95 debuga(_("sort command: %s\n"),csort
);
99 if((fp_ou
=fopen(general3
,"w"))==NULL
) {
100 debuga(_("(topsites) Cannot open log file %s\n"),general3
);
104 if ((line
=longline_create())==NULL
) {
105 debuga(_("Not enough memory to read file %s\n"),general2
);
109 while((buf
=longline_read(fp_in
,line
))!=NULL
) {
110 ger_read(buf
,&item
,general2
);
111 if(item
.total
) continue;
114 url_len
=strlen(item
.url
);
115 if (!ourl
|| url_len
>=ourl_size
) {
117 ourl
=realloc(ourl
,ourl_size
);
119 debuga(_("Not enough memory to store the url\n"));
123 strcpy(ourl
,item
.url
);
127 if(strcmp(item
.url
,ourl
) != 0) {
129 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
130 to print a long long int unless it is exactly 64-bits long.
132 fprintf(fp_ou
,"%"PRIu64
"\t%"PRIu64
"\t%"PRIu64
"\t%s\n",(uint64_t)tnacc
,(uint64_t)tnbytes
,(uint64_t)tntime
,ourl
);
133 url_len
=strlen(item
.url
);
134 if (url_len
>=ourl_size
) {
136 ourl
=realloc(ourl
,ourl_size
);
138 debuga(_("Not enough memory to store the url\n"));
142 strcpy(ourl
,item
.url
);
149 tnbytes
+=item
.nbytes
;
151 #ifdef ENABLE_DOUBLE_CHECK_DATA
153 ttnbytes
+=item
.nbytes
;
158 longline_destroy(&line
);
162 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
163 to print a long long int unless it is exactly 64-bits long.
165 fprintf(fp_ou
,"%"PRIu64
"\t%"PRIu64
"\t%"PRIu64
"\t%s\n",(uint64_t)tnacc
,(uint64_t)tnbytes
,(uint64_t)tntime
,ourl
);
171 #ifdef ENABLE_DOUBLE_CHECK_DATA
172 if (ttnacc
!=globstat
.nacc
|| ttnbytes
!=globstat
.nbytes
|| ttntime
!=globstat
.elap
) {
173 debuga(_("Total statistics mismatch when reading %s to produce the top sites\n"),general2
);
178 if (unlink(general2
)) {
179 debuga(_("Cannot delete %s - %s\n"),general2
,strerror(errno
));
183 if((TopsitesSort
& TOPSITE_SORT_CONNECT
) != 0) {
184 sortf
="-k 1,1 -k 2,2";
185 } else if((TopsitesSort
& TOPSITE_SORT_BYTES
) != 0) {
186 sortf
="-k 2,2 -k 1,1";
187 } else if((TopsitesSort
& TOPSITE_SORT_TIME
) != 0) {
190 sortf
="-k 2,2 -k 1,1"; //default is BYTES
192 if((TopsitesSort
& TOPSITE_SORT_REVERSE
) != 0) {
198 if (snprintf(csort
,sizeof(csort
),"sort -t \"\t\" %s -n %s -o \"%s\" \"%s\"",sortt
,sortf
,sites
,general3
)>=sizeof(csort
)) {
199 debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),general3
,sites
);
202 cstatus
=system(csort
);
203 if (!WIFEXITED(cstatus
) || WEXITSTATUS(cstatus
)) {
204 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus
));
205 debuga(_("sort command: %s\n"),csort
);
208 if((fp_in
=fopen(sites
,"r"))==NULL
) {
209 debuga(_("(topsites) Cannot open log file %s\n"),sites
);
210 debuga(_("sort command: %s\n"),csort
);
214 if (unlink(general3
)) {
215 debuga(_("Cannot delete %s - %s\n"),general3
,strerror(errno
));
219 if((fp_ou
=fopen(report
,"w"))==NULL
) {
220 debuga(_("(topsites) Cannot open log file %s\n"),report
);
224 write_html_header(fp_ou
,(IndexTree
== INDEX_TREE_DATE
) ? 3 : 1,_("Top sites"),HTML_JS_SORTTABLE
);
225 fputs("<tr><td class=\"header_c\">",fp_ou
);
226 fprintf(fp_ou
,_("Period: %s"),period
.html
);
227 fputs("</td></tr>\n",fp_ou
);
228 fputs("<tr><th class=\"header_c\">",fp_ou
);
229 fprintf(fp_ou
,_("Top %d sites"),TopSitesNum
);
230 fputs("</th></tr>\n",fp_ou
);
231 close_html_header(fp_ou
);
233 fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\"",fp_ou
);
234 if (SortTableJs
[0]) fputs(" class=\"sortable\"",fp_ou
);
236 fprintf(fp_ou
,"<thead><tr><th class=\"header_l\">%s</th><th class=\"header_l",_("NUM"));
237 if (SortTableJs
[0]) fputs(" sorttable_alpha",fp_ou
);
238 fprintf(fp_ou
,"\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr></thead>\n",_("ACCESSED SITE"),_("CONNECT"),_("BYTES"),_("TIME"));
243 if ((line
=longline_create())==NULL
) {
244 debuga(_("Not enough memory to read file %s\n"),sites
);
248 while(regs
<TopSitesNum
&& (buf
=longline_read(fp_in
,line
))!=NULL
) {
249 getword_start(&gwarea
,buf
);
250 if (getword_atoll(&nacc
,&gwarea
,'\t')<0) {
251 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),sites
);
254 if (nacc
== 0) continue;
255 if (getword_atoll(&nbytes
,&gwarea
,'\t')<0 || getword_atoll(&ntime
,&gwarea
,'\t')<0) {
256 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),sites
);
259 if (getword_ptr(buf
,&url
,&gwarea
,'\t')<0) {
260 debuga(_("The url is invalid in file %s\n"),sites
);
268 fprintf(fp_ou
,"<tr><td class=\"data\">%d</td><td class=\"data2 link\">",++regs
);
270 if(BlockIt
[0] != '\0') {
271 fprintf(fp_ou
,"<a href=\"%s%s?url=\"",wwwDocumentRoot
,BlockIt
);
272 output_html_url(fp_ou
,url
);
273 fputs("\"><img src=\"../images/sarg-squidguard-block.png\"></a> ",fp_ou
);
276 fputs("<a href=\"http://",fp_ou
);
277 output_html_url(fp_ou
,url
);
279 output_html_string(fp_ou
,url
,100);
280 fputs("</a></td><td class=\"data\"",fp_ou
);
281 if (SortTableJs
[0]) fprintf(fp_ou
," sorttable_customkey=\"%"PRId64
"\"",(uint64_t)twork1
);
282 fprintf(fp_ou
,">%s</td>",fixnum(twork1
,1));
283 fputs("<td class=\"data\"",fp_ou
);
284 if (SortTableJs
[0]) fprintf(fp_ou
," sorttable_customkey=\"%"PRId64
"\"",(uint64_t)twork2
);
285 fprintf(fp_ou
,">%s</td>",fixnum(twork2
,1));
286 fputs("<td class=\"data\"",fp_ou
);
287 if (SortTableJs
[0]) fprintf(fp_ou
," sorttable_customkey=\"%"PRId64
"\"",(uint64_t)twork3
);
288 fprintf(fp_ou
,">%s</td></tr>\n",fixtime(twork3
));
291 longline_destroy(&line
);
293 fputs("</table></div>\n",fp_ou
);
294 if (write_html_trailer(fp_ou
)<0)
295 debuga(_("Write error in file %s\n"),report
);
296 if (fclose(fp_ou
)==EOF
)
297 debuga(_("Failed to close file %s - %s\n"),report
,strerror(errno
));