]> git.ipfire.org Git - thirdparty/sarg.git/blame - email.c
Rename configure.in as configure.ac
[thirdparty/sarg.git] / email.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
110ce984 3 * 1998, 2015
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
9dc20988
FM
30#ifdef ENABLE_DOUBLE_CHECK_DATA
31extern struct globalstatstruct globstat;
32#endif
33
2824ec9b 34int geramail(const char *dirname, int debug, const char *outdir, const char *email, const char *TempDir)
25697a35 35{
9bd92830
FM
36 FILE *fp_in, *fp_top1, *fp_top2, *fp_top3;
37 long long int ttnbytes=0, ttnacc=0, tnacc=0;
38 long long int tnbytes=0, ttnelap=0, tnelap=0;
39 long long int nacc, nbytes, elap;
40 long long int avgacc, avgelap;
41 double perc=0.00;
42 double perc2=0.00;
43 int posicao=0;
44 char olduser[MAX_USER_LEN], csort[MAXLEN];
45 char wger[MAXLEN], top1[MAXLEN], top2[MAXLEN], top3[MAXLEN], user[MAX_USER_LEN];
46 char strip1[MAXLEN], strip2[MAXLEN], strip3[MAXLEN], strip4[MAXLEN], strip5[MAXLEN], strip6[MAXLEN], strip7[MAXLEN];
47 char *buf;
48 char warea[MAXLEN];
9e023838 49 char Subject[120];
9bd92830
FM
50 int totuser=0;
51 time_t t;
52 struct tm *local;
53 int cstatus;
54 struct getwordstruct gwarea;
55 struct generalitemstruct item;
56 longline line;
57 const struct userinfostruct *uinfo;
58
59 snprintf(wger,sizeof(wger),"%s/sarg-general",dirname);
60 if((fp_in=fopen(wger,"r"))==NULL) {
af961877 61 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),wger,strerror(errno));
9bd92830
FM
62 exit(EXIT_FAILURE);
63 }
64
9bd92830
FM
65 snprintf(top2,sizeof(top2),"%s/top.tmp",dirname);
66 if((fp_top2=fopen(top2,"w"))==NULL) {
af961877 67 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),top2,strerror(errno));
9bd92830
FM
68 exit(EXIT_FAILURE);
69 }
70
71 olduser[0]='\0';
72 totuser=0;
73
74 if ((line=longline_create())==NULL) {
af961877 75 debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),wger);
9bd92830
FM
76 exit(EXIT_FAILURE);
77 }
78
79 while((buf=longline_read(fp_in,line))!=NULL) {
80 ger_read(buf,&item,wger);
81 if(item.total) continue;
007905af 82 if(strcmp(olduser,item.user) != 0) {
9bd92830
FM
83 totuser++;
84
85 if (olduser[0] != '\0') {
25697a35 86#if defined(__FreeBSD__)
9bd92830 87 fprintf(fp_top2,"%s\t%qu\t%qu\t%qu\n",olduser,tnbytes,tnacc,tnelap);
25697a35 88#else
9bd92830 89 fprintf(fp_top2,"%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",olduser,(uint64_t)tnbytes,(uint64_t)tnacc,(uint64_t)tnelap);
25697a35 90#endif
9bd92830
FM
91 ttnbytes+=tnbytes;
92 ttnacc+=tnacc;
93 ttnelap+=tnelap;
94 }
95 strcpy(olduser,item.user);
96 tnbytes=0;
97 tnacc=0;
98 tnelap=0;
99 }
100
101 tnbytes+=item.nbytes;
102 tnacc+=item.nacc;
103 tnelap+=item.nelap;
104 }
204781f4 105 if (fclose(fp_in)==EOF) {
af961877 106 debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),wger,strerror(errno));
204781f4
FM
107 exit(EXIT_FAILURE);
108 }
9bd92830
FM
109 longline_destroy(&line);
110
111 if (olduser[0] != '\0') {
25697a35 112#if defined(__FreeBSD__)
9bd92830 113 fprintf(fp_top2,"%s\t%qu\t%qu\t%qu\n",olduser,tnbytes,tnacc,tnelap);
25697a35 114#else
9bd92830 115 fprintf(fp_top2,"%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",olduser,(uint64_t)tnbytes,(uint64_t)tnacc,(uint64_t)tnelap);
25697a35 116#endif
9bd92830
FM
117 ttnbytes+=tnbytes;
118 ttnacc+=tnacc;
119 ttnelap+=tnelap;
120 }
121
507460ae 122 if (fclose(fp_top2)==EOF) {
af961877 123 debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),top2,strerror(errno));
507460ae
FM
124 exit(EXIT_FAILURE);
125 }
9bd92830 126
9dc20988
FM
127#ifdef ENABLE_DOUBLE_CHECK_DATA
128 if (ttnacc!=globstat.nacc || ttnbytes!=globstat.nbytes || ttnelap!=globstat.elap) {
af961877 129 debuga(__FILE__,__LINE__,_("Total statistics mismatch when reading \"%s\" to produce the email report\n"),wger);
9dc20988
FM
130 exit(EXIT_FAILURE);
131 }
132#endif
133
78eeb33f 134 if (snprintf(csort,sizeof(csort),"sort -n -T \"%s\" -t \"\t\" -r -k 2,2 -o \"%s\" \"%s\"", TempDir, top1, top2)>=sizeof(csort)) {
af961877 135 debuga(__FILE__,__LINE__,_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),top2,top1);
78eeb33f
FM
136 exit(EXIT_FAILURE);
137 }
9bd92830
FM
138 cstatus=system(csort);
139 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
af961877
FM
140 debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEXITSTATUS(cstatus));
141 debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
9bd92830
FM
142 exit(EXIT_FAILURE);
143 }
144
11767c6a 145 if (!KeepTempLog && unlink(top2)) {
af961877 146 debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),top2,strerror(errno));
08f9b029
FM
147 exit(EXIT_FAILURE);
148 }
9bd92830
FM
149
150 if((fp_top1=fopen(top1,"r"))==NULL) {
af961877 151 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),top1,strerror(errno));
9bd92830
FM
152 exit(EXIT_FAILURE);
153 }
154
155 snprintf(top3,sizeof(top3),"%s/report",dirname);
156 if((fp_top3=fopen(top3,"w"))==NULL) {
af961877 157 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),top3,strerror(errno));
9bd92830
FM
158 exit(EXIT_FAILURE);
159 }
160
a87d4d11 161 safe_strcpy(strip1,_("Squid User Access Report"),sizeof(strip1));
9bd92830
FM
162 strip_latin(strip1);
163 fprintf(fp_top3,"%s\n",strip1);
164
a87d4d11 165 safe_strcpy(strip1,_("Decreasing Access (bytes)"),sizeof(strip1));
9bd92830
FM
166 strip_latin(strip1);
167 fprintf(fp_top3,"%s\n",strip1);
168
a87d4d11 169 safe_strcpy(strip1,_("Period"),sizeof(strip1));
9bd92830
FM
170 strip_latin(strip1);
171 fprintf(fp_top3,"%s %s\n\n",strip1,period.text);
172
a87d4d11 173 safe_strcpy(strip1,_("NUM"),sizeof(strip1));
9bd92830 174 strip_latin(strip1);
a87d4d11 175 safe_strcpy(strip2,_("USERID"),sizeof(strip2));
9bd92830 176 strip_latin(strip2);
a87d4d11 177 safe_strcpy(strip3,_("CONNECT"),sizeof(strip3));
9bd92830 178 strip_latin(strip3);
a87d4d11 179 safe_strcpy(strip4,_("BYTES"),sizeof(strip4));
9bd92830 180 strip_latin(strip4);
a87d4d11 181 safe_strcpy(strip5,_("ELAPSED TIME"),sizeof(strip5));
9bd92830 182 strip_latin(strip5);
a87d4d11 183 safe_strcpy(strip6,_("MILLISEC"),sizeof(strip6));
9bd92830 184 strip_latin(strip6);
eb7ab620 185 safe_strcpy(strip7,pgettext("duration","TIME"),sizeof(strip7));
9bd92830
FM
186 strip_latin(strip7);
187
188 fprintf(fp_top3,"%-7s %-20s %-8s %-15s %%%-6s %-10s %-10s %%%-7s\n------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",strip1,strip2,strip3,strip4,strip4,strip5,strip6,strip7);
189
007905af 190 while(fgets(warea,sizeof(warea),fp_top1)) {
9bd92830
FM
191 fixendofline(warea);
192 getword_start(&gwarea,warea);
193 if (getword(user,sizeof(user),&gwarea,'\t')<0) {
af961877 194 debuga(__FILE__,__LINE__,_("Invalid user ID in file \"%s\"\n"),top1);
9bd92830
FM
195 exit(EXIT_FAILURE);
196 }
197 if (getword_atoll(&nbytes,&gwarea,'\t')<0) {
af961877 198 debuga(__FILE__,__LINE__,_("Invalid number of bytes in file \"%s\"\n"),top1);
9bd92830
FM
199 exit(EXIT_FAILURE);
200 }
201 if (getword_atoll(&nacc,&gwarea,'\t')<0) {
af961877 202 debuga(__FILE__,__LINE__,_("Invalid number of accesses in file \"%s\"\n"),top1);
9bd92830
FM
203 exit(EXIT_FAILURE);
204 }
205 if (getword_atoll(&elap,&gwarea,'\0')<0) {
af961877 206 debuga(__FILE__,__LINE__,_("Invalid elapsed time in file \"%s\"\n"),top1);
9bd92830
FM
207 exit(EXIT_FAILURE);
208 }
209
210 uinfo=userinfo_find_from_id(user);
211 if (!uinfo) {
af961877 212 debuga(__FILE__,__LINE__,_("Unknown user ID %s in file \"%s\"\n"),user,top1);
9bd92830
FM
213 exit(EXIT_FAILURE);
214 }
215
216 perc=(ttnbytes) ? nbytes * 100. / ttnbytes : 0;
217 perc2=(ttnelap) ? elap * 100. / ttnelap : 0;
218
219 posicao++;
25697a35
GS
220
221#if defined(__FreeBSD__)
9bd92830 222 fprintf(fp_top3,"%7d %20s %8lld %15s %3.2lf%% %10s %10qu %3.2lf%%\n",posicao,uinfo->label,nacc,fixnum(nbytes,1),perc,buildtime(elap),elap,perc2);
25697a35 223#else
9bd92830 224 fprintf(fp_top3,"%7d %20s %8"PRIu64" %15s %3.2lf%% %10s %10"PRIu64" %3.2lf%%\n",posicao,uinfo->label,(uint64_t)nacc,fixnum(nbytes,1),perc,buildtime(elap),(uint64_t)elap,perc2);
25697a35 225#endif
9bd92830 226 }
02808722 227
9bd92830
FM
228 // output total
229 fputs("------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",fp_top3);
25697a35 230#if defined(__FreeBSD__)
9bd92830 231 fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",_("TOTAL")," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
25697a35 232#else
9bd92830 233 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);
25697a35 234#endif
25697a35 235
9bd92830
FM
236 // compute and write average
237 if (totuser>0) {
238 tnbytes=(totuser) ? ttnbytes / totuser : 0;
239 avgacc=ttnacc/totuser;
240 avgelap=ttnelap/totuser;
241 } else {
242 tnbytes=0;
243 avgacc=0;
244 avgelap=0;
245 }
246
a87d4d11 247 safe_strcpy(strip1,_("AVERAGE"),sizeof(strip1));
9bd92830 248 strip_latin(strip1);
25697a35 249#if defined(__FreeBSD__)
9bd92830 250 fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",strip1," ",avgacc,fixnum(tnbytes,1)," ",buildtime(avgelap),avgelap);
25697a35 251#else
9bd92830 252 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);
25697a35 253#endif
25697a35 254
204781f4 255 if (fclose(fp_top1)==EOF) {
af961877 256 debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),top1,strerror(errno));
204781f4
FM
257 exit(EXIT_FAILURE);
258 }
11767c6a 259 if (!KeepTempLog && unlink(top1)) {
af961877 260 debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),top1,strerror(errno));
08f9b029
FM
261 exit(EXIT_FAILURE);
262 }
25697a35 263
9bd92830
FM
264 t = time(NULL);
265 local = localtime(&t);
266 fprintf(fp_top3, "\n%s\n", asctime(local));
25697a35 267
507460ae 268 if (fclose(fp_top3)==EOF) {
af961877 269 debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),top3,strerror(errno));
507460ae
FM
270 exit(EXIT_FAILURE);
271 }
25697a35 272
9bd92830
FM
273 if(strcmp(email,"stdout") == 0) {
274 if((fp_top3=fopen(top3,"r"))==NULL) {
af961877 275 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),top3,strerror(errno));
9bd92830
FM
276 exit(EXIT_FAILURE);
277 }
25697a35 278
9bd92830
FM
279 while(fgets(warea,sizeof(warea),fp_top3)!=NULL)
280 fputs(warea,stdout);
ef4acae5 281 if (fclose(fp_top3)==EOF) {
af961877 282 debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),top3,strerror(errno));
ef4acae5
FM
283 exit(EXIT_FAILURE);
284 }
9bd92830 285 } else {
9e023838 286 /* TRANSLATORS: The string is formatted using strftime. You can use
cf80155e 287 any string formatting marker allowed by strftime. */
9e023838 288 strftime(Subject,sizeof(Subject),_("SARG report, %c"),local);
bde5073d 289 snprintf(warea,sizeof(warea),"%s -s \"%s\" \"%s\" <\"%s\"",MailUtility,Subject,email,top3);
9e023838 290 if (debug)
af961877 291 debuga(__FILE__,__LINE__,_("Sending mail with command: %s\n"),warea);
9bd92830
FM
292 cstatus=system(warea);
293 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
af961877
FM
294 debuga(__FILE__,__LINE__,_("command return status %d\n"),WEXITSTATUS(cstatus));
295 debuga(__FILE__,__LINE__,_("command: %s\n"),warea);
9bd92830
FM
296 exit(EXIT_FAILURE);
297 }
298 }
fb7c5f27 299
9bd92830 300 //unlinkdir(TempDir,0);
25697a35 301
9bd92830 302 return (0);
25697a35 303}