]>
Commit | Line | Data |
---|---|---|
25697a35 | 1 | /* |
94ff9470 | 2 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net |
110ce984 | 3 | * 1998, 2015 |
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" |
137eb63d | 29 | #include "include/filelist.h" |
25697a35 | 30 | |
a3a7e143 FM |
31 | struct TopUserStatistics |
32 | { | |
33 | long long int ttnbytes; | |
34 | long long int ttnacc; | |
35 | long long int ttnelap; | |
36 | long long int ttnincache; | |
37 | long long int ttnoucache; | |
38 | int totuser; | |
39 | }; | |
40 | ||
41 | struct SortInfoStruct | |
42 | { | |
43 | const char *sort_field; | |
44 | const char *sort_order; | |
45 | }; | |
46 | ||
9dc20988 | 47 | extern struct globalstatstruct globstat; |
27d1fa35 | 48 | extern bool smartfilter; |
137eb63d | 49 | extern FileListObject UserAgentLog; |
9dc20988 | 50 | |
124ec0b0 FM |
51 | /*! |
52 | Save the total number of users. The number is written in sarg-users and set | |
53 | in a global variable for further reference. | |
54 | ||
55 | \param totuser The total number of users. | |
56 | */ | |
57 | static void set_total_users(int totuser) | |
58 | { | |
59 | char tusr[1024]; | |
60 | FILE *fp_ou; | |
bd43d81f | 61 | |
124ec0b0 FM |
62 | snprintf(tusr,sizeof(tusr),"%s/sarg-users",outdirname); |
63 | if((fp_ou=fopen(tusr,"w"))==NULL) { | |
af961877 | 64 | debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),tusr,strerror(errno)); |
124ec0b0 FM |
65 | exit(EXIT_FAILURE); |
66 | } | |
67 | fprintf(fp_ou,"%d\n",totuser); | |
507460ae | 68 | if (fclose(fp_ou)==EOF) { |
af961877 | 69 | debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),tusr,strerror(errno)); |
507460ae FM |
70 | exit(EXIT_FAILURE); |
71 | } | |
124ec0b0 FM |
72 | globstat.totuser=totuser; |
73 | } | |
74 | ||
a3a7e143 FM |
75 | /*! |
76 | * Generate a HTML report with the users downloading the most. | |
77 | * | |
78 | * \param ListFile Name of the file with the sorted list of users. | |
79 | * \param Statis Statistics about the data collected from the log file. | |
80 | * \param SortInfo Strings explaining how the list was sorted. | |
81 | */ | |
82 | static void TopUser_HtmlReport(const char *ListFile,struct TopUserStatistics *Statis,struct SortInfoStruct *SortInfo) | |
25697a35 | 83 | { |
800eafb8 | 84 | FileObject *fp_top1 = NULL; |
a3a7e143 | 85 | FILE *fp_top3 = NULL; |
9bd92830 FM |
86 | long long int nbytes; |
87 | long long int nacc; | |
88 | long long int elap, incac, oucac; | |
89 | double perc=0.00; | |
90 | double perc2=0.00; | |
91 | double inperc=0.00, ouperc=0.00; | |
92 | int posicao=0; | |
a3a7e143 | 93 | char top3[MAXLEN]; |
124ec0b0 | 94 | char user[MAX_USER_LEN]; |
9bd92830 FM |
95 | char title[80]; |
96 | char *warea; | |
a3a7e143 FM |
97 | bool ntopuser=false; |
98 | int topcount=0; | |
9bd92830 FM |
99 | struct getwordstruct gwarea; |
100 | longline line; | |
a58e6d54 | 101 | struct userinfostruct *uinfo; |
9bd92830 | 102 | |
a3a7e143 FM |
103 | if ((fp_top1=FileObject_Open(ListFile))==NULL) { |
104 | debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),ListFile,FileObject_GetLastOpenError()); | |
08f9b029 FM |
105 | exit(EXIT_FAILURE); |
106 | } | |
9bd92830 | 107 | |
56b97279 | 108 | snprintf(top3,sizeof(top3),"%s/"INDEX_HTML_FILE,outdirname); |
a3a7e143 | 109 | if ((fp_top3=fopen(top3,"w"))==NULL) { |
af961877 | 110 | debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),top3,strerror(errno)); |
9bd92830 FM |
111 | exit(EXIT_FAILURE); |
112 | } | |
113 | ||
114 | snprintf(title,sizeof(title),_("SARG report for %s"),period.text); | |
115 | write_html_header(fp_top3,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,title,HTML_JS_SORTTABLE); | |
116 | fputs("<tr><td class=\"header_c\">",fp_top3); | |
117 | fprintf(fp_top3,_("Period: %s"),period.html); | |
118 | fputs("</td></tr>\n",fp_top3); | |
124ec0b0 FM |
119 | if ((ReportType & REPORT_TYPE_TOPUSERS) != 0) { |
120 | fputs("<tr><td class=\"header_c\">",fp_top3); | |
a3a7e143 | 121 | fprintf(fp_top3,_("Sort: %s, %s"),SortInfo->sort_field,SortInfo->sort_order); |
124ec0b0 FM |
122 | fputs("</td></tr>\n",fp_top3); |
123 | fprintf(fp_top3,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Top users")); | |
124 | } else { | |
125 | /* TRANSLATORS: This is the title of the main report page when no | |
126 | * top users list are requested. | |
127 | */ | |
128 | fprintf(fp_top3,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Table of content")); | |
129 | } | |
9bd92830 FM |
130 | close_html_header(fp_top3); |
131 | ||
1219fb1a FM |
132 | if (!indexonly) { |
133 | fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_top3); | |
134 | if((ReportType & REPORT_TYPE_TOPSITES) != 0 && !Privacy) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"topsites.html\">%s</a></td></tr>\n",_("Top sites")); | |
135 | if((ReportType & REPORT_TYPE_SITES_USERS) != 0 && !Privacy) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"siteuser.html\">%s</a></td></tr>\n",_("Sites & Users")); | |
136 | if(dansguardian_count) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"dansguardian.html\">%s</a></td></tr>\n",_("DansGuardian")); | |
330b1c52 | 137 | if(redirector_count) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"redirector.html\">%s</a></td></tr>\n",_("Redirector")); |
11284535 | 138 | if (is_download()) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"download.html\">%s</a></td></tr>\n",_("Downloads")); |
8e53b2e7 | 139 | if (is_denied()) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"denied.html\">%s</a></td></tr>\n",_("Denied accesses")); |
16b013cc | 140 | if (is_authfail()) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"authfail.html\">%s</a></td></tr>\n",_("Authentication Failures")); |
1219fb1a | 141 | if(smartfilter) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"smartfilter.html\">%s</a></td></tr>\n",_("SmartFilter")); |
137eb63d | 142 | if (!FileList_IsEmpty(UserAgentLog) && useragent_count) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"useragent.html\">%s</a></td></tr>\n",_("Useragent")); |
1219fb1a FM |
143 | fputs("<tr><td></td></tr>\n</table></div>\n",fp_top3); |
144 | } | |
9bd92830 FM |
145 | |
146 | if ((ReportType & REPORT_TYPE_TOPUSERS) == 0) { | |
147 | fputs("</body>\n</html>\n",fp_top3); | |
507460ae | 148 | if (fclose (fp_top3)==EOF) { |
af961877 | 149 | debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),top3,strerror(errno)); |
507460ae FM |
150 | exit(EXIT_FAILURE); |
151 | } | |
af961877 | 152 | if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("No top users report because it is not configured in report_type\n")); |
9bd92830 FM |
153 | return; |
154 | } | |
155 | ||
156 | fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\"",fp_top3); | |
157 | if (SortTableJs[0]) | |
158 | fputs(" class=\"sortable\"",fp_top3); | |
159 | fputs(">\n<thead><tr>",fp_top3); | |
160 | ||
161 | if((TopUserFields & TOPUSERFIELDS_NUM) != 0) | |
162 | fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("NUM")); | |
1219fb1a | 163 | if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0 && !indexonly) { |
9bd92830 FM |
164 | fputs("<th class=\"header_l",fp_top3); |
165 | if (SortTableJs[0]) fputs(" sorttable_nosort",fp_top3); | |
166 | fputs("\"></th>",fp_top3); | |
167 | } | |
168 | if((TopUserFields & TOPUSERFIELDS_USERID) != 0) { | |
169 | fputs("<th class=\"header_l",fp_top3); | |
170 | if (SortTableJs[0]) fputs(" sorttable_alpha",fp_top3); | |
171 | fprintf(fp_top3,"\">%s</th>",_("USERID")); | |
172 | } | |
8e134f1a FM |
173 | if((TopUserFields & TOPUSERFIELDS_USERIP) != 0) { |
174 | fputs("<th class=\"header_l",fp_top3); | |
175 | if (SortTableJs[0]) fputs(" sorttable_alpha",fp_top3); | |
176 | fprintf(fp_top3,"\">%s</th>",_("USERIP")); | |
177 | } | |
9bd92830 FM |
178 | if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0) |
179 | fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("CONNECT")); | |
180 | if((TopUserFields & TOPUSERFIELDS_BYTES) != 0) | |
181 | fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("BYTES")); | |
182 | if((TopUserFields & TOPUSERFIELDS_SETYB) != 0) | |
183 | fprintf(fp_top3,"<th class=\"header_l\">%%%s</th>",_("BYTES")); | |
184 | if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0) | |
185 | fprintf(fp_top3,"<th class=\"header_c\" colspan=\"2\">%s</th><th style=\"display:none;\"></th>",_("IN-CACHE-OUT")); | |
186 | if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0) | |
187 | fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("ELAPSED TIME")); | |
188 | if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0) | |
189 | fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("MILLISEC")); | |
190 | if((TopUserFields & TOPUSERFIELDS_PTIME) != 0) | |
eb7ab620 | 191 | fprintf(fp_top3,"<th class=\"header_l\">%%%s</th>",pgettext("duration","TIME")); |
9bd92830 FM |
192 | |
193 | fputs("</tr></thead>\n",fp_top3); | |
194 | ||
58c772bb FM |
195 | greport_prepare(); |
196 | ||
9bd92830 | 197 | if ((line=longline_create())==NULL) { |
a3a7e143 | 198 | debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),ListFile); |
9bd92830 FM |
199 | exit(EXIT_FAILURE); |
200 | } | |
201 | ||
a3a7e143 | 202 | while ((warea=longline_read(fp_top1,line))!=NULL) { |
9bd92830 FM |
203 | getword_start(&gwarea,warea); |
204 | if (getword(user,sizeof(user),&gwarea,'\t')<0) { | |
a3a7e143 | 205 | debuga(__FILE__,__LINE__,_("Invalid user in file \"%s\"\n"),ListFile); |
9bd92830 FM |
206 | exit(EXIT_FAILURE); |
207 | } | |
208 | if (getword_atoll(&nbytes,&gwarea,'\t')<0) { | |
a3a7e143 | 209 | debuga(__FILE__,__LINE__,_("Invalid number of bytes in file \"%s\"\n"),ListFile); |
9bd92830 FM |
210 | exit(EXIT_FAILURE); |
211 | } | |
212 | if (getword_atoll(&nacc,&gwarea,'\t')<0) { | |
a3a7e143 | 213 | debuga(__FILE__,__LINE__,_("Invalid number of accesses in file \"%s\"\n"),ListFile); |
9bd92830 FM |
214 | exit(EXIT_FAILURE); |
215 | } | |
216 | if (getword_atoll(&elap,&gwarea,'\t')<0) { | |
a3a7e143 | 217 | debuga(__FILE__,__LINE__,_("Invalid elapsed time in file \"%s\"\n"),ListFile); |
9bd92830 FM |
218 | exit(EXIT_FAILURE); |
219 | } | |
220 | if (getword_atoll(&incac,&gwarea,'\t')<0) { | |
a3a7e143 | 221 | debuga(__FILE__,__LINE__,_("Invalid in-cache size in file \"%s\"\n"),ListFile); |
9bd92830 FM |
222 | exit(EXIT_FAILURE); |
223 | } | |
224 | if (getword_atoll(&oucac,&gwarea,'\n')<0) { | |
a3a7e143 | 225 | debuga(__FILE__,__LINE__,_("Invalid out-of-cache size in file \"%s\"\n"),ListFile); |
9bd92830 FM |
226 | exit(EXIT_FAILURE); |
227 | } | |
a3a7e143 | 228 | if (nacc < 1) |
9bd92830 | 229 | continue; |
a3a7e143 FM |
230 | ntopuser=true; |
231 | if (TopUsersNum>0 && topcount>=TopUsersNum) break; | |
9bd92830 FM |
232 | |
233 | uinfo=userinfo_find_from_id(user); | |
234 | if (!uinfo) { | |
a3a7e143 | 235 | debuga(__FILE__,__LINE__,_("Unknown user ID %s in file \"%s\"\n"),user,ListFile); |
9bd92830 FM |
236 | exit(EXIT_FAILURE); |
237 | } | |
a58e6d54 | 238 | uinfo->topuser=1; |
9bd92830 FM |
239 | |
240 | fputs("<tr>",fp_top3); | |
241 | ||
242 | posicao++; | |
243 | if((TopUserFields & TOPUSERFIELDS_NUM) != 0) | |
244 | fprintf(fp_top3,"<td class=\"data\">%d</td>",posicao); | |
245 | ||
1219fb1a FM |
246 | if (!indexonly) { |
247 | if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0) { | |
248 | fputs("<td class=\"data2\">",fp_top3); | |
25697a35 | 249 | #ifdef HAVE_GD |
1219fb1a | 250 | if(Graphs && GraphFont[0]!='\0') { |
e3e6aef4 | 251 | greport_day(uinfo); |
1219fb1a FM |
252 | //fprintf(fp_top3,"<a href=\"%s/graph_day.png\"><img src=\"%s/graph.png\" title=\"%s\" alt=\"G\"></a> ",uinfo->filename,ImageFile,_("Graphic")); |
253 | fprintf(fp_top3,"<a href=\"%s/graph.html\"><img src=\"%s/graph.png\" title=\"%s\" alt=\"G\"></a> ",uinfo->filename,ImageFile,_("Graphic")); | |
254 | } | |
25697a35 | 255 | #endif |
e3e6aef4 | 256 | report_day(uinfo); |
1219fb1a | 257 | fprintf(fp_top3,"<a href=\"%s/d%s.html\"><img src=\"%s/datetime.png\" title=\"%s\" alt=\"T\"></a></td>",uinfo->filename,uinfo->filename,ImageFile,_("date/time report")); |
e3e6aef4 | 258 | day_deletefile(uinfo); |
08f9b029 | 259 | } |
9bd92830 FM |
260 | } |
261 | if((TopUserFields & TOPUSERFIELDS_USERID) != 0) { | |
1219fb1a FM |
262 | if((ReportType & REPORT_TYPE_USERS_SITES) == 0 || indexonly) |
263 | fprintf(fp_top3,"<td class=\"data2\">%s</td>",uinfo->label); | |
9bd92830 FM |
264 | else |
265 | fprintf(fp_top3,"<td class=\"data2\"><a href=\"%s/%s.html\">%s</a></td>",uinfo->filename,uinfo->filename,uinfo->label); | |
266 | } | |
8e134f1a FM |
267 | if((TopUserFields & TOPUSERFIELDS_USERIP) != 0) { |
268 | fprintf(fp_top3,"<td class=\"data2\">%s</td>",uinfo->ip); | |
269 | } | |
9bd92830 FM |
270 | if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0) { |
271 | fputs("<td class=\"data\"",fp_top3); | |
272 | if (SortTableJs[0]) fprintf(fp_top3," sorttable_customkey=\"%"PRId64"\"",(int64_t)nacc); | |
273 | fprintf(fp_top3,">%s</td>",fixnum(nacc,1)); | |
274 | } | |
275 | if((TopUserFields & TOPUSERFIELDS_BYTES) != 0) { | |
276 | fputs("<td class=\"data\"",fp_top3); | |
a3a7e143 FM |
277 | if (SortTableJs[0]) fprintf(fp_top3," sorttable_customkey=\"%"PRId64"\"",(int64_t)nbytes); |
278 | fprintf(fp_top3,">%s</td>",fixnum(nbytes,1)); | |
9bd92830 FM |
279 | } |
280 | if((TopUserFields & TOPUSERFIELDS_SETYB) != 0) { | |
a3a7e143 | 281 | perc=(Statis->ttnbytes) ? nbytes * 100. / Statis->ttnbytes : 0.; |
9bd92830 FM |
282 | fprintf(fp_top3,"<td class=\"data\">%3.2lf%%</td>",perc); |
283 | } | |
284 | if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0) { | |
a3a7e143 FM |
285 | inperc=(nbytes) ? incac * 100. / nbytes : 0.; |
286 | ouperc=(nbytes) ? oucac * 100. / nbytes : 0.; | |
9bd92830 | 287 | fprintf(fp_top3,"<td class=\"data\">%3.2lf%%</td><td class=\"data\">%3.2lf%%</td>",inperc,ouperc); |
04a01ed3 | 288 | #ifdef ENABLE_DOUBLE_CHECK_DATA |
4e59f0a6 | 289 | if ((inperc!=0. || ouperc!=0.) && fabs(inperc+ouperc-100.)>=0.01) { |
af961877 | 290 | debuga(__FILE__,__LINE__,_("The total of the in-cache and cache-miss is not 100%% at position %d (user %s)\n"),posicao,uinfo->label); |
9bd92830 | 291 | } |
04a01ed3 | 292 | #endif |
9bd92830 FM |
293 | } |
294 | if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0) { | |
295 | fputs("<td class=\"data\"",fp_top3); | |
a3a7e143 FM |
296 | if (SortTableJs[0]) fprintf(fp_top3," sorttable_customkey=\"%"PRId64"\"",(int64_t)elap); |
297 | fprintf(fp_top3,">%s</td>",buildtime(elap)); | |
9bd92830 FM |
298 | } |
299 | if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0) { | |
300 | fputs("<td class=\"data\"",fp_top3); | |
a3a7e143 FM |
301 | if (SortTableJs[0]) fprintf(fp_top3," sorttable_customkey=\"%"PRId64"\"",(int64_t)elap); |
302 | fprintf(fp_top3,">%s</td>",fixnum2(elap,1)); | |
9bd92830 FM |
303 | } |
304 | if((TopUserFields & TOPUSERFIELDS_PTIME) != 0) { | |
a3a7e143 | 305 | perc2=(Statis->ttnelap) ? elap * 100. / Statis->ttnelap : 0.; |
9bd92830 FM |
306 | fprintf(fp_top3,"<td class=\"data\">%3.2lf%%</td>",perc2); |
307 | } | |
308 | ||
309 | fputs("</tr>\n",fp_top3); | |
310 | ||
311 | topcount++; | |
312 | } | |
800eafb8 | 313 | if (FileObject_Close(fp_top1)) { |
a3a7e143 | 314 | debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),ListFile,FileObject_GetLastCloseError()); |
204781f4 FM |
315 | exit(EXIT_FAILURE); |
316 | } | |
a3a7e143 FM |
317 | if (!KeepTempLog && unlink(ListFile)) { |
318 | debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),ListFile,strerror(errno)); | |
08f9b029 FM |
319 | exit(EXIT_FAILURE); |
320 | } | |
9bd92830 FM |
321 | longline_destroy(&line); |
322 | ||
a3a7e143 | 323 | if ((TopUserFields & TOPUSERFIELDS_TOTAL) != 0) { |
9bd92830 FM |
324 | fputs("<tfoot><tr>",fp_top3); |
325 | if((TopUserFields & TOPUSERFIELDS_NUM) != 0) | |
326 | fputs("<td></td>",fp_top3); | |
1219fb1a | 327 | if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0 && !indexonly) |
9bd92830 | 328 | fputs("<td></td>",fp_top3); |
8e134f1a FM |
329 | if((TopUserFields & TOPUSERFIELDS_USERIP) != 0) |
330 | fprintf(fp_top3,"<th class=\"header_l\" colspan=\"2\">%s</th>",_("TOTAL")); | |
331 | else | |
332 | fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("TOTAL")); | |
9bd92830 FM |
333 | |
334 | if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0) | |
a3a7e143 | 335 | fprintf(fp_top3,"<th class=\"header_r\">%s</th>",fixnum(Statis->ttnacc,1)); |
9bd92830 | 336 | if((TopUserFields & TOPUSERFIELDS_BYTES) != 0) |
a3a7e143 | 337 | fprintf(fp_top3,"<th class=\"header_r\">%15s</th>",fixnum(Statis->ttnbytes,1)); |
9bd92830 FM |
338 | if((TopUserFields & TOPUSERFIELDS_SETYB) != 0) |
339 | fputs("<td></td>",fp_top3); | |
340 | if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0) | |
341 | { | |
a3a7e143 FM |
342 | inperc=(Statis->ttnbytes) ? Statis->ttnincache * 100. / Statis->ttnbytes : 0.; |
343 | ouperc=(Statis->ttnbytes) ? Statis->ttnoucache *100. / Statis->ttnbytes : 0.; | |
9bd92830 | 344 | fprintf(fp_top3,"<th class=\"header_r\">%3.2lf%%</th><th class=\"header_r\">%3.2lf%%</th>",inperc,ouperc); |
04a01ed3 | 345 | #ifdef ENABLE_DOUBLE_CHECK_DATA |
9bd92830 | 346 | if (fabs(inperc+ouperc-100.)>=0.01) { |
af961877 | 347 | debuga(__FILE__,__LINE__,_("The total of the in-cache and cache-miss is not 100%%\n")); |
9bd92830 | 348 | } |
04a01ed3 | 349 | #endif |
9bd92830 FM |
350 | } |
351 | if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0) | |
a3a7e143 | 352 | fprintf(fp_top3,"<th class=\"header_r\">%s</th>",buildtime(Statis->ttnelap)); |
9bd92830 | 353 | if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0) |
a3a7e143 | 354 | fprintf(fp_top3,"<th class=\"header_r\">%s</th>",fixnum2(Statis->ttnelap,1)); |
9bd92830 FM |
355 | |
356 | fputs("</tr>\n",fp_top3); | |
357 | } | |
58c772bb | 358 | greport_cleanup(); |
9bd92830 | 359 | |
a3a7e143 | 360 | if (ntopuser && (TopUserFields & TOPUSERFIELDS_AVERAGE) != 0) { |
9bd92830 FM |
361 | fputs("<tr>",fp_top3); |
362 | if((TopUserFields & TOPUSERFIELDS_NUM) != 0) | |
363 | fputs("<td></td>",fp_top3); | |
1219fb1a | 364 | if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0 && !indexonly) |
9bd92830 | 365 | fputs("<td></td>",fp_top3); |
8e134f1a FM |
366 | if((TopUserFields & TOPUSERFIELDS_USERIP) != 0) |
367 | fprintf(fp_top3,"<th class=\"header_l\" colspan=\"2\">%s</th>",_("AVERAGE")); | |
368 | else | |
369 | fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("AVERAGE")); | |
9bd92830 FM |
370 | |
371 | if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0) | |
a3a7e143 | 372 | fprintf(fp_top3,"<th class=\"header_r\">%s</th>",fixnum(Statis->ttnacc/Statis->totuser,1)); |
9bd92830 | 373 | if((TopUserFields & TOPUSERFIELDS_BYTES) != 0) { |
a3a7e143 FM |
374 | nbytes=(Statis->totuser) ? Statis->ttnbytes / Statis->totuser : 0; |
375 | fprintf(fp_top3,"<th class=\"header_r\">%15s</th>",fixnum(nbytes,1)); | |
9bd92830 FM |
376 | } |
377 | if((TopUserFields & TOPUSERFIELDS_SETYB) != 0) | |
378 | fputs("<td></td>",fp_top3); | |
379 | if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0) | |
380 | fputs("<td></td><td></td>",fp_top3); | |
381 | if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0) | |
a3a7e143 | 382 | fprintf(fp_top3,"<th class=\"header_r\">%s</th>",buildtime(Statis->ttnelap/Statis->totuser)); |
9bd92830 | 383 | if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0) |
a3a7e143 | 384 | fprintf(fp_top3,"<th class=\"header_r\">%s</th>",fixnum2(Statis->ttnelap/Statis->totuser,1)); |
9bd92830 FM |
385 | fputs("</tr></tfoot>\n",fp_top3); |
386 | } | |
387 | ||
388 | fputs("</table></div>\n",fp_top3); | |
342bd723 | 389 | write_html_trailer(fp_top3); |
507460ae | 390 | if (fclose(fp_top3)==EOF) { |
af961877 | 391 | debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),top3,strerror(errno)); |
507460ae FM |
392 | exit(EXIT_FAILURE); |
393 | } | |
a3a7e143 FM |
394 | } |
395 | ||
6bbb47cf FM |
396 | /*! |
397 | Generate the top user email report. | |
398 | */ | |
399 | static void TopUser_TextEmail(const char *ListFile,struct TopUserStatistics *Statis,struct SortInfoStruct *SortInfo) | |
400 | { | |
401 | FileObject *fp_top1; | |
402 | FILE *fp_mail; | |
403 | longline line; | |
404 | struct getwordstruct gwarea; | |
405 | char *warea; | |
406 | char user[MAX_USER_LEN]; | |
407 | char strip1[MAXLEN], strip2[MAXLEN], strip3[MAXLEN], strip4[MAXLEN], strip5[MAXLEN], strip6[MAXLEN], strip7[MAXLEN]; | |
408 | long long int nbytes; | |
409 | long long int nacc; | |
410 | long long int elap, incac, oucac; | |
411 | double perc=0.00; | |
412 | double perc2=0.00; | |
413 | long long int tnbytes=0; | |
414 | long long int avgacc, avgelap; | |
415 | int topcount=0; | |
416 | struct userinfostruct *uinfo; | |
417 | time_t t; | |
418 | struct tm *local; | |
419 | const char *Subject; | |
420 | ||
421 | if ((fp_top1=FileObject_Open(ListFile))==NULL) { | |
422 | debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),ListFile,FileObject_GetLastOpenError()); | |
423 | exit(EXIT_FAILURE); | |
424 | } | |
425 | ||
426 | fp_mail=Email_OutputFile("topuser"); | |
427 | ||
428 | if ((line=longline_create())==NULL) { | |
429 | debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),ListFile); | |
430 | exit(EXIT_FAILURE); | |
431 | } | |
432 | ||
433 | safe_strcpy(strip1,_("Squid User Access Report"),sizeof(strip1)); | |
434 | strip_latin(strip1); | |
435 | fprintf(fp_mail,"%s\n",strip1); | |
436 | ||
437 | snprintf(strip1,sizeof(strip1),_("Sort: %s, %s"),SortInfo->sort_field,SortInfo->sort_order); | |
438 | strip_latin(strip1); | |
439 | fprintf(fp_mail,"%s\n",strip1); | |
440 | ||
441 | snprintf(strip1,sizeof(strip1),_("Period: %s"),period.text); | |
442 | strip_latin(strip1); | |
443 | fprintf(fp_mail,"%s\n\n",strip1); | |
444 | ||
445 | safe_strcpy(strip1,_("NUM"),sizeof(strip1)); | |
446 | strip_latin(strip1); | |
447 | safe_strcpy(strip2,_("USERID"),sizeof(strip2)); | |
448 | strip_latin(strip2); | |
449 | safe_strcpy(strip3,_("CONNECT"),sizeof(strip3)); | |
450 | strip_latin(strip3); | |
451 | safe_strcpy(strip4,_("BYTES"),sizeof(strip4)); | |
452 | strip_latin(strip4); | |
453 | safe_strcpy(strip5,_("ELAPSED TIME"),sizeof(strip5)); | |
454 | strip_latin(strip5); | |
455 | safe_strcpy(strip6,_("MILLISEC"),sizeof(strip6)); | |
456 | strip_latin(strip6); | |
457 | safe_strcpy(strip7,pgettext("duration","TIME"),sizeof(strip7)); | |
458 | strip_latin(strip7); | |
459 | ||
460 | fprintf(fp_mail,"%-7s %-20s %-9s %-15s %%%-6s %-11s %-10s %%%-7s\n------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",strip1,strip2,strip3,strip4,strip4,strip5,strip6,strip7); | |
461 | ||
462 | ||
463 | while ((warea=longline_read(fp_top1,line))!=NULL) { | |
464 | getword_start(&gwarea,warea); | |
465 | if (getword(user,sizeof(user),&gwarea,'\t')<0) { | |
466 | debuga(__FILE__,__LINE__,_("Invalid user in file \"%s\"\n"),ListFile); | |
467 | exit(EXIT_FAILURE); | |
468 | } | |
469 | if (getword_atoll(&nbytes,&gwarea,'\t')<0) { | |
470 | debuga(__FILE__,__LINE__,_("Invalid number of bytes in file \"%s\"\n"),ListFile); | |
471 | exit(EXIT_FAILURE); | |
472 | } | |
473 | if (getword_atoll(&nacc,&gwarea,'\t')<0) { | |
474 | debuga(__FILE__,__LINE__,_("Invalid number of accesses in file \"%s\"\n"),ListFile); | |
475 | exit(EXIT_FAILURE); | |
476 | } | |
477 | if (getword_atoll(&elap,&gwarea,'\t')<0) { | |
478 | debuga(__FILE__,__LINE__,_("Invalid elapsed time in file \"%s\"\n"),ListFile); | |
479 | exit(EXIT_FAILURE); | |
480 | } | |
481 | if (getword_atoll(&incac,&gwarea,'\t')<0) { | |
482 | debuga(__FILE__,__LINE__,_("Invalid in-cache size in file \"%s\"\n"),ListFile); | |
483 | exit(EXIT_FAILURE); | |
484 | } | |
485 | if (getword_atoll(&oucac,&gwarea,'\n')<0) { | |
486 | debuga(__FILE__,__LINE__,_("Invalid out-of-cache size in file \"%s\"\n"),ListFile); | |
487 | exit(EXIT_FAILURE); | |
488 | } | |
489 | if (nacc < 1) | |
490 | continue; | |
491 | if (TopUsersNum>0 && topcount>=TopUsersNum) break; | |
492 | ||
493 | uinfo=userinfo_find_from_id(user); | |
494 | if (!uinfo) { | |
495 | debuga(__FILE__,__LINE__,_("Unknown user ID %s in file \"%s\"\n"),user,ListFile); | |
496 | exit(EXIT_FAILURE); | |
497 | } | |
498 | uinfo->topuser=1; | |
499 | ||
500 | perc=(Statis->ttnbytes) ? nbytes * 100. / Statis->ttnbytes : 0; | |
501 | perc2=(Statis->ttnelap) ? elap * 100. / Statis->ttnelap : 0; | |
502 | ||
503 | topcount++; | |
504 | ||
505 | #if defined(__FreeBSD__) | |
506 | fprintf(fp_mail,"%7d %20s %8lld %15s %5.2lf%% %10s %10qu %3.2lf%%\n",topcount,uinfo->label,nacc,fixnum(nbytes,1),perc,buildtime(elap),elap,perc2); | |
507 | #else | |
508 | fprintf(fp_mail,"%7d %20s %8"PRIu64" %15s %6.2lf%% %10s %10"PRIu64" %3.2lf%%\n",topcount,uinfo->label,(uint64_t)nacc,fixnum(nbytes,1),perc,buildtime(elap),(uint64_t)elap,perc2); | |
509 | #endif | |
510 | } | |
511 | if (FileObject_Close(fp_top1)) { | |
512 | debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),ListFile,FileObject_GetLastCloseError()); | |
513 | exit(EXIT_FAILURE); | |
514 | } | |
515 | if (!KeepTempLog && unlink(ListFile)) { | |
516 | debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),ListFile,strerror(errno)); | |
517 | exit(EXIT_FAILURE); | |
518 | } | |
519 | longline_destroy(&line); | |
520 | ||
521 | // output total | |
522 | fputs("------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",fp_mail); | |
523 | #if defined(__FreeBSD__) | |
524 | fprintf(fp_mail,"%-7s %20s %8qu %15s %8s %9s %10qu\n",_("TOTAL")," ",Statis->ttnacc,fixnum(Statis->ttnbytes,1)," ",buildtime(Statis->ttnelap),Statis->ttnelap); | |
525 | #else | |
526 | fprintf(fp_mail,"%-7s %20s %8"PRIu64" %15s %8s %9s %10"PRIu64"\n",_("TOTAL")," ",(uint64_t)Statis->ttnacc,fixnum(Statis->ttnbytes,1)," ",buildtime(Statis->ttnelap),(uint64_t)Statis->ttnelap); | |
527 | #endif | |
528 | ||
529 | // compute and write average | |
530 | if (Statis->totuser>0) { | |
531 | tnbytes=Statis->ttnbytes / Statis->totuser; | |
532 | avgacc=Statis->ttnacc/Statis->totuser; | |
533 | avgelap=Statis->ttnelap/Statis->totuser; | |
534 | } else { | |
535 | tnbytes=0; | |
536 | avgacc=0; | |
537 | avgelap=0; | |
538 | } | |
539 | ||
540 | safe_strcpy(strip1,_("AVERAGE"),sizeof(strip1)); | |
541 | strip_latin(strip1); | |
542 | #if defined(__FreeBSD__) | |
543 | fprintf(fp_mail,"%-7s %20s %8qu %15s %8s %9s %10qu\n",strip1," ",avgacc,fixnum(tnbytes,1)," ",buildtime(avgelap),avgelap); | |
544 | #else | |
545 | fprintf(fp_mail,"%-7s %20s %8"PRIu64" %15s %8s %9s %10"PRIu64"\n",strip1," ",(uint64_t)avgacc,fixnum(tnbytes,1)," ",buildtime(avgelap),(uint64_t)avgelap); | |
546 | #endif | |
547 | ||
548 | t = time(NULL); | |
549 | local = localtime(&t); | |
550 | fprintf(fp_mail, "\n%s\n", asctime(local)); | |
551 | ||
552 | /* TRANSLATORS: This is the e-mail subject. */ | |
553 | Subject=_("Sarg: top user report"); | |
554 | Email_Send(fp_mail,Subject); | |
555 | } | |
556 | ||
a3a7e143 FM |
557 | /*! |
558 | * Produce a report with the user downloading the most data. | |
559 | */ | |
560 | void topuser(void) | |
561 | { | |
562 | FileObject *fp_in = NULL; | |
563 | FILE *fp_top2; | |
564 | char wger[MAXLEN]; | |
565 | char top1[MAXLEN]; | |
566 | char top2[MAXLEN]; | |
567 | longline line; | |
568 | long long int tnacc=0; | |
569 | long long int tnbytes=0, tnelap=0; | |
570 | long long int tnincache=0, tnoucache=0; | |
571 | char *warea; | |
572 | struct generalitemstruct item; | |
573 | char olduser[MAX_USER_LEN], csort[MAXLEN]; | |
574 | const char *sfield="-n -k 2,2"; | |
575 | const char *order; | |
576 | int cstatus; | |
577 | struct TopUserStatistics Statis; | |
578 | struct SortInfoStruct SortInfo; | |
579 | ||
580 | if (debugz>=LogLevel_Process) | |
581 | debuga(__FILE__,__LINE__,_("Creating top users report...\n")); | |
582 | ||
583 | memset(&Statis,0,sizeof(Statis)); | |
584 | ||
585 | snprintf(wger,sizeof(wger),"%s/sarg-general",outdirname); | |
586 | if ((fp_in=FileObject_Open(wger))==NULL) { | |
587 | debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),wger,FileObject_GetLastOpenError()); | |
588 | exit(EXIT_FAILURE); | |
589 | } | |
590 | ||
591 | snprintf(top2,sizeof(top2),"%s/top.tmp",outdirname); | |
592 | if ((fp_top2=fopen(top2,"w"))==NULL) { | |
593 | debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),top2,strerror(errno)); | |
594 | exit(EXIT_FAILURE); | |
595 | } | |
596 | ||
597 | olduser[0]='\0'; | |
598 | ||
599 | if ((line=longline_create())==NULL) { | |
600 | debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),wger); | |
601 | exit(EXIT_FAILURE); | |
602 | } | |
603 | ||
604 | while ((warea=longline_read(fp_in,line))!=NULL) { | |
605 | ger_read(warea,&item,wger); | |
606 | if (item.total) continue; | |
607 | if (strcmp(olduser,item.user) != 0) { | |
608 | Statis.totuser++; | |
609 | ||
610 | if (olduser[0] != '\0') { | |
611 | /* | |
612 | This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable | |
613 | to print a long long int unless it is exactly 64-bits long. | |
614 | */ | |
615 | fprintf(fp_top2,"%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",olduser,(uint64_t)tnbytes,(uint64_t)tnacc,(uint64_t)tnelap,(uint64_t)tnincache,(uint64_t)tnoucache); | |
616 | ||
617 | Statis.ttnbytes+=tnbytes; | |
618 | Statis.ttnacc+=tnacc; | |
619 | Statis.ttnelap+=tnelap; | |
620 | Statis.ttnincache+=tnincache; | |
621 | Statis.ttnoucache+=tnoucache; | |
622 | } | |
623 | safe_strcpy(olduser,item.user,sizeof(olduser)); | |
624 | tnbytes=0; | |
625 | tnacc=0; | |
626 | tnelap=0; | |
627 | tnincache=0; | |
628 | tnoucache=0; | |
629 | } | |
630 | ||
631 | tnbytes+=item.nbytes; | |
632 | tnacc+=item.nacc; | |
633 | tnelap+=item.nelap; | |
634 | tnincache+=item.incache; | |
635 | tnoucache+=item.oucache; | |
636 | } | |
637 | if (FileObject_Close(fp_in)) { | |
638 | debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),wger,FileObject_GetLastCloseError()); | |
639 | exit(EXIT_FAILURE); | |
640 | } | |
641 | longline_destroy(&line); | |
642 | ||
643 | if (olduser[0] != '\0') { | |
644 | /* | |
645 | This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable | |
646 | to print a long long int unless it is exactly 64-bits long. | |
647 | */ | |
648 | fprintf(fp_top2,"%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",olduser,(uint64_t)tnbytes,(uint64_t)tnacc,(uint64_t)tnelap,(uint64_t)tnincache,(uint64_t)tnoucache); | |
649 | ||
650 | Statis.ttnbytes+=tnbytes; | |
651 | Statis.ttnacc+=tnacc; | |
652 | Statis.ttnelap+=tnelap; | |
653 | Statis.ttnincache+=tnincache; | |
654 | Statis.ttnoucache+=tnoucache; | |
655 | } | |
656 | if (fclose(fp_top2)==EOF) { | |
657 | debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),top2,strerror(errno)); | |
658 | exit(EXIT_FAILURE); | |
659 | } | |
660 | ||
661 | #ifdef ENABLE_DOUBLE_CHECK_DATA | |
662 | if (Statis.ttnacc!=globstat.nacc || Statis.ttnbytes!=globstat.nbytes || Statis.ttnelap!=globstat.elap || | |
663 | Statis.ttnincache!=globstat.incache || Statis.ttnoucache!=globstat.oucache) { | |
664 | debuga(__FILE__,__LINE__,_("Total statistics mismatch when reading \"%s\" to produce the top users\n"),wger); | |
665 | exit(EXIT_FAILURE); | |
666 | } | |
667 | #endif | |
668 | ||
669 | set_total_users(Statis.totuser); | |
670 | ||
671 | if((TopuserSort & TOPUSER_SORT_USER) != 0) { | |
672 | sfield="-k 1,1"; | |
673 | SortInfo.sort_field=_("user"); | |
674 | } else if((TopuserSort & TOPUSER_SORT_CONNECT) != 0) { | |
675 | sfield="-n -k 3,3"; | |
676 | SortInfo.sort_field=_("connect"); | |
677 | } else if((TopuserSort & TOPUSER_SORT_TIME) != 0) { | |
678 | sfield="-n -k 4,4"; | |
679 | SortInfo.sort_field=pgettext("duration","time"); | |
680 | } else { | |
681 | SortInfo.sort_field=_("bytes"); | |
682 | } | |
683 | ||
684 | if((TopuserSort & TOPUSER_SORT_REVERSE) == 0) { | |
685 | order=""; | |
686 | SortInfo.sort_order=_("normal"); | |
687 | } else { | |
688 | order="-r"; | |
689 | SortInfo.sort_order=_("reverse"); | |
690 | } | |
691 | ||
692 | snprintf(top1,sizeof(top1),"%s/top",outdirname); | |
693 | if (snprintf(csort,sizeof(csort),"sort -T \"%s\" -t \"\t\" %s %s -o \"%s\" \"%s\"", tmp, order, sfield, top1, top2)>=sizeof(csort)) { | |
694 | debuga(__FILE__,__LINE__,_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),top2,top1); | |
695 | exit(EXIT_FAILURE); | |
696 | } | |
697 | cstatus=system(csort); | |
698 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
699 | debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEXITSTATUS(cstatus)); | |
700 | debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort); | |
701 | exit(EXIT_FAILURE); | |
702 | } | |
703 | ||
704 | if (!KeepTempLog && unlink(top2)) { | |
705 | debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),top2,strerror(errno)); | |
706 | exit(EXIT_FAILURE); | |
707 | } | |
9bd92830 | 708 | |
6bbb47cf FM |
709 | if (email[0]) |
710 | TopUser_TextEmail(top1,&Statis,&SortInfo); | |
711 | else | |
712 | TopUser_HtmlReport(top1,&Statis,&SortInfo); | |
25697a35 | 713 | } |