]> git.ipfire.org Git - thirdparty/sarg.git/blob - email.c
Compute the report date without resorting to the system command.
[thirdparty/sarg.git] / email.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 #include "include/defs.h"
28
29 int geramail(const char *dirname, int debug, const char *outdir, int userip, const char *email, const char *TempDir)
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;
38 char olduser[MAXLEN], csort[MAXLEN], period[MAXLEN], arqper[MAXLEN];
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];
41 char strip1[MAXLEN], strip2[MAXLEN], strip3[MAXLEN], strip4[MAXLEN], strip5[MAXLEN], strip6[MAXLEN], strip7[MAXLEN];
42 char incac[30], oucac[30];
43 int totuser=0;
44 time_t t;
45 struct tm *local;
46 int cstatus;
47 char warea[MAXLEN];
48
49 strcpy(wger,dirname);
50 strcpy(top1,dirname);
51 strcpy(top2,dirname);
52 strcpy(top3,dirname);
53 strcpy(tusr,dirname);
54 strcat(wger,"/sarg-general");
55 strcat(top1,"/top");
56 strcat(top2,"/top.tmp");
57 strcat(top3,"/report");
58 strcat(tusr,"/sarg-users");
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
75 olduser[0]='\0';
76 totuser=0;
77
78 while(fgets(warea,sizeof(warea),fp_in))
79 {
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 }
123
124 if(strcmp(olduser,user) != 0)
125 {
126 totuser++;
127
128 if (olduser[0] != '\0') {
129 #if defined(__FreeBSD__)
130 fprintf(fp_top2,"%s %qu %qu %qu\n",olduser,tnbytes,tnacc,tnelap);
131 #elif defined(__alpha) || __ALPHA
132 fprintf(fp_top2,"%s %ld %ld %ld\n",olduser,tnbytes,tnacc,tnelap);
133 #else
134 fprintf(fp_top2,"%s %lld %lld %lld\n",olduser,tnbytes,tnacc,tnelap);
135 #endif
136 ttnbytes=ttnbytes+tnbytes;
137 ttnacc=ttnacc+tnacc;
138 ttnelap=ttnelap+tnelap;
139 }
140 strcpy(olduser,user);
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);
149 }
150
151 if (olduser[0] != '\0') {
152 #if defined(__FreeBSD__)
153 fprintf(fp_top2,"%s %qu %qu %qu\n",olduser,tnbytes,tnacc,tnelap);
154 #elif defined(__alpha) || __ALPHA
155 fprintf(fp_top2,"%s %ld %ld %ld\n",olduser,tnbytes,tnacc,tnelap);
156 #else
157 fprintf(fp_top2,"%s %lld %lld %lld\n",olduser,tnbytes,tnacc,tnelap);
158 #endif
159 ttnbytes=ttnbytes+tnbytes;
160 ttnacc=ttnacc+tnacc;
161 ttnelap=ttnelap+tnelap;
162 }
163
164 fclose(fp_in);
165 fclose(fp_top2);
166
167 sprintf(csort,"sort -n -T '%s' -r -k 2,2 -o '%s' '%s'", TempDir, top1, top2);
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 }
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 }
181 #if defined(__FreeBSD__)
182 fprintf(fp_top1,"TOTAL %qu %qu %qu\n",ttnbytes,ttnacc,ttnelap);
183 #elif defined(__alpha) || __ALPHA
184 fprintf(fp_top1,"TOTAL %ld %ld %ld\n",ttnbytes,ttnacc,ttnelap);
185 #else
186 fprintf(fp_top1,"TOTAL %lld %lld %lld\n",ttnbytes,ttnacc,ttnelap);
187 #endif
188 fclose(fp_top1);
189
190 /*
191 * Obtem o period
192 */
193
194 strcpy(arqper,dirname);
195 strcat(arqper,"/sarg-period");
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
202 fgets(period,sizeof(period),fp_in);
203 fclose(fp_in);
204
205 if((fp_top1=fopen(top1,"r"))==NULL) {
206 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top1);
207 exit(1);
208 }
209
210 if((fp_top3=fopen(top3,"w"))==NULL) {
211 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top3);
212 exit(1);
213 }
214
215 sprintf(strip1,"%s",text[88]);
216 strip_latin(strip1);
217 fprintf(fp_top3,"%s\n",strip1);
218
219 sprintf(strip1,"%s",text[97]);
220 strip_latin(strip1);
221 fprintf(fp_top3,"%s\n",strip1);
222
223 sprintf(strip1,"%s",text[89]);
224 strip_latin(strip1);
225 fprintf(fp_top3,"%s %s\n\n",strip1,period);
226
227 sprintf(strip1,"%s",text[100]);
228 strip_latin(strip1);
229 sprintf(strip2,"%s",text[98]);
230 strip_latin(strip2);
231 sprintf(strip3,"%s",text[92]);
232 strip_latin(strip3);
233 sprintf(strip4,"%s",text[93]);
234 strip_latin(strip4);
235 sprintf(strip5,"%s",text[94]);
236 strip_latin(strip5);
237 sprintf(strip6,"%s",text[95]);
238 strip_latin(strip6);
239 sprintf(strip7,"%s",text[99]);
240 strip_latin(strip7);
241
242 fprintf(fp_top3,"%-7s %-20s %-8s %-15s %%%-6s %-10s %-10s %%%-7s\n------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",strip1,strip2,strip3,strip4,strip4,strip5,strip6,strip7);
243
244 while(fgets(warea,sizeof(warea),fp_top1))
245 {
246 if (getword(user,sizeof(user),warea,' ')<0) {
247 printf("SARG: Maybe you have a broken user in your %s file.\n",top1);
248 exit(1);
249 }
250 if (getword(nbytes,sizeof(nbytes),warea,' ')<0) {
251 printf("SARG: Maybe you have a broken number of bytes in your %s file.\n",top1);
252 exit(1);
253 }
254 if (getword(nacc,sizeof(nacc),warea,' ')<0) {
255 printf("SARG: Maybe you have a broken number of access in your %s file.\n",top1);
256 exit(1);
257 }
258 if (getword(elap,sizeof(elap),warea,' ')<0) {
259 printf("SARG: Maybe you have a broken elapsed time in your %s file.\n",top1);
260 exit(1);
261 }
262
263 if(strchr(user,'_') != NULL)
264 fixip(user);
265
266 strcpy(user2,user);
267
268 tnbytes=atol(nbytes);
269
270 if(tnbytes) {
271 perc=ttnbytes / 100;
272 perc=tnbytes / perc;
273 } else perc = 0;
274
275 if(atol(elap)) {
276 perc2=ttnelap / 100;
277 perc2=atol(elap) / perc2;
278 } else perc2 = 0;
279
280 posicao++;
281 tnelap=atol(elap);
282
283 sprintf(wperc,"%3.2f%%",perc);
284 sprintf(wperc2,"%3.2f%%",perc2);
285
286 if(strcmp(user,"TOTAL") == 0){
287 fputs("------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",fp_top3);
288 #if defined(__FreeBSD__)
289 fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",text[107]," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
290 #elif defined(__alpha) || __ALPHA
291 fprintf(fp_top3,"%-7s %20s %8ld %15s %8s %9s %10ld\n",text[107]," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
292 #else
293 fprintf(fp_top3,"%-7s %20s %8lld %15s %8s %9s %10lld\n",text[107]," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
294 #endif
295 } else {
296 #if defined(__FreeBSD__)
297 fprintf(fp_top3,"%7d %20s %8s %15s %7s %10s %10qu %7s%%\n",posicao,user2,nacc,fixnum(tnbytes,1),wperc,buildtime(tnelap),tnelap,wperc2);
298 #elif defined(__alpha) || __ALPHA
299 fprintf(fp_top3,"%7d %20s %8s %15s %7s %10s %10ld %7s%%\n",posicao,user2,nacc,fixnum(tnbytes,1),wperc,buildtime(tnelap),tnelap,wperc2);
300 #else
301 fprintf(fp_top3,"%7d %20s %8s %15s %7s %10s %10lld %7s\n",posicao,user2,nacc,fixnum(tnbytes,1),wperc,buildtime(tnelap),tnelap,wperc2);
302 #endif
303 }
304 }
305
306 if(ttnbytes) {
307 tnbytes=ttnbytes / totuser;
308 } else tnbytes=0;
309
310 sprintf(strip1,"%s",text[96]);
311 strip_latin(strip1);
312 #if defined(__FreeBSD__)
313 fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser);
314 #elif defined(__alpha) || __ALPHA
315 fprintf(fp_top3,"%-7s %20s %8ld %15s %8s %9s %10ld\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser);
316 #else
317 fprintf(fp_top3,"%-7s %20s %8lld %15s %8s %9s %10lld\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser);
318 #endif
319
320 fclose(fp_top1);
321 unlink(top1);
322
323 t = time(NULL);
324 local = localtime(&t);
325 fprintf(fp_top3, "\n%s\n", asctime(local));
326
327 fclose(fp_top3);
328
329 if(strcmp(email,"stdout") == 0) {
330 if((fp_top3=fopen(top3,"r"))==NULL) {
331 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top3);
332 exit(1);
333 }
334
335 while(fgets(buf,sizeof(buf),fp_top3)!=NULL)
336 printf("%s",buf);
337 } else {
338 sprintf(buf,"'%s' -s 'SARG %s, %s' '%s' <'%s'",MailUtility,text[55],asctime(local),email,top3);
339 cstatus=system(buf);
340 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
341 fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus));
342 fprintf(stderr, "SARG: command: %s\n",buf);
343 exit(1);
344 }
345 }
346
347 sprintf(csort,"rm -rf '%s/sarg'",TempDir);
348 system(csort);
349
350 return (0);
351 }