]> git.ipfire.org Git - thirdparty/sarg.git/blob - topsites.c
Check for the return code when deleting a file
[thirdparty/sarg.git] / topsites.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2010
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 void topsites(void)
31 {
32 FILE *fp_in, *fp_ou;
33
34 char *buf;
35 char *url;
36 char *ourl=NULL;
37 char csort[255];
38 char general[MAXLEN];
39 char general2[MAXLEN];
40 char general3[MAXLEN];
41 char sites[MAXLEN];
42 char report[MAXLEN];
43 const char *sortf;
44 const char *sortt;
45 long long int nacc;
46 long long int nbytes;
47 long long int ntime;
48 long long int tnacc=0;
49 long long int tnbytes=0;
50 long long int tntime=0;
51 long long int twork1=0, twork2=0, twork3=0;
52 int regs=0;
53 int cstatus;
54 int url_len;
55 int ourl_size=0;
56 struct getwordstruct gwarea;
57 longline line;
58 struct generalitemstruct item;
59
60 if(Privacy)
61 return;
62
63 sprintf(general,"%s/sarg-general",outdirname);
64 sprintf(sites,"%s/sarg-sites",outdirname);
65 sprintf(general2,"%s/sarg-general2",outdirname);
66 sprintf(general3,"%s/sarg-general3",outdirname);
67
68 if ((ReportType & REPORT_TYPE_TOPUSERS) == 0)
69 sprintf(report,"%s/index.html",outdirname);
70 else
71 sprintf(report,"%s/topsites.html",outdirname);
72
73 sprintf(csort,"sort -k 4,4 -o \"%s\" \"%s\"",general2,general);
74 cstatus=system(csort);
75 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
76 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
77 debuga(_("sort command: %s\n"),csort);
78 exit(EXIT_FAILURE);
79 }
80
81 if((fp_in=fopen(general2,"r"))==NULL) {
82 debuga(_("(topsites) Cannot open log file %s\n"),general2);
83 debuga(_("sort command: %s\n"),csort);
84 exit(EXIT_FAILURE);
85 }
86
87 if((fp_ou=fopen(general3,"w"))==NULL) {
88 debuga(_("(topsites) Cannot open log file %s\n"),general3);
89 exit(EXIT_FAILURE);
90 }
91
92 if ((line=longline_create())==NULL) {
93 debuga(_("Not enough memory to read file %s\n"),general2);
94 exit(EXIT_FAILURE);
95 }
96
97 while((buf=longline_read(fp_in,line))!=NULL) {
98 ger_read(buf,&item,general2);
99 if(item.total) continue;
100
101 if(!regs) {
102 url_len=strlen(item.url);
103 if (!ourl || url_len>=ourl_size) {
104 ourl_size=url_len+1;
105 ourl=realloc(ourl,ourl_size);
106 if (!ourl) {
107 debuga(_("Not enough memory to store the url\n"));
108 exit(EXIT_FAILURE);
109 }
110 }
111 strcpy(ourl,item.url);
112 regs++;
113 }
114
115 if(strcmp(item.url,ourl) != 0) {
116 /*
117 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
118 to print a long long int unless it is exactly 64-bits long.
119 */
120 fprintf(fp_ou,"%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%s\n",(uint64_t)tnacc,(uint64_t)tnbytes,(uint64_t)tntime,ourl);
121 url_len=strlen(item.url);
122 if (url_len>=ourl_size) {
123 ourl_size=url_len+1;
124 ourl=realloc(ourl,ourl_size);
125 if (!ourl) {
126 debuga(_("Not enough memory to store the url\n"));
127 exit(EXIT_FAILURE);
128 }
129 }
130 strcpy(ourl,item.url);
131 tnacc=0;
132 tnbytes=0;
133 tntime=0;
134 }
135
136 tnacc+=item.nacc;
137 tnbytes+=item.nbytes;
138 tntime+=item.nelap;
139 }
140 fclose(fp_in);
141 if (unlink(general2)) {
142 debuga(_("Cannot delete %s - %s\n"),general2,strerror(errno));
143 exit(EXIT_FAILURE);
144 }
145 longline_destroy(&line);
146
147 if (ourl) {
148 /*
149 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
150 to print a long long int unless it is exactly 64-bits long.
151 */
152 fprintf(fp_ou,"%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%s\n",(uint64_t)tnacc,(uint64_t)tnbytes,(uint64_t)tntime,ourl);
153 free(ourl);
154 }
155
156 fclose(fp_ou);
157
158 if((TopsitesSort & TOPSITE_SORT_CONNECT) != 0) {
159 sortf="-k 1,1 -k 2,2";
160 } else if((TopsitesSort & TOPSITE_SORT_BYTES) != 0) {
161 sortf="-k 2,2 -k 1,1";
162 } else if((TopsitesSort & TOPSITE_SORT_TIME) != 0) {
163 sortf="-k 3,3";
164 } else {
165 sortf="-k 2,2 -k 1,1"; //default is BYTES
166 }
167 if((TopsitesSort & TOPSITE_SORT_REVERSE) != 0) {
168 sortt="-r";
169 } else {
170 sortt="";
171 }
172
173 sprintf(csort,"sort %s -n %s -o \"%s\" \"%s\"",sortt,sortf,sites,general3);
174 cstatus=system(csort);
175 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
176 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
177 debuga(_("sort command: %s\n"),csort);
178 exit(EXIT_FAILURE);
179 }
180 if((fp_in=fopen(sites,"r"))==NULL) {
181 debuga(_("(topsites) Cannot open log file %s\n"),sites);
182 debuga(_("sort command: %s\n"),csort);
183 exit(EXIT_FAILURE);
184 }
185
186 if (unlink(general3)) {
187 debuga(_("Cannot delete %s - %s\n"),general3,strerror(errno));
188 exit(EXIT_FAILURE);
189 }
190
191 if((fp_ou=fopen(report,"w"))==NULL) {
192 debuga(_("(topsites) Cannot open log file %s\n"),report);
193 exit(EXIT_FAILURE);
194 }
195
196 write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Top sites"),HTML_JS_SORTTABLE);
197 fputs("<tr><td class=\"header_c\">",fp_ou);
198 fprintf(fp_ou,_("Period: %s"),period.html);
199 fputs("</td></tr>\n",fp_ou);
200 fputs("<tr><th class=\"header_c\">",fp_ou);
201 fprintf(fp_ou,_("Top %d sites"),TopSitesNum);
202 fputs("</th></tr>\n",fp_ou);
203 close_html_header(fp_ou);
204
205 fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\"",fp_ou);
206 if (SortTableJs[0]) fputs(" class=\"sortable\"",fp_ou);
207 fputs(">\n",fp_ou);
208 fprintf(fp_ou,"<thead><tr><th class=\"header_l\">%s</th><th class=\"header_l",_("NUM"));
209 if (SortTableJs[0]) fputs(" sorttable_alpha",fp_ou);
210 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"));
211
212 regs=0;
213 ntopsites = 0;
214
215 if ((line=longline_create())==NULL) {
216 debuga(_("Not enough memory to read file %s\n"),sites);
217 exit(EXIT_FAILURE);
218 }
219
220 while(regs<TopSitesNum && (buf=longline_read(fp_in,line))!=NULL) {
221 getword_start(&gwarea,buf);
222 if (getword_atoll(&nacc,&gwarea,'\t')<0) {
223 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),sites);
224 exit(EXIT_FAILURE);
225 }
226 if (nacc == 0) continue;
227 if (getword_atoll(&nbytes,&gwarea,'\t')<0 || getword_atoll(&ntime,&gwarea,'\t')<0) {
228 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),sites);
229 exit(EXIT_FAILURE);
230 }
231 if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
232 debuga(_("The url is invalid in file %s\n"),sites);
233 exit(EXIT_FAILURE);
234 }
235
236 twork1=nacc;
237 twork2=nbytes;
238 twork3=ntime;
239
240 fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2 link\">",++regs);
241
242 if(BlockIt[0] != '\0') {
243 fprintf(fp_ou,"<a href=\"%s%s?url=\"",wwwDocumentRoot,BlockIt);
244 output_html_url(fp_ou,url);
245 fputs("\"><img src=\"../images/sarg-squidguard-block.png\"></a>&nbsp;",fp_ou);
246 }
247
248 fputs("<a href=\"http://",fp_ou);
249 output_html_url(fp_ou,url);
250 fputs("\">",fp_ou);
251 output_html_string(fp_ou,url,100);
252 fputs("</a></td><td class=\"data\"",fp_ou);
253 if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%"PRId64"\"",(uint64_t)twork1);
254 fprintf(fp_ou,">%s</td>",fixnum(twork1,1));
255 fputs("<td class=\"data\"",fp_ou);
256 if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%"PRId64"\"",(uint64_t)twork2);
257 fprintf(fp_ou,">%s</td>",fixnum(twork2,1));
258 fputs("<td class=\"data\"",fp_ou);
259 if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%"PRId64"\"",(uint64_t)twork3);
260 fprintf(fp_ou,">%s</td></tr>\n",fixtime(twork3));
261 }
262 fclose(fp_in);
263 longline_destroy(&line);
264
265 fputs("</table></div>\n",fp_ou);
266 if (write_html_trailer(fp_ou)<0)
267 debuga(_("Write error in file %s\n"),report);
268 if (fclose(fp_ou)==EOF)
269 debuga(_("Failed to close file %s - %s\n"),report,strerror(errno));
270
271 return;
272 }