]> git.ipfire.org Git - thirdparty/sarg.git/blob - email.c
Be more consistent with the use of the temporary directory (i.e. always use /tmp...
[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, const char *email, const char *TempDir)
31 {
32 FILE *fp_in, *fp_top1, *fp_top2, *fp_top3;
33 long long int ttnbytes=0, ttnacc=0, tnacc=0;
34 long long int tnbytes=0, ttnelap=0, tnelap=0;
35 long long int nacc, nbytes, elap;
36 long long int avgacc, avgelap;
37 double perc=0.00;
38 double perc2=0.00;
39 int posicao=0;
40 char olduser[MAX_USER_LEN], csort[MAXLEN];
41 char wger[MAXLEN], top1[MAXLEN], top2[MAXLEN], top3[MAXLEN], user[MAX_USER_LEN];
42 char strip1[MAXLEN], strip2[MAXLEN], strip3[MAXLEN], strip4[MAXLEN], strip5[MAXLEN], strip6[MAXLEN], strip7[MAXLEN];
43 char buf[MAXLEN];
44 int totuser=0;
45 time_t t;
46 struct tm *local;
47 int cstatus;
48 char warea[MAXLEN];
49 struct getwordstruct gwarea;
50 struct generalitemstruct item;
51
52 snprintf(wger,sizeof(wger),"%s/sarg-general",dirname);
53 if((fp_in=fopen(wger,"r"))==NULL) {
54 debuga(_("(email) Cannot open file %s\n"),wger);
55 exit(EXIT_FAILURE);
56 }
57
58 snprintf(top1,sizeof(top1),"%s/top",dirname);
59 if((fp_top1=fopen(top1,"w"))==NULL) {
60 debuga(_("(email) Cannot open file %s\n"),top1);
61 exit(EXIT_FAILURE);
62 }
63
64 snprintf(top2,sizeof(top2),"%s/top.tmp",dirname);
65 if((fp_top2=fopen(top2,"w"))==NULL) {
66 debuga(_("(email) Cannot open file %s\n"),top2);
67 exit(EXIT_FAILURE);
68 }
69
70 olduser[0]='\0';
71 totuser=0;
72
73 while(fgets(warea,sizeof(warea),fp_in)) {
74 ger_read(warea,&item,wger);
75 if(item.total) continue;
76 if(strcmp(olduser,item.user) != 0)
77 {
78 totuser++;
79
80 if (olduser[0] != '\0') {
81 #if defined(__FreeBSD__)
82 fprintf(fp_top2,"%s\t%qu\t%qu\t%qu\n",olduser,tnbytes,tnacc,tnelap);
83 #else
84 fprintf(fp_top2,"%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",olduser,(uint64_t)tnbytes,(uint64_t)tnacc,(uint64_t)tnelap);
85 #endif
86 ttnbytes+=tnbytes;
87 ttnacc+=tnacc;
88 ttnelap+=tnelap;
89 }
90 strcpy(olduser,item.user);
91 tnbytes=0;
92 tnacc=0;
93 tnelap=0;
94 }
95
96 tnbytes+=item.nbytes;
97 tnacc+=item.nacc;
98 tnelap+=item.nelap;
99 }
100
101 if (olduser[0] != '\0') {
102 #if defined(__FreeBSD__)
103 fprintf(fp_top2,"%s\t%qu\t%qu\t%qu\n",olduser,tnbytes,tnacc,tnelap);
104 #else
105 fprintf(fp_top2,"%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",olduser,(uint64_t)tnbytes,(uint64_t)tnacc,(uint64_t)tnelap);
106 #endif
107 ttnbytes+=tnbytes;
108 ttnacc+=tnacc;
109 ttnelap+=tnelap;
110 }
111
112 fclose(fp_in);
113 fclose(fp_top2);
114
115 sprintf(csort,"sort -n -T \"%s\" -r -k 2,2 -o \"%s\" \"%s\"", TempDir, top1, top2);
116 cstatus=system(csort);
117 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
118 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
119 debuga(_("sort command: %s\n"),csort);
120 exit(EXIT_FAILURE);
121 }
122
123 unlink(top2);
124
125 if((fp_top1=fopen(top1,"a"))==NULL) {
126 debuga(_("(email) Cannot open file %s\n"),top1);
127 exit(EXIT_FAILURE);
128 }
129 #if defined(__FreeBSD__)
130 fprintf(fp_top1,"TOTAL\t%qu\t%qu\t%qu\n",ttnbytes,ttnacc,ttnelap);
131 #else
132 fprintf(fp_top1,"TOTAL\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",(uint64_t)ttnbytes,(uint64_t)ttnacc,(uint64_t)ttnelap);
133 #endif
134 fclose(fp_top1);
135
136 if((fp_top1=fopen(top1,"r"))==NULL) {
137 debuga(_("(email) Cannot open file %s\n"),top1);
138 exit(EXIT_FAILURE);
139 }
140
141 snprintf(top3,sizeof(top3),"%s/report",dirname);
142 if((fp_top3=fopen(top3,"w"))==NULL) {
143 debuga(_("(email) Cannot open file %s\n"),top3);
144 exit(EXIT_FAILURE);
145 }
146
147 strcpy(strip1,_("Squid User Access Report"));
148 strip_latin(strip1);
149 fprintf(fp_top3,"%s\n",strip1);
150
151 strcpy(strip1,_("Decreasing Access (bytes)"));
152 strip_latin(strip1);
153 fprintf(fp_top3,"%s\n",strip1);
154
155 strcpy(strip1,_("Period"));
156 strip_latin(strip1);
157 fprintf(fp_top3,"%s %s\n\n",strip1,period.text);
158
159 strcpy(strip1,_("NUM"));
160 strip_latin(strip1);
161 strcpy(strip2,_("USERID"));
162 strip_latin(strip2);
163 strcpy(strip3,_("CONNECT"));
164 strip_latin(strip3);
165 strcpy(strip4,_("BYTES"));
166 strip_latin(strip4);
167 strcpy(strip5,_("ELAPSED TIME"));
168 strip_latin(strip5);
169 strcpy(strip6,_("MILLISEC"));
170 strip_latin(strip6);
171 strcpy(strip7,_("TIME"));
172 strip_latin(strip7);
173
174 fprintf(fp_top3,"%-7s %-20s %-8s %-15s %%%-6s %-10s %-10s %%%-7s\n------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",strip1,strip2,strip3,strip4,strip4,strip5,strip6,strip7);
175
176 while(fgets(warea,sizeof(warea),fp_top1))
177 {
178 getword_start(&gwarea,warea);
179 if (getword(user,sizeof(user),&gwarea,'\t')<0) {
180 debuga(_("There is an invalid user ID in file %s\n"),top1);
181 exit(EXIT_FAILURE);
182 }
183 if (getword_atoll(&nbytes,&gwarea,'\t')<0) {
184 debuga(_("There is an invalid number of bytes in file %s\n"),top1);
185 exit(EXIT_FAILURE);
186 }
187 if (getword_atoll(&nacc,&gwarea,'\t')<0) {
188 debuga(_("There is an invalid number of access in file %s\n"),top1);
189 exit(EXIT_FAILURE);
190 }
191 if (getword_atoll(&elap,&gwarea,'\t')<0) {
192 debuga(_("There is an invalid elapsed time in file %s\n"),top1);
193 exit(EXIT_FAILURE);
194 }
195
196 perc=(ttnbytes) ? nbytes * 100. / ttnbytes : 0;
197 perc2=(ttnelap) ? elap * 100. / ttnelap : 0;
198
199 posicao++;
200
201 if(strcmp(user,"TOTAL") == 0){
202 fputs("------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",fp_top3);
203 #if defined(__FreeBSD__)
204 fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",_("TOTAL")," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
205 #else
206 fprintf(fp_top3,"%-7s %20s %8"PRIu64" %15s %8s %9s %10"PRIu64"\n",_("TOTAL")," ",(uint64_t)ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),(uint64_t)ttnelap);
207 #endif
208 } else {
209 #if defined(__FreeBSD__)
210 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);
211 #else
212 fprintf(fp_top3,"%7d %20s %8"PRIu64" %15s %3.2lf%% %10s %10"PRIu64" %3.2lf%%\n",posicao,user,(uint64_t)nacc,fixnum(nbytes,1),perc,buildtime(elap),(uint64_t)elap,perc2);
213 #endif
214 }
215 }
216
217 if (totuser>0) {
218 tnbytes=(totuser) ? ttnbytes / totuser : 0;
219 avgacc=ttnacc/totuser;
220 avgelap=ttnelap/totuser;
221 } else {
222 tnbytes=0;
223 avgacc=0;
224 avgelap=0;
225 }
226
227 strcpy(strip1,_("AVERAGE"));
228 strip_latin(strip1);
229 #if defined(__FreeBSD__)
230 fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",strip1," ",avgacc,fixnum(tnbytes,1)," ",buildtime(avgelap),avgelap);
231 #else
232 fprintf(fp_top3,"%-7s %20s %8"PRIu64" %15s %8s %9s %10"PRIu64"\n",strip1," ",(uint64_t)avgacc,fixnum(tnbytes,1)," ",buildtime(avgelap),(uint64_t)avgelap);
233 #endif
234
235 fclose(fp_top1);
236 unlink(top1);
237
238 t = time(NULL);
239 local = localtime(&t);
240 fprintf(fp_top3, "\n%s\n", asctime(local));
241
242 fclose(fp_top3);
243
244 if(strcmp(email,"stdout") == 0) {
245 if((fp_top3=fopen(top3,"r"))==NULL) {
246 debuga(_("(email) Cannot open file %s\n"),top3);
247 exit(EXIT_FAILURE);
248 }
249
250 while(fgets(buf,sizeof(buf),fp_top3)!=NULL)
251 fputs(buf,stdout);
252 } else {
253 snprintf(buf,sizeof(buf),"\"%s\" -s \"SARG %s, %s\" \"%s\" <\"%s\"",MailUtility,_("Report"),asctime(local),email,top3);
254 cstatus=system(buf);
255 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
256 debuga(_("command return status %d\n"),WEXITSTATUS(cstatus));
257 debuga(_("command: %s\n"),buf);
258 exit(EXIT_FAILURE);
259 }
260 }
261
262 unlinkdir(TempDir,0);
263
264 return (0);
265 }