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