]> git.ipfire.org Git - thirdparty/sarg.git/blame - email.c
This commit was generated by cvs2svn to compensate for changes in r2,
[thirdparty/sarg.git] / email.c
CommitLineData
25697a35
GS
1/*
2 * AUTHOR: Pedro Lineu Orso orso@brturbo.com.br
3 * 1998, 2005
4 * SARG Squid Analysis Report Generator http://sarg-squid.org
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
28int geramail(const char *dirname, int debug, const char *outdir, int userip, char *email, char *TempDir)
29{
30
31 FILE *fp_in, *fp_top1, *fp_top2, *fp_top3;
32 long long int ttnbytes=0, ttnacc=0, tnacc=0;
33 long long int tnbytes=0, ttnelap=0, tnelap=0;
34 float perc=0.00;
35 float perc2=0.00;
36 int posicao=0;
37 char olduser[MAXLEN], csort[MAXLEN], periodo[MAXLEN], arqper[MAXLEN];
38 char wger[MAXLEN], top1[MAXLEN], top2[MAXLEN], top3[MAXLEN], user[MAXLEN], nacc[20], nbytes[20], url[1024], preg[MAXLEN], tusr[MAXLEN];
39 char ip[MAXLEN], hora[9], data[11], elap[15], user2[MAXLEN], wperc[8], wperc2[8];
40 char strip1[MAXLEN], strip2[MAXLEN], strip3[MAXLEN], strip4[MAXLEN], strip5[MAXLEN], strip6[MAXLEN], strip7[MAXLEN];
41 char incac[16], oucac[16];
42 int totuser=0;
43 time_t t;
44 struct tm *local;
45
46 strcpy(wger,dirname);
47 strcpy(top1,dirname);
48 strcpy(top2,dirname);
49 strcpy(top3,dirname);
50 strcpy(tusr,dirname);
51 strcat(wger,"/geral");
52 strcat(top1,"/top");
53 strcat(top2,"/top.tmp");
54 strcat(top3,"/report");
55 strcat(tusr,"/usuarios");
56
57 if((fp_in=fopen(wger,"r"))==NULL) {
58 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],wger);
59 exit(1);
60 }
61
62 if((fp_top1=fopen(top1,"w"))==NULL) {
63 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top1);
64 exit(1);
65 }
66
67 if((fp_top2=fopen(top2,"w"))==NULL) {
68 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top2);
69 exit(1);
70 }
71
72 fscanf(fp_in,"%s",user);
73 fscanf(fp_in,"%s",nacc);
74 fscanf(fp_in,"%s",nbytes);
75 fscanf(fp_in,"%s",url);
76 fscanf(fp_in,"%s",ip);
77 fscanf(fp_in,"%s",hora);
78 fscanf(fp_in,"%s",data);
79 fscanf(fp_in,"%s",elap);
80 fscanf(fp_in,"%s",incac);
81 fscanf(fp_in,"%s",oucac);
82
83 strcpy(olduser,user);
84 totuser=1;
85
86 while(!feof(fp_in))
87 {
88
89 if(strcmp(olduser,user) != 0)
90 {
91 if(strcmp(user,"TOTAL") != 0)
92 totuser++;
93
94#if defined(__FreeBSD__)
95 sprintf(preg,"%s %15qu %15qu %15qu\n",olduser,tnbytes,tnacc,tnelap);
96#elif defined(__alpha) || __ALPHA
97 sprintf(preg,"%s %15ld %15ld %15ld\n",olduser,tnbytes,tnacc,tnelap);
98#else
99 sprintf(preg,"%s %15lld %15lld %15lld\n",olduser,tnbytes,tnacc,tnelap);
100#endif
101 fputs(preg,fp_top2);
102 strcpy(olduser,user);
103 ttnbytes=ttnbytes+tnbytes;
104 ttnacc=ttnacc+tnacc;
105 ttnelap=ttnelap+tnelap;
106 tnbytes=0;
107 tnacc=0;
108 tnelap=0;
109 }
110
111 tnbytes=tnbytes+atol(nbytes);
112 tnacc=tnacc+atol(nacc);
113 tnelap=tnelap+atol(elap);
114
115 fscanf(fp_in,"%s",user);
116 fscanf(fp_in,"%s",nacc);
117 fscanf(fp_in,"%s",nbytes);
118 fscanf(fp_in,"%s",url);
119 fscanf(fp_in,"%s",ip);
120 fscanf(fp_in,"%s",hora);
121 fscanf(fp_in,"%s",data);
122 fscanf(fp_in,"%s",elap);
123 fscanf(fp_in,"%s",incac);
124 fscanf(fp_in,"%s",oucac);
125
126 if(strcmp(user,"TOTAL") == 0)
127 continue;
128 }
129#if defined(__FreeBSD__)
130 sprintf(preg,"%s %15qu %15qu %15qu\n",olduser,tnbytes,tnacc,tnelap);
131#elif defined(__alpha) || __ALPHA
132 sprintf(preg,"%s %15ld %15ld %15ld\n",olduser,tnbytes,tnacc,tnelap);
133#else
134 sprintf(preg,"%s %15lld %15lld %15lld\n",olduser,tnbytes,tnacc,tnelap);
135#endif
136 fputs(preg,fp_top2);
137 ttnbytes=ttnbytes+tnbytes;
138 ttnacc=ttnacc+tnacc;
139 ttnelap=ttnelap+tnelap;
140
141#if defined(__FreeBSD__)
142 sprintf(preg,"TOTAL %15qu %15qu %15qu\n",ttnbytes,ttnacc,ttnelap);
143#elif defined(__alpha) || __ALPHA
144 sprintf(preg,"TOTAL %15ld %15ld %15ld\n",ttnbytes,ttnacc,ttnelap);
145#else
146 sprintf(preg,"TOTAL %15lld %15lld %15lld\n",ttnbytes,ttnacc,ttnelap);
147#endif
148
149 fclose(fp_in);
150 fclose(fp_top2);
151
152 sprintf(csort,"sort -n -T %s -r -k 2,2 -o %s %s", TempDir, top1, top2);
153 system(csort);
154
155 unlink(top2);
156
157 if((fp_top1=fopen(top1,"a"))==NULL) {
158 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top1);
159 exit(1);
160 }
161 fputs(preg,fp_top1);
162 fclose(fp_top1);
163
164 if((fp_top1=fopen(top1,"r"))==NULL) {
165 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top1);
166 exit(1);
167 }
168
169 if((fp_top3=fopen(top3,"w"))==NULL) {
170 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top3);
171 exit(1);
172 }
173
174 /*
175 * Obtem o periodo
176 */
177
178 strcpy(arqper,dirname);
179 strcat(arqper,"/periodo");
180
181 if ((fp_in = fopen(arqper, "r")) == 0){
182 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],arqper);
183 exit(1);
184 }
185
186 fgets(periodo,sizeof(periodo),fp_in);
187 fclose(fp_in);
188
189 sprintf(strip1,"%s",text[88]);
190 strip_latin(strip1);
191 sprintf(preg,"%s\n",strip1);
192 fputs(preg,fp_top3);
193
194 sprintf(strip1,"%s",text[97]);
195 strip_latin(strip1);
196 sprintf(preg,"%s\n",strip1);
197 fputs(preg,fp_top3);
198
199 sprintf(strip1,"%s",text[89]);
200 strip_latin(strip1);
201 sprintf(preg,"%s %s\n\n",strip1,periodo);
202 fputs(preg,fp_top3);
203
204 sprintf(strip1,"%s",text[100]);
205 strip_latin(strip1);
206 sprintf(strip2,"%s",text[98]);
207 strip_latin(strip2);
208 sprintf(strip3,"%s",text[92]);
209 strip_latin(strip3);
210 sprintf(strip4,"%s",text[93]);
211 strip_latin(strip4);
212 sprintf(strip5,"%s",text[94]);
213 strip_latin(strip5);
214 sprintf(strip6,"%s",text[95]);
215 strip_latin(strip6);
216 sprintf(strip7,"%s",text[99]);
217 strip_latin(strip7);
218
219 sprintf(preg,"%-7s %-20s %-8s %-15s %%%-6s %-10s %-10s %%%-7s\n------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",strip1,strip2,strip3,strip4,strip4,strip5,strip6,strip7);
220 fputs(preg,fp_top3);
221
222 fscanf(fp_top1,"%s",user);
223 fscanf(fp_top1,"%s",nbytes);
224 fscanf(fp_top1,"%s",nacc);
225 fscanf(fp_top1,"%s",elap);
226
227 while(!feof(fp_top1))
228 {
229
230 if(strstr(user,"_") != 0)
231 fixip(user);
232
233 strcpy(user2,user);
234
235 tnbytes=atol(nbytes);
236
237 if(tnbytes) {
238 perc=ttnbytes / 100;
239 perc=tnbytes / perc;
240 } else perc = 0;
241
242 if(atol(elap)) {
243 perc2=ttnelap / 100;
244 perc2=atol(elap) / perc2;
245 } else perc2 = 0;
246
247 posicao++;
248 tnelap=atol(elap);
249
250 sprintf(wperc,"%3.2f%%",perc);
251 sprintf(wperc2,"%3.2f%%",perc2);
252
253#if defined(__FreeBSD__)
254 sprintf(preg,"%7d %20s %8s %15s %7s %10s %10qu %7s%%\n",posicao,user2,nacc,fixnum(tnbytes,1),wperc,buildtime(tnelap),tnelap,wperc2);
255#elif defined(__alpha) || __ALPHA
256 sprintf(preg,"%7d %20s %8s %15s %7s %10s %10ld %7s%%\n",posicao,user2,nacc,fixnum(tnbytes,1),wperc,buildtime(tnelap),tnelap,wperc2);
257#else
258 sprintf(preg,"%7d %20s %8s %15s %7s %10s %10lld %7s\n",posicao,user2,nacc,fixnum(tnbytes,1),wperc,buildtime(tnelap),tnelap,wperc2);
259#endif
260
261 if(strstr(user,"TOTAL") != 0){
262 sprintf(preg,"------- -------------------- -------- --------------- ------- ---------- ---------- -------\n");
263 fputs(preg,fp_top3);
264#if defined(__FreeBSD__)
265 sprintf(preg,"%-7s %20s %8qu %15s %8s %9s %10qu\n",text[107]," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
266#elif defined(__alpha) || __ALPHA
267 sprintf(preg,"%-7s %20s %8ld %15s %8s %9s %10ld\n",text[107]," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
268#else
269 sprintf(preg,"%-7s %20s %8lld %15s %8s %9s %10lld\n",text[107]," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
270#endif
271 }
272
273 fputs(preg,fp_top3);
274
275 fscanf(fp_top1,"%s",user);
276 fscanf(fp_top1,"%s",nbytes);
277 fscanf(fp_top1,"%s",nacc);
278 fscanf(fp_top1,"%s",elap);
279 }
280
281 if(ttnbytes) {
282 tnbytes=ttnbytes / totuser;
283 } else tnbytes=0;
284
285 sprintf(strip1,"%s",text[96]);
286 strip_latin(strip1);
287#if defined(__FreeBSD__)
288 sprintf(preg,"%-7s %20s %8qu %15s %8s %9s %10qu\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser);
289#elif defined(__alpha) || __ALPHA
290 sprintf(preg,"%-7s %20s %8ld %15s %8s %9s %10ld\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser);
291#else
292 sprintf(preg,"%-7s %20s %8lld %15s %8s %9s %10lld\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser);
293#endif
294 fputs(preg,fp_top3);
295
296 fclose(fp_top1);
297 unlink(top1);
298
299 t = time(NULL);
300 local = localtime(&t);
301 sprintf(preg, "\n%s\n", asctime(local));
302 fputs(preg,fp_top3);
303
304 fclose(fp_top3);
305
306 if(strcmp(email,"stdout") == 0) {
307 if((fp_top3=fopen(top3,"r"))==NULL) {
308 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top3);
309 exit(1);
310 }
311
312 while(fgets(buf,sizeof(buf),fp_top3)!=NULL)
313 printf("%s",buf);
314 } else {
315 sprintf(buf,"%s -s 'SARG %s, %s' %s <%s",MailUtility,text[55],asctime(local),email,top3);
316 system(buf);
317 }
318
319 sprintf(csort,"rm -r %s/sarg_tmp",TempDir);
320 system(csort);
321
322 return (0);
323}