]> git.ipfire.org Git - thirdparty/sarg.git/blame - email.c
Append \n at the end of every debuga
[thirdparty/sarg.git] / email.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
1164c474 3 * 1998, 2010
25697a35
GS
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
1164c474
FM
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
25697a35
GS
9 * ---------------------------------------------------------------------
10 *
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.
15 *
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.
20 *
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.
24 *
25 */
26
27#include "include/conf.h"
5f3cfd1d 28#include "include/defs.h"
25697a35 29
32e71fa4 30int geramail(const char *dirname, int debug, const char *outdir, int userip, const char *email, const char *TempDir)
25697a35
GS
31{
32
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;
2240dcea
FM
36 long long int nacc, nbytes, elap;
37 double perc=0.00;
38 double perc2=0.00;
25697a35 39 int posicao=0;
2240dcea
FM
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];
25697a35 42 char strip1[MAXLEN], strip2[MAXLEN], strip3[MAXLEN], strip4[MAXLEN], strip5[MAXLEN], strip6[MAXLEN], strip7[MAXLEN];
25697a35
GS
43 int totuser=0;
44 time_t t;
45 struct tm *local;
456d78a5 46 int cstatus;
fb7c5f27 47 char warea[MAXLEN];
9c7c6346 48 struct getwordstruct gwarea;
2240dcea 49 struct generalitemstruct item;
25697a35
GS
50
51 strcpy(wger,dirname);
52 strcpy(top1,dirname);
53 strcpy(top2,dirname);
54 strcpy(top3,dirname);
55 strcpy(tusr,dirname);
d6e703cc 56 strcat(wger,"/sarg-general");
25697a35
GS
57 strcat(top1,"/top");
58 strcat(top2,"/top.tmp");
59 strcat(top3,"/report");
d6e703cc 60 strcat(tusr,"/sarg-users");
25697a35
GS
61
62 if((fp_in=fopen(wger,"r"))==NULL) {
10210234 63 debuga(_("(email) Cannot open file: %s\n"),wger);
25697a35
GS
64 exit(1);
65 }
66
67 if((fp_top1=fopen(top1,"w"))==NULL) {
10210234 68 debuga(_("(email) Cannot open file: %s\n"),top1);
25697a35
GS
69 exit(1);
70 }
71
72 if((fp_top2=fopen(top2,"w"))==NULL) {
10210234 73 debuga(_("SARG: (email) Cannot open file: %s\n"),top2);
25697a35
GS
74 exit(1);
75 }
76
fb7c5f27
FM
77 olduser[0]='\0';
78 totuser=0;
79
9c7c6346 80 while(fgets(warea,sizeof(warea),fp_in)) {
2240dcea
FM
81 ger_read(warea,&item,wger);
82 if(item.total) continue;
83 if(strcmp(olduser,item.user) != 0)
25697a35 84 {
fb7c5f27 85 totuser++;
25697a35 86
fb7c5f27 87 if (olduser[0] != '\0') {
25697a35 88#if defined(__FreeBSD__)
120d768c 89 fprintf(fp_top2,"%s\t%qu\t%qu\t%qu\n",olduser,tnbytes,tnacc,tnelap);
25697a35 90#elif defined(__alpha) || __ALPHA
120d768c 91 fprintf(fp_top2,"%s\t%ld\t%ld\t%ld\n",olduser,tnbytes,tnacc,tnelap);
25697a35 92#else
120d768c 93 fprintf(fp_top2,"%s\t%lld\t%lld\t%lld\n",olduser,tnbytes,tnacc,tnelap);
25697a35 94#endif
2240dcea
FM
95 ttnbytes+=tnbytes;
96 ttnacc+=tnacc;
97 ttnelap+=tnelap;
fb7c5f27 98 }
2240dcea 99 strcpy(olduser,item.user);
25697a35
GS
100 tnbytes=0;
101 tnacc=0;
102 tnelap=0;
103 }
104
2240dcea
FM
105 tnbytes+=item.nbytes;
106 tnacc+=item.nacc;
107 tnelap+=item.nelap;
25697a35 108 }
fb7c5f27
FM
109
110 if (olduser[0] != '\0') {
25697a35 111#if defined(__FreeBSD__)
120d768c 112 fprintf(fp_top2,"%s\t%qu\t%qu\t%qu\n",olduser,tnbytes,tnacc,tnelap);
25697a35 113#elif defined(__alpha) || __ALPHA
120d768c 114 fprintf(fp_top2,"%s\t%ld\t%ld\t%ld\n",olduser,tnbytes,tnacc,tnelap);
25697a35 115#else
120d768c 116 fprintf(fp_top2,"%s\t%lld\t%lld\t%lld\n",olduser,tnbytes,tnacc,tnelap);
25697a35 117#endif
2240dcea
FM
118 ttnbytes+=tnbytes;
119 ttnacc+=tnacc;
120 ttnelap+=tnelap;
fb7c5f27 121 }
25697a35 122
25697a35
GS
123 fclose(fp_in);
124 fclose(fp_top2);
125
9a2efbd0 126 sprintf(csort,"sort -n -T \"%s\" -r -k 2,2 -o \"%s\" \"%s\"", TempDir, top1, top2);
456d78a5
FM
127 cstatus=system(csort);
128 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
10210234
FM
129 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
130 debuga(_("sort command: %s\n"),csort);
456d78a5
FM
131 exit(1);
132 }
25697a35
GS
133
134 unlink(top2);
135
136 if((fp_top1=fopen(top1,"a"))==NULL) {
c36c7384 137 fprintf(stderr, "SARG: (email) %s: %s\n",_("Cannot open file"),top1);
25697a35
GS
138 exit(1);
139 }
354c1a68 140#if defined(__FreeBSD__)
120d768c 141 fprintf(fp_top1,"TOTAL\t%qu\t%qu\t%qu\n",ttnbytes,ttnacc,ttnelap);
354c1a68 142#elif defined(__alpha) || __ALPHA
120d768c 143 fprintf(fp_top1,"TOTAL\t%ld\t%ld\t%ld\n",ttnbytes,ttnacc,ttnelap);
354c1a68 144#else
120d768c 145 fprintf(fp_top1,"TOTAL\t%lld\t%lld\t%lld\n",ttnbytes,ttnacc,ttnelap);
354c1a68 146#endif
25697a35
GS
147 fclose(fp_top1);
148
fb7c5f27
FM
149 /*
150 * Obtem o period
151 */
25697a35
GS
152
153 strcpy(arqper,dirname);
d6e703cc 154 strcat(arqper,"/sarg-period");
25697a35
GS
155
156 if ((fp_in = fopen(arqper, "r")) == 0){
c36c7384 157 fprintf(stderr, "SARG: (email) %s: %s\n",_("Cannot open file"),arqper);
25697a35
GS
158 exit(1);
159 }
160
05b90947
FM
161 if (!fgets(period,sizeof(period),fp_in)) {
162 fprintf(stderr,"SARG: (email) read error in %s\n",arqper);
163 exit(1);
164 }
25697a35
GS
165 fclose(fp_in);
166
fb7c5f27 167 if((fp_top1=fopen(top1,"r"))==NULL) {
c36c7384 168 fprintf(stderr, "SARG: (email) %s: %s\n",_("Cannot open file"),top1);
fb7c5f27
FM
169 exit(1);
170 }
171
172 if((fp_top3=fopen(top3,"w"))==NULL) {
c36c7384 173 fprintf(stderr, "SARG: (email) %s: %s\n",_("Cannot open file"),top3);
fb7c5f27
FM
174 exit(1);
175 }
176
c36c7384 177 strcpy(strip1,_("Squid User Access Report"));
25697a35 178 strip_latin(strip1);
354c1a68 179 fprintf(fp_top3,"%s\n",strip1);
25697a35 180
c36c7384 181 strcpy(strip1,_("Decreasing Access (bytes)"));
25697a35 182 strip_latin(strip1);
354c1a68 183 fprintf(fp_top3,"%s\n",strip1);
25697a35 184
c36c7384 185 strcpy(strip1,_("Period"));
25697a35 186 strip_latin(strip1);
354c1a68 187 fprintf(fp_top3,"%s %s\n\n",strip1,period);
25697a35 188
c36c7384 189 strcpy(strip1,_("NUM"));
25697a35 190 strip_latin(strip1);
c36c7384 191 strcpy(strip2,_("USERID"));
25697a35 192 strip_latin(strip2);
c36c7384 193 strcpy(strip3,_("CONNECT"));
25697a35 194 strip_latin(strip3);
c36c7384 195 strcpy(strip4,_("BYTES"));
25697a35 196 strip_latin(strip4);
c36c7384 197 strcpy(strip5,_("ELAPSED TIME"));
25697a35 198 strip_latin(strip5);
c36c7384 199 strcpy(strip6,_("MILISEC"));
25697a35 200 strip_latin(strip6);
c36c7384 201 strcpy(strip7,_("TIME"));
25697a35
GS
202 strip_latin(strip7);
203
354c1a68 204 fprintf(fp_top3,"%-7s %-20s %-8s %-15s %%%-6s %-10s %-10s %%%-7s\n------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",strip1,strip2,strip3,strip4,strip4,strip5,strip6,strip7);
25697a35 205
fb7c5f27 206 while(fgets(warea,sizeof(warea),fp_top1))
25697a35 207 {
9c7c6346
FM
208 getword_start(&gwarea,warea);
209 if (getword(user,sizeof(user),&gwarea,'\t')<0) {
10210234 210 debuga(_("There is an invalid user ID in file %s\n"),top1);
fb7c5f27
FM
211 exit(1);
212 }
2240dcea 213 if (getword_atoll(&nbytes,&gwarea,'\t')<0) {
10210234 214 debuga(_("There is an invalid number of bytes in file %s\n"),top1);
fb7c5f27
FM
215 exit(1);
216 }
2240dcea 217 if (getword_atoll(&nacc,&gwarea,'\t')<0) {
10210234 218 debuga(_("There is an invalid number of access in file %s\n"),top1);
fb7c5f27
FM
219 exit(1);
220 }
2240dcea 221 if (getword_atoll(&elap,&gwarea,'\t')<0) {
10210234 222 debuga(_("There is an invalid elapsed time in file %s\n"),top1);
fb7c5f27
FM
223 exit(1);
224 }
25697a35 225
2240dcea
FM
226 perc=(ttnbytes) ? nbytes * 100. / ttnbytes : 0;
227 perc2=(ttnelap) ? elap * 100. / ttnelap : 0;
25697a35
GS
228
229 posicao++;
25697a35 230
fb7c5f27 231 if(strcmp(user,"TOTAL") == 0){
354c1a68 232 fputs("------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",fp_top3);
25697a35 233#if defined(__FreeBSD__)
c36c7384 234 fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",_("TOTAL")," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
25697a35 235#elif defined(__alpha) || __ALPHA
c36c7384 236 fprintf(fp_top3,"%-7s %20s %8ld %15s %8s %9s %10ld\n",_("TOTAL")," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
25697a35 237#else
c36c7384 238 fprintf(fp_top3,"%-7s %20s %8lld %15s %8s %9s %10lld\n",_("TOTAL")," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
25697a35 239#endif
354c1a68 240 } else {
25697a35 241#if defined(__FreeBSD__)
f2ec8c75 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);
25697a35 243#elif defined(__alpha) || __ALPHA
f2ec8c75 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);
25697a35 245#else
f2ec8c75 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);
25697a35
GS
247#endif
248 }
25697a35
GS
249 }
250
2240dcea 251 tnbytes=(totuser) ? ttnbytes / totuser : 0;
25697a35 252
c36c7384 253 strcpy(strip1,_("AVERAGE"));
25697a35
GS
254 strip_latin(strip1);
255#if defined(__FreeBSD__)
354c1a68 256 fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser);
25697a35 257#elif defined(__alpha) || __ALPHA
354c1a68 258 fprintf(fp_top3,"%-7s %20s %8ld %15s %8s %9s %10ld\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser);
25697a35 259#else
354c1a68 260 fprintf(fp_top3,"%-7s %20s %8lld %15s %8s %9s %10lld\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser);
25697a35 261#endif
25697a35
GS
262
263 fclose(fp_top1);
264 unlink(top1);
265
266 t = time(NULL);
267 local = localtime(&t);
354c1a68 268 fprintf(fp_top3, "\n%s\n", asctime(local));
25697a35
GS
269
270 fclose(fp_top3);
271
272 if(strcmp(email,"stdout") == 0) {
273 if((fp_top3=fopen(top3,"r"))==NULL) {
10210234 274 debuga(_("(email) Cannot open file: %s\n"),top3);
25697a35
GS
275 exit(1);
276 }
277
278 while(fgets(buf,sizeof(buf),fp_top3)!=NULL)
f2ec8c75 279 fputs(buf,stdout);
25697a35 280 } else {
c36c7384 281 sprintf(buf,"\"%s\" -s \"SARG %s, %s\" \"%s\" <\"%s\"",MailUtility,_("Report"),asctime(local),email,top3);
456d78a5
FM
282 cstatus=system(buf);
283 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
10210234
FM
284 debuga(_("command return status %d\n"),WEXITSTATUS(cstatus));
285 debuga(_("command: %s\n"),buf);
456d78a5
FM
286 exit(1);
287 }
288 }
fb7c5f27 289
51465d08 290 if (snprintf(warea,sizeof(warea),"%s/sarg",TempDir)>=sizeof(warea)) {
10210234 291 debuga(_("Temporary directory name too long: %s\n"),warea);
05b90947
FM
292 exit(1);
293 }
51465d08 294 unlinkdir(warea,0);
25697a35
GS
295
296 return (0);
297}