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