]>
git.ipfire.org Git - thirdparty/sarg.git/blob - email.c
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
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"
27 #include "include/defs.h"
29 int geramail(const char *dirname
, int debug
, const char *outdir
, int userip
, const char *email
, const char *TempDir
)
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;
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];
54 strcat(wger
,"/sarg-general");
56 strcat(top2
,"/top.tmp");
57 strcat(top3
,"/report");
58 strcat(tusr
,"/sarg-users");
60 if((fp_in
=fopen(wger
,"r"))==NULL
) {
61 fprintf(stderr
, "SARG: (email) %s: %s\n",text
[45],wger
);
65 if((fp_top1
=fopen(top1
,"w"))==NULL
) {
66 fprintf(stderr
, "SARG: (email) %s: %s\n",text
[45],top1
);
70 if((fp_top2
=fopen(top2
,"w"))==NULL
) {
71 fprintf(stderr
, "SARG: (email) %s: %s\n",text
[45],top2
);
78 while(fgets(warea
,sizeof(warea
),fp_in
))
80 if (getword(user
,sizeof(user
),warea
,' ')<0) {
81 printf("SARG: Maybe you have a broken user in your %s file.\n",wger
);
84 if(strcmp(user
,"TOTAL") == 0) {
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
);
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
);
95 if (getword(url
,sizeof(url
),warea
,' ')<0) {
96 printf("SARG: Maybe you have a broken url in your %s file.\n",wger
);
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
);
103 if (getword(hora
,sizeof(hora
),warea
,' ')<0) {
104 printf("SARG: Maybe you have a broken time in your %s file.\n",wger
);
107 if (getword(data
,sizeof(data
),warea
,' ')<0) {
108 printf("SARG: Maybe you have a broken date in your %s file.\n",wger
);
111 if (getword(elap
,sizeof(elap
),warea
,' ')<0) {
112 printf("SARG: Maybe you have a broken elapsed time in your %s file.\n",wger
);
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
);
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
);
124 if(strcmp(olduser
,user
) != 0)
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
);
134 fprintf(fp_top2
,"%s %lld %lld %lld\n",olduser
,tnbytes
,tnacc
,tnelap
);
136 ttnbytes
=ttnbytes
+tnbytes
;
138 ttnelap
=ttnelap
+tnelap
;
140 strcpy(olduser
,user
);
146 tnbytes
=tnbytes
+atol(nbytes
);
147 tnacc
=tnacc
+atol(nacc
);
148 tnelap
=tnelap
+atol(elap
);
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
);
157 fprintf(fp_top2
,"%s %lld %lld %lld\n",olduser
,tnbytes
,tnacc
,tnelap
);
159 ttnbytes
=ttnbytes
+tnbytes
;
161 ttnelap
=ttnelap
+tnelap
;
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
);
177 if((fp_top1
=fopen(top1
,"a"))==NULL
) {
178 fprintf(stderr
, "SARG: (email) %s: %s\n",text
[45],top1
);
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
);
186 fprintf(fp_top1
,"TOTAL %lld %lld %lld\n",ttnbytes
,ttnacc
,ttnelap
);
194 strcpy(arqper
,dirname
);
195 strcat(arqper
,"/sarg-period");
197 if ((fp_in
= fopen(arqper
, "r")) == 0){
198 fprintf(stderr
, "SARG: (email) %s: %s\n",text
[45],arqper
);
202 fgets(period
,sizeof(period
),fp_in
);
205 if((fp_top1
=fopen(top1
,"r"))==NULL
) {
206 fprintf(stderr
, "SARG: (email) %s: %s\n",text
[45],top1
);
210 if((fp_top3
=fopen(top3
,"w"))==NULL
) {
211 fprintf(stderr
, "SARG: (email) %s: %s\n",text
[45],top3
);
215 sprintf(strip1
,"%s",text
[88]);
217 fprintf(fp_top3
,"%s\n",strip1
);
219 sprintf(strip1
,"%s",text
[97]);
221 fprintf(fp_top3
,"%s\n",strip1
);
223 sprintf(strip1
,"%s",text
[89]);
225 fprintf(fp_top3
,"%s %s\n\n",strip1
,period
);
227 sprintf(strip1
,"%s",text
[100]);
229 sprintf(strip2
,"%s",text
[98]);
231 sprintf(strip3
,"%s",text
[92]);
233 sprintf(strip4
,"%s",text
[93]);
235 sprintf(strip5
,"%s",text
[94]);
237 sprintf(strip6
,"%s",text
[95]);
239 sprintf(strip7
,"%s",text
[99]);
242 fprintf(fp_top3
,"%-7s %-20s %-8s %-15s %%%-6s %-10s %-10s %%%-7s\n------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",strip1
,strip2
,strip3
,strip4
,strip4
,strip5
,strip6
,strip7
);
244 while(fgets(warea
,sizeof(warea
),fp_top1
))
246 if (getword(user
,sizeof(user
),warea
,' ')<0) {
247 printf("SARG: Maybe you have a broken user in your %s file.\n",top1
);
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
);
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
);
258 if (getword(elap
,sizeof(elap
),warea
,' ')<0) {
259 printf("SARG: Maybe you have a broken elapsed time in your %s file.\n",top1
);
263 if(strchr(user
,'_') != NULL
)
268 tnbytes
=atol(nbytes
);
277 perc2
=atol(elap
) / perc2
;
283 sprintf(wperc
,"%3.2f%%",perc
);
284 sprintf(wperc2
,"%3.2f%%",perc2
);
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
);
293 fprintf(fp_top3
,"%-7s %20s %8lld %15s %8s %9s %10lld\n",text
[107]," ",ttnacc
,fixnum(ttnbytes
,1)," ",buildtime(ttnelap
),ttnelap
);
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
);
301 fprintf(fp_top3
,"%7d %20s %8s %15s %7s %10s %10lld %7s\n",posicao
,user2
,nacc
,fixnum(tnbytes
,1),wperc
,buildtime(tnelap
),tnelap
,wperc2
);
307 tnbytes
=ttnbytes
/ totuser
;
310 sprintf(strip1
,"%s",text
[96]);
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
);
317 fprintf(fp_top3
,"%-7s %20s %8lld %15s %8s %9s %10lld\n",strip1
," ",ttnacc
/totuser
,fixnum(tnbytes
,1)," ",buildtime(ttnelap
/totuser
),ttnelap
/totuser
);
324 local
= localtime(&t
);
325 fprintf(fp_top3
, "\n%s\n", asctime(local
));
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
);
335 while(fgets(buf
,sizeof(buf
),fp_top3
)!=NULL
)
338 sprintf(buf
,"'%s' -s 'SARG %s, %s' '%s' <'%s'",MailUtility
,text
[55],asctime(local
),email
,top3
);
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
);
347 sprintf(csort
,"rm -rf '%s/sarg'",TempDir
);