]> git.ipfire.org Git - thirdparty/sarg.git/blob - topsites.c
Detect external commands failures and print the exact command that produced the error...
[thirdparty/sarg.git] / topsites.c
1 /*
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
3 * 1998, 2008
4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
5 *
6 * SARG donations:
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
23 *
24 */
25
26 #include "include/conf.h"
27
28 void topsites()
29 {
30
31 FILE *fp_in, *fp_ou;
32
33 char url[MAXLEN];
34 char ourl[MAXLEN];
35 char nacc[20];
36 char nbytes[20];
37 char ntime[20];
38 char ntemp[40];
39 char ttnacc[20];
40 char ttnbytes[20];
41 char ttntime[20];
42 char csort[255];
43 char general[MAXLEN];
44 char general2[MAXLEN];
45 char general3[MAXLEN];
46 char per[MAXLEN];
47 char sites[MAXLEN];
48 char report[MAXLEN];
49 char period[100];
50 char sortf[10];
51 char sortt[10];
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;
56 int regs=0;
57 int cstatus;
58
59 if(strcmp(Privacy,"yes") == 0)
60 return;
61
62 sprintf(general,"%s/sarg-general",dirname);
63 sprintf(sites,"%s/sarg-sites",dirname);
64 sprintf(general2,"%s/sarg-general2",dirname);
65 sprintf(general3,"%s/sarg-general3",dirname);
66 sprintf(per,"%s/sarg-period",dirname);
67
68 if (strstr(ReportType,"topusers") == 0)
69 sprintf(report,"%s/index.html",dirname);
70 else
71 sprintf(report,"%s/topsites.html",dirname);
72
73 if ((fp_in = fopen(per, "r")) == 0) {
74 fprintf(stderr, "SARG: (topuser) %s: %s\n",text[45],per);
75 exit(1);
76 }
77
78 fgets(period,sizeof(period),fp_in);
79 fclose(fp_in);
80
81 sprintf(csort,"sort -k 4,4 -o '%s' '%s'",general2,general);
82 cstatus=system(csort);
83 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
84 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
85 fprintf(stderr, "SARG: sort command: %s\n",csort);
86 exit(1);
87 }
88
89 if((fp_in=fopen(general2,"r"))==NULL) {
90 fprintf(stderr, "SARG: (topsite) %s: %s\n",text[8],general2);
91 fprintf(stderr, "SARG: sort command: %s\n",csort);
92 exit(1);
93 }
94
95 if((fp_ou=fopen(general3,"w"))==NULL) {
96 fprintf(stderr, "SARG: (topsite) %s: %s\n",text[8],general3);
97 exit(1);
98 }
99
100 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
101 if (getword(url,sizeof(url),buf,' ')<0) {
102 printf("SARG: Maybe you have an invalid user in your %s file of the topsites.\n",general2);
103 exit(1);
104 }
105 if(strcmp(url,"TOTAL") == 0) {
106 if (getword(ttnacc,sizeof(ttnacc),buf,' ')<0) {
107 printf("SARG: Maybe you have an invalid total number of access in your %s file of the topsites.\n",general2);
108 exit(1);
109 }
110 if (getword(ttnbytes,sizeof(ttnbytes),buf,' ')<0) {
111 printf("SARG: Maybe you have an invalid total number of bytes in your %s file of the topsites.\n",general2);
112 exit(1);
113 }
114 if (getword(ttntime,sizeof(ttntime),buf,' ')<0) {
115 printf("SARG: Maybe you have an invalid total time in your %s file of the topsites.\n",general2);
116 exit(1);
117 }
118 continue;
119 }
120 if (getword(nacc,sizeof(nacc),buf,' ')<0) {
121 printf("SARG: Maybe you have an invalid number of access in your %s file of the topsites.\n",general2);
122 exit(1);
123 }
124 if (getword(nbytes,sizeof(nbytes),buf,' ')<0) {
125 printf("SARG: Maybe you have an invalid number of bytes in your %s file of the topsites.\n",general2);
126 exit(1);
127 }
128 if (getword(url,sizeof(url),buf,' ')<0) {
129 printf("SARG: Maybe you have an invalid url in your %s file of the topsites.\n",general2);
130 exit(1);
131 }
132 if (getword(ntemp,sizeof(ntemp),buf,' ')<0) {
133 printf("SARG: Maybe you have a broken record or garbage in column 5 in your %s file of the topsites.\n",general2);
134 exit(1);
135 }
136 if (getword(ntemp,sizeof(ntemp),buf,' ')<0) {
137 printf("SARG: Maybe you have a broken record or garbage in column 6 in your %s file of the topsites.\n",general2);
138 exit(1);
139 }
140 if (getword(ntemp,sizeof(ntemp),buf,' ')<0) {
141 printf("SARG: Maybe you have a broken record or garbage in column 7 in your %s file of the topsites.\n",general2);
142 exit(1);
143 }
144 if (getword(ntime,sizeof(ntime),buf,' ')<0) {
145 printf("SARG: Maybe you have a broken record or garbage in column 8 in your %s file of the topsites.\n",general2);
146 exit(1);
147 }
148
149 if(!regs) {
150 strcpy(ourl,url);
151 regs++;
152 }
153
154 if(strcmp(url,ourl) != 0) {
155 my_lltoa(tnacc,val1,15);
156 my_lltoa(tnbytes,val2,15);
157 my_lltoa(tntime,val3,15);
158 sprintf(buf,"%s %s %s %s\n",val1,val2,val3,ourl);
159 fputs(buf, fp_ou);
160 strcpy(ourl,url);
161 tnacc=0;
162 tnbytes=0;
163 tntime=0;
164 }
165
166 tnacc+=my_atoll(nacc);
167 tnbytes+=my_atoll(nbytes);
168 tntime+=my_atoll(ntime);
169 }
170
171 my_lltoa(tnacc,val1,15);
172 my_lltoa(tnbytes,val2,15);
173 my_lltoa(tntime,val3,15);
174 sprintf(buf,"%s %s %s %s\n",val1,val2,val3,ourl);
175 fputs(buf, fp_ou);
176
177 fclose(fp_in);
178 fclose(fp_ou);
179 unlink(general2);
180
181 strlow(TopsitesSortField);
182 strlow(TopsitesSortType);
183
184 if(strcmp(TopsitesSortField,"connect") == 0)
185 strcpy(sortf,"1,1");
186 if(strcmp(TopsitesSortField,"bytes") == 0)
187 strcpy(sortf,"2,2");
188 if(strcmp(TopsitesSortType,"a") == 0)
189 strcpy(sortt," ");
190 if(strcmp(TopsitesSortType,"d") == 0)
191 strcpy(sortt,"-r");
192
193 sprintf(csort,"sort %s -k %s -o '%s' '%s'",sortt,sortf,sites,general3);
194 cstatus=system(csort);
195 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
196 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
197 fprintf(stderr, "SARG: sort command: %s\n",csort);
198 exit(1);
199 }
200 if((fp_in=fopen(sites,"r"))==NULL) {
201 fprintf(stderr, "SARG: (topsite) %s: %s\n",text[8],sites);
202 fprintf(stderr, "SARG: sort command: %s\n",csort);
203 exit(1);
204 }
205
206 unlink(general2);
207 unlink(general3);
208
209 if((fp_ou=fopen(report,"w"))==NULL) {
210 fprintf(stderr, "SARG: (topsite) %s: %s\n",text[8],report);
211 exit(1);
212 }
213
214 regs=0;
215
216 fprintf(fp_ou, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n<html>\n<head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet);
217 css(fp_ou);
218 fputs("</head>\n",fp_ou);
219 fprintf(fp_ou,"<body bgcolor=%s text=%s background='%s'>\n",BgColor,TxColor,BgImage);
220 if(strlen(LogoImage) > 0) fprintf(fp_ou, "<center><table cellpadding=\"0\" cellspacing=\"0\">\n<tr><th class=\"logo\"><img src='%s' border=0 align=absmiddle width=%s height=%s>&nbsp;%s</th></tr>\n<tr><td height=\"5\"></td></tr>\n</table>\n",LogoImage,Width,Height,LogoText);
221
222 if(strcmp(IndexTree,"date") == 0)
223 show_sarg(fp_ou,"../../..");
224 else
225 show_sarg(fp_ou, "..");
226
227 fputs("<center><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ou);
228 sprintf(url,"<tr><th class=\"title\">%s</th></tr>\n",Title);
229 fputs(url,fp_ou);
230
231 sprintf(url,"<tr><td class=\"header3\">%s: %s</td></tr>\n",text[89],period);
232 fputs(url,fp_ou);
233 sprintf(url,"<tr><th class=\"header3\">%s %s %s</th></tr>\n",text[83],TopSitesNum,text[84]);
234 fputs(url,fp_ou);
235 fputs("</table></center>\n",fp_ou);
236
237 fputs("<center><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_ou);
238 fputs("<tr><td></td></tr>\n",fp_ou);
239 sprintf(url,"<tr><th class=\"header\">%s</th><th class=\"header\">%s</th> \
240 <th class=\"header\">%s</th><th class=\"header\">%s</th> \
241 <th class=\"header\">%s</th></tr>\n", \
242 text[100],text[91],text[92],text[93],text[99]);
243 fputs(url,fp_ou);
244
245 regs=1;
246 ntopsites = 0;
247
248 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
249 if(regs>atoi(TopSitesNum))
250 break;
251 if (getword(nacc,sizeof(nacc),buf,' ')<0) {
252 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",sites);
253 exit(1);
254 }
255 if (atoi(nacc) == 0) continue;
256 if (getword(nbytes,sizeof(nbytes),buf,' ')<0 ||
257 getword(ntime,sizeof(ntime),buf,' ')<0 || getword(url,sizeof(url),buf,' ')<0) {
258 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",sites);
259 exit(1);
260 }
261
262 twork1=my_atoll(nacc);
263 twork2=my_atoll(nbytes);
264 twork3=my_atoll(ntime);
265
266 sprintf(wwork1,"%s",fixnum(twork1,1));
267 sprintf(wwork2,"%s",fixnum(twork2,1));
268 sprintf(wwork3,"%s",fixtime(twork3,1));
269
270 if(strlen(BlockIt) > 0)
271 sprintf(BlockImage,"<a href=\"%s%s?url=%s\"><img src=\"../images/sarg-squidguard-block.png\" border=\"0\"></a>&nbsp;",wwwDocumentRoot,BlockIt,url);
272 else BlockImage[0]='\0';
273
274
275 sprintf(ourl,"<tr><td class=\"data\">%d</td><td class=\"data2\">%s<a href=\"http://%s\"><font class=\"link\">%s</font></td><td class=\"data\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s</td></tr>\n",regs,BlockImage,url,url,wwork1,wwork2,wwork3);
276 fputs(ourl,fp_ou);
277 regs++;
278 }
279
280
281 fputs("</table></center>\n",fp_ou);
282
283 show_info(fp_ou);
284
285 fputs("</body>\n</html>\n",fp_ou);
286
287 fclose(fp_in);
288 fclose(fp_ou);
289
290 return;
291
292 }