]>
Commit | Line | Data |
---|---|---|
25697a35 | 1 | /* |
94ff9470 | 2 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net |
110ce984 | 3 | * 1998, 2015 |
25697a35 GS |
4 | * |
5 | * SARG donations: | |
6 | * please look at http://sarg.sourceforge.net/donations.php | |
1164c474 FM |
7 | * Support: |
8 | * http://sourceforge.net/projects/sarg/forums/forum/363374 | |
25697a35 GS |
9 | * --------------------------------------------------------------------- |
10 | * | |
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. | |
15 | * | |
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. | |
20 | * | |
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. | |
24 | * | |
25 | */ | |
26 | ||
27 | #include "include/conf.h" | |
5f3cfd1d | 28 | #include "include/defs.h" |
25697a35 | 29 | |
9dc20988 FM |
30 | #ifdef ENABLE_DOUBLE_CHECK_DATA |
31 | extern struct globalstatstruct globstat; | |
32 | #endif | |
33 | ||
32e71fa4 | 34 | void topsites(void) |
25697a35 | 35 | { |
9bd92830 FM |
36 | FILE *fp_in, *fp_ou; |
37 | ||
38 | char *buf; | |
39 | char *url; | |
40 | char *ourl=NULL; | |
997a4328 | 41 | char csort[4096]; |
9bd92830 FM |
42 | char general[MAXLEN]; |
43 | char general2[MAXLEN]; | |
44 | char general3[MAXLEN]; | |
45 | char sites[MAXLEN]; | |
46 | char report[MAXLEN]; | |
95092e58 | 47 | char ouser[MAX_USER_LEN]=""; |
9bd92830 FM |
48 | const char *sortf; |
49 | const char *sortt; | |
50 | long long int nacc; | |
51 | long long int nbytes; | |
52 | long long int ntime; | |
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; | |
9dc20988 FM |
57 | #ifdef ENABLE_DOUBLE_CHECK_DATA |
58 | long long int ttnacc=0; | |
59 | long long int ttnbytes=0; | |
60 | long long int ttntime=0; | |
61 | #endif | |
95092e58 | 62 | int nusers=0; |
9bd92830 FM |
63 | int regs=0; |
64 | int cstatus; | |
65 | int url_len; | |
66 | int ourl_size=0; | |
67 | struct getwordstruct gwarea; | |
68 | longline line; | |
69 | struct generalitemstruct item; | |
70 | ||
9f93fec3 | 71 | if(Privacy) { |
af961877 | 72 | if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("Top sites report not produced because privacy option is on\n")); |
9bd92830 | 73 | return; |
9f93fec3 | 74 | } |
b7413c4c | 75 | if (debugz>=LogLevel_Process) |
af961877 | 76 | debuga(__FILE__,__LINE__,_("Creating top sites report...\n")); |
9bd92830 FM |
77 | |
78 | sprintf(general,"%s/sarg-general",outdirname); | |
79 | sprintf(sites,"%s/sarg-sites",outdirname); | |
80 | sprintf(general2,"%s/sarg-general2",outdirname); | |
81 | sprintf(general3,"%s/sarg-general3",outdirname); | |
82 | ||
124ec0b0 | 83 | sprintf(report,"%s/topsites.html",outdirname); |
9bd92830 | 84 | |
95092e58 | 85 | if (snprintf(csort,sizeof(csort),"sort -t \"\t\" -k 4,4 -k 1,1 -o \"%s\" \"%s\"",general2,general)>=sizeof(csort)) { |
af961877 | 86 | debuga(__FILE__,__LINE__,_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),general,general2); |
78eeb33f FM |
87 | exit(EXIT_FAILURE); |
88 | } | |
9bd92830 FM |
89 | cstatus=system(csort); |
90 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
af961877 FM |
91 | debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEXITSTATUS(cstatus)); |
92 | debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort); | |
9bd92830 FM |
93 | exit(EXIT_FAILURE); |
94 | } | |
95 | ||
96 | if((fp_in=fopen(general2,"r"))==NULL) { | |
af961877 FM |
97 | debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),general2,strerror(errno)); |
98 | debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort); | |
007905af | 99 | exit(EXIT_FAILURE); |
9bd92830 FM |
100 | } |
101 | ||
102 | if((fp_ou=fopen(general3,"w"))==NULL) { | |
af961877 | 103 | debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),general3,strerror(errno)); |
007905af | 104 | exit(EXIT_FAILURE); |
9bd92830 FM |
105 | } |
106 | ||
107 | if ((line=longline_create())==NULL) { | |
af961877 | 108 | debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),general2); |
9bd92830 FM |
109 | exit(EXIT_FAILURE); |
110 | } | |
111 | ||
112 | while((buf=longline_read(fp_in,line))!=NULL) { | |
113 | ger_read(buf,&item,general2); | |
114 | if(item.total) continue; | |
115 | ||
116 | if(!regs) { | |
117 | url_len=strlen(item.url); | |
118 | if (!ourl || url_len>=ourl_size) { | |
119 | ourl_size=url_len+1; | |
120 | ourl=realloc(ourl,ourl_size); | |
121 | if (!ourl) { | |
af961877 | 122 | debuga(__FILE__,__LINE__,_("Not enough memory to store the url\n")); |
9bd92830 FM |
123 | exit(EXIT_FAILURE); |
124 | } | |
125 | } | |
126 | strcpy(ourl,item.url); | |
127 | regs++; | |
128 | } | |
129 | ||
130 | if(strcmp(item.url,ourl) != 0) { | |
131 | /* | |
132 | This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable | |
133 | to print a long long int unless it is exactly 64-bits long. | |
134 | */ | |
95092e58 | 135 | fprintf(fp_ou,"%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%d\t%s\n",(uint64_t)tnacc,(uint64_t)tnbytes,(uint64_t)tntime,nusers,ourl); |
9bd92830 FM |
136 | url_len=strlen(item.url); |
137 | if (url_len>=ourl_size) { | |
138 | ourl_size=url_len+1; | |
139 | ourl=realloc(ourl,ourl_size); | |
140 | if (!ourl) { | |
af961877 | 141 | debuga(__FILE__,__LINE__,_("Not enough memory to store the url\n")); |
9bd92830 FM |
142 | exit(EXIT_FAILURE); |
143 | } | |
144 | } | |
145 | strcpy(ourl,item.url); | |
95092e58 | 146 | strcpy(ouser,item.user); |
9bd92830 FM |
147 | tnacc=0; |
148 | tnbytes=0; | |
149 | tntime=0; | |
95092e58 FM |
150 | nusers=1; |
151 | } else if (strcmp(item.user,ouser)!=0) { | |
152 | strcpy(ouser,item.user); | |
153 | nusers++; | |
9bd92830 FM |
154 | } |
155 | ||
156 | tnacc+=item.nacc; | |
157 | tnbytes+=item.nbytes; | |
158 | tntime+=item.nelap; | |
9dc20988 FM |
159 | #ifdef ENABLE_DOUBLE_CHECK_DATA |
160 | ttnacc+=item.nacc; | |
161 | ttnbytes+=item.nbytes; | |
162 | ttntime+=item.nelap; | |
163 | #endif | |
9bd92830 | 164 | } |
204781f4 | 165 | if (fclose(fp_in)==EOF) { |
af961877 | 166 | debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),general2,strerror(errno)); |
204781f4 FM |
167 | exit(EXIT_FAILURE); |
168 | } | |
9bd92830 FM |
169 | longline_destroy(&line); |
170 | ||
171 | if (ourl) { | |
172 | /* | |
173 | This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable | |
174 | to print a long long int unless it is exactly 64-bits long. | |
175 | */ | |
95092e58 | 176 | fprintf(fp_ou,"%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%d\t%s\n",(uint64_t)tnacc,(uint64_t)tnbytes,(uint64_t)tntime,nusers,ourl); |
9bd92830 FM |
177 | free(ourl); |
178 | } | |
179 | ||
507460ae | 180 | if (fclose(fp_ou)==EOF) { |
af961877 | 181 | debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),general3,strerror(errno)); |
507460ae FM |
182 | exit(EXIT_FAILURE); |
183 | } | |
9bd92830 | 184 | |
9dc20988 FM |
185 | #ifdef ENABLE_DOUBLE_CHECK_DATA |
186 | if (ttnacc!=globstat.nacc || ttnbytes!=globstat.nbytes || ttntime!=globstat.elap) { | |
af961877 | 187 | debuga(__FILE__,__LINE__,_("Total statistics mismatch when reading \"%s\" to produce the top sites\n"),general2); |
9dc20988 FM |
188 | exit(EXIT_FAILURE); |
189 | } | |
190 | #endif | |
191 | ||
11767c6a | 192 | if (!KeepTempLog && unlink(general2)) { |
af961877 | 193 | debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),general2,strerror(errno)); |
9dc20988 FM |
194 | exit(EXIT_FAILURE); |
195 | } | |
196 | ||
9bd92830 FM |
197 | if((TopsitesSort & TOPSITE_SORT_CONNECT) != 0) { |
198 | sortf="-k 1,1 -k 2,2"; | |
199 | } else if((TopsitesSort & TOPSITE_SORT_BYTES) != 0) { | |
200 | sortf="-k 2,2 -k 1,1"; | |
201 | } else if((TopsitesSort & TOPSITE_SORT_TIME) != 0) { | |
202 | sortf="-k 3,3"; | |
7d82ea1a FM |
203 | } else if((TopsitesSort & TOPSITE_SORT_USER) != 0) { |
204 | sortf="-k 4,4 -k 1,1 -k 2,2"; | |
9bd92830 FM |
205 | } else { |
206 | sortf="-k 2,2 -k 1,1"; //default is BYTES | |
207 | } | |
208 | if((TopsitesSort & TOPSITE_SORT_REVERSE) != 0) { | |
209 | sortt="-r"; | |
210 | } else { | |
211 | sortt=""; | |
212 | } | |
213 | ||
78eeb33f | 214 | if (snprintf(csort,sizeof(csort),"sort -t \"\t\" %s -n %s -o \"%s\" \"%s\"",sortt,sortf,sites,general3)>=sizeof(csort)) { |
af961877 | 215 | debuga(__FILE__,__LINE__,_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),general3,sites); |
78eeb33f FM |
216 | exit(EXIT_FAILURE); |
217 | } | |
9bd92830 FM |
218 | cstatus=system(csort); |
219 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
af961877 FM |
220 | debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEXITSTATUS(cstatus)); |
221 | debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort); | |
9bd92830 FM |
222 | exit(EXIT_FAILURE); |
223 | } | |
224 | if((fp_in=fopen(sites,"r"))==NULL) { | |
af961877 FM |
225 | debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),sites,strerror(errno)); |
226 | debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort); | |
9bd92830 FM |
227 | exit(EXIT_FAILURE); |
228 | } | |
229 | ||
11767c6a | 230 | if (!KeepTempLog && unlink(general3)) { |
af961877 | 231 | debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),general3,strerror(errno)); |
08f9b029 FM |
232 | exit(EXIT_FAILURE); |
233 | } | |
9bd92830 FM |
234 | |
235 | if((fp_ou=fopen(report,"w"))==NULL) { | |
af961877 | 236 | debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),report,strerror(errno)); |
007905af | 237 | exit(EXIT_FAILURE); |
9bd92830 FM |
238 | } |
239 | ||
240 | write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Top sites"),HTML_JS_SORTTABLE); | |
241 | fputs("<tr><td class=\"header_c\">",fp_ou); | |
242 | fprintf(fp_ou,_("Period: %s"),period.html); | |
243 | fputs("</td></tr>\n",fp_ou); | |
244 | fputs("<tr><th class=\"header_c\">",fp_ou); | |
245 | fprintf(fp_ou,_("Top %d sites"),TopSitesNum); | |
246 | fputs("</th></tr>\n",fp_ou); | |
247 | close_html_header(fp_ou); | |
248 | ||
249 | fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\"",fp_ou); | |
250 | if (SortTableJs[0]) fputs(" class=\"sortable\"",fp_ou); | |
251 | fputs(">\n",fp_ou); | |
87e8625b FM |
252 | fprintf(fp_ou,"<thead><tr><th class=\"header_l\">%s</th><th class=\"header_l", |
253 | /* TRANSLATORS: This is a column header showing the position of the entry in the sorted list. */ | |
254 | _("NUM")); | |
9bd92830 | 255 | if (SortTableJs[0]) fputs(" sorttable_alpha",fp_ou); |
87e8625b FM |
256 | 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", |
257 | /* TRANSLATORS: This is a column header showing the URL of the visited sites. */ | |
258 | _("ACCESSED SITE"), | |
259 | /* TRANSLATORS: This is a column header showing the number of connections to a visited site. */ | |
260 | _("CONNECT"), | |
261 | /* TRANSLATORS: This is a column header showing the number of transfered bytes. */ | |
262 | _("BYTES"), | |
263 | /* TRANSLATORS: This is a column header showing the time spent by the proxy processing the requests. */ | |
eb7ab620 | 264 | pgettext("duration","TIME"), |
87e8625b FM |
265 | /* TRANSLATORS: This is a column header showing the number of users who visited a sites. */ |
266 | _("USERS")); | |
9bd92830 FM |
267 | |
268 | regs=0; | |
269 | ntopsites = 0; | |
270 | ||
271 | if ((line=longline_create())==NULL) { | |
af961877 | 272 | debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),sites); |
9bd92830 FM |
273 | exit(EXIT_FAILURE); |
274 | } | |
275 | ||
276 | while(regs<TopSitesNum && (buf=longline_read(fp_in,line))!=NULL) { | |
277 | getword_start(&gwarea,buf); | |
278 | if (getword_atoll(&nacc,&gwarea,'\t')<0) { | |
af961877 | 279 | debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),sites); |
9bd92830 FM |
280 | exit(EXIT_FAILURE); |
281 | } | |
282 | if (nacc == 0) continue; | |
283 | if (getword_atoll(&nbytes,&gwarea,'\t')<0 || getword_atoll(&ntime,&gwarea,'\t')<0) { | |
af961877 | 284 | debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),sites); |
9bd92830 FM |
285 | exit(EXIT_FAILURE); |
286 | } | |
95092e58 | 287 | if (getword_atoi(&nusers,&gwarea,'\t')<0) { |
af961877 | 288 | debuga(__FILE__,__LINE__,_("Invalid number of users in file \"%s\"\n"),sites); |
95092e58 FM |
289 | exit(EXIT_FAILURE); |
290 | } | |
9bd92830 | 291 | if (getword_ptr(buf,&url,&gwarea,'\t')<0) { |
af961877 | 292 | debuga(__FILE__,__LINE__,_("Invalid url in file \"%s\"\n"),sites); |
9bd92830 FM |
293 | exit(EXIT_FAILURE); |
294 | } | |
295 | ||
296 | twork1=nacc; | |
297 | twork2=nbytes; | |
298 | twork3=ntime; | |
299 | ||
67a93701 | 300 | fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2\">",++regs); |
9bd92830 | 301 | |
67a93701 | 302 | if(BlockIt[0] != '\0' && url[0]!=ALIAS_PREFIX) { |
9bd92830 FM |
303 | fprintf(fp_ou,"<a href=\"%s%s?url=\"",wwwDocumentRoot,BlockIt); |
304 | output_html_url(fp_ou,url); | |
305 | fputs("\"><img src=\"../images/sarg-squidguard-block.png\"></a> ",fp_ou); | |
306 | } | |
307 | ||
6fa33a32 | 308 | output_html_link(fp_ou,url,100); |
67a93701 | 309 | fputs("</td><td class=\"data\"",fp_ou); |
9bd92830 FM |
310 | if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%"PRId64"\"",(uint64_t)twork1); |
311 | fprintf(fp_ou,">%s</td>",fixnum(twork1,1)); | |
312 | fputs("<td class=\"data\"",fp_ou); | |
313 | if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%"PRId64"\"",(uint64_t)twork2); | |
314 | fprintf(fp_ou,">%s</td>",fixnum(twork2,1)); | |
315 | fputs("<td class=\"data\"",fp_ou); | |
316 | if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%"PRId64"\"",(uint64_t)twork3); | |
95092e58 FM |
317 | fprintf(fp_ou,">%s</td>",fixtime(twork3)); |
318 | fputs("<td class=\"data\"",fp_ou); | |
319 | if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%d\"",nusers); | |
320 | fprintf(fp_ou,">%s</td></tr>\n",fixnum(nusers,1)); | |
9bd92830 | 321 | } |
204781f4 | 322 | if (fclose(fp_in)==EOF) { |
af961877 | 323 | debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),sites,strerror(errno)); |
204781f4 FM |
324 | exit(EXIT_FAILURE); |
325 | } | |
9bd92830 FM |
326 | longline_destroy(&line); |
327 | ||
328 | fputs("</table></div>\n",fp_ou); | |
342bd723 | 329 | write_html_trailer(fp_ou); |
507460ae | 330 | if (fclose(fp_ou)==EOF) { |
af961877 | 331 | debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),report,strerror(errno)); |
507460ae FM |
332 | exit(EXIT_FAILURE); |
333 | } | |
9bd92830 FM |
334 | |
335 | return; | |
25697a35 | 336 | } |