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