]>
git.ipfire.org Git - thirdparty/sarg.git/blob - email.c
2 * AUTHOR: Pedro Lineu Orso orso@penguintech.com.br
4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
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.
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.
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.
26 #include "include/conf.h"
28 int geramail(const char *dirname
, int debug
, const char *outdir
, int userip
, char *email
, char *TempDir
)
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;
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];
51 strcat(wger
,"/geral");
53 strcat(top2
,"/top.tmp");
54 strcat(top3
,"/report");
55 strcat(tusr
,"/users");
57 if((fp_in
=fopen(wger
,"r"))==NULL
) {
58 fprintf(stderr
, "SARG: (email) %s: %s\n",text
[45],wger
);
62 if((fp_top1
=fopen(top1
,"w"))==NULL
) {
63 fprintf(stderr
, "SARG: (email) %s: %s\n",text
[45],top1
);
67 if((fp_top2
=fopen(top2
,"w"))==NULL
) {
68 fprintf(stderr
, "SARG: (email) %s: %s\n",text
[45],top2
);
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
);
89 if(strcmp(olduser
,user
) != 0)
91 if(strcmp(user
,"TOTAL") != 0)
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
);
99 sprintf(preg
,"%s %15lld %15lld %15lld\n",olduser
,tnbytes
,tnacc
,tnelap
);
102 strcpy(olduser
,user
);
103 ttnbytes
=ttnbytes
+tnbytes
;
105 ttnelap
=ttnelap
+tnelap
;
111 tnbytes
=tnbytes
+atol(nbytes
);
112 tnacc
=tnacc
+atol(nacc
);
113 tnelap
=tnelap
+atol(elap
);
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
);
126 if(strcmp(user
,"TOTAL") == 0)
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
);
134 sprintf(preg
,"%s %15lld %15lld %15lld\n",olduser
,tnbytes
,tnacc
,tnelap
);
137 ttnbytes
=ttnbytes
+tnbytes
;
139 ttnelap
=ttnelap
+tnelap
;
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
);
146 sprintf(preg
,"TOTAL %15lld %15lld %15lld\n",ttnbytes
,ttnacc
,ttnelap
);
152 sprintf(csort
,"sort -n -T %s -r -k 2,2 -o '%s' '%s'", TempDir
, top1
, top2
);
157 if((fp_top1
=fopen(top1
,"a"))==NULL
) {
158 fprintf(stderr
, "SARG: (email) %s: %s\n",text
[45],top1
);
164 if((fp_top1
=fopen(top1
,"r"))==NULL
) {
165 fprintf(stderr
, "SARG: (email) %s: %s\n",text
[45],top1
);
169 if((fp_top3
=fopen(top3
,"w"))==NULL
) {
170 fprintf(stderr
, "SARG: (email) %s: %s\n",text
[45],top3
);
178 strcpy(arqper
,dirname
);
179 strcat(arqper
,"/periodo");
181 if ((fp_in
= fopen(arqper
, "r")) == 0){
182 fprintf(stderr
, "SARG: (email) %s: %s\n",text
[45],arqper
);
186 fgets(periodo
,sizeof(periodo
),fp_in
);
189 sprintf(strip1
,"%s",text
[88]);
191 sprintf(preg
,"%s\n",strip1
);
194 sprintf(strip1
,"%s",text
[97]);
196 sprintf(preg
,"%s\n",strip1
);
199 sprintf(strip1
,"%s",text
[89]);
201 sprintf(preg
,"%s %s\n\n",strip1
,periodo
);
204 sprintf(strip1
,"%s",text
[100]);
206 sprintf(strip2
,"%s",text
[98]);
208 sprintf(strip3
,"%s",text
[92]);
210 sprintf(strip4
,"%s",text
[93]);
212 sprintf(strip5
,"%s",text
[94]);
214 sprintf(strip6
,"%s",text
[95]);
216 sprintf(strip7
,"%s",text
[99]);
219 sprintf(preg
,"%-7s %-20s %-8s %-15s %%%-6s %-10s %-10s %%%-7s\n------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",strip1
,strip2
,strip3
,strip4
,strip4
,strip5
,strip6
,strip7
);
222 fscanf(fp_top1
,"%s",user
);
223 fscanf(fp_top1
,"%s",nbytes
);
224 fscanf(fp_top1
,"%s",nacc
);
225 fscanf(fp_top1
,"%s",elap
);
227 while(!feof(fp_top1
))
230 if(strstr(user
,"_") != 0)
235 tnbytes
=atol(nbytes
);
244 perc2
=atol(elap
) / perc2
;
250 sprintf(wperc
,"%3.2f%%",perc
);
251 sprintf(wperc2
,"%3.2f%%",perc2
);
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
);
258 sprintf(preg
,"%7d %20s %8s %15s %7s %10s %10lld %7s\n",posicao
,user2
,nacc
,fixnum(tnbytes
,1),wperc
,buildtime(tnelap
),tnelap
,wperc2
);
261 if(strstr(user
,"TOTAL") != 0){
262 sprintf(preg
,"------- -------------------- -------- --------------- ------- ---------- ---------- -------\n");
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
);
269 sprintf(preg
,"%-7s %20s %8lld %15s %8s %9s %10lld\n",text
[107]," ",ttnacc
,fixnum(ttnbytes
,1)," ",buildtime(ttnelap
),ttnelap
);
275 fscanf(fp_top1
,"%s",user
);
276 fscanf(fp_top1
,"%s",nbytes
);
277 fscanf(fp_top1
,"%s",nacc
);
278 fscanf(fp_top1
,"%s",elap
);
282 tnbytes
=ttnbytes
/ totuser
;
285 sprintf(strip1
,"%s",text
[96]);
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
);
292 sprintf(preg
,"%-7s %20s %8lld %15s %8s %9s %10lld\n",strip1
," ",ttnacc
/totuser
,fixnum(tnbytes
,1)," ",buildtime(ttnelap
/totuser
),ttnelap
/totuser
);
300 local
= localtime(&t
);
301 sprintf(preg
, "\n%s\n", asctime(local
));
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
);
312 while(fgets(buf
,sizeof(buf
),fp_top3
)!=NULL
)
315 sprintf(buf
,"%s -s 'SARG %s, %s' %s <%s",MailUtility
,text
[55],asctime(local
),email
,top3
);
319 sprintf(csort
,"rm -r %s/sarg_tmp",TempDir
);