]> git.ipfire.org Git - thirdparty/sarg.git/blob - topsites.c
Normalize the messages to have less messages to translate.
[thirdparty/sarg.git] / topsites.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2013
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
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"
28 #include "include/defs.h"
29
30 #ifdef ENABLE_DOUBLE_CHECK_DATA
31 extern struct globalstatstruct globstat;
32 #endif
33
34 void topsites(void)
35 {
36 FILE *fp_in, *fp_ou;
37
38 char *buf;
39 char *url;
40 char *ourl=NULL;
41 char csort[4096];
42 char general[MAXLEN];
43 char general2[MAXLEN];
44 char general3[MAXLEN];
45 char sites[MAXLEN];
46 char report[MAXLEN];
47 char ouser[MAX_USER_LEN]="";
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;
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
62 int nusers=0;
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
71 if(Privacy) {
72 if (debugz>=LogLevel_Process) debugaz(_("Top sites report not produced because privacy option is on\n"));
73 return;
74 }
75 if (debugz>=LogLevel_Process)
76 debuga(_("Creating top sites report...\n"));
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
83 sprintf(report,"%s/topsites.html",outdirname);
84
85 if (snprintf(csort,sizeof(csort),"sort -t \"\t\" -k 4,4 -k 1,1 -o \"%s\" \"%s\"",general2,general)>=sizeof(csort)) {
86 debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),general,general2);
87 exit(EXIT_FAILURE);
88 }
89 cstatus=system(csort);
90 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
91 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
92 debuga(_("sort command: %s\n"),csort);
93 exit(EXIT_FAILURE);
94 }
95
96 if((fp_in=fopen(general2,"r"))==NULL) {
97 debuga(_("Cannot open file \"%s\": %s\n"),general2,strerror(errno));
98 debuga(_("sort command: %s\n"),csort);
99 exit(EXIT_FAILURE);
100 }
101
102 if((fp_ou=fopen(general3,"w"))==NULL) {
103 debuga(_("Cannot open file \"%s\": %s\n"),general3,strerror(errno));
104 exit(EXIT_FAILURE);
105 }
106
107 if ((line=longline_create())==NULL) {
108 debuga(_("Not enough memory to read file %s\n"),general2);
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) {
122 debuga(_("Not enough memory to store the url\n"));
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 */
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);
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) {
141 debuga(_("Not enough memory to store the url\n"));
142 exit(EXIT_FAILURE);
143 }
144 }
145 strcpy(ourl,item.url);
146 strcpy(ouser,item.user);
147 tnacc=0;
148 tnbytes=0;
149 tntime=0;
150 nusers=1;
151 } else if (strcmp(item.user,ouser)!=0) {
152 strcpy(ouser,item.user);
153 nusers++;
154 }
155
156 tnacc+=item.nacc;
157 tnbytes+=item.nbytes;
158 tntime+=item.nelap;
159 #ifdef ENABLE_DOUBLE_CHECK_DATA
160 ttnacc+=item.nacc;
161 ttnbytes+=item.nbytes;
162 ttntime+=item.nelap;
163 #endif
164 }
165 fclose(fp_in);
166 longline_destroy(&line);
167
168 if (ourl) {
169 /*
170 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
171 to print a long long int unless it is exactly 64-bits long.
172 */
173 fprintf(fp_ou,"%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%d\t%s\n",(uint64_t)tnacc,(uint64_t)tnbytes,(uint64_t)tntime,nusers,ourl);
174 free(ourl);
175 }
176
177 if (fclose(fp_ou)==EOF) {
178 debuga(_("Write error in \"%s\": %s\n"),general3,strerror(errno));
179 exit(EXIT_FAILURE);
180 }
181
182 #ifdef ENABLE_DOUBLE_CHECK_DATA
183 if (ttnacc!=globstat.nacc || ttnbytes!=globstat.nbytes || ttntime!=globstat.elap) {
184 debuga(_("Total statistics mismatch when reading %s to produce the top sites\n"),general2);
185 exit(EXIT_FAILURE);
186 }
187 #endif
188
189 if (!KeepTempLog && unlink(general2)) {
190 debuga(_("Cannot delete \"%s\": %s\n"),general2,strerror(errno));
191 exit(EXIT_FAILURE);
192 }
193
194 if((TopsitesSort & TOPSITE_SORT_CONNECT) != 0) {
195 sortf="-k 1,1 -k 2,2";
196 } else if((TopsitesSort & TOPSITE_SORT_BYTES) != 0) {
197 sortf="-k 2,2 -k 1,1";
198 } else if((TopsitesSort & TOPSITE_SORT_TIME) != 0) {
199 sortf="-k 3,3";
200 } else if((TopsitesSort & TOPSITE_SORT_USER) != 0) {
201 sortf="-k 4,4 -k 1,1 -k 2,2";
202 } else {
203 sortf="-k 2,2 -k 1,1"; //default is BYTES
204 }
205 if((TopsitesSort & TOPSITE_SORT_REVERSE) != 0) {
206 sortt="-r";
207 } else {
208 sortt="";
209 }
210
211 if (snprintf(csort,sizeof(csort),"sort -t \"\t\" %s -n %s -o \"%s\" \"%s\"",sortt,sortf,sites,general3)>=sizeof(csort)) {
212 debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),general3,sites);
213 exit(EXIT_FAILURE);
214 }
215 cstatus=system(csort);
216 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
217 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
218 debuga(_("sort command: %s\n"),csort);
219 exit(EXIT_FAILURE);
220 }
221 if((fp_in=fopen(sites,"r"))==NULL) {
222 debuga(_("Cannot open file \"%s\": %s\n"),sites,strerror(errno));
223 debuga(_("sort command: %s\n"),csort);
224 exit(EXIT_FAILURE);
225 }
226
227 if (!KeepTempLog && unlink(general3)) {
228 debuga(_("Cannot delete \"%s\": %s\n"),general3,strerror(errno));
229 exit(EXIT_FAILURE);
230 }
231
232 if((fp_ou=fopen(report,"w"))==NULL) {
233 debuga(_("Cannot open file \"%s\": %s\n"),report,strerror(errno));
234 exit(EXIT_FAILURE);
235 }
236
237 write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Top sites"),HTML_JS_SORTTABLE);
238 fputs("<tr><td class=\"header_c\">",fp_ou);
239 fprintf(fp_ou,_("Period: %s"),period.html);
240 fputs("</td></tr>\n",fp_ou);
241 fputs("<tr><th class=\"header_c\">",fp_ou);
242 fprintf(fp_ou,_("Top %d sites"),TopSitesNum);
243 fputs("</th></tr>\n",fp_ou);
244 close_html_header(fp_ou);
245
246 fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\"",fp_ou);
247 if (SortTableJs[0]) fputs(" class=\"sortable\"",fp_ou);
248 fputs(">\n",fp_ou);
249 fprintf(fp_ou,"<thead><tr><th class=\"header_l\">%s</th><th class=\"header_l",
250 /* TRANSLATORS: This is a column header showing the position of the entry in the sorted list. */
251 _("NUM"));
252 if (SortTableJs[0]) fputs(" sorttable_alpha",fp_ou);
253 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",
254 /* TRANSLATORS: This is a column header showing the URL of the visited sites. */
255 _("ACCESSED SITE"),
256 /* TRANSLATORS: This is a column header showing the number of connections to a visited site. */
257 _("CONNECT"),
258 /* TRANSLATORS: This is a column header showing the number of transfered bytes. */
259 _("BYTES"),
260 /* TRANSLATORS: This is a column header showing the time spent by the proxy processing the requests. */
261 pgettext("duration","TIME"),
262 /* TRANSLATORS: This is a column header showing the number of users who visited a sites. */
263 _("USERS"));
264
265 regs=0;
266 ntopsites = 0;
267
268 if ((line=longline_create())==NULL) {
269 debuga(_("Not enough memory to read file %s\n"),sites);
270 exit(EXIT_FAILURE);
271 }
272
273 while(regs<TopSitesNum && (buf=longline_read(fp_in,line))!=NULL) {
274 getword_start(&gwarea,buf);
275 if (getword_atoll(&nacc,&gwarea,'\t')<0) {
276 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),sites);
277 exit(EXIT_FAILURE);
278 }
279 if (nacc == 0) continue;
280 if (getword_atoll(&nbytes,&gwarea,'\t')<0 || getword_atoll(&ntime,&gwarea,'\t')<0) {
281 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),sites);
282 exit(EXIT_FAILURE);
283 }
284 if (getword_atoi(&nusers,&gwarea,'\t')<0) {
285 debuga(_("The number of users is invalid in file %s\n"),sites);
286 exit(EXIT_FAILURE);
287 }
288 if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
289 debuga(_("The url is invalid in file %s\n"),sites);
290 exit(EXIT_FAILURE);
291 }
292
293 twork1=nacc;
294 twork2=nbytes;
295 twork3=ntime;
296
297 fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2\">",++regs);
298
299 if(BlockIt[0] != '\0' && url[0]!=ALIAS_PREFIX) {
300 fprintf(fp_ou,"<a href=\"%s%s?url=\"",wwwDocumentRoot,BlockIt);
301 output_html_url(fp_ou,url);
302 fputs("\"><img src=\"../images/sarg-squidguard-block.png\"></a>&nbsp;",fp_ou);
303 }
304
305 output_html_link(fp_ou,url,100);
306 fputs("</td><td class=\"data\"",fp_ou);
307 if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%"PRId64"\"",(uint64_t)twork1);
308 fprintf(fp_ou,">%s</td>",fixnum(twork1,1));
309 fputs("<td class=\"data\"",fp_ou);
310 if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%"PRId64"\"",(uint64_t)twork2);
311 fprintf(fp_ou,">%s</td>",fixnum(twork2,1));
312 fputs("<td class=\"data\"",fp_ou);
313 if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%"PRId64"\"",(uint64_t)twork3);
314 fprintf(fp_ou,">%s</td>",fixtime(twork3));
315 fputs("<td class=\"data\"",fp_ou);
316 if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%d\"",nusers);
317 fprintf(fp_ou,">%s</td></tr>\n",fixnum(nusers,1));
318 }
319 fclose(fp_in);
320 longline_destroy(&line);
321
322 fputs("</table></div>\n",fp_ou);
323 if (write_html_trailer(fp_ou)<0)
324 debuga(_("Write error in file %s\n"),report);
325 if (fclose(fp_ou)==EOF) {
326 debuga(_("Write error in \"%s\": %s\n"),report,strerror(errno));
327 exit(EXIT_FAILURE);
328 }
329
330 return;
331 }