]>
Commit | Line | Data |
---|---|---|
25697a35 | 1 | /* |
c37945ed FM |
2 | * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com |
3 | * 1998, 2008 | |
94ff9470 | 4 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net |
25697a35 GS |
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" | |
5f3cfd1d | 27 | #include "include/defs.h" |
25697a35 | 28 | |
32e71fa4 | 29 | int geramail(const char *dirname, int debug, const char *outdir, int userip, const char *email, const char *TempDir) |
25697a35 GS |
30 | { |
31 | ||
32 | FILE *fp_in, *fp_top1, *fp_top2, *fp_top3; | |
33 | long long int ttnbytes=0, ttnacc=0, tnacc=0; | |
34 | long long int tnbytes=0, ttnelap=0, tnelap=0; | |
35 | float perc=0.00; | |
36 | float perc2=0.00; | |
37 | int posicao=0; | |
d6e703cc | 38 | char olduser[MAXLEN], csort[MAXLEN], period[MAXLEN], arqper[MAXLEN]; |
fb7c5f27 FM |
39 | char wger[MAXLEN], top1[MAXLEN], top2[MAXLEN], top3[MAXLEN], user[MAXLEN], nacc[20], nbytes[20], url[MAXLEN], tusr[MAXLEN]; |
40 | char ip[MAXLEN], hora[9], data[15], elap[16], user2[MAXLEN], wperc[8], wperc2[8]; | |
25697a35 | 41 | char strip1[MAXLEN], strip2[MAXLEN], strip3[MAXLEN], strip4[MAXLEN], strip5[MAXLEN], strip6[MAXLEN], strip7[MAXLEN]; |
fb7c5f27 | 42 | char incac[30], oucac[30]; |
25697a35 GS |
43 | int totuser=0; |
44 | time_t t; | |
45 | struct tm *local; | |
456d78a5 | 46 | int cstatus; |
fb7c5f27 | 47 | char warea[MAXLEN]; |
25697a35 GS |
48 | |
49 | strcpy(wger,dirname); | |
50 | strcpy(top1,dirname); | |
51 | strcpy(top2,dirname); | |
52 | strcpy(top3,dirname); | |
53 | strcpy(tusr,dirname); | |
d6e703cc | 54 | strcat(wger,"/sarg-general"); |
25697a35 GS |
55 | strcat(top1,"/top"); |
56 | strcat(top2,"/top.tmp"); | |
57 | strcat(top3,"/report"); | |
d6e703cc | 58 | strcat(tusr,"/sarg-users"); |
25697a35 GS |
59 | |
60 | if((fp_in=fopen(wger,"r"))==NULL) { | |
61 | fprintf(stderr, "SARG: (email) %s: %s\n",text[45],wger); | |
62 | exit(1); | |
63 | } | |
64 | ||
65 | if((fp_top1=fopen(top1,"w"))==NULL) { | |
66 | fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top1); | |
67 | exit(1); | |
68 | } | |
69 | ||
70 | if((fp_top2=fopen(top2,"w"))==NULL) { | |
71 | fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top2); | |
72 | exit(1); | |
73 | } | |
74 | ||
fb7c5f27 FM |
75 | olduser[0]='\0'; |
76 | totuser=0; | |
77 | ||
78 | while(fgets(warea,sizeof(warea),fp_in)) | |
25697a35 | 79 | { |
fb7c5f27 FM |
80 | if (getword(user,sizeof(user),warea,' ')<0) { |
81 | printf("SARG: Maybe you have a broken user in your %s file.\n",wger); | |
82 | exit(1); | |
83 | } | |
84 | if(strcmp(user,"TOTAL") == 0) { | |
85 | continue; | |
86 | } | |
87 | if (getword(nacc,sizeof(nacc),warea,' ')<0) { | |
88 | printf("SARG: Maybe you have a broken number of access in your %s file.\n",wger); | |
89 | exit(1); | |
90 | } | |
91 | if (getword(nbytes,sizeof(nbytes),warea,' ')<0) { | |
92 | printf("SARG: Maybe you have a broken number of bytes in your %s file.\n",wger); | |
93 | exit(1); | |
94 | } | |
95 | if (getword(url,sizeof(url),warea,' ')<0) { | |
96 | printf("SARG: Maybe you have a broken url in your %s file.\n",wger); | |
97 | exit(1); | |
98 | } | |
99 | if (getword(ip,sizeof(ip),warea,' ')<0) { | |
100 | printf("SARG: Maybe you have a broken source IP address in your %s file.\n",wger); | |
101 | exit(1); | |
102 | } | |
103 | if (getword(hora,sizeof(hora),warea,' ')<0) { | |
104 | printf("SARG: Maybe you have a broken time in your %s file.\n",wger); | |
105 | exit(1); | |
106 | } | |
107 | if (getword(data,sizeof(data),warea,' ')<0) { | |
108 | printf("SARG: Maybe you have a broken date in your %s file.\n",wger); | |
109 | exit(1); | |
110 | } | |
111 | if (getword(elap,sizeof(elap),warea,' ')<0) { | |
112 | printf("SARG: Maybe you have a broken elapsed time in your %s file.\n",wger); | |
113 | exit(1); | |
114 | } | |
115 | if (getword(incac,sizeof(incac),warea,' ')<0) { | |
116 | printf("SARG: Maybe you have a broken in cache column in your %s file.\n",wger); | |
117 | exit(1); | |
118 | } | |
119 | if (getword(oucac,sizeof(oucac),warea,0)<0) { | |
120 | printf("SARG: Maybe you have a broken not in cache column in your %s file.\n",wger); | |
121 | exit(1); | |
122 | } | |
25697a35 GS |
123 | |
124 | if(strcmp(olduser,user) != 0) | |
125 | { | |
fb7c5f27 | 126 | totuser++; |
25697a35 | 127 | |
fb7c5f27 | 128 | if (olduser[0] != '\0') { |
25697a35 | 129 | #if defined(__FreeBSD__) |
fb7c5f27 | 130 | fprintf(fp_top2,"%s %qu %qu %qu\n",olduser,tnbytes,tnacc,tnelap); |
25697a35 | 131 | #elif defined(__alpha) || __ALPHA |
fb7c5f27 | 132 | fprintf(fp_top2,"%s %ld %ld %ld\n",olduser,tnbytes,tnacc,tnelap); |
25697a35 | 133 | #else |
fb7c5f27 | 134 | fprintf(fp_top2,"%s %lld %lld %lld\n",olduser,tnbytes,tnacc,tnelap); |
25697a35 | 135 | #endif |
fb7c5f27 FM |
136 | ttnbytes=ttnbytes+tnbytes; |
137 | ttnacc=ttnacc+tnacc; | |
138 | ttnelap=ttnelap+tnelap; | |
139 | } | |
140 | strcpy(olduser,user); | |
25697a35 GS |
141 | tnbytes=0; |
142 | tnacc=0; | |
143 | tnelap=0; | |
144 | } | |
145 | ||
146 | tnbytes=tnbytes+atol(nbytes); | |
147 | tnacc=tnacc+atol(nacc); | |
148 | tnelap=tnelap+atol(elap); | |
25697a35 | 149 | } |
fb7c5f27 FM |
150 | |
151 | if (olduser[0] != '\0') { | |
25697a35 | 152 | #if defined(__FreeBSD__) |
fb7c5f27 | 153 | fprintf(fp_top2,"%s %qu %qu %qu\n",olduser,tnbytes,tnacc,tnelap); |
25697a35 | 154 | #elif defined(__alpha) || __ALPHA |
fb7c5f27 | 155 | fprintf(fp_top2,"%s %ld %ld %ld\n",olduser,tnbytes,tnacc,tnelap); |
25697a35 | 156 | #else |
fb7c5f27 | 157 | fprintf(fp_top2,"%s %lld %lld %lld\n",olduser,tnbytes,tnacc,tnelap); |
25697a35 | 158 | #endif |
fb7c5f27 FM |
159 | ttnbytes=ttnbytes+tnbytes; |
160 | ttnacc=ttnacc+tnacc; | |
161 | ttnelap=ttnelap+tnelap; | |
162 | } | |
25697a35 | 163 | |
25697a35 GS |
164 | fclose(fp_in); |
165 | fclose(fp_top2); | |
166 | ||
9a2efbd0 | 167 | sprintf(csort,"sort -n -T \"%s\" -r -k 2,2 -o \"%s\" \"%s\"", TempDir, top1, top2); |
456d78a5 FM |
168 | cstatus=system(csort); |
169 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
170 | fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus)); | |
171 | fprintf(stderr, "SARG: sort command: %s\n",csort); | |
172 | exit(1); | |
173 | } | |
25697a35 GS |
174 | |
175 | unlink(top2); | |
176 | ||
177 | if((fp_top1=fopen(top1,"a"))==NULL) { | |
178 | fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top1); | |
179 | exit(1); | |
180 | } | |
354c1a68 | 181 | #if defined(__FreeBSD__) |
fb7c5f27 | 182 | fprintf(fp_top1,"TOTAL %qu %qu %qu\n",ttnbytes,ttnacc,ttnelap); |
354c1a68 | 183 | #elif defined(__alpha) || __ALPHA |
fb7c5f27 | 184 | fprintf(fp_top1,"TOTAL %ld %ld %ld\n",ttnbytes,ttnacc,ttnelap); |
354c1a68 | 185 | #else |
fb7c5f27 | 186 | fprintf(fp_top1,"TOTAL %lld %lld %lld\n",ttnbytes,ttnacc,ttnelap); |
354c1a68 | 187 | #endif |
25697a35 GS |
188 | fclose(fp_top1); |
189 | ||
fb7c5f27 FM |
190 | /* |
191 | * Obtem o period | |
192 | */ | |
25697a35 GS |
193 | |
194 | strcpy(arqper,dirname); | |
d6e703cc | 195 | strcat(arqper,"/sarg-period"); |
25697a35 GS |
196 | |
197 | if ((fp_in = fopen(arqper, "r")) == 0){ | |
198 | fprintf(stderr, "SARG: (email) %s: %s\n",text[45],arqper); | |
199 | exit(1); | |
200 | } | |
201 | ||
05b90947 FM |
202 | if (!fgets(period,sizeof(period),fp_in)) { |
203 | fprintf(stderr,"SARG: (email) read error in %s\n",arqper); | |
204 | exit(1); | |
205 | } | |
25697a35 GS |
206 | fclose(fp_in); |
207 | ||
fb7c5f27 FM |
208 | if((fp_top1=fopen(top1,"r"))==NULL) { |
209 | fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top1); | |
210 | exit(1); | |
211 | } | |
212 | ||
213 | if((fp_top3=fopen(top3,"w"))==NULL) { | |
214 | fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top3); | |
215 | exit(1); | |
216 | } | |
217 | ||
25697a35 GS |
218 | sprintf(strip1,"%s",text[88]); |
219 | strip_latin(strip1); | |
354c1a68 | 220 | fprintf(fp_top3,"%s\n",strip1); |
25697a35 GS |
221 | |
222 | sprintf(strip1,"%s",text[97]); | |
223 | strip_latin(strip1); | |
354c1a68 | 224 | fprintf(fp_top3,"%s\n",strip1); |
25697a35 GS |
225 | |
226 | sprintf(strip1,"%s",text[89]); | |
227 | strip_latin(strip1); | |
354c1a68 | 228 | fprintf(fp_top3,"%s %s\n\n",strip1,period); |
25697a35 GS |
229 | |
230 | sprintf(strip1,"%s",text[100]); | |
231 | strip_latin(strip1); | |
232 | sprintf(strip2,"%s",text[98]); | |
233 | strip_latin(strip2); | |
234 | sprintf(strip3,"%s",text[92]); | |
235 | strip_latin(strip3); | |
236 | sprintf(strip4,"%s",text[93]); | |
237 | strip_latin(strip4); | |
238 | sprintf(strip5,"%s",text[94]); | |
239 | strip_latin(strip5); | |
240 | sprintf(strip6,"%s",text[95]); | |
241 | strip_latin(strip6); | |
242 | sprintf(strip7,"%s",text[99]); | |
243 | strip_latin(strip7); | |
244 | ||
354c1a68 | 245 | fprintf(fp_top3,"%-7s %-20s %-8s %-15s %%%-6s %-10s %-10s %%%-7s\n------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",strip1,strip2,strip3,strip4,strip4,strip5,strip6,strip7); |
25697a35 | 246 | |
fb7c5f27 | 247 | while(fgets(warea,sizeof(warea),fp_top1)) |
25697a35 | 248 | { |
fb7c5f27 FM |
249 | if (getword(user,sizeof(user),warea,' ')<0) { |
250 | printf("SARG: Maybe you have a broken user in your %s file.\n",top1); | |
251 | exit(1); | |
252 | } | |
253 | if (getword(nbytes,sizeof(nbytes),warea,' ')<0) { | |
254 | printf("SARG: Maybe you have a broken number of bytes in your %s file.\n",top1); | |
255 | exit(1); | |
256 | } | |
257 | if (getword(nacc,sizeof(nacc),warea,' ')<0) { | |
258 | printf("SARG: Maybe you have a broken number of access in your %s file.\n",top1); | |
259 | exit(1); | |
260 | } | |
261 | if (getword(elap,sizeof(elap),warea,' ')<0) { | |
262 | printf("SARG: Maybe you have a broken elapsed time in your %s file.\n",top1); | |
263 | exit(1); | |
264 | } | |
25697a35 | 265 | |
fb7c5f27 | 266 | if(strchr(user,'_') != NULL) |
25697a35 GS |
267 | fixip(user); |
268 | ||
269 | strcpy(user2,user); | |
270 | ||
271 | tnbytes=atol(nbytes); | |
272 | ||
273 | if(tnbytes) { | |
274 | perc=ttnbytes / 100; | |
275 | perc=tnbytes / perc; | |
276 | } else perc = 0; | |
277 | ||
278 | if(atol(elap)) { | |
279 | perc2=ttnelap / 100; | |
280 | perc2=atol(elap) / perc2; | |
281 | } else perc2 = 0; | |
282 | ||
283 | posicao++; | |
284 | tnelap=atol(elap); | |
285 | ||
286 | sprintf(wperc,"%3.2f%%",perc); | |
287 | sprintf(wperc2,"%3.2f%%",perc2); | |
288 | ||
fb7c5f27 | 289 | if(strcmp(user,"TOTAL") == 0){ |
354c1a68 | 290 | fputs("------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",fp_top3); |
25697a35 | 291 | #if defined(__FreeBSD__) |
354c1a68 | 292 | fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",text[107]," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap); |
25697a35 | 293 | #elif defined(__alpha) || __ALPHA |
354c1a68 | 294 | fprintf(fp_top3,"%-7s %20s %8ld %15s %8s %9s %10ld\n",text[107]," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap); |
25697a35 | 295 | #else |
354c1a68 | 296 | fprintf(fp_top3,"%-7s %20s %8lld %15s %8s %9s %10lld\n",text[107]," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap); |
25697a35 | 297 | #endif |
354c1a68 | 298 | } else { |
25697a35 | 299 | #if defined(__FreeBSD__) |
354c1a68 | 300 | fprintf(fp_top3,"%7d %20s %8s %15s %7s %10s %10qu %7s%%\n",posicao,user2,nacc,fixnum(tnbytes,1),wperc,buildtime(tnelap),tnelap,wperc2); |
25697a35 | 301 | #elif defined(__alpha) || __ALPHA |
354c1a68 | 302 | fprintf(fp_top3,"%7d %20s %8s %15s %7s %10s %10ld %7s%%\n",posicao,user2,nacc,fixnum(tnbytes,1),wperc,buildtime(tnelap),tnelap,wperc2); |
25697a35 | 303 | #else |
354c1a68 | 304 | fprintf(fp_top3,"%7d %20s %8s %15s %7s %10s %10lld %7s\n",posicao,user2,nacc,fixnum(tnbytes,1),wperc,buildtime(tnelap),tnelap,wperc2); |
25697a35 GS |
305 | #endif |
306 | } | |
25697a35 GS |
307 | } |
308 | ||
309 | if(ttnbytes) { | |
310 | tnbytes=ttnbytes / totuser; | |
311 | } else tnbytes=0; | |
312 | ||
313 | sprintf(strip1,"%s",text[96]); | |
314 | strip_latin(strip1); | |
315 | #if defined(__FreeBSD__) | |
354c1a68 | 316 | fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser); |
25697a35 | 317 | #elif defined(__alpha) || __ALPHA |
354c1a68 | 318 | fprintf(fp_top3,"%-7s %20s %8ld %15s %8s %9s %10ld\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser); |
25697a35 | 319 | #else |
354c1a68 | 320 | fprintf(fp_top3,"%-7s %20s %8lld %15s %8s %9s %10lld\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser); |
25697a35 | 321 | #endif |
25697a35 GS |
322 | |
323 | fclose(fp_top1); | |
324 | unlink(top1); | |
325 | ||
326 | t = time(NULL); | |
327 | local = localtime(&t); | |
354c1a68 | 328 | fprintf(fp_top3, "\n%s\n", asctime(local)); |
25697a35 GS |
329 | |
330 | fclose(fp_top3); | |
331 | ||
332 | if(strcmp(email,"stdout") == 0) { | |
333 | if((fp_top3=fopen(top3,"r"))==NULL) { | |
334 | fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top3); | |
335 | exit(1); | |
336 | } | |
337 | ||
338 | while(fgets(buf,sizeof(buf),fp_top3)!=NULL) | |
339 | printf("%s",buf); | |
340 | } else { | |
eb08bfe3 | 341 | sprintf(buf,"\"%s\" -s \"SARG %s, %s\" \"%s\" <\"%s\"",MailUtility,text[55],asctime(local),email,top3); |
456d78a5 FM |
342 | cstatus=system(buf); |
343 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
344 | fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus)); | |
345 | fprintf(stderr, "SARG: command: %s\n",buf); | |
346 | exit(1); | |
347 | } | |
348 | } | |
fb7c5f27 | 349 | |
05b90947 FM |
350 | if (snprintf(csort,sizeof(csort),"rm -rf \"%s/sarg\"",TempDir)>=sizeof(csort)) { |
351 | fprintf(stderr,"SARG: Temporary directory name too long: %s\n",TempDir); | |
352 | exit(1); | |
353 | } | |
354 | cstatus=system(csort); | |
355 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
356 | fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus)); | |
357 | fprintf(stderr, "SARG: command: %s\n",csort); | |
358 | exit(1); | |
359 | } | |
25697a35 GS |
360 | |
361 | return (0); | |
362 | } |