]> git.ipfire.org Git - thirdparty/sarg.git/blob - topuser.c
Be more consistent with the use of the temporary directory (i.e. always use /tmp...
[thirdparty/sarg.git] / topuser.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2010
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
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"
28 #include "include/defs.h"
29
30 void topuser(void)
31 {
32
33 FILE *fp_in = NULL, *fp_ou = NULL, *fp_top1 = NULL, *fp_top2 = NULL, *fp_top3 = NULL;
34 long long int ttnbytes=0, ttnacc=0, tnacc=0;
35 long long int tnbytes=0, ttnelap=0, tnelap=0;
36 long long int tnincache=0, tnoucache=0, ttnincache=0, ttnoucache=0;
37 long long int nbytes;
38 long long int nacc;
39 long long int elap, incac, oucac;
40 double perc=0.00;
41 double perc2=0.00;
42 double inperc=0.00, ouperc=0.00;
43 int posicao=0;
44 char olduser[MAX_USER_LEN], csort[MAXLEN];
45 char wger[MAXLEN], top1[MAXLEN], top2[MAXLEN], top3[MAXLEN];
46 char user[MAX_USER_LEN], tusr[MAXLEN];
47 char ipantes[MAXLEN], nameantes[MAXLEN];
48 const char *sfield="-n -k 2,2";
49 const char *order="-r";
50 char title[80];
51 char *warea;
52 int totuser=0;
53 int topcount=0;
54 int cstatus;
55 struct getwordstruct gwarea;
56 longline line;
57 struct generalitemstruct item;
58 const struct userinfostruct *uinfo;
59
60 ipantes[0]='\0';
61 nameantes[0]='\0';
62
63 ntopuser = 0;
64 snprintf(wger,sizeof(wger),"%s/sarg-general",outdirname);
65 if((fp_in=fopen(wger,"r"))==NULL) {
66 debuga(_("(topuser) Cannot open file %s\n"),wger);
67 exit(EXIT_FAILURE);
68 }
69
70 snprintf(top2,sizeof(top2),"%s/top.tmp",outdirname);
71 if((fp_top2=fopen(top2,"w"))==NULL) {
72 debuga(_("(topuser) 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 the file %s\n"),wger);
81 exit(EXIT_FAILURE);
82 }
83
84 while((warea=longline_read(fp_in,line))!=NULL) {
85 ger_read(warea,&item,wger);
86 if(item.total) continue;
87 if(strcmp(olduser,item.user) != 0) {
88 totuser++;
89
90 if (olduser[0] != '\0') {
91 /*
92 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
93 to print a long long int unless it is exactly 64-bits long.
94 */
95 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);
96
97 ttnbytes+=tnbytes;
98 ttnacc+=tnacc;
99 ttnelap+=tnelap;
100 ttnincache+=tnincache;
101 ttnoucache+=tnoucache;
102 }
103 strcpy(olduser,item.user);
104 tnbytes=0;
105 tnacc=0;
106 tnelap=0;
107 tnincache=0;
108 tnoucache=0;
109 }
110
111 tnbytes+=item.nbytes;
112 tnacc+=item.nacc;
113 tnelap+=item.nelap;
114 tnincache+=item.incache;
115 tnoucache+=item.oucache;
116 }
117 fclose(fp_in);
118 longline_destroy(&line);
119
120 if (olduser[0] != '\0') {
121 /*
122 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
123 to print a long long int unless it is exactly 64-bits long.
124 */
125 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);
126
127 ttnbytes+=tnbytes;
128 ttnacc+=tnacc;
129 ttnelap+=tnelap;
130 ttnincache+=tnincache;
131 ttnoucache+=tnoucache;
132 }
133 fclose(fp_top2);
134
135 strup(TopuserSortField);
136 strlow(TopuserSortOrder);
137
138 if(strcmp(TopuserSortField,"USER") == 0)
139 sfield="-k 1,1";
140
141 if(strcmp(TopuserSortField,"CONNECT") == 0)
142 sfield="-n -k 3,3";
143
144 if(strcmp(TopuserSortField,"TIME") == 0)
145 sfield="-n -k 4,4";
146
147 if(strcmp(TopuserSortOrder,"normal") == 0)
148 order="";
149
150 snprintf(top1,sizeof(top1),"%s/top",outdirname);
151 sprintf(csort,"sort -T \"%s\" %s %s -o \"%s\" \"%s\"", tmp, order, sfield, top1, top2);
152 cstatus=system(csort);
153 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
154 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
155 debuga(_("sort command: %s\n"),csort);
156 exit(EXIT_FAILURE);
157 }
158
159 if((fp_top1=fopen(top1,"r"))==NULL) {
160 debuga(_("(topuser) Cannot open file %s\n"),top1);
161 exit(EXIT_FAILURE);
162 }
163
164 unlink(top2);
165
166 snprintf(top3,sizeof(top3),"%s/index.html",outdirname);
167 if((fp_top3=fopen(top3,"w"))==NULL) {
168 debuga(_("(topuser) Cannot open file %s\n"),top3);
169 exit(EXIT_FAILURE);
170 }
171
172 snprintf(title,sizeof(title),_("SARG report for %s"),period.text);
173 write_html_header(fp_top3,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,title);
174 fputs("<tr><td class=\"header_c\">",fp_top3);
175 fprintf(fp_top3,_("Period: %s"),period.html);
176 fputs("</td></tr>\n",fp_top3);
177 fprintf(fp_top3,"<tr><td class=\"header_c\">%s: %s, %s</td></tr>\n",_("Sort"),TopuserSortField,TopuserSortOrder);
178 fprintf(fp_top3,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Top users"));
179 close_html_header(fp_top3);
180
181 fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_top3);
182
183 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"));
184 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"));
185 if(dansguardian_count) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"dansguardian.html\">%s</a></td></tr>\n",_("DansGuardian"));
186 if(squidguard_count) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"redirector.html\">%s</a></td></tr>\n",_("Redirector"));
187 if ((ReportType & REPORT_TYPE_DOWNLOADS) != 0 && download_count && !Privacy && ndownload) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"download.html\">%s</a></td></tr>\n",_("Downloads"));
188 if ((ReportType & REPORT_TYPE_DENIED) != 0 && denied_count && !Privacy) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"denied.html\">%s</a></td></tr>\n",_("Denied accesses"));
189 if ((ReportType & REPORT_TYPE_AUTH_FAILURES) != 0 && authfail_count && !Privacy) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"authfail.html\">%s</a></td></tr>\n",_("Authentication Failures"));
190 if(smartfilter) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"smartfilter.html\">%s</a></td></tr>\n",_("SmartFilter"));
191 if(UserAgentLog[0] != '\0' && useragent_count) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"useragent.html\">%s</a></td></tr>\n",_("Useragent"));
192 fputs("<tr><td></td></tr>\n</table></div>\n",fp_top3);
193
194 if ((ReportType & REPORT_TYPE_TOPUSERS) == 0) {
195 fputs("</body>\n</html>\n",fp_top3);
196 fclose (fp_top3);
197 return;
198 }
199
200 fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_top3);
201 fputs("<tr>",fp_top3);
202
203 if((TopUserFields & TOPUSERFIELDS_NUM) != 0)
204 fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("NUM"));
205 if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0)
206 fputs("<th class=\"header_l\"></th>",fp_top3);
207 if((TopUserFields & TOPUSERFIELDS_USERID) != 0)
208 fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("USERID"));
209 if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0)
210 fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("CONNECT"));
211 if((TopUserFields & TOPUSERFIELDS_BYTES) != 0)
212 fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("BYTES"));
213 if((TopUserFields & TOPUSERFIELDS_SETYB) != 0)
214 fprintf(fp_top3,"<th class=\"header_l\">%%%s</th>",_("BYTES"));
215 if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0)
216 fprintf(fp_top3,"<th class=\"header_c\" colspan=\"2\">%s</th>",_("IN-CACHE-OUT"));
217 if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0)
218 fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("ELAPSED TIME"));
219 if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0)
220 fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("MILLISEC"));
221 if((TopUserFields & TOPUSERFIELDS_PTIME) != 0)
222 fprintf(fp_top3,"<th class=\"header_l\">%%%s</th>",_("TIME"));
223
224 fputs("</tr>\n",fp_top3);
225
226 ntopuser = 0;
227
228 if ((line=longline_create())==NULL) {
229 debuga(_("Not enough memory to read the downloaded files\n"));
230 exit(EXIT_FAILURE);
231 }
232
233 while((warea=longline_read(fp_top1,line))!=NULL) {
234 getword_start(&gwarea,warea);
235 if (getword(user,sizeof(user),&gwarea,'\t')<0) {
236 debuga(_("There is a broken user in file %s\n"),top1);
237 exit(EXIT_FAILURE);
238 }
239 if (getword_atoll(&nbytes,&gwarea,'\t')<0) {
240 debuga(_("There is a broken number of bytes in file %s\n"),top1);
241 exit(EXIT_FAILURE);
242 }
243 if (getword_atoll(&nacc,&gwarea,'\t')<0) {
244 debuga(_("There is a broken number of access in file %s\n"),top1);
245 exit(EXIT_FAILURE);
246 }
247 if (getword_atoll(&elap,&gwarea,'\t')<0) {
248 debuga(_("There is a broken elpased time in file %s\n"),top1);
249 exit(EXIT_FAILURE);
250 }
251 if (getword_atoll(&incac,&gwarea,'\t')<0) {
252 debuga(_("There is a broken in-cache size in file %s\n"),top1);
253 exit(EXIT_FAILURE);
254 }
255 if (getword_atoll(&oucac,&gwarea,'\n')<0) {
256 debuga(_("There is a broken out-of-cache size in file %s\n"),top1);
257 exit(EXIT_FAILURE);
258 }
259 if(nacc < 1)
260 continue;
261 ntopuser = 1;
262 if(TopUsersNum > 0 && topcount >= TopUsersNum) break;
263 tnbytes=nbytes;
264 tnelap=elap;
265
266 uinfo=userinfo_find_from_id(user);
267 if (!uinfo) {
268 debuga(_("Unknown user ID %s in file %s\n"),user,top1);
269 exit(EXIT_FAILURE);
270 }
271
272 fputs("<tr>",fp_top3);
273
274 posicao++;
275 if((TopUserFields & TOPUSERFIELDS_NUM) != 0)
276 fprintf(fp_top3,"<td class=\"data\">%d</td>",posicao);
277
278 if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0) {
279 fputs("<td class=\"data2\">",fp_top3);
280 #ifdef HAVE_GD
281 if(Graphs && GraphFont[0]!='\0') {
282 //fprintf(fp_top3,"<a href=\"%s/graph_day.png\"><img src=\"%s/graph.png\" title=\"%s\" alt=\"G\"></a>&nbsp;",uinfo->filename,ImageFile,_("Graphic"));
283 fprintf(fp_top3,"<a href=\"%s/graph.html\"><img src=\"%s/graph.png\" title=\"%s\" alt=\"G\"></a>&nbsp;",uinfo->filename,ImageFile,_("Graphic"));
284 }
285 #endif
286 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"));
287 } else {
288 sprintf(val1,"%s/d%s.html",outdirname,uinfo->filename);
289 unlink(val1);
290 }
291 if((TopUserFields & TOPUSERFIELDS_USERID) != 0) {
292 if((ReportType & REPORT_TYPE_USERS_SITES) == 0)
293 fprintf(fp_top3,"<td class=\"data2\">%s</td>",name);
294 else
295 fprintf(fp_top3,"<td class=\"data2\"><a href=\"%s/%s.html\">%s</a></td>",uinfo->filename,uinfo->filename,uinfo->label);
296 }
297 if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0)
298 fprintf(fp_top3,"<td class=\"data\">%s</td>",fixnum(nacc,1));
299 if((TopUserFields & TOPUSERFIELDS_BYTES) != 0)
300 fprintf(fp_top3,"<td class=\"data\">%s</td>",fixnum(tnbytes,1));
301 if((TopUserFields & TOPUSERFIELDS_SETYB) != 0) {
302 perc=(ttnbytes) ? tnbytes * 100. / ttnbytes : 0.;
303 fprintf(fp_top3,"<td class=\"data\">%3.2lf%%</td>",perc);
304 }
305 if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0) {
306 inperc=(tnbytes) ? incac * 100. / tnbytes : 0.;
307 ouperc=(tnbytes) ? oucac * 100. / tnbytes : 0.;
308 fprintf(fp_top3,"<td class=\"data\">%3.2lf%%</td><td class=\"data\">%3.2lf%%</td>",inperc,ouperc);
309 }
310 if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0)
311 fprintf(fp_top3,"<td class=\"data\">%s</td>",buildtime(tnelap));
312 if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0)
313 fprintf(fp_top3,"<td class=\"data\">%s</td>",fixnum2(tnelap,1));
314 if((TopUserFields & TOPUSERFIELDS_PTIME) != 0) {
315 perc2=(ttnelap) ? elap * 100. / ttnelap : 0.;
316 fprintf(fp_top3,"<td class=\"data\">%3.2lf%%</td>",perc2);
317 }
318
319 fputs("</tr>\n",fp_top3);
320
321 topcount++;
322 }
323 fclose(fp_top1);
324 unlink(top1);
325 longline_destroy(&line);
326
327 if((TopUserFields & TOPUSERFIELDS_TOTAL) != 0) {
328 fputs("<tr>",fp_top3);
329 if((TopUserFields & TOPUSERFIELDS_NUM) != 0)
330 fputs("<td></td>",fp_top3);
331 if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0)
332 fputs("<td></td>",fp_top3);
333 fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("TOTAL"));
334
335 if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0)
336 fprintf(fp_top3,"<th class=\"header_r\">%s</th>",fixnum(ttnacc,1));
337 if((TopUserFields & TOPUSERFIELDS_BYTES) != 0)
338 fprintf(fp_top3,"<th class=\"header_r\">%15s</th>",fixnum(ttnbytes,1));
339 if((TopUserFields & TOPUSERFIELDS_SETYB) != 0)
340 fputs("<td></td>",fp_top3);
341 if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0)
342 {
343 inperc=(ttnbytes) ? ttnincache * 100. / ttnbytes : 0.;
344 ouperc=(ttnbytes) ? ttnoucache *100. / ttnbytes : 0.;
345 fprintf(fp_top3,"<th class=\"header_r\">%3.2lf%%</th><th class=\"header_r\">%3.2lf%%</th>",inperc,ouperc);
346 }
347 if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0)
348 fprintf(fp_top3,"<th class=\"header_r\">%s</th>",buildtime(ttnelap));
349 if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0)
350 fprintf(fp_top3,"<th class=\"header_r\">%s</th>",fixnum2(ttnelap,1));
351
352 fputs("</tr>\n",fp_top3);
353 }
354
355 if(ntopuser && (TopUserFields & TOPUSERFIELDS_AVERAGE) != 0) {
356 fputs("<tr>",fp_top3);
357 if((TopUserFields & TOPUSERFIELDS_NUM) != 0)
358 fputs("<td></td>",fp_top3);
359 if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0)
360 fputs("<td></td>",fp_top3);
361 fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("AVERAGE"));
362
363 if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0)
364 fprintf(fp_top3,"<th class=\"header_r\">%s</th>",fixnum(ttnacc/totuser,1));
365 if((TopUserFields & TOPUSERFIELDS_BYTES) != 0) {
366 tnbytes=(totuser) ? ttnbytes / totuser : 0;
367 fprintf(fp_top3,"<th class=\"header_r\">%15s</th>",fixnum(tnbytes,1));
368 }
369 if((TopUserFields & TOPUSERFIELDS_SETYB) != 0)
370 fputs("<td></td>",fp_top3);
371 if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0)
372 fputs("<td></td><td></td>",fp_top3);
373 if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0)
374 fprintf(fp_top3,"<th class=\"header_r\">%s</th>",buildtime(ttnelap/totuser));
375 if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0)
376 fprintf(fp_top3,"<th class=\"header_r\">%s</th>",fixnum2(ttnelap/totuser,1));
377 fputs("</tr>\n",fp_top3);
378 }
379
380 fputs("</table></div>\n",fp_top3);
381 if (write_html_trailer(fp_top3)<0)
382 debuga(_("Write error in top user list %s\n"),top3);
383 if (fclose(fp_top3)==EOF)
384 debuga(_("Failed to close the top user list %s - %s\n"),top3,strerror(errno));
385
386 snprintf(tusr,sizeof(tusr),"%s/sarg-users",outdirname);
387 if((fp_ou=fopen(tusr,"w"))==NULL) {
388 debuga(_("(topuser) Cannot open file %s\n"),tusr);
389 exit(EXIT_FAILURE);
390 }
391 fprintf(fp_ou,"%d\n",totuser);
392 if (fclose(fp_ou)==EOF)
393 debuga(_("Failed to close file %s - %s\n"),tusr,strerror(errno));
394
395 return;
396 }