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