]>
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
];
48 struct getwordstruct gwarea
;
49 struct generalitemstruct item
;
56 strcat(wger
,"/sarg-general");
58 strcat(top2
,"/top.tmp");
59 strcat(top3
,"/report");
60 strcat(tusr
,"/sarg-users");
62 if((fp_in
=fopen(wger
,"r"))==NULL
) {
63 debuga(_("(email) Cannot open file %s\n"),wger
);
67 if((fp_top1
=fopen(top1
,"w"))==NULL
) {
68 debuga(_("(email) Cannot open file %s\n"),top1
);
72 if((fp_top2
=fopen(top2
,"w"))==NULL
) {
73 debuga(_("(email) Cannot open file %s\n"),top2
);
80 while(fgets(warea
,sizeof(warea
),fp_in
)) {
81 ger_read(warea
,&item
,wger
);
82 if(item
.total
) continue;
83 if(strcmp(olduser
,item
.user
) != 0)
87 if (olduser
[0] != '\0') {
88 #if defined(__FreeBSD__)
89 fprintf(fp_top2
,"%s\t%qu\t%qu\t%qu\n",olduser
,tnbytes
,tnacc
,tnelap
);
90 #elif defined(__alpha) || __ALPHA
91 fprintf(fp_top2
,"%s\t%ld\t%ld\t%ld\n",olduser
,tnbytes
,tnacc
,tnelap
);
93 fprintf(fp_top2
,"%s\t%lld\t%lld\t%lld\n",olduser
,tnbytes
,tnacc
,tnelap
);
99 strcpy(olduser
,item
.user
);
105 tnbytes
+=item
.nbytes
;
110 if (olduser
[0] != '\0') {
111 #if defined(__FreeBSD__)
112 fprintf(fp_top2
,"%s\t%qu\t%qu\t%qu\n",olduser
,tnbytes
,tnacc
,tnelap
);
113 #elif defined(__alpha) || __ALPHA
114 fprintf(fp_top2
,"%s\t%ld\t%ld\t%ld\n",olduser
,tnbytes
,tnacc
,tnelap
);
116 fprintf(fp_top2
,"%s\t%lld\t%lld\t%lld\n",olduser
,tnbytes
,tnacc
,tnelap
);
126 sprintf(csort
,"sort -n -T \"%s\" -r -k 2,2 -o \"%s\" \"%s\"", TempDir
, top1
, top2
);
127 cstatus
=system(csort
);
128 if (!WIFEXITED(cstatus
) || WEXITSTATUS(cstatus
)) {
129 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus
));
130 debuga(_("sort command: %s\n"),csort
);
136 if((fp_top1
=fopen(top1
,"a"))==NULL
) {
137 debuga(_("(email) Cannot open file %s\n"),top1
);
140 #if defined(__FreeBSD__)
141 fprintf(fp_top1
,"TOTAL\t%qu\t%qu\t%qu\n",ttnbytes
,ttnacc
,ttnelap
);
142 #elif defined(__alpha) || __ALPHA
143 fprintf(fp_top1
,"TOTAL\t%ld\t%ld\t%ld\n",ttnbytes
,ttnacc
,ttnelap
);
145 fprintf(fp_top1
,"TOTAL\t%lld\t%lld\t%lld\n",ttnbytes
,ttnacc
,ttnelap
);
153 strcpy(arqper
,dirname
);
154 strcat(arqper
,"/sarg-period");
156 if ((fp_in
= fopen(arqper
, "r")) == 0){
157 debuga(_("(email) Cannot open file %s\n"),arqper
);
161 if (!fgets(period
,sizeof(period
),fp_in
)) {
162 debuga(_("(email) read error in %s\n"),arqper
);
167 if((fp_top1
=fopen(top1
,"r"))==NULL
) {
168 debuga(_("(email) Cannot open file %s\n"),top1
);
172 if((fp_top3
=fopen(top3
,"w"))==NULL
) {
173 debuga(_("(email) Cannot open file %s\n"),top3
);
177 strcpy(strip1
,_("Squid User Access Report"));
179 fprintf(fp_top3
,"%s\n",strip1
);
181 strcpy(strip1
,_("Decreasing Access (bytes)"));
183 fprintf(fp_top3
,"%s\n",strip1
);
185 strcpy(strip1
,_("Period"));
187 fprintf(fp_top3
,"%s %s\n\n",strip1
,period
);
189 strcpy(strip1
,_("NUM"));
191 strcpy(strip2
,_("USERID"));
193 strcpy(strip3
,_("CONNECT"));
195 strcpy(strip4
,_("BYTES"));
197 strcpy(strip5
,_("ELAPSED TIME"));
199 strcpy(strip6
,_("MILISEC"));
201 strcpy(strip7
,_("TIME"));
204 fprintf(fp_top3
,"%-7s %-20s %-8s %-15s %%%-6s %-10s %-10s %%%-7s\n------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",strip1
,strip2
,strip3
,strip4
,strip4
,strip5
,strip6
,strip7
);
206 while(fgets(warea
,sizeof(warea
),fp_top1
))
208 getword_start(&gwarea
,warea
);
209 if (getword(user
,sizeof(user
),&gwarea
,'\t')<0) {
210 debuga(_("There is an invalid user ID in file %s\n"),top1
);
213 if (getword_atoll(&nbytes
,&gwarea
,'\t')<0) {
214 debuga(_("There is an invalid number of bytes in file %s\n"),top1
);
217 if (getword_atoll(&nacc
,&gwarea
,'\t')<0) {
218 debuga(_("There is an invalid number of access in file %s\n"),top1
);
221 if (getword_atoll(&elap
,&gwarea
,'\t')<0) {
222 debuga(_("There is an invalid elapsed time in file %s\n"),top1
);
226 perc
=(ttnbytes
) ? nbytes
* 100. / ttnbytes
: 0;
227 perc2
=(ttnelap
) ? elap
* 100. / ttnelap
: 0;
231 if(strcmp(user
,"TOTAL") == 0){
232 fputs("------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",fp_top3
);
233 #if defined(__FreeBSD__)
234 fprintf(fp_top3
,"%-7s %20s %8qu %15s %8s %9s %10qu\n",_("TOTAL")," ",ttnacc
,fixnum(ttnbytes
,1)," ",buildtime(ttnelap
),ttnelap
);
235 #elif defined(__alpha) || __ALPHA
236 fprintf(fp_top3
,"%-7s %20s %8ld %15s %8s %9s %10ld\n",_("TOTAL")," ",ttnacc
,fixnum(ttnbytes
,1)," ",buildtime(ttnelap
),ttnelap
);
238 fprintf(fp_top3
,"%-7s %20s %8lld %15s %8s %9s %10lld\n",_("TOTAL")," ",ttnacc
,fixnum(ttnbytes
,1)," ",buildtime(ttnelap
),ttnelap
);
241 #if defined(__FreeBSD__)
242 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
);
243 #elif defined(__alpha) || __ALPHA
244 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
);
246 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
);
251 tnbytes
=(totuser
) ? ttnbytes
/ totuser
: 0;
253 strcpy(strip1
,_("AVERAGE"));
255 #if defined(__FreeBSD__)
256 fprintf(fp_top3
,"%-7s %20s %8qu %15s %8s %9s %10qu\n",strip1
," ",ttnacc
/totuser
,fixnum(tnbytes
,1)," ",buildtime(ttnelap
/totuser
),ttnelap
/totuser
);
257 #elif defined(__alpha) || __ALPHA
258 fprintf(fp_top3
,"%-7s %20s %8ld %15s %8s %9s %10ld\n",strip1
," ",ttnacc
/totuser
,fixnum(tnbytes
,1)," ",buildtime(ttnelap
/totuser
),ttnelap
/totuser
);
260 fprintf(fp_top3
,"%-7s %20s %8lld %15s %8s %9s %10lld\n",strip1
," ",ttnacc
/totuser
,fixnum(tnbytes
,1)," ",buildtime(ttnelap
/totuser
),ttnelap
/totuser
);
267 local
= localtime(&t
);
268 fprintf(fp_top3
, "\n%s\n", asctime(local
));
272 if(strcmp(email
,"stdout") == 0) {
273 if((fp_top3
=fopen(top3
,"r"))==NULL
) {
274 debuga(_("(email) Cannot open file %s\n"),top3
);
278 while(fgets(buf
,sizeof(buf
),fp_top3
)!=NULL
)
281 sprintf(buf
,"\"%s\" -s \"SARG %s, %s\" \"%s\" <\"%s\"",MailUtility
,_("Report"),asctime(local
),email
,top3
);
283 if (!WIFEXITED(cstatus
) || WEXITSTATUS(cstatus
)) {
284 debuga(_("command return status %d\n"),WEXITSTATUS(cstatus
));
285 debuga(_("command: %s\n"),buf
);
290 if (snprintf(warea
,sizeof(warea
),"%s/sarg",TempDir
)>=sizeof(warea
)) {
291 debuga(_("Temporary directory name too long: %s\n"),warea
);