]> git.ipfire.org Git - thirdparty/sarg.git/blame - email.c
Fix creation of the report's file name from the mangling of the user name
[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{
9bd92830
FM
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;
44 char warea[MAXLEN];
45 int totuser=0;
46 time_t t;
47 struct tm *local;
48 int cstatus;
49 struct getwordstruct gwarea;
50 struct generalitemstruct item;
51 longline line;
52 const struct userinfostruct *uinfo;
53
54 snprintf(wger,sizeof(wger),"%s/sarg-general",dirname);
55 if((fp_in=fopen(wger,"r"))==NULL) {
56 debuga(_("(email) Cannot open file %s\n"),wger);
57 exit(EXIT_FAILURE);
58 }
59
60 snprintf(top1,sizeof(top1),"%s/top",dirname);
61 if((fp_top1=fopen(top1,"w"))==NULL) {
62 debuga(_("(email) Cannot open file %s\n"),top1);
63 exit(EXIT_FAILURE);
64 }
65
66 snprintf(top2,sizeof(top2),"%s/top.tmp",dirname);
67 if((fp_top2=fopen(top2,"w"))==NULL) {
68 debuga(_("(email) Cannot open file %s\n"),top2);
69 exit(EXIT_FAILURE);
70 }
71
72 olduser[0]='\0';
73 totuser=0;
74
75 if ((line=longline_create())==NULL) {
76 debuga(_("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;
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 }
106 fclose(fp_in);
107 longline_destroy(&line);
108
109 if (olduser[0] != '\0') {
25697a35 110#if defined(__FreeBSD__)
9bd92830 111 fprintf(fp_top2,"%s\t%qu\t%qu\t%qu\n",olduser,tnbytes,tnacc,tnelap);
25697a35 112#else
9bd92830 113 fprintf(fp_top2,"%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",olduser,(uint64_t)tnbytes,(uint64_t)tnacc,(uint64_t)tnelap);
25697a35 114#endif
9bd92830
FM
115 ttnbytes+=tnbytes;
116 ttnacc+=tnacc;
117 ttnelap+=tnelap;
118 }
119
120 fclose(fp_top2);
121
122 sprintf(csort,"sort -n -T \"%s\" -r -k 2,2 -o \"%s\" \"%s\"", TempDir, top1, top2);
123 cstatus=system(csort);
124 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
125 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
126 debuga(_("sort command: %s\n"),csort);
127 exit(EXIT_FAILURE);
128 }
129
08f9b029
FM
130 if (unlink(top2)) {
131 debuga(_("Cannot delete %s - %s\n"),top2,strerror(errno));
132 exit(EXIT_FAILURE);
133 }
9bd92830
FM
134
135 if((fp_top1=fopen(top1,"r"))==NULL) {
136 debuga(_("(email) Cannot open file %s\n"),top1);
137 exit(EXIT_FAILURE);
138 }
139
140 snprintf(top3,sizeof(top3),"%s/report",dirname);
141 if((fp_top3=fopen(top3,"w"))==NULL) {
142 debuga(_("(email) Cannot open file %s\n"),top3);
143 exit(EXIT_FAILURE);
144 }
145
146 strcpy(strip1,_("Squid User Access Report"));
147 strip_latin(strip1);
148 fprintf(fp_top3,"%s\n",strip1);
149
150 strcpy(strip1,_("Decreasing Access (bytes)"));
151 strip_latin(strip1);
152 fprintf(fp_top3,"%s\n",strip1);
153
154 strcpy(strip1,_("Period"));
155 strip_latin(strip1);
156 fprintf(fp_top3,"%s %s\n\n",strip1,period.text);
157
158 strcpy(strip1,_("NUM"));
159 strip_latin(strip1);
160 strcpy(strip2,_("USERID"));
161 strip_latin(strip2);
162 strcpy(strip3,_("CONNECT"));
163 strip_latin(strip3);
164 strcpy(strip4,_("BYTES"));
165 strip_latin(strip4);
166 strcpy(strip5,_("ELAPSED TIME"));
167 strip_latin(strip5);
168 strcpy(strip6,_("MILLISEC"));
169 strip_latin(strip6);
170 strcpy(strip7,_("TIME"));
171 strip_latin(strip7);
172
173 fprintf(fp_top3,"%-7s %-20s %-8s %-15s %%%-6s %-10s %-10s %%%-7s\n------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",strip1,strip2,strip3,strip4,strip4,strip5,strip6,strip7);
174
007905af 175 while(fgets(warea,sizeof(warea),fp_top1)) {
9bd92830
FM
176 fixendofline(warea);
177 getword_start(&gwarea,warea);
178 if (getword(user,sizeof(user),&gwarea,'\t')<0) {
179 debuga(_("There is an invalid user ID in file %s\n"),top1);
180 exit(EXIT_FAILURE);
181 }
182 if (getword_atoll(&nbytes,&gwarea,'\t')<0) {
183 debuga(_("There is an invalid number of bytes in file %s\n"),top1);
184 exit(EXIT_FAILURE);
185 }
186 if (getword_atoll(&nacc,&gwarea,'\t')<0) {
187 debuga(_("There is an invalid number of access in file %s\n"),top1);
188 exit(EXIT_FAILURE);
189 }
190 if (getword_atoll(&elap,&gwarea,'\0')<0) {
191 debuga(_("There is an invalid elapsed time in file %s\n"),top1);
192 exit(EXIT_FAILURE);
193 }
194
195 uinfo=userinfo_find_from_id(user);
196 if (!uinfo) {
197 debuga(_("Unknown user ID %s in file %s\n"),user,top1);
198 exit(EXIT_FAILURE);
199 }
200
201 perc=(ttnbytes) ? nbytes * 100. / ttnbytes : 0;
202 perc2=(ttnelap) ? elap * 100. / ttnelap : 0;
203
204 posicao++;
25697a35
GS
205
206#if defined(__FreeBSD__)
9bd92830 207 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 208#else
9bd92830 209 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 210#endif
9bd92830 211 }
02808722 212
9bd92830
FM
213 // output total
214 fputs("------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",fp_top3);
25697a35 215#if defined(__FreeBSD__)
9bd92830 216 fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",_("TOTAL")," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
25697a35 217#else
9bd92830 218 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 219#endif
25697a35 220
9bd92830
FM
221 // compute and write average
222 if (totuser>0) {
223 tnbytes=(totuser) ? ttnbytes / totuser : 0;
224 avgacc=ttnacc/totuser;
225 avgelap=ttnelap/totuser;
226 } else {
227 tnbytes=0;
228 avgacc=0;
229 avgelap=0;
230 }
231
232 strcpy(strip1,_("AVERAGE"));
233 strip_latin(strip1);
25697a35 234#if defined(__FreeBSD__)
9bd92830 235 fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",strip1," ",avgacc,fixnum(tnbytes,1)," ",buildtime(avgelap),avgelap);
25697a35 236#else
9bd92830 237 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 238#endif
25697a35 239
9bd92830 240 fclose(fp_top1);
08f9b029
FM
241 if (unlink(top1)) {
242 debuga(_("Cannot delete %s - %s\n"),top1,strerror(errno));
243 exit(EXIT_FAILURE);
244 }
25697a35 245
9bd92830
FM
246 t = time(NULL);
247 local = localtime(&t);
248 fprintf(fp_top3, "\n%s\n", asctime(local));
25697a35 249
9bd92830 250 fclose(fp_top3);
25697a35 251
9bd92830
FM
252 if(strcmp(email,"stdout") == 0) {
253 if((fp_top3=fopen(top3,"r"))==NULL) {
254 debuga(_("(email) Cannot open file %s\n"),top3);
255 exit(EXIT_FAILURE);
256 }
25697a35 257
9bd92830
FM
258 while(fgets(warea,sizeof(warea),fp_top3)!=NULL)
259 fputs(warea,stdout);
260 } else {
261 snprintf(warea,sizeof(warea),"%s -s \"SARG %s, %s\" \"%s\" <\"%s\"",MailUtility,_("Report"),asctime(local),email,top3);
262 cstatus=system(warea);
263 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
264 debuga(_("command return status %d\n"),WEXITSTATUS(cstatus));
265 debuga(_("command: %s\n"),warea);
266 exit(EXIT_FAILURE);
267 }
268 }
fb7c5f27 269
9bd92830 270 //unlinkdir(TempDir,0);
25697a35 271
9bd92830 272 return (0);
25697a35 273}