]>
Commit | Line | Data |
---|---|---|
25697a35 | 1 | /* |
94ff9470 | 2 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net |
fbd133bb | 3 | * 1998, 2011 |
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 | ||
78eeb33f FM |
133 | if (snprintf(csort,sizeof(csort),"sort -n -T \"%s\" -t \"\t\" -r -k 2,2 -o \"%s\" \"%s\"", TempDir, top1, top2)>=sizeof(csort)) { |
134 | debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),top2,top1); | |
135 | exit(EXIT_FAILURE); | |
136 | } | |
9bd92830 FM |
137 | cstatus=system(csort); |
138 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
139 | debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); | |
140 | debuga(_("sort command: %s\n"),csort); | |
141 | exit(EXIT_FAILURE); | |
142 | } | |
143 | ||
08f9b029 FM |
144 | if (unlink(top2)) { |
145 | debuga(_("Cannot delete %s - %s\n"),top2,strerror(errno)); | |
146 | exit(EXIT_FAILURE); | |
147 | } | |
9bd92830 FM |
148 | |
149 | if((fp_top1=fopen(top1,"r"))==NULL) { | |
150 | debuga(_("(email) Cannot open file %s\n"),top1); | |
151 | exit(EXIT_FAILURE); | |
152 | } | |
153 | ||
154 | snprintf(top3,sizeof(top3),"%s/report",dirname); | |
155 | if((fp_top3=fopen(top3,"w"))==NULL) { | |
156 | debuga(_("(email) Cannot open file %s\n"),top3); | |
157 | exit(EXIT_FAILURE); | |
158 | } | |
159 | ||
160 | strcpy(strip1,_("Squid User Access Report")); | |
161 | strip_latin(strip1); | |
162 | fprintf(fp_top3,"%s\n",strip1); | |
163 | ||
164 | strcpy(strip1,_("Decreasing Access (bytes)")); | |
165 | strip_latin(strip1); | |
166 | fprintf(fp_top3,"%s\n",strip1); | |
167 | ||
168 | strcpy(strip1,_("Period")); | |
169 | strip_latin(strip1); | |
170 | fprintf(fp_top3,"%s %s\n\n",strip1,period.text); | |
171 | ||
172 | strcpy(strip1,_("NUM")); | |
173 | strip_latin(strip1); | |
174 | strcpy(strip2,_("USERID")); | |
175 | strip_latin(strip2); | |
176 | strcpy(strip3,_("CONNECT")); | |
177 | strip_latin(strip3); | |
178 | strcpy(strip4,_("BYTES")); | |
179 | strip_latin(strip4); | |
180 | strcpy(strip5,_("ELAPSED TIME")); | |
181 | strip_latin(strip5); | |
182 | strcpy(strip6,_("MILLISEC")); | |
183 | strip_latin(strip6); | |
184 | strcpy(strip7,_("TIME")); | |
185 | strip_latin(strip7); | |
186 | ||
187 | fprintf(fp_top3,"%-7s %-20s %-8s %-15s %%%-6s %-10s %-10s %%%-7s\n------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",strip1,strip2,strip3,strip4,strip4,strip5,strip6,strip7); | |
188 | ||
007905af | 189 | while(fgets(warea,sizeof(warea),fp_top1)) { |
9bd92830 FM |
190 | fixendofline(warea); |
191 | getword_start(&gwarea,warea); | |
192 | if (getword(user,sizeof(user),&gwarea,'\t')<0) { | |
193 | debuga(_("There is an invalid user ID in file %s\n"),top1); | |
194 | exit(EXIT_FAILURE); | |
195 | } | |
196 | if (getword_atoll(&nbytes,&gwarea,'\t')<0) { | |
197 | debuga(_("There is an invalid number of bytes in file %s\n"),top1); | |
198 | exit(EXIT_FAILURE); | |
199 | } | |
200 | if (getword_atoll(&nacc,&gwarea,'\t')<0) { | |
201 | debuga(_("There is an invalid number of access in file %s\n"),top1); | |
202 | exit(EXIT_FAILURE); | |
203 | } | |
204 | if (getword_atoll(&elap,&gwarea,'\0')<0) { | |
205 | debuga(_("There is an invalid elapsed time in file %s\n"),top1); | |
206 | exit(EXIT_FAILURE); | |
207 | } | |
208 | ||
209 | uinfo=userinfo_find_from_id(user); | |
210 | if (!uinfo) { | |
211 | debuga(_("Unknown user ID %s in file %s\n"),user,top1); | |
212 | exit(EXIT_FAILURE); | |
213 | } | |
214 | ||
215 | perc=(ttnbytes) ? nbytes * 100. / ttnbytes : 0; | |
216 | perc2=(ttnelap) ? elap * 100. / ttnelap : 0; | |
217 | ||
218 | posicao++; | |
25697a35 GS |
219 | |
220 | #if defined(__FreeBSD__) | |
9bd92830 | 221 | 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 | 222 | #else |
9bd92830 | 223 | 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 | 224 | #endif |
9bd92830 | 225 | } |
02808722 | 226 | |
9bd92830 FM |
227 | // output total |
228 | fputs("------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",fp_top3); | |
25697a35 | 229 | #if defined(__FreeBSD__) |
9bd92830 | 230 | fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",_("TOTAL")," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap); |
25697a35 | 231 | #else |
9bd92830 | 232 | 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 | 233 | #endif |
25697a35 | 234 | |
9bd92830 FM |
235 | // compute and write average |
236 | if (totuser>0) { | |
237 | tnbytes=(totuser) ? ttnbytes / totuser : 0; | |
238 | avgacc=ttnacc/totuser; | |
239 | avgelap=ttnelap/totuser; | |
240 | } else { | |
241 | tnbytes=0; | |
242 | avgacc=0; | |
243 | avgelap=0; | |
244 | } | |
245 | ||
246 | strcpy(strip1,_("AVERAGE")); | |
247 | strip_latin(strip1); | |
25697a35 | 248 | #if defined(__FreeBSD__) |
9bd92830 | 249 | fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",strip1," ",avgacc,fixnum(tnbytes,1)," ",buildtime(avgelap),avgelap); |
25697a35 | 250 | #else |
9bd92830 | 251 | 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 | 252 | #endif |
25697a35 | 253 | |
9bd92830 | 254 | fclose(fp_top1); |
08f9b029 FM |
255 | if (unlink(top1)) { |
256 | debuga(_("Cannot delete %s - %s\n"),top1,strerror(errno)); | |
257 | exit(EXIT_FAILURE); | |
258 | } | |
25697a35 | 259 | |
9bd92830 FM |
260 | t = time(NULL); |
261 | local = localtime(&t); | |
262 | fprintf(fp_top3, "\n%s\n", asctime(local)); | |
25697a35 | 263 | |
9bd92830 | 264 | fclose(fp_top3); |
25697a35 | 265 | |
9bd92830 FM |
266 | if(strcmp(email,"stdout") == 0) { |
267 | if((fp_top3=fopen(top3,"r"))==NULL) { | |
268 | debuga(_("(email) Cannot open file %s\n"),top3); | |
269 | exit(EXIT_FAILURE); | |
270 | } | |
25697a35 | 271 | |
9bd92830 FM |
272 | while(fgets(warea,sizeof(warea),fp_top3)!=NULL) |
273 | fputs(warea,stdout); | |
274 | } else { | |
275 | snprintf(warea,sizeof(warea),"%s -s \"SARG %s, %s\" \"%s\" <\"%s\"",MailUtility,_("Report"),asctime(local),email,top3); | |
276 | cstatus=system(warea); | |
277 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
278 | debuga(_("command return status %d\n"),WEXITSTATUS(cstatus)); | |
279 | debuga(_("command: %s\n"),warea); | |
280 | exit(EXIT_FAILURE); | |
281 | } | |
282 | } | |
fb7c5f27 | 283 | |
9bd92830 | 284 | //unlinkdir(TempDir,0); |
25697a35 | 285 | |
9bd92830 | 286 | return (0); |
25697a35 | 287 | } |