]> git.ipfire.org Git - thirdparty/sarg.git/blame - email.c
Removed Pedro Orso's e-mail as per his request
[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;
36 float perc=0.00;
37 float perc2=0.00;
38 int posicao=0;
d6e703cc 39 char olduser[MAXLEN], csort[MAXLEN], period[MAXLEN], arqper[MAXLEN];
fb7c5f27
FM
40 char wger[MAXLEN], top1[MAXLEN], top2[MAXLEN], top3[MAXLEN], user[MAXLEN], nacc[20], nbytes[20], url[MAXLEN], tusr[MAXLEN];
41 char ip[MAXLEN], hora[9], data[15], elap[16], user2[MAXLEN], wperc[8], wperc2[8];
25697a35 42 char strip1[MAXLEN], strip2[MAXLEN], strip3[MAXLEN], strip4[MAXLEN], strip5[MAXLEN], strip6[MAXLEN], strip7[MAXLEN];
fb7c5f27 43 char incac[30], oucac[30];
25697a35
GS
44 int totuser=0;
45 time_t t;
46 struct tm *local;
456d78a5 47 int cstatus;
fb7c5f27 48 char warea[MAXLEN];
9c7c6346 49 struct getwordstruct gwarea;
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) {
63 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],wger);
64 exit(1);
65 }
66
67 if((fp_top1=fopen(top1,"w"))==NULL) {
68 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top1);
69 exit(1);
70 }
71
72 if((fp_top2=fopen(top2,"w"))==NULL) {
73 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top2);
74 exit(1);
75 }
76
fb7c5f27
FM
77 olduser[0]='\0';
78 totuser=0;
79
9c7c6346
FM
80 while(fgets(warea,sizeof(warea),fp_in)) {
81 getword_start(&gwarea,warea);
82 if (getword(user,sizeof(user),&gwarea,'\t')<0) {
fb7c5f27
FM
83 printf("SARG: Maybe you have a broken user in your %s file.\n",wger);
84 exit(1);
85 }
86 if(strcmp(user,"TOTAL") == 0) {
87 continue;
88 }
9c7c6346 89 if (getword(nacc,sizeof(nacc),&gwarea,'\t')<0) {
fb7c5f27
FM
90 printf("SARG: Maybe you have a broken number of access in your %s file.\n",wger);
91 exit(1);
92 }
9c7c6346 93 if (getword(nbytes,sizeof(nbytes),&gwarea,'\t')<0) {
fb7c5f27
FM
94 printf("SARG: Maybe you have a broken number of bytes in your %s file.\n",wger);
95 exit(1);
96 }
9c7c6346 97 if (getword(url,sizeof(url),&gwarea,'\t')<0) {
fb7c5f27
FM
98 printf("SARG: Maybe you have a broken url in your %s file.\n",wger);
99 exit(1);
100 }
9c7c6346 101 if (getword(ip,sizeof(ip),&gwarea,'\t')<0) {
fb7c5f27
FM
102 printf("SARG: Maybe you have a broken source IP address in your %s file.\n",wger);
103 exit(1);
104 }
9c7c6346 105 if (getword(hora,sizeof(hora),&gwarea,'\t')<0) {
fb7c5f27
FM
106 printf("SARG: Maybe you have a broken time in your %s file.\n",wger);
107 exit(1);
108 }
9c7c6346 109 if (getword(data,sizeof(data),&gwarea,'\t')<0) {
fb7c5f27
FM
110 printf("SARG: Maybe you have a broken date in your %s file.\n",wger);
111 exit(1);
112 }
9c7c6346 113 if (getword(elap,sizeof(elap),&gwarea,'\t')<0) {
fb7c5f27
FM
114 printf("SARG: Maybe you have a broken elapsed time in your %s file.\n",wger);
115 exit(1);
116 }
9c7c6346 117 if (getword(incac,sizeof(incac),&gwarea,'\t')<0) {
fb7c5f27
FM
118 printf("SARG: Maybe you have a broken in cache column in your %s file.\n",wger);
119 exit(1);
120 }
9c7c6346 121 if (getword(oucac,sizeof(oucac),&gwarea,0)<0) {
fb7c5f27
FM
122 printf("SARG: Maybe you have a broken not in cache column in your %s file.\n",wger);
123 exit(1);
124 }
25697a35
GS
125
126 if(strcmp(olduser,user) != 0)
127 {
fb7c5f27 128 totuser++;
25697a35 129
fb7c5f27 130 if (olduser[0] != '\0') {
25697a35 131#if defined(__FreeBSD__)
120d768c 132 fprintf(fp_top2,"%s\t%qu\t%qu\t%qu\n",olduser,tnbytes,tnacc,tnelap);
25697a35 133#elif defined(__alpha) || __ALPHA
120d768c 134 fprintf(fp_top2,"%s\t%ld\t%ld\t%ld\n",olduser,tnbytes,tnacc,tnelap);
25697a35 135#else
120d768c 136 fprintf(fp_top2,"%s\t%lld\t%lld\t%lld\n",olduser,tnbytes,tnacc,tnelap);
25697a35 137#endif
fb7c5f27
FM
138 ttnbytes=ttnbytes+tnbytes;
139 ttnacc=ttnacc+tnacc;
140 ttnelap=ttnelap+tnelap;
141 }
142 strcpy(olduser,user);
25697a35
GS
143 tnbytes=0;
144 tnacc=0;
145 tnelap=0;
146 }
147
148 tnbytes=tnbytes+atol(nbytes);
149 tnacc=tnacc+atol(nacc);
150 tnelap=tnelap+atol(elap);
25697a35 151 }
fb7c5f27
FM
152
153 if (olduser[0] != '\0') {
25697a35 154#if defined(__FreeBSD__)
120d768c 155 fprintf(fp_top2,"%s\t%qu\t%qu\t%qu\n",olduser,tnbytes,tnacc,tnelap);
25697a35 156#elif defined(__alpha) || __ALPHA
120d768c 157 fprintf(fp_top2,"%s\t%ld\t%ld\t%ld\n",olduser,tnbytes,tnacc,tnelap);
25697a35 158#else
120d768c 159 fprintf(fp_top2,"%s\t%lld\t%lld\t%lld\n",olduser,tnbytes,tnacc,tnelap);
25697a35 160#endif
fb7c5f27
FM
161 ttnbytes=ttnbytes+tnbytes;
162 ttnacc=ttnacc+tnacc;
163 ttnelap=ttnelap+tnelap;
164 }
25697a35 165
25697a35
GS
166 fclose(fp_in);
167 fclose(fp_top2);
168
9a2efbd0 169 sprintf(csort,"sort -n -T \"%s\" -r -k 2,2 -o \"%s\" \"%s\"", TempDir, top1, top2);
456d78a5
FM
170 cstatus=system(csort);
171 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
172 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
173 fprintf(stderr, "SARG: sort command: %s\n",csort);
174 exit(1);
175 }
25697a35
GS
176
177 unlink(top2);
178
179 if((fp_top1=fopen(top1,"a"))==NULL) {
180 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top1);
181 exit(1);
182 }
354c1a68 183#if defined(__FreeBSD__)
120d768c 184 fprintf(fp_top1,"TOTAL\t%qu\t%qu\t%qu\n",ttnbytes,ttnacc,ttnelap);
354c1a68 185#elif defined(__alpha) || __ALPHA
120d768c 186 fprintf(fp_top1,"TOTAL\t%ld\t%ld\t%ld\n",ttnbytes,ttnacc,ttnelap);
354c1a68 187#else
120d768c 188 fprintf(fp_top1,"TOTAL\t%lld\t%lld\t%lld\n",ttnbytes,ttnacc,ttnelap);
354c1a68 189#endif
25697a35
GS
190 fclose(fp_top1);
191
fb7c5f27
FM
192 /*
193 * Obtem o period
194 */
25697a35
GS
195
196 strcpy(arqper,dirname);
d6e703cc 197 strcat(arqper,"/sarg-period");
25697a35
GS
198
199 if ((fp_in = fopen(arqper, "r")) == 0){
200 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],arqper);
201 exit(1);
202 }
203
05b90947
FM
204 if (!fgets(period,sizeof(period),fp_in)) {
205 fprintf(stderr,"SARG: (email) read error in %s\n",arqper);
206 exit(1);
207 }
25697a35
GS
208 fclose(fp_in);
209
fb7c5f27
FM
210 if((fp_top1=fopen(top1,"r"))==NULL) {
211 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top1);
212 exit(1);
213 }
214
215 if((fp_top3=fopen(top3,"w"))==NULL) {
216 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top3);
217 exit(1);
218 }
219
120d768c 220 strcpy(strip1,text[88]);
25697a35 221 strip_latin(strip1);
354c1a68 222 fprintf(fp_top3,"%s\n",strip1);
25697a35 223
120d768c 224 strcpy(strip1,text[97]);
25697a35 225 strip_latin(strip1);
354c1a68 226 fprintf(fp_top3,"%s\n",strip1);
25697a35 227
120d768c 228 strcpy(strip1,text[89]);
25697a35 229 strip_latin(strip1);
354c1a68 230 fprintf(fp_top3,"%s %s\n\n",strip1,period);
25697a35 231
120d768c 232 strcpy(strip1,text[100]);
25697a35 233 strip_latin(strip1);
120d768c 234 strcpy(strip2,text[98]);
25697a35 235 strip_latin(strip2);
120d768c 236 strcpy(strip3,text[92]);
25697a35 237 strip_latin(strip3);
120d768c 238 strcpy(strip4,text[93]);
25697a35 239 strip_latin(strip4);
120d768c 240 strcpy(strip5,text[94]);
25697a35 241 strip_latin(strip5);
120d768c 242 strcpy(strip6,text[95]);
25697a35 243 strip_latin(strip6);
120d768c 244 strcpy(strip7,text[99]);
25697a35
GS
245 strip_latin(strip7);
246
354c1a68 247 fprintf(fp_top3,"%-7s %-20s %-8s %-15s %%%-6s %-10s %-10s %%%-7s\n------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",strip1,strip2,strip3,strip4,strip4,strip5,strip6,strip7);
25697a35 248
fb7c5f27 249 while(fgets(warea,sizeof(warea),fp_top1))
25697a35 250 {
9c7c6346
FM
251 getword_start(&gwarea,warea);
252 if (getword(user,sizeof(user),&gwarea,'\t')<0) {
fb7c5f27
FM
253 printf("SARG: Maybe you have a broken user in your %s file.\n",top1);
254 exit(1);
255 }
9c7c6346 256 if (getword(nbytes,sizeof(nbytes),&gwarea,'\t')<0) {
fb7c5f27
FM
257 printf("SARG: Maybe you have a broken number of bytes in your %s file.\n",top1);
258 exit(1);
259 }
9c7c6346 260 if (getword(nacc,sizeof(nacc),&gwarea,'\t')<0) {
fb7c5f27
FM
261 printf("SARG: Maybe you have a broken number of access in your %s file.\n",top1);
262 exit(1);
263 }
9c7c6346 264 if (getword(elap,sizeof(elap),&gwarea,'\t')<0) {
fb7c5f27
FM
265 printf("SARG: Maybe you have a broken elapsed time in your %s file.\n",top1);
266 exit(1);
267 }
25697a35 268
fb7c5f27 269 if(strchr(user,'_') != NULL)
25697a35
GS
270 fixip(user);
271
272 strcpy(user2,user);
273
274 tnbytes=atol(nbytes);
275
276 if(tnbytes) {
277 perc=ttnbytes / 100;
278 perc=tnbytes / perc;
279 } else perc = 0;
280
281 if(atol(elap)) {
282 perc2=ttnelap / 100;
283 perc2=atol(elap) / perc2;
284 } else perc2 = 0;
285
286 posicao++;
287 tnelap=atol(elap);
288
289 sprintf(wperc,"%3.2f%%",perc);
290 sprintf(wperc2,"%3.2f%%",perc2);
291
fb7c5f27 292 if(strcmp(user,"TOTAL") == 0){
354c1a68 293 fputs("------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",fp_top3);
25697a35 294#if defined(__FreeBSD__)
354c1a68 295 fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",text[107]," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
25697a35 296#elif defined(__alpha) || __ALPHA
354c1a68 297 fprintf(fp_top3,"%-7s %20s %8ld %15s %8s %9s %10ld\n",text[107]," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
25697a35 298#else
354c1a68 299 fprintf(fp_top3,"%-7s %20s %8lld %15s %8s %9s %10lld\n",text[107]," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
25697a35 300#endif
354c1a68 301 } else {
25697a35 302#if defined(__FreeBSD__)
354c1a68 303 fprintf(fp_top3,"%7d %20s %8s %15s %7s %10s %10qu %7s%%\n",posicao,user2,nacc,fixnum(tnbytes,1),wperc,buildtime(tnelap),tnelap,wperc2);
25697a35 304#elif defined(__alpha) || __ALPHA
354c1a68 305 fprintf(fp_top3,"%7d %20s %8s %15s %7s %10s %10ld %7s%%\n",posicao,user2,nacc,fixnum(tnbytes,1),wperc,buildtime(tnelap),tnelap,wperc2);
25697a35 306#else
354c1a68 307 fprintf(fp_top3,"%7d %20s %8s %15s %7s %10s %10lld %7s\n",posicao,user2,nacc,fixnum(tnbytes,1),wperc,buildtime(tnelap),tnelap,wperc2);
25697a35
GS
308#endif
309 }
25697a35
GS
310 }
311
312 if(ttnbytes) {
313 tnbytes=ttnbytes / totuser;
314 } else tnbytes=0;
315
120d768c 316 strcpy(strip1,text[96]);
25697a35
GS
317 strip_latin(strip1);
318#if defined(__FreeBSD__)
354c1a68 319 fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser);
25697a35 320#elif defined(__alpha) || __ALPHA
354c1a68 321 fprintf(fp_top3,"%-7s %20s %8ld %15s %8s %9s %10ld\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser);
25697a35 322#else
354c1a68 323 fprintf(fp_top3,"%-7s %20s %8lld %15s %8s %9s %10lld\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser);
25697a35 324#endif
25697a35
GS
325
326 fclose(fp_top1);
327 unlink(top1);
328
329 t = time(NULL);
330 local = localtime(&t);
354c1a68 331 fprintf(fp_top3, "\n%s\n", asctime(local));
25697a35
GS
332
333 fclose(fp_top3);
334
335 if(strcmp(email,"stdout") == 0) {
336 if((fp_top3=fopen(top3,"r"))==NULL) {
337 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top3);
338 exit(1);
339 }
340
341 while(fgets(buf,sizeof(buf),fp_top3)!=NULL)
342 printf("%s",buf);
343 } else {
eb08bfe3 344 sprintf(buf,"\"%s\" -s \"SARG %s, %s\" \"%s\" <\"%s\"",MailUtility,text[55],asctime(local),email,top3);
456d78a5
FM
345 cstatus=system(buf);
346 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
347 fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus));
348 fprintf(stderr, "SARG: command: %s\n",buf);
349 exit(1);
350 }
351 }
fb7c5f27 352
51465d08
FM
353 if (snprintf(warea,sizeof(warea),"%s/sarg",TempDir)>=sizeof(warea)) {
354 fprintf(stderr,"SARG: Temporary directory name too long: %s\n",warea);
05b90947
FM
355 exit(1);
356 }
51465d08 357 unlinkdir(warea,0);
25697a35
GS
358
359 return (0);
360}