]> git.ipfire.org Git - thirdparty/sarg.git/blob - email.c
Keep the user name intact in the report.
[thirdparty/sarg.git] / email.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2010
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
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"
28 #include "include/defs.h"
29
30 int geramail(const char *dirname, int debug, const char *outdir, int userip, const char *email, const char *TempDir)
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 long long int nacc, nbytes, elap;
37 double perc=0.00;
38 double perc2=0.00;
39 int posicao=0;
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];
43 int totuser=0;
44 time_t t;
45 struct tm *local;
46 int cstatus;
47 char warea[MAXLEN];
48 struct getwordstruct gwarea;
49 struct generalitemstruct item;
50
51 strcpy(wger,dirname);
52 strcpy(top1,dirname);
53 strcpy(top2,dirname);
54 strcpy(top3,dirname);
55 strcpy(tusr,dirname);
56 strcat(wger,"/sarg-general");
57 strcat(top1,"/top");
58 strcat(top2,"/top.tmp");
59 strcat(top3,"/report");
60 strcat(tusr,"/sarg-users");
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
77 olduser[0]='\0';
78 totuser=0;
79
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)
84 {
85 totuser++;
86
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);
92 #else
93 fprintf(fp_top2,"%s\t%lld\t%lld\t%lld\n",olduser,tnbytes,tnacc,tnelap);
94 #endif
95 ttnbytes+=tnbytes;
96 ttnacc+=tnacc;
97 ttnelap+=tnelap;
98 }
99 strcpy(olduser,item.user);
100 tnbytes=0;
101 tnacc=0;
102 tnelap=0;
103 }
104
105 tnbytes+=item.nbytes;
106 tnacc+=item.nacc;
107 tnelap+=item.nelap;
108 }
109
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);
115 #else
116 fprintf(fp_top2,"%s\t%lld\t%lld\t%lld\n",olduser,tnbytes,tnacc,tnelap);
117 #endif
118 ttnbytes+=tnbytes;
119 ttnacc+=tnacc;
120 ttnelap+=tnelap;
121 }
122
123 fclose(fp_in);
124 fclose(fp_top2);
125
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 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
130 fprintf(stderr, "SARG: sort command: %s\n",csort);
131 exit(1);
132 }
133
134 unlink(top2);
135
136 if((fp_top1=fopen(top1,"a"))==NULL) {
137 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top1);
138 exit(1);
139 }
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);
144 #else
145 fprintf(fp_top1,"TOTAL\t%lld\t%lld\t%lld\n",ttnbytes,ttnacc,ttnelap);
146 #endif
147 fclose(fp_top1);
148
149 /*
150 * Obtem o period
151 */
152
153 strcpy(arqper,dirname);
154 strcat(arqper,"/sarg-period");
155
156 if ((fp_in = fopen(arqper, "r")) == 0){
157 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],arqper);
158 exit(1);
159 }
160
161 if (!fgets(period,sizeof(period),fp_in)) {
162 fprintf(stderr,"SARG: (email) read error in %s\n",arqper);
163 exit(1);
164 }
165 fclose(fp_in);
166
167 if((fp_top1=fopen(top1,"r"))==NULL) {
168 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top1);
169 exit(1);
170 }
171
172 if((fp_top3=fopen(top3,"w"))==NULL) {
173 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top3);
174 exit(1);
175 }
176
177 strcpy(strip1,text[88]);
178 strip_latin(strip1);
179 fprintf(fp_top3,"%s\n",strip1);
180
181 strcpy(strip1,text[97]);
182 strip_latin(strip1);
183 fprintf(fp_top3,"%s\n",strip1);
184
185 strcpy(strip1,text[89]);
186 strip_latin(strip1);
187 fprintf(fp_top3,"%s %s\n\n",strip1,period);
188
189 strcpy(strip1,text[100]);
190 strip_latin(strip1);
191 strcpy(strip2,text[98]);
192 strip_latin(strip2);
193 strcpy(strip3,text[92]);
194 strip_latin(strip3);
195 strcpy(strip4,text[93]);
196 strip_latin(strip4);
197 strcpy(strip5,text[94]);
198 strip_latin(strip5);
199 strcpy(strip6,text[95]);
200 strip_latin(strip6);
201 strcpy(strip7,text[99]);
202 strip_latin(strip7);
203
204 fprintf(fp_top3,"%-7s %-20s %-8s %-15s %%%-6s %-10s %-10s %%%-7s\n------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",strip1,strip2,strip3,strip4,strip4,strip5,strip6,strip7);
205
206 while(fgets(warea,sizeof(warea),fp_top1))
207 {
208 getword_start(&gwarea,warea);
209 if (getword(user,sizeof(user),&gwarea,'\t')<0) {
210 debuga(_("There may be a broken user in file %s"),top1);
211 exit(1);
212 }
213 if (getword_atoll(&nbytes,&gwarea,'\t')<0) {
214 debuga(_("There may be a broken number of bytes in file %s"),top1);
215 exit(1);
216 }
217 if (getword_atoll(&nacc,&gwarea,'\t')<0) {
218 debuga(_("There is a broken number of access in file %s"),top1);
219 exit(1);
220 }
221 if (getword_atoll(&elap,&gwarea,'\t')<0) {
222 debuga(_("There is a broken elapsed time in file %s"),top1);
223 exit(1);
224 }
225
226 perc=(ttnbytes) ? nbytes * 100. / ttnbytes : 0;
227 perc2=(ttnelap) ? elap * 100. / ttnelap : 0;
228
229 posicao++;
230
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",text[107]," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
235 #elif defined(__alpha) || __ALPHA
236 fprintf(fp_top3,"%-7s %20s %8ld %15s %8s %9s %10ld\n",text[107]," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
237 #else
238 fprintf(fp_top3,"%-7s %20s %8lld %15s %8s %9s %10lld\n",text[107]," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
239 #endif
240 } else {
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);
245 #else
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);
247 #endif
248 }
249 }
250
251 tnbytes=(totuser) ? ttnbytes / totuser : 0;
252
253 strcpy(strip1,text[96]);
254 strip_latin(strip1);
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);
259 #else
260 fprintf(fp_top3,"%-7s %20s %8lld %15s %8s %9s %10lld\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser);
261 #endif
262
263 fclose(fp_top1);
264 unlink(top1);
265
266 t = time(NULL);
267 local = localtime(&t);
268 fprintf(fp_top3, "\n%s\n", asctime(local));
269
270 fclose(fp_top3);
271
272 if(strcmp(email,"stdout") == 0) {
273 if((fp_top3=fopen(top3,"r"))==NULL) {
274 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top3);
275 exit(1);
276 }
277
278 while(fgets(buf,sizeof(buf),fp_top3)!=NULL)
279 fputs(buf,stdout);
280 } else {
281 sprintf(buf,"\"%s\" -s \"SARG %s, %s\" \"%s\" <\"%s\"",MailUtility,text[55],asctime(local),email,top3);
282 cstatus=system(buf);
283 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
284 fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus));
285 fprintf(stderr, "SARG: command: %s\n",buf);
286 exit(1);
287 }
288 }
289
290 if (snprintf(warea,sizeof(warea),"%s/sarg",TempDir)>=sizeof(warea)) {
291 fprintf(stderr,"SARG: Temporary directory name too long: %s\n",warea);
292 exit(1);
293 }
294 unlinkdir(warea,0);
295
296 return (0);
297 }