]>
Commit | Line | Data |
---|---|---|
25697a35 | 1 | /* |
94ff9470 | 2 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net |
61d965f3 | 3 | * 1998, 2012 |
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]; | |
47 | const char *sortf; | |
48 | const char *sortt; | |
49 | long long int nacc; | |
50 | long long int nbytes; | |
51 | long long int ntime; | |
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; | |
9dc20988 FM |
56 | #ifdef ENABLE_DOUBLE_CHECK_DATA |
57 | long long int ttnacc=0; | |
58 | long long int ttnbytes=0; | |
59 | long long int ttntime=0; | |
60 | #endif | |
9bd92830 FM |
61 | int regs=0; |
62 | int cstatus; | |
63 | int url_len; | |
64 | int ourl_size=0; | |
65 | struct getwordstruct gwarea; | |
66 | longline line; | |
67 | struct generalitemstruct item; | |
68 | ||
9f93fec3 FM |
69 | if(Privacy) { |
70 | if (debugz) debugaz(_("Top sites report not produced because privacy option is on\n")); | |
9bd92830 | 71 | return; |
9f93fec3 | 72 | } |
9bd92830 FM |
73 | |
74 | sprintf(general,"%s/sarg-general",outdirname); | |
75 | sprintf(sites,"%s/sarg-sites",outdirname); | |
76 | sprintf(general2,"%s/sarg-general2",outdirname); | |
77 | sprintf(general3,"%s/sarg-general3",outdirname); | |
78 | ||
124ec0b0 | 79 | sprintf(report,"%s/topsites.html",outdirname); |
9bd92830 | 80 | |
78eeb33f FM |
81 | if (snprintf(csort,sizeof(csort),"sort -t \"\t\" -k 4,4 -o \"%s\" \"%s\"",general2,general)>=sizeof(csort)) { |
82 | debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),general,general2); | |
83 | exit(EXIT_FAILURE); | |
84 | } | |
9bd92830 FM |
85 | cstatus=system(csort); |
86 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
87 | debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); | |
88 | debuga(_("sort command: %s\n"),csort); | |
89 | exit(EXIT_FAILURE); | |
90 | } | |
91 | ||
92 | if((fp_in=fopen(general2,"r"))==NULL) { | |
007905af FM |
93 | debuga(_("(topsites) Cannot open log file %s\n"),general2); |
94 | debuga(_("sort command: %s\n"),csort); | |
95 | exit(EXIT_FAILURE); | |
9bd92830 FM |
96 | } |
97 | ||
98 | if((fp_ou=fopen(general3,"w"))==NULL) { | |
007905af FM |
99 | debuga(_("(topsites) Cannot open log file %s\n"),general3); |
100 | exit(EXIT_FAILURE); | |
9bd92830 FM |
101 | } |
102 | ||
103 | if ((line=longline_create())==NULL) { | |
104 | debuga(_("Not enough memory to read file %s\n"),general2); | |
105 | exit(EXIT_FAILURE); | |
106 | } | |
107 | ||
108 | while((buf=longline_read(fp_in,line))!=NULL) { | |
109 | ger_read(buf,&item,general2); | |
110 | if(item.total) continue; | |
111 | ||
112 | if(!regs) { | |
113 | url_len=strlen(item.url); | |
114 | if (!ourl || url_len>=ourl_size) { | |
115 | ourl_size=url_len+1; | |
116 | ourl=realloc(ourl,ourl_size); | |
117 | if (!ourl) { | |
118 | debuga(_("Not enough memory to store the url\n")); | |
119 | exit(EXIT_FAILURE); | |
120 | } | |
121 | } | |
122 | strcpy(ourl,item.url); | |
123 | regs++; | |
124 | } | |
125 | ||
126 | if(strcmp(item.url,ourl) != 0) { | |
127 | /* | |
128 | This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable | |
129 | to print a long long int unless it is exactly 64-bits long. | |
130 | */ | |
131 | fprintf(fp_ou,"%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%s\n",(uint64_t)tnacc,(uint64_t)tnbytes,(uint64_t)tntime,ourl); | |
132 | url_len=strlen(item.url); | |
133 | if (url_len>=ourl_size) { | |
134 | ourl_size=url_len+1; | |
135 | ourl=realloc(ourl,ourl_size); | |
136 | if (!ourl) { | |
137 | debuga(_("Not enough memory to store the url\n")); | |
138 | exit(EXIT_FAILURE); | |
139 | } | |
140 | } | |
141 | strcpy(ourl,item.url); | |
142 | tnacc=0; | |
143 | tnbytes=0; | |
144 | tntime=0; | |
145 | } | |
146 | ||
147 | tnacc+=item.nacc; | |
148 | tnbytes+=item.nbytes; | |
149 | tntime+=item.nelap; | |
9dc20988 FM |
150 | #ifdef ENABLE_DOUBLE_CHECK_DATA |
151 | ttnacc+=item.nacc; | |
152 | ttnbytes+=item.nbytes; | |
153 | ttntime+=item.nelap; | |
154 | #endif | |
9bd92830 FM |
155 | } |
156 | fclose(fp_in); | |
9bd92830 FM |
157 | longline_destroy(&line); |
158 | ||
159 | if (ourl) { | |
160 | /* | |
161 | This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable | |
162 | to print a long long int unless it is exactly 64-bits long. | |
163 | */ | |
164 | fprintf(fp_ou,"%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%s\n",(uint64_t)tnacc,(uint64_t)tnbytes,(uint64_t)tntime,ourl); | |
165 | free(ourl); | |
166 | } | |
167 | ||
168 | fclose(fp_ou); | |
169 | ||
9dc20988 FM |
170 | #ifdef ENABLE_DOUBLE_CHECK_DATA |
171 | if (ttnacc!=globstat.nacc || ttnbytes!=globstat.nbytes || ttntime!=globstat.elap) { | |
172 | debuga(_("Total statistics mismatch when reading %s to produce the top sites\n"),general2); | |
173 | exit(EXIT_FAILURE); | |
174 | } | |
175 | #endif | |
176 | ||
177 | if (unlink(general2)) { | |
178 | debuga(_("Cannot delete %s - %s\n"),general2,strerror(errno)); | |
179 | exit(EXIT_FAILURE); | |
180 | } | |
181 | ||
9bd92830 FM |
182 | if((TopsitesSort & TOPSITE_SORT_CONNECT) != 0) { |
183 | sortf="-k 1,1 -k 2,2"; | |
184 | } else if((TopsitesSort & TOPSITE_SORT_BYTES) != 0) { | |
185 | sortf="-k 2,2 -k 1,1"; | |
186 | } else if((TopsitesSort & TOPSITE_SORT_TIME) != 0) { | |
187 | sortf="-k 3,3"; | |
188 | } else { | |
189 | sortf="-k 2,2 -k 1,1"; //default is BYTES | |
190 | } | |
191 | if((TopsitesSort & TOPSITE_SORT_REVERSE) != 0) { | |
192 | sortt="-r"; | |
193 | } else { | |
194 | sortt=""; | |
195 | } | |
196 | ||
78eeb33f FM |
197 | if (snprintf(csort,sizeof(csort),"sort -t \"\t\" %s -n %s -o \"%s\" \"%s\"",sortt,sortf,sites,general3)>=sizeof(csort)) { |
198 | debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),general3,sites); | |
199 | exit(EXIT_FAILURE); | |
200 | } | |
9bd92830 FM |
201 | cstatus=system(csort); |
202 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
203 | debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); | |
204 | debuga(_("sort command: %s\n"),csort); | |
205 | exit(EXIT_FAILURE); | |
206 | } | |
207 | if((fp_in=fopen(sites,"r"))==NULL) { | |
208 | debuga(_("(topsites) Cannot open log file %s\n"),sites); | |
209 | debuga(_("sort command: %s\n"),csort); | |
210 | exit(EXIT_FAILURE); | |
211 | } | |
212 | ||
08f9b029 FM |
213 | if (unlink(general3)) { |
214 | debuga(_("Cannot delete %s - %s\n"),general3,strerror(errno)); | |
215 | exit(EXIT_FAILURE); | |
216 | } | |
9bd92830 FM |
217 | |
218 | if((fp_ou=fopen(report,"w"))==NULL) { | |
007905af FM |
219 | debuga(_("(topsites) Cannot open log file %s\n"),report); |
220 | exit(EXIT_FAILURE); | |
9bd92830 FM |
221 | } |
222 | ||
223 | write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Top sites"),HTML_JS_SORTTABLE); | |
224 | fputs("<tr><td class=\"header_c\">",fp_ou); | |
225 | fprintf(fp_ou,_("Period: %s"),period.html); | |
226 | fputs("</td></tr>\n",fp_ou); | |
227 | fputs("<tr><th class=\"header_c\">",fp_ou); | |
228 | fprintf(fp_ou,_("Top %d sites"),TopSitesNum); | |
229 | fputs("</th></tr>\n",fp_ou); | |
230 | close_html_header(fp_ou); | |
231 | ||
232 | fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\"",fp_ou); | |
233 | if (SortTableJs[0]) fputs(" class=\"sortable\"",fp_ou); | |
234 | fputs(">\n",fp_ou); | |
235 | fprintf(fp_ou,"<thead><tr><th class=\"header_l\">%s</th><th class=\"header_l",_("NUM")); | |
236 | if (SortTableJs[0]) fputs(" sorttable_alpha",fp_ou); | |
237 | 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")); | |
238 | ||
239 | regs=0; | |
240 | ntopsites = 0; | |
241 | ||
242 | if ((line=longline_create())==NULL) { | |
243 | debuga(_("Not enough memory to read file %s\n"),sites); | |
244 | exit(EXIT_FAILURE); | |
245 | } | |
246 | ||
247 | while(regs<TopSitesNum && (buf=longline_read(fp_in,line))!=NULL) { | |
248 | getword_start(&gwarea,buf); | |
249 | if (getword_atoll(&nacc,&gwarea,'\t')<0) { | |
250 | debuga(_("Maybe you have a broken record or garbage in your %s file\n"),sites); | |
251 | exit(EXIT_FAILURE); | |
252 | } | |
253 | if (nacc == 0) continue; | |
254 | if (getword_atoll(&nbytes,&gwarea,'\t')<0 || getword_atoll(&ntime,&gwarea,'\t')<0) { | |
255 | debuga(_("Maybe you have a broken record or garbage in your %s file\n"),sites); | |
256 | exit(EXIT_FAILURE); | |
257 | } | |
258 | if (getword_ptr(buf,&url,&gwarea,'\t')<0) { | |
259 | debuga(_("The url is invalid in file %s\n"),sites); | |
260 | exit(EXIT_FAILURE); | |
261 | } | |
262 | ||
263 | twork1=nacc; | |
264 | twork2=nbytes; | |
265 | twork3=ntime; | |
266 | ||
67a93701 | 267 | fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2\">",++regs); |
9bd92830 | 268 | |
67a93701 | 269 | if(BlockIt[0] != '\0' && url[0]!=ALIAS_PREFIX) { |
9bd92830 FM |
270 | fprintf(fp_ou,"<a href=\"%s%s?url=\"",wwwDocumentRoot,BlockIt); |
271 | output_html_url(fp_ou,url); | |
272 | fputs("\"><img src=\"../images/sarg-squidguard-block.png\"></a> ",fp_ou); | |
273 | } | |
274 | ||
6fa33a32 | 275 | output_html_link(fp_ou,url,100); |
67a93701 | 276 | fputs("</td><td class=\"data\"",fp_ou); |
9bd92830 FM |
277 | if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%"PRId64"\"",(uint64_t)twork1); |
278 | fprintf(fp_ou,">%s</td>",fixnum(twork1,1)); | |
279 | fputs("<td class=\"data\"",fp_ou); | |
280 | if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%"PRId64"\"",(uint64_t)twork2); | |
281 | fprintf(fp_ou,">%s</td>",fixnum(twork2,1)); | |
282 | fputs("<td class=\"data\"",fp_ou); | |
283 | if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%"PRId64"\"",(uint64_t)twork3); | |
284 | fprintf(fp_ou,">%s</td></tr>\n",fixtime(twork3)); | |
285 | } | |
286 | fclose(fp_in); | |
287 | longline_destroy(&line); | |
288 | ||
289 | fputs("</table></div>\n",fp_ou); | |
290 | if (write_html_trailer(fp_ou)<0) | |
291 | debuga(_("Write error in file %s\n"),report); | |
292 | if (fclose(fp_ou)==EOF) | |
293 | debuga(_("Failed to close file %s - %s\n"),report,strerror(errno)); | |
294 | ||
295 | return; | |
25697a35 | 296 | } |