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