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