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