]> git.ipfire.org Git - thirdparty/sarg.git/blob - topuser.c
Output w3c compliant strict html (almost every output).
[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[MAXLEN], csort[MAXLEN], period[MAXLEN], arqper[MAXLEN];
45 char wger[MAXLEN], top1[MAXLEN], top2[MAXLEN], top3[MAXLEN];
46 char user[MAXLEN], tusr[MAXLEN];
47 char ip[MAXLEN], time[30], date[30];
48 char ipantes[MAXLEN], nameantes[MAXLEN];
49 char sfield[10]="2,2";
50 char order[255]="-r";
51 char title[80];
52 int totuser=0;
53 int topcount=0;
54 char *s;
55 int cstatus;
56 char warea[MAXLEN];
57 char user2[MAXLEN];
58 char name[MAXLEN];
59 struct getwordstruct gwarea;
60
61 ipantes[0]='\0';
62 nameantes[0]='\0';
63
64 /*
65 * get period
66 */
67 strcpy(arqper,dirname);
68 strcat(arqper,"/sarg-period");
69 if ((fp_in = fopen(arqper, "r")) == 0) {
70 fprintf(stderr, "SARG: (topuser) %s: %s\n",text[45],arqper);
71 exit(1);
72 }
73 if (!fgets(period,sizeof(period),fp_in)) {
74 fprintf(stderr,"SARG: (topuser) Read error in %s\n",arqper);
75 exit(1);
76 }
77 fclose(fp_in);
78
79 strcpy(wger,dirname);
80 strcpy(top1,dirname);
81 strcpy(top2,dirname);
82 strcpy(top3,dirname);
83 strcpy(tusr,dirname);
84 strcat(wger,"/sarg-general");
85 strcat(top1,"/top");
86 strcat(top2,"/top.tmp");
87 strcat(tusr,"/sarg-users");
88 strcat(top3,"/index.html");
89
90 ntopuser = 0;
91 if((fp_in=fopen(wger,"r"))==NULL) {
92 fprintf(stderr, "SARG: (topuser) %s: %s\n",text[45],wger);
93 exit(1);
94 }
95
96 if((fp_top2=fopen(top2,"w"))==NULL) {
97 fprintf(stderr, "SARG: (topuser) %s: %s\n",text[45],top2);
98 exit(1);
99 }
100
101 olduser[0]='\0';
102 totuser=0;
103
104 while(fgets(warea,sizeof(warea),fp_in)) {
105 getword_start(&gwarea,warea);
106 if (getword(user,sizeof(user),&gwarea,'\t')<0) {
107 printf("SARG: Maybe you have a broken user in your %s file.\n",wger);
108 exit(1);
109 }
110 if(strcmp(user,"TOTAL") == 0) {
111 continue;
112 }
113 if (getword_atoll(&nacc,&gwarea,'\t')<0) {
114 printf("SARG: Maybe you have a broken number of access in your %s file.\n",wger);
115 exit(1);
116 }
117 if (getword_atoll(&nbytes,&gwarea,'\t')<0) {
118 printf("SARG: Maybe you have a broken number of bytes in your %s file.\n",wger);
119 exit(1);
120 }
121 if (getword(url,sizeof(url),&gwarea,'\t')<0) {
122 printf("SARG: Maybe you have a broken url in your %s file.\n",wger);
123 exit(1);
124 }
125 if (getword(ip,sizeof(ip),&gwarea,'\t')<0) {
126 printf("SARG: Maybe you have a broken user's IP in your %s file.\n",wger);
127 exit(1);
128 }
129 if (getword(time,sizeof(time),&gwarea,'\t')<0) {
130 printf("SARG: Maybe you have a broken time in your %s file.\n",wger);
131 exit(1);
132 }
133 if (getword(date,sizeof(date),&gwarea,'\t')<0) {
134 printf("SARG: Maybe you have a broken date in your %s file.\n",wger);
135 exit(1);
136 }
137 if (getword_atoll(&elap,&gwarea,'\t')<0) {
138 printf("SARG: Maybe you have a broken download duration in your %s file.\n",wger);
139 exit(1);
140 }
141 if (getword_atoll(&incac,&gwarea,'\t')<0) {
142 printf("SARG: Maybe you have a broken in cache download in your %s file.\n",wger);
143 exit(1);
144 }
145 if (getword_atoll(&oucac,&gwarea,'\n')<0) {
146 printf("SARG: Maybe you have a broken not in cache download in your %s file.\n",wger);
147 exit(1);
148 }
149 if(strcmp(olduser,user) != 0) {
150 totuser++;
151
152 if (olduser[0] != '\0') {
153 my_lltoa(tnbytes,val1,15);
154 my_lltoa(tnacc,val2,15);
155 my_lltoa(tnelap,val3,15);
156 my_lltoa(tnincache,val4,15);
157 my_lltoa(tnoucache,val5,15);
158 fprintf(fp_top2,"%s\t%s\t%s\t%s\t%s\t%s\n",olduser,val1,val2,val3,val4,val5);
159
160 ttnbytes+=tnbytes;
161 ttnacc+=tnacc;
162 ttnelap+=tnelap;
163 ttnincache+=tnincache;
164 ttnoucache+=tnoucache;
165 }
166 strcpy(olduser,user);
167 tnbytes=0;
168 tnacc=0;
169 tnelap=0;
170 tnincache=0;
171 tnoucache=0;
172 }
173
174 tnbytes+=nbytes;
175 tnacc+=nacc;
176 tnelap+=elap;
177 tnincache+=incac;
178 tnoucache+=oucac;
179 }
180 fclose(fp_in);
181
182 if (olduser[0] != '\0') {
183 my_lltoa(tnbytes,val1,15);
184 my_lltoa(tnacc,val2,15);
185 my_lltoa(tnelap,val3,15);
186 my_lltoa(tnincache,val4,15);
187 my_lltoa(tnoucache,val5,15);
188 fprintf(fp_top2,"%s\t%s\t%s\t%s\t%s\t%s\n",olduser,val1,val2,val3,val4,val5);
189
190 ttnbytes+=tnbytes;
191 ttnacc+=tnacc;
192 ttnelap+=tnelap;
193 ttnincache+=tnincache;
194 ttnoucache+=tnoucache;
195 }
196 fclose(fp_top2);
197
198 strup(TopuserSortField);
199 strlow(TopuserSortOrder);
200
201 if(strcmp(TopuserSortField,"USER") == 0)
202 strcpy(sfield,"1,1");
203
204 if(strcmp(TopuserSortField,"CONNECT") == 0)
205 strcpy(sfield,"3,3");
206
207 if(strcmp(TopuserSortField,"TIME") == 0)
208 strcpy(sfield,"4,4");
209
210 if(strcmp(TopuserSortOrder,"normal") == 0)
211 order[0]='\0';
212
213 sprintf(csort,"sort -n -T \"%s\" %s -k %s -o \"%s\" \"%s\"", TempDir, order, sfield, top1, top2);
214 cstatus=system(csort);
215 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
216 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
217 fprintf(stderr, "SARG: sort command: %s\n",csort);
218 exit(1);
219 }
220
221 if((fp_top1=fopen(top1,"r"))==NULL) {
222 fprintf(stderr, "SARG: (topuser) %s: %s\n",text[45],top1);
223 exit(1);
224 }
225
226 unlink(top2);
227
228 if((fp_top3=fopen(top3,"w"))==NULL) {
229 fprintf(stderr, "SARG: (topuser) %s: %s\n",text[45],top3);
230 exit(1);
231 }
232
233 snprintf(title,sizeof(title),_("SARG report for %s"),period);
234 write_html_header(fp_top3,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,title);
235 fprintf(fp_top3,"<tr><td class=\"header_c\">%s: %s</td></tr>\n",text[89],period);
236 fprintf(fp_top3,"<tr><td class=\"header_c\">%s: %s, %s</td></tr>\n",text[104],TopuserSortField,TopuserSortOrder);
237 fprintf(fp_top3,"<tr><th class=\"header_c\">%s</th></tr>\n",text[137]);
238 close_html_header(fp_top3);
239
240 fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_top3);
241 fputs("<tr><td>&nbsp;</td><td>&nbsp;</td></tr>\n",fp_top3);
242
243 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",text[119]);
244 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",text[85]);
245 if(dansguardian_count) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"dansguardian.html\">%s</a></td></tr>\n",text[128]);
246 if(squidguard_count) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"squidguard.html\">%s</a></td></tr>\n",text[122]);
247 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",text[125]);
248 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",text[118]);
249 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",text[117]);
250 if(smartfilter) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"smartfilter.html\">%s</a></td></tr>\n",text[116]);
251 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"));
252 fputs("<tr><td></td></tr>\n",fp_top3);
253
254 if ((ReportType & REPORT_TYPE_TOPUSERS) == 0) {
255 fputs("</body>\n</html>\n",fp_top3);
256 fclose (fp_top3);
257 return;
258 }
259
260 fputs("<tr>",fp_top3);
261
262 if((TopUserFields & TOPUSERFIELDS_NUM) != 0)
263 fprintf(fp_top3,"<th class=\"header_l\">%s</th>",text[100]);
264 if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0)
265 fputs("<th class=\"header_l\"></th>",fp_top3);
266 if((TopUserFields & TOPUSERFIELDS_USERID) != 0)
267 fprintf(fp_top3,"<th class=\"header_l\">%s</th>",text[98]);
268 if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0)
269 fprintf(fp_top3,"<th class=\"header_l\">%s</th>",text[92]);
270 if((TopUserFields & TOPUSERFIELDS_BYTES) != 0)
271 fprintf(fp_top3,"<th class=\"header_l\">%s</th>",text[93]);
272 if((TopUserFields & TOPUSERFIELDS_SETYB) != 0)
273 fprintf(fp_top3,"<th class=\"header_l\">%%%s</th>",text[93]);
274 if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0)
275 fprintf(fp_top3,"<th class=\"header_c\" colspan=\"2\">%s-%s-%s</th>",text[113],text[114],text[112]);
276 if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0)
277 fprintf(fp_top3,"<th class=\"header_l\">%s</th>",text[94]);
278 if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0)
279 fprintf(fp_top3,"<th class=\"header_l\">%s</th>",text[95]);
280 if((TopUserFields & TOPUSERFIELDS_PTIME) != 0)
281 fprintf(fp_top3,"<th class=\"header_l\">%%%s</th>",text[99]);
282
283 fputs("</tr>\n",fp_top3);
284
285 ntopuser = 0;
286
287 while(fgets(warea,sizeof(warea),fp_top1)) {
288 getword_start(&gwarea,warea);
289 if (getword(user,sizeof(user),&gwarea,'\t')<0) {
290 printf("SARG: Maybe you have a broken user in your %s file.\n",top1);
291 exit(1);
292 }
293 if (getword_atoll(&nbytes,&gwarea,'\t')<0) {
294 printf("SARG: Maybe you have a broken number of bytes in your %s file.\n",top1);
295 exit(1);
296 }
297 if (getword_atoll(&nacc,&gwarea,'\t')<0) {
298 printf("SARG: Maybe you have a broken number of access in your %s file.\n",top1);
299 exit(1);
300 }
301 if (getword_atoll(&elap,&gwarea,'\t')<0) {
302 printf("SARG: Maybe you have a broken elpased time in your %s file.\n",top1);
303 exit(1);
304 }
305 if (getword_atoll(&incac,&gwarea,'\t')<0) {
306 printf("SARG: Maybe you have a broken in-cache size in your %s file.\n",top1);
307 exit(1);
308 }
309 if (getword_atoll(&oucac,&gwarea,'\n')<0) {
310 printf("SARG: Maybe you have a broken out-of-cache size in your %s file.\n",top1);
311 exit(1);
312 }
313 if(nacc < 1)
314 continue;
315 ntopuser = 1;
316 if(TopUsersNum > 0 && topcount >= TopUsersNum) break;
317 tnbytes=nbytes;
318 tnelap=elap;
319
320 strcpy(user2,user);
321 if(userip) {
322 fixip(user2);
323 if(Ip2Name) {
324 if(strcmp(user2,ipantes) != 0) {
325 strcpy(ipantes,user2);
326 ip2name(user2,sizeof(user2));
327 strcpy(nameantes,user2);
328 } else strcpy(user2,nameantes);
329 }
330 }
331
332 fputs("<tr>",fp_top3);
333
334 posicao++;
335 if((TopUserFields & TOPUSERFIELDS_NUM) != 0)
336 fprintf(fp_top3,"<td class=\"data\">%d</td>",posicao);
337
338 user_find(name, sizeof(name), user2);
339 if(Ip2Name &&
340 ((str=(char *) strstr(name, ".")) != (char *) NULL) &&
341 ((str=(char *) strstr(str+1, ".")) != (char *) NULL))
342 ip2name(name,sizeof(name));
343 if(dotinuser && strchr(name,'_')) {
344 subs(name,sizeof(name),"_",".");
345 }
346
347 if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0) {
348 sprintf(ltext110,"%s",text[110]);
349 for(s=ltext110; *s; ++s)
350 *s=tolower(*s);
351 fputs("<td class=\"data2\">",fp_top3);
352 #ifdef HAVE_GD
353 if(Graphs) {
354 fprintf(fp_top3,"<a href=\"%s/graph_day.png\"><img src=\"%s/graph.png\" title=\"%s\" alt=\"G\"></a>&nbsp;",user,ImageFile,text[126]);
355 }
356 #endif
357 fprintf(fp_top3,"<a href=\"%s/d%s.html\"><img src=\"%s/datetime.png\" title=\"%s",user,user,ImageFile,ltext110);
358 #ifdef HAVE_GD
359 fprintf(fp_top3," %s",text[55]);
360 #endif
361 fputs("\" alt=\"T\"></a></td>",fp_top3);
362 } else {
363 sprintf(val1,"%s/d%s.html",dirname,user);
364 unlink(val1);
365 }
366 if((TopUserFields & TOPUSERFIELDS_USERID) != 0) {
367 if((ReportType & REPORT_TYPE_USERS_SITES) == 0)
368 fprintf(fp_top3,"<td class=\"data2\">%s</td>",name);
369 else
370 fprintf(fp_top3,"<td class=\"data2\"><a href=\"%s/%s.html\">%s</a></td>",user,user,name);
371 }
372 if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0)
373 fprintf(fp_top3,"<td class=\"data\">%s</td>",fixnum(nacc,1));
374 if((TopUserFields & TOPUSERFIELDS_BYTES) != 0)
375 fprintf(fp_top3,"<td class=\"data\">%s</td>",fixnum(tnbytes,1));
376 if((TopUserFields & TOPUSERFIELDS_SETYB) != 0) {
377 perc=(ttnbytes) ? tnbytes * 100. / ttnbytes : 0.;
378 fprintf(fp_top3,"<td class=\"data\">%3.2lf%%</td>",perc);
379 }
380 if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0) {
381 inperc=(tnbytes) ? incac * 100. / tnbytes : 0.;
382 ouperc=(tnbytes) ? oucac * 100. / tnbytes : 0.;
383 fprintf(fp_top3,"<td class=\"data\">%3.2lf%%</td><td class=\"data\">%3.2lf%%</td>",inperc,ouperc);
384 }
385 if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0)
386 fprintf(fp_top3,"<td class=\"data\">%s</td>",buildtime(tnelap));
387 if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0)
388 fprintf(fp_top3,"<td class=\"data\">%s</td>",fixnum2(tnelap,1));
389 if((TopUserFields & TOPUSERFIELDS_PTIME) != 0) {
390 perc2=(ttnelap) ? elap * 100. / ttnelap : 0.;
391 fprintf(fp_top3,"<td class=\"data\">%3.2lf%%</td>",perc2);
392 }
393
394 fputs("</tr>\n",fp_top3);
395
396 topcount++;
397 }
398 fclose(fp_top1);
399 unlink(top1);
400
401 if((TopUserFields & TOPUSERFIELDS_TOTAL) != 0) {
402 fputs("<tr>",fp_top3);
403 if((TopUserFields & TOPUSERFIELDS_NUM) != 0)
404 fputs("<td></td>",fp_top3);
405 if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0)
406 fputs("<td></td>",fp_top3);
407 fprintf(fp_top3,"<th class=\"header_l\">%s</th>",text[107]);
408
409 if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0)
410 fprintf(fp_top3,"<th class=\"header_r\">%s</th>",fixnum(ttnacc,1));
411 if((TopUserFields & TOPUSERFIELDS_BYTES) != 0)
412 fprintf(fp_top3,"<th class=\"header_r\">%15s</th>",fixnum(ttnbytes,1));
413 if((TopUserFields & TOPUSERFIELDS_SETYB) != 0)
414 fputs("<td></td>",fp_top3);
415 if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0)
416 {
417 inperc=(ttnbytes) ? ttnincache * 100. / ttnbytes : 0.;
418 ouperc=(ttnbytes) ? ttnoucache *100. / ttnbytes : 0.;
419 fprintf(fp_top3,"<th class=\"header_r\">%3.2lf%%</th><th class=\"header_r\">%3.2lf%%</th>",inperc,ouperc);
420 }
421 if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0)
422 fprintf(fp_top3,"<th class=\"header_r\">%s</th>",buildtime(ttnelap));
423 if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0)
424 fprintf(fp_top3,"<th class=\"header_r\">%s</th>",fixnum2(ttnelap,1));
425
426 fputs("</tr>\n",fp_top3);
427 }
428
429 if(ntopuser && (TopUserFields & TOPUSERFIELDS_AVERAGE) != 0) {
430 fputs("<tr>",fp_top3);
431 if((TopUserFields & TOPUSERFIELDS_NUM) != 0)
432 fputs("<td></td>",fp_top3);
433 if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0)
434 fputs("<td></td>",fp_top3);
435 fprintf(fp_top3,"<th class=\"header_l\">%s</th>",text[96]);
436
437 if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0)
438 fprintf(fp_top3,"<th class=\"header_r\">%s</th>",fixnum(ttnacc/totuser,1));
439 if((TopUserFields & TOPUSERFIELDS_BYTES) != 0) {
440 tnbytes=(totuser) ? ttnbytes / totuser : 0;
441 fprintf(fp_top3,"<th class=\"header_r\">%15s</th>",fixnum(tnbytes,1));
442 }
443 if((TopUserFields & TOPUSERFIELDS_SETYB) != 0)
444 fputs("<td></td>",fp_top3);
445 if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0)
446 fputs("<td></td><td></td>",fp_top3);
447 if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0)
448 fprintf(fp_top3,"<th class=\"header_r\">%s</th>",buildtime(ttnelap/totuser));
449 if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0)
450 fprintf(fp_top3,"<th class=\"header_r\">%s</th>",fixnum2(ttnelap/totuser,1));
451 fputs("</tr>\n",fp_top3);
452 }
453
454 fputs("</table></div>\n",fp_top3);
455 write_html_trailer(fp_top3);
456 fclose(fp_top3);
457
458 if((fp_ou=fopen(tusr,"w"))==NULL) {
459 fprintf(stderr, "SARG: (topuser) %s: %s\n",text[45],tusr);
460 exit(1);
461 }
462 fprintf(fp_ou,"%d\n",totuser);
463 fclose(fp_ou);
464
465 return;
466 }