]>
git.ipfire.org Git - thirdparty/sarg.git/blob - email.c
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
6 * please look at http://sarg.sourceforge.net/donations.php
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
27 #include "include/conf.h"
28 #include "include/defs.h"
30 int geramail(const char *dirname
, int debug
, const char *outdir
, int userip
, const char *email
, const char *TempDir
)
33 FILE *fp_in
, *fp_top1
, *fp_top2
, *fp_top3
;
34 long long int ttnbytes
=0, ttnacc
=0, tnacc
=0;
35 long long int tnbytes
=0, ttnelap
=0, tnelap
=0;
36 long long int nacc
, nbytes
, elap
;
40 char olduser
[MAX_USER_LEN
], csort
[MAXLEN
], period
[MAXLEN
], arqper
[MAXLEN
];
41 char wger
[MAXLEN
], top1
[MAXLEN
], top2
[MAXLEN
], top3
[MAXLEN
], user
[MAX_USER_LEN
], tusr
[MAXLEN
];
42 char strip1
[MAXLEN
], strip2
[MAXLEN
], strip3
[MAXLEN
], strip4
[MAXLEN
], strip5
[MAXLEN
], strip6
[MAXLEN
], strip7
[MAXLEN
];
49 struct getwordstruct gwarea
;
50 struct generalitemstruct item
;
57 strcat(wger
,"/sarg-general");
59 strcat(top2
,"/top.tmp");
60 strcat(top3
,"/report");
61 strcat(tusr
,"/sarg-users");
63 if((fp_in
=fopen(wger
,"r"))==NULL
) {
64 debuga(_("(email) Cannot open file %s\n"),wger
);
68 if((fp_top1
=fopen(top1
,"w"))==NULL
) {
69 debuga(_("(email) Cannot open file %s\n"),top1
);
73 if((fp_top2
=fopen(top2
,"w"))==NULL
) {
74 debuga(_("(email) Cannot open file %s\n"),top2
);
81 while(fgets(warea
,sizeof(warea
),fp_in
)) {
82 ger_read(warea
,&item
,wger
);
83 if(item
.total
) continue;
84 if(strcmp(olduser
,item
.user
) != 0)
88 if (olduser
[0] != '\0') {
89 #if defined(__FreeBSD__)
90 fprintf(fp_top2
,"%s\t%qu\t%qu\t%qu\n",olduser
,tnbytes
,tnacc
,tnelap
);
91 #elif defined(__alpha) || __ALPHA
92 fprintf(fp_top2
,"%s\t%ld\t%ld\t%ld\n",olduser
,tnbytes
,tnacc
,tnelap
);
94 fprintf(fp_top2
,"%s\t%lld\t%lld\t%lld\n",olduser
,tnbytes
,tnacc
,tnelap
);
100 strcpy(olduser
,item
.user
);
106 tnbytes
+=item
.nbytes
;
111 if (olduser
[0] != '\0') {
112 #if defined(__FreeBSD__)
113 fprintf(fp_top2
,"%s\t%qu\t%qu\t%qu\n",olduser
,tnbytes
,tnacc
,tnelap
);
114 #elif defined(__alpha) || __ALPHA
115 fprintf(fp_top2
,"%s\t%ld\t%ld\t%ld\n",olduser
,tnbytes
,tnacc
,tnelap
);
117 fprintf(fp_top2
,"%s\t%lld\t%lld\t%lld\n",olduser
,tnbytes
,tnacc
,tnelap
);
127 sprintf(csort
,"sort -n -T \"%s\" -r -k 2,2 -o \"%s\" \"%s\"", TempDir
, top1
, top2
);
128 cstatus
=system(csort
);
129 if (!WIFEXITED(cstatus
) || WEXITSTATUS(cstatus
)) {
130 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus
));
131 debuga(_("sort command: %s\n"),csort
);
137 if((fp_top1
=fopen(top1
,"a"))==NULL
) {
138 debuga(_("(email) Cannot open file %s\n"),top1
);
141 #if defined(__FreeBSD__)
142 fprintf(fp_top1
,"TOTAL\t%qu\t%qu\t%qu\n",ttnbytes
,ttnacc
,ttnelap
);
143 #elif defined(__alpha) || __ALPHA
144 fprintf(fp_top1
,"TOTAL\t%ld\t%ld\t%ld\n",ttnbytes
,ttnacc
,ttnelap
);
146 fprintf(fp_top1
,"TOTAL\t%lld\t%lld\t%lld\n",ttnbytes
,ttnacc
,ttnelap
);
154 strcpy(arqper
,dirname
);
155 strcat(arqper
,"/sarg-period");
157 if ((fp_in
= fopen(arqper
, "r")) == 0){
158 debuga(_("(email) Cannot open file %s\n"),arqper
);
162 if (!fgets(period
,sizeof(period
),fp_in
)) {
163 debuga(_("(email) read error in %s\n"),arqper
);
168 if((fp_top1
=fopen(top1
,"r"))==NULL
) {
169 debuga(_("(email) Cannot open file %s\n"),top1
);
173 if((fp_top3
=fopen(top3
,"w"))==NULL
) {
174 debuga(_("(email) Cannot open file %s\n"),top3
);
178 strcpy(strip1
,_("Squid User Access Report"));
180 fprintf(fp_top3
,"%s\n",strip1
);
182 strcpy(strip1
,_("Decreasing Access (bytes)"));
184 fprintf(fp_top3
,"%s\n",strip1
);
186 strcpy(strip1
,_("Period"));
188 fprintf(fp_top3
,"%s %s\n\n",strip1
,period
);
190 strcpy(strip1
,_("NUM"));
192 strcpy(strip2
,_("USERID"));
194 strcpy(strip3
,_("CONNECT"));
196 strcpy(strip4
,_("BYTES"));
198 strcpy(strip5
,_("ELAPSED TIME"));
200 strcpy(strip6
,_("MILISEC"));
202 strcpy(strip7
,_("TIME"));
205 fprintf(fp_top3
,"%-7s %-20s %-8s %-15s %%%-6s %-10s %-10s %%%-7s\n------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",strip1
,strip2
,strip3
,strip4
,strip4
,strip5
,strip6
,strip7
);
207 while(fgets(warea
,sizeof(warea
),fp_top1
))
209 getword_start(&gwarea
,warea
);
210 if (getword(user
,sizeof(user
),&gwarea
,'\t')<0) {
211 debuga(_("There is an invalid user ID in file %s\n"),top1
);
214 if (getword_atoll(&nbytes
,&gwarea
,'\t')<0) {
215 debuga(_("There is an invalid number of bytes in file %s\n"),top1
);
218 if (getword_atoll(&nacc
,&gwarea
,'\t')<0) {
219 debuga(_("There is an invalid number of access in file %s\n"),top1
);
222 if (getword_atoll(&elap
,&gwarea
,'\t')<0) {
223 debuga(_("There is an invalid elapsed time in file %s\n"),top1
);
227 perc
=(ttnbytes
) ? nbytes
* 100. / ttnbytes
: 0;
228 perc2
=(ttnelap
) ? elap
* 100. / ttnelap
: 0;
232 if(strcmp(user
,"TOTAL") == 0){
233 fputs("------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",fp_top3
);
234 #if defined(__FreeBSD__)
235 fprintf(fp_top3
,"%-7s %20s %8qu %15s %8s %9s %10qu\n",_("TOTAL")," ",ttnacc
,fixnum(ttnbytes
,1)," ",buildtime(ttnelap
),ttnelap
);
236 #elif defined(__alpha) || __ALPHA
237 fprintf(fp_top3
,"%-7s %20s %8ld %15s %8s %9s %10ld\n",_("TOTAL")," ",ttnacc
,fixnum(ttnbytes
,1)," ",buildtime(ttnelap
),ttnelap
);
239 fprintf(fp_top3
,"%-7s %20s %8lld %15s %8s %9s %10lld\n",_("TOTAL")," ",ttnacc
,fixnum(ttnbytes
,1)," ",buildtime(ttnelap
),ttnelap
);
242 #if defined(__FreeBSD__)
243 fprintf(fp_top3
,"%7d %20s %8lld %15s %3.2lf%% %10s %10qu %3.2lf%%\n",posicao
,user
,nacc
,fixnum(nbytes
,1),perc
,buildtime(elap
),elap
,perc2
);
244 #elif defined(__alpha) || __ALPHA
245 fprintf(fp_top3
,"%7d %20s %8lld %15s %3.2lf%% %10s %10ld %3.2lf%%\n",posicao
,user
,nacc
,fixnum(nbytes
,1),perc
,buildtime(elap
),elap
,perc2
);
247 fprintf(fp_top3
,"%7d %20s %8lld %15s %3.2lf%% %10s %10lld %3.2lf%%\n",posicao
,user
,nacc
,fixnum(nbytes
,1),perc
,buildtime(elap
),elap
,perc2
);
252 tnbytes
=(totuser
) ? ttnbytes
/ totuser
: 0;
254 strcpy(strip1
,_("AVERAGE"));
256 #if defined(__FreeBSD__)
257 fprintf(fp_top3
,"%-7s %20s %8qu %15s %8s %9s %10qu\n",strip1
," ",ttnacc
/totuser
,fixnum(tnbytes
,1)," ",buildtime(ttnelap
/totuser
),ttnelap
/totuser
);
258 #elif defined(__alpha) || __ALPHA
259 fprintf(fp_top3
,"%-7s %20s %8ld %15s %8s %9s %10ld\n",strip1
," ",ttnacc
/totuser
,fixnum(tnbytes
,1)," ",buildtime(ttnelap
/totuser
),ttnelap
/totuser
);
261 fprintf(fp_top3
,"%-7s %20s %8lld %15s %8s %9s %10lld\n",strip1
," ",ttnacc
/totuser
,fixnum(tnbytes
,1)," ",buildtime(ttnelap
/totuser
),ttnelap
/totuser
);
268 local
= localtime(&t
);
269 fprintf(fp_top3
, "\n%s\n", asctime(local
));
273 if(strcmp(email
,"stdout") == 0) {
274 if((fp_top3
=fopen(top3
,"r"))==NULL
) {
275 debuga(_("(email) Cannot open file %s\n"),top3
);
279 while(fgets(buf
,sizeof(buf
),fp_top3
)!=NULL
)
282 snprintf(buf
,sizeof(buf
),"\"%s\" -s \"SARG %s, %s\" \"%s\" <\"%s\"",MailUtility
,_("Report"),asctime(local
),email
,top3
);
284 if (!WIFEXITED(cstatus
) || WEXITSTATUS(cstatus
)) {
285 debuga(_("command return status %d\n"),WEXITSTATUS(cstatus
));
286 debuga(_("command: %s\n"),buf
);
291 if (snprintf(warea
,sizeof(warea
),"%s/sarg",TempDir
)>=sizeof(warea
)) {
292 debuga(_("Temporary directory name too long: %s\n"),warea
);