2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
6 * please look at http://sarg.sourceforge.net/donations.php
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
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.
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.
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.
27 #include "include/conf.h"
28 #include "include/defs.h"
30 #ifdef ENABLE_DOUBLE_CHECK_DATA
31 extern struct globalstatstruct globstat
;
34 int geramail(const char *dirname
, int debug
, const char *outdir
, const char *email
, const char *TempDir
)
36 FILE *fp_in
, *fp_top1
, *fp_top2
, *fp_top3
;
37 long long int ttnbytes
=0, ttnacc
=0, tnacc
=0;
38 long long int tnbytes
=0, ttnelap
=0, tnelap
=0;
39 long long int nacc
, nbytes
, elap
;
40 long long int avgacc
, avgelap
;
44 char olduser
[MAX_USER_LEN
], csort
[MAXLEN
];
45 char wger
[MAXLEN
], top1
[MAXLEN
], top2
[MAXLEN
], top3
[MAXLEN
], user
[MAX_USER_LEN
];
46 char strip1
[MAXLEN
], strip2
[MAXLEN
], strip3
[MAXLEN
], strip4
[MAXLEN
], strip5
[MAXLEN
], strip6
[MAXLEN
], strip7
[MAXLEN
];
54 struct getwordstruct gwarea
;
55 struct generalitemstruct item
;
57 const struct userinfostruct
*uinfo
;
59 snprintf(wger
,sizeof(wger
),"%s/sarg-general",dirname
);
60 if((fp_in
=fopen(wger
,"r"))==NULL
) {
61 debuga(_("Cannot open file \"%s\": %s\n"),wger
,strerror(errno
));
65 snprintf(top2
,sizeof(top2
),"%s/top.tmp",dirname
);
66 if((fp_top2
=fopen(top2
,"w"))==NULL
) {
67 debuga(_("Cannot open file \"%s\": %s\n"),top2
,strerror(errno
));
74 if ((line
=longline_create())==NULL
) {
75 debuga(_("Not enough memory to read file %s\n"),wger
);
79 while((buf
=longline_read(fp_in
,line
))!=NULL
) {
80 ger_read(buf
,&item
,wger
);
81 if(item
.total
) continue;
82 if(strcmp(olduser
,item
.user
) != 0) {
85 if (olduser
[0] != '\0') {
86 #if defined(__FreeBSD__)
87 fprintf(fp_top2
,"%s\t%qu\t%qu\t%qu\n",olduser
,tnbytes
,tnacc
,tnelap
);
89 fprintf(fp_top2
,"%s\t%"PRIu64
"\t%"PRIu64
"\t%"PRIu64
"\n",olduser
,(uint64_t)tnbytes
,(uint64_t)tnacc
,(uint64_t)tnelap
);
95 strcpy(olduser
,item
.user
);
101 tnbytes
+=item
.nbytes
;
106 longline_destroy(&line
);
108 if (olduser
[0] != '\0') {
109 #if defined(__FreeBSD__)
110 fprintf(fp_top2
,"%s\t%qu\t%qu\t%qu\n",olduser
,tnbytes
,tnacc
,tnelap
);
112 fprintf(fp_top2
,"%s\t%"PRIu64
"\t%"PRIu64
"\t%"PRIu64
"\n",olduser
,(uint64_t)tnbytes
,(uint64_t)tnacc
,(uint64_t)tnelap
);
119 if (fclose(fp_top2
)==EOF
) {
120 debuga(_("Write error in \"%s\": %s\n"),top2
,strerror(errno
));
124 #ifdef ENABLE_DOUBLE_CHECK_DATA
125 if (ttnacc
!=globstat
.nacc
|| ttnbytes
!=globstat
.nbytes
|| ttnelap
!=globstat
.elap
) {
126 debuga(_("Total statistics mismatch when reading %s to produce the email report\n"),wger
);
131 if (snprintf(csort
,sizeof(csort
),"sort -n -T \"%s\" -t \"\t\" -r -k 2,2 -o \"%s\" \"%s\"", TempDir
, top1
, top2
)>=sizeof(csort
)) {
132 debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),top2
,top1
);
135 cstatus
=system(csort
);
136 if (!WIFEXITED(cstatus
) || WEXITSTATUS(cstatus
)) {
137 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus
));
138 debuga(_("sort command: %s\n"),csort
);
142 if (!KeepTempLog
&& unlink(top2
)) {
143 debuga(_("Cannot delete \"%s\": %s\n"),top2
,strerror(errno
));
147 if((fp_top1
=fopen(top1
,"r"))==NULL
) {
148 debuga(_("Cannot open file \"%s\": %s\n"),top1
,strerror(errno
));
152 snprintf(top3
,sizeof(top3
),"%s/report",dirname
);
153 if((fp_top3
=fopen(top3
,"w"))==NULL
) {
154 debuga(_("Cannot open file \"%s\": %s\n"),top3
,strerror(errno
));
158 safe_strcpy(strip1
,_("Squid User Access Report"),sizeof(strip1
));
160 fprintf(fp_top3
,"%s\n",strip1
);
162 safe_strcpy(strip1
,_("Decreasing Access (bytes)"),sizeof(strip1
));
164 fprintf(fp_top3
,"%s\n",strip1
);
166 safe_strcpy(strip1
,_("Period"),sizeof(strip1
));
168 fprintf(fp_top3
,"%s %s\n\n",strip1
,period
.text
);
170 safe_strcpy(strip1
,_("NUM"),sizeof(strip1
));
172 safe_strcpy(strip2
,_("USERID"),sizeof(strip2
));
174 safe_strcpy(strip3
,_("CONNECT"),sizeof(strip3
));
176 safe_strcpy(strip4
,_("BYTES"),sizeof(strip4
));
178 safe_strcpy(strip5
,_("ELAPSED TIME"),sizeof(strip5
));
180 safe_strcpy(strip6
,_("MILLISEC"),sizeof(strip6
));
182 safe_strcpy(strip7
,pgettext("duration","TIME"),sizeof(strip7
));
185 fprintf(fp_top3
,"%-7s %-20s %-8s %-15s %%%-6s %-10s %-10s %%%-7s\n------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",strip1
,strip2
,strip3
,strip4
,strip4
,strip5
,strip6
,strip7
);
187 while(fgets(warea
,sizeof(warea
),fp_top1
)) {
189 getword_start(&gwarea
,warea
);
190 if (getword(user
,sizeof(user
),&gwarea
,'\t')<0) {
191 debuga(_("There is an invalid user ID in file %s\n"),top1
);
194 if (getword_atoll(&nbytes
,&gwarea
,'\t')<0) {
195 debuga(_("There is an invalid number of bytes in file %s\n"),top1
);
198 if (getword_atoll(&nacc
,&gwarea
,'\t')<0) {
199 debuga(_("There is an invalid number of access in file %s\n"),top1
);
202 if (getword_atoll(&elap
,&gwarea
,'\0')<0) {
203 debuga(_("There is an invalid elapsed time in file %s\n"),top1
);
207 uinfo
=userinfo_find_from_id(user
);
209 debuga(_("Unknown user ID %s in file %s\n"),user
,top1
);
213 perc
=(ttnbytes
) ? nbytes
* 100. / ttnbytes
: 0;
214 perc2
=(ttnelap
) ? elap
* 100. / ttnelap
: 0;
218 #if defined(__FreeBSD__)
219 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
);
221 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
);
226 fputs("------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",fp_top3
);
227 #if defined(__FreeBSD__)
228 fprintf(fp_top3
,"%-7s %20s %8qu %15s %8s %9s %10qu\n",_("TOTAL")," ",ttnacc
,fixnum(ttnbytes
,1)," ",buildtime(ttnelap
),ttnelap
);
230 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
);
233 // compute and write average
235 tnbytes
=(totuser
) ? ttnbytes
/ totuser
: 0;
236 avgacc
=ttnacc
/totuser
;
237 avgelap
=ttnelap
/totuser
;
244 safe_strcpy(strip1
,_("AVERAGE"),sizeof(strip1
));
246 #if defined(__FreeBSD__)
247 fprintf(fp_top3
,"%-7s %20s %8qu %15s %8s %9s %10qu\n",strip1
," ",avgacc
,fixnum(tnbytes
,1)," ",buildtime(avgelap
),avgelap
);
249 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
);
253 if (!KeepTempLog
&& unlink(top1
)) {
254 debuga(_("Cannot delete \"%s\": %s\n"),top1
,strerror(errno
));
259 local
= localtime(&t
);
260 fprintf(fp_top3
, "\n%s\n", asctime(local
));
262 if (fclose(fp_top3
)==EOF
) {
263 debuga(_("Write error in \"%s\": %s\n"),top3
,strerror(errno
));
267 if(strcmp(email
,"stdout") == 0) {
268 if((fp_top3
=fopen(top3
,"r"))==NULL
) {
269 debuga(_("Cannot open file \"%s\": %s\n"),top3
,strerror(errno
));
273 while(fgets(warea
,sizeof(warea
),fp_top3
)!=NULL
)
275 if (fclose(fp_top3
)==EOF
) {
276 debuga(_("Read error in temporary mail file %s: %s\n"),top3
,strerror(errno
));
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
);
285 debuga(_("Sending mail with command: %s\n"),warea
);
286 cstatus
=system(warea
);
287 if (!WIFEXITED(cstatus
) || WEXITSTATUS(cstatus
)) {
288 debuga(_("command return status %d\n"),WEXITSTATUS(cstatus
));
289 debuga(_("command: %s\n"),warea
);
294 //unlinkdir(TempDir,0);