]>
git.ipfire.org Git - thirdparty/sarg.git/blob - topuser.c
6b79d78104b595f34bc3087552cc3821991ccd2d
2 * AUTHOR: Pedro Lineu Orso orso@penguintech.com.br
4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
26 #include "include/conf.h"
31 FILE *fp_in
, *fp_ou
, *fp_top1
, *fp_top2
, *fp_top3
;
32 long long int ttnbytes
=0, ttnacc
=0, tnacc
=0;
33 long long int tnbytes
=0, ttnelap
=0, tnelap
=0;
34 long long int tnincache
=0, tnoucache
=0, ttnincache
=0, ttnoucache
=0;
37 float inperc
=0.00, ouperc
=0.00;
39 char olduser
[MAXLEN
], csort
[MAXLEN
], periodo
[MAXLEN
], arqper
[MAXLEN
];
40 char wger
[MAXLEN
], top1
[MAXLEN
], top2
[MAXLEN
], top3
[MAXLEN
];
41 char user
[MAXLEN
], nacc
[20], nbytes
[20], preg
[MAXLEN
], tusr
[MAXLEN
];
42 char ip
[MAXLEN
], hora
[9], data
[11], elap
[15], incac
[15], oucac
[15], html
[MAXLEN
];
43 char ipantes
[MAXLEN
], nameantes
[MAXLEN
];
44 char sfield
[10]="2,2";
60 strcat(wger
,"/geral");
62 strcat(top2
,"/top.tmp");
63 strcat(tusr
,"/usuarios");
64 strcat(top3
,"/index.html");
66 if((fp_in
=fopen(wger
,"r"))==NULL
) {
67 fprintf(stderr
, "SARG: (topuser) %s: %s\n",text
[45],wger
);
71 if((fp_top2
=fopen(top2
,"w"))==NULL
) {
72 fprintf(stderr
, "SARG: (topuser) %s: %s\n",text
[45],top2
);
76 fscanf(fp_in
,"%s",user
);
77 fscanf(fp_in
,"%s",nacc
);
78 fscanf(fp_in
,"%s",nbytes
);
79 fscanf(fp_in
,"%s",url
);
80 fscanf(fp_in
,"%s",ip
);
81 fscanf(fp_in
,"%s",hora
);
82 fscanf(fp_in
,"%s",data
);
83 fscanf(fp_in
,"%s",elap
);
84 fscanf(fp_in
,"%s",incac
);
85 fscanf(fp_in
,"%s",oucac
);
91 if(strcmp(olduser
,user
) != 0) {
92 if(strcmp(user
,"TOTAL") != 0)
95 my_lltoa(tnbytes
,val1
,15);
96 my_lltoa(tnacc
,val2
,15);
97 my_lltoa(tnelap
,val3
,15);
98 my_lltoa(tnincache
,val4
,15);
99 my_lltoa(tnoucache
,val5
,15);
100 sprintf(preg
,"%s %s %s %s %s %s\n",olduser
,val1
,val2
,val3
,val4
,val5
);
103 strcpy(olduser
,user
);
107 ttnincache
+=tnincache
;
108 ttnoucache
+=tnoucache
;
116 tnbytes
+=my_atoll(nbytes
);
117 tnacc
+=my_atoll(nacc
);
118 tnelap
+=my_atoll(elap
);
119 tnincache
+=my_atoll(incac
);
120 tnoucache
+=my_atoll(oucac
);
122 fscanf(fp_in
,"%s",user
);
123 fscanf(fp_in
,"%s",nacc
);
124 fscanf(fp_in
,"%s",nbytes
);
125 fscanf(fp_in
,"%s",url
);
126 fscanf(fp_in
,"%s",ip
);
127 fscanf(fp_in
,"%s",hora
);
128 fscanf(fp_in
,"%s",data
);
129 fscanf(fp_in
,"%s",elap
);
130 fscanf(fp_in
,"%s",incac
);
131 fscanf(fp_in
,"%s",oucac
);
133 if(strcmp(user
,"TOTAL") == 0)
137 my_lltoa(tnbytes
,val1
,15);
138 my_lltoa(tnacc
,val2
,15);
139 my_lltoa(tnelap
,val3
,15);
140 my_lltoa(tnincache
,val4
,15);
141 my_lltoa(tnoucache
,val5
,15);
142 sprintf(preg
,"%s %s %s %s %s %s\n",olduser
,val1
,val2
,val3
,val4
,val5
);
148 ttnincache
+=tnincache
;
149 ttnoucache
+=tnoucache
;
151 my_lltoa(ttnbytes
,val1
,15);
152 my_lltoa(ttnacc
,val2
,15);
153 my_lltoa(ttnelap
,val3
,15);
154 my_lltoa(ttnincache
,val4
,15);
155 my_lltoa(ttnoucache
,val5
,15);
156 sprintf(preg
,"TOTAL %s %s %s %s %s\n",val1
,val2
,val3
,val4
,val5
);
160 strup(TopuserSortField
);
161 strlow(TopuserSortOrder
);
163 if(strcmp(TopuserSortField
,"USER") == 0)
164 strcpy(sfield
,"1,1");
166 if(strcmp(TopuserSortField
,"CONNECT") == 0)
167 strcpy(sfield
,"3,3");
169 if(strcmp(TopuserSortField
,"TIME") == 0)
170 strcpy(sfield
,"4,4");
172 if(strcmp(TopuserSortOrder
,"normal") == 0)
175 sprintf(csort
,"sort -n -T %s %s -k %s -o '%s' '%s'", TempDir
, order
, sfield
, top1
, top2
);
180 if((fp_top1
=fopen(top1
,"a"))==NULL
) {
181 fprintf(stderr
, "SARG: (topuser) %s: %s\n",text
[45],top1
);
187 if((fp_top1
=fopen(top1
,"r"))==NULL
) {
188 fprintf(stderr
, "SARG: (topuser) %s: %s\n",text
[45],top1
);
192 if((fp_top3
=fopen(top3
,"w"))==NULL
) {
193 fprintf(stderr
, "SARG: (topuser) %s: %s\n",text
[45],top3
);
201 strcpy(arqper
,dirname
);
202 strcat(arqper
,"/periodo");
204 if ((fp_in
= fopen(arqper
, "r")) == 0) {
205 fprintf(stderr
, "SARG: (topuser) %s: %s\n",text
[45],arqper
);
209 fgets(periodo
,sizeof(periodo
),fp_in
);
212 fputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"",fp_top3
);
213 fputs(" \"http://www.w3.org/TR/html4/loose.dtd\">\n",fp_top3
);
214 fputs("<html>\n",fp_top3
);
215 fputs("<head>\n",fp_top3
);
216 sprintf(html
," <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet
);
218 fputs("</head>\n",fp_top3
);
222 sprintf(preg
,"<body class=\"body\">");
225 if(strlen(LogoImage
) > 0) {
226 fputs("<center><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_top3
);
227 sprintf(preg
,"<tr><th class=\"logo\"><img src='%s' border=0 align=absmiddle width=%s height=%s> %s</th></tr>\n",LogoImage
,Width
,Height
,LogoText
);
229 fputs("<tr><td height=\"5\"></td></tr>\n",fp_top3
);
230 fputs("</table>\n",fp_top3
);
233 show_sarg(fp_top3
, "..");
234 fputs("<center><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_top3
);
235 sprintf(preg
,"<tr><th align=\"center\" class=\"title\">%s</th></tr>\n",Title
);
238 sprintf(preg
,"<tr><td class=\"header3\">%s: %s</td></tr>\n",text
[89],periodo
);
240 strcat(wheader
,(char *)text
[104]);
241 strcat(wheader
,": ");
242 strcat(wheader
,TopuserSortField
);
243 strcat(wheader
,", ");
244 strcat(wheader
,TopuserSortOrder
);
245 sprintf(preg
,"<tr><td class=\"header3\">%s</td></tr>\n",wheader
);
247 sprintf(preg
,"<tr><td class=\"header3\">Topuser %s</td></tr>\n",text
[55]);
250 fputs("</table></center>\n",fp_top3
);
251 fputs("<center><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_top3
);
252 fputs("<tr><td><br></td><td></td></tr>\n",fp_top3
);
254 if(strstr(ReportType
,"topsites") != 0) {
255 sprintf(preg
,"<tr><td class=\"link\" colspan=11><a href=\"topsites.html\"><font class=\"link\">%s</font></a><font class=\"text\"> %s</font></td></tr>\n",text
[119],text
[55]);
259 if(strstr(ReportType
,"sites_users") != 0) {
260 sprintf(preg
,"<tr><td class=\"link\" colspan=11><a href=\"siteuser.html\"><font class=\"link\">%s</font></a><font class=\"text\"> %s</font></td></tr>\n",text
[85],text
[55]);
264 if(dansguardian_count
) {
265 sprintf(preg
,"<tr><td class=\"link\" colspan=11><a href=\"dansguardian.html\"><font class=\"link\">%s</font></a><font class=\"text\"> %s</font></td></tr>\n",text
[128],text
[55]);
269 if(squidguard_count
) {
270 sprintf(preg
,"<tr><td class=\"link\" colspan=11><a href=\"squidguard.html\"><font class=\"link\">%s</font></a><font class=\"text\"> %s</font></td></tr>\n",text
[122],text
[55]);
275 sprintf(preg
,"<tr><td class=\"link\" colspan=11><a href=\"download.html\"><font class=\"link\">%s</font></a><font class=\"text\"> %s</font></td></tr>\n",text
[125],text
[55]);
280 sprintf(preg
,"<tr><td class=\"link\" colspan=11><a href=\"denied.html\"><font class=\"link\">%s</font></a><font class=\"text\"> %s</font></td></tr>\n",text
[118],text
[55]);
285 sprintf(preg
,"<tr><td class=\"link\" colspan=11><a href=\"authfail.html\"><font class=\"link\">%s</font></a><font class=\"text\"> %s</font></td></tr>\n",text
[117],text
[55]);
290 sprintf(preg
,"<tr><td class=\"link\" colspan=11><a href=\"smartfilter.html\"><font class=\"link\">%s</font></a><font class=\"text\"> %s</font></td></tr>\n",text
[116],text
[55]);
293 strcpy(preg
,"<tr><td></td></tr>\n");
296 strcpy(val1
,text
[100]);
297 strcpy(val2
,text
[98]);
298 strcpy(val3
,text
[92]);
299 strcpy(val4
,text
[93]);
300 sprintf(val11
,"%%%s",text
[93]);
301 sprintf(val5
,"%s-%s-%s",text
[113],text
[114],text
[112]);
302 strcpy(val6
,text
[94]);
303 strcpy(val7
,text
[95]);
304 sprintf(val8
,"%%%s",text
[99]);
305 strcpy(val9
,"colspan=2");
308 strcpy(hbc1
,"class=\"header\"");
309 strcpy(hbc2
,"class=\"header\"");
310 strcpy(hbc3
,"class=\"header\"");
311 strcpy(hbc4
,"class=\"header\"");
312 strcpy(hbc5
,"class=\"header3\"");
313 strcpy(hbc6
,"class=\"header\"");
314 strcpy(hbc7
,"class=\"header\"");
315 strcpy(hbc8
,"class=\"header\"");
316 strcpy(hbc9
,"class=\"header\"");
317 strcpy(hbc10
,"class=\"header\"");
319 if(strstr(TopUserFields
,"NUM") == 0) {
323 if(strstr(TopUserFields
,"USERID") == 0) {
327 if(strstr(TopUserFields
,"CONNECT") == 0) {
331 if(strstr(TopUserFields
,"BYTES") == 0) {
335 if(strstr(TopUserFields
,"SETYB") == 0) {
339 if(strstr(TopUserFields
,"IN-CACHE-OUT") == 0) {
343 strcpy(val10
,"<td></td>");
345 if(strstr(TopUserFields
,"USED_TIME") == 0) {
349 if(strstr(TopUserFields
,"MILISEC") == 0) {
353 if(strstr(TopUserFields
,"%TIME") == 0) {
358 sprintf(preg
,"<tr><th %s>%s</th><th %s></th><th %s>%s</th><th %s>%s</th><th %s>%s</th><th %s>%s</th><th %s %s>%s%s</th><th %s>%s</th><th %s>%s</th><th %s>%s</th></tr>\n",hbc1
,val1
,hbc1
,hbc2
,val2
,hbc3
,val3
,hbc4
,val4
,hbc9
,val11
,hbc5
,val9
,val5
,val10
,hbc6
,val6
,hbc7
,val7
,hbc8
,val8
);
362 fscanf(fp_top1
,"%s",user
);
363 fscanf(fp_top1
,"%s",nbytes
);
364 fscanf(fp_top1
,"%s",nacc
);
365 fscanf(fp_top1
,"%s",elap
);
366 fscanf(fp_top1
,"%s",incac
);
367 fscanf(fp_top1
,"%s",oucac
);
369 while(!feof(fp_top1
)) {
370 if(atoi(TopUsersNum
) > 0 && topcount
>= atoi(TopUsersNum
)) goto final
;
372 tnbytes
=my_atoll(nbytes
);
376 perc
=perc
/ ttnbytes
;
381 perc2
=((perc2
* 100) / ttnelap
);
386 inperc
=((inperc
* 100) / tnbytes
);
391 ouperc
=((ouperc
* 100) / tnbytes
);
398 if(strcmp(user
,"TOTAL") != 0){
399 sprintf(href
,"<a href='%s/",user
);
400 sprintf(href2
,"<a href='%s/d",user
);
401 sprintf(href3
,"<a href='%s/graph_day.png'>",user
);
404 strcat(href
,".html'>");
405 strcat(href2
,".html'>");
408 if(strcmp(Graphs
,"yes") != 0)
413 tnelap
=my_atoll(elap
);
417 if(strcmp(Ip2Name
,"yes") == 0) {
418 if(strcmp(user2
,ipantes
) != 0) {
419 strcpy(ipantes
,user2
);
421 strcpy(nameantes
,user2
);
422 } else strcpy(user2
,nameantes
);
426 if(strstr(ReportType
,"date_time") != 0) {
427 sprintf(ltext110
,"%s",text
[110]);
429 for(s
=ltext110
; *s
; ++s
)
433 bzero(href2
, MAXLEN
);
435 sprintf(val1
,"%s/d%s.html",dirname
,user
);
439 if(UserTabFile
[0] != '\0' && strstr(user2
,".") != 0) {
440 sprintf(warea
,":%s:",user2
);
441 if((str
=(char *) strstr(userfile
,warea
)) != (char *) NULL
) {
443 str2
=(char *) strstr(str
+1,":");
446 while(str2
[z1
] != ':') {
450 } else strcpy(name
,user2
);
451 } else strcpy(name
,user2
);
453 if(strcmp(Ip2Name
,"yes") == 0) {
454 if((str
=(char *) strstr(name
, ".")) != (char *) NULL
) {
455 if((str
=(char *) strstr(str
+1, ".")) != (char *) NULL
)
460 twork
=my_atoll(nacc
);
461 my_lltoa(twork
,nacc
,0);
462 sprintf(wwork1
,"%s",fixnum(twork
,1));
463 sprintf(wwork2
,"%s",fixnum(tnbytes
,1));
464 sprintf(wwork3
,"%s",fixnum(tnelap
,1));
466 sprintf(val1
,"%d",posicao
);
468 sprintf(val2
,"%s<img src=\"../images/graph.png\" border=\"0\" title=\"%s %s\"></a> %s<img src=\"../images/datetime.png\" border=\"0\" title=\"%s %s\">\n",href3
,text
[126],text
[55],href2
,ltext110
,text
[55]);
470 sprintf(val2
,"%s<img src=\"../images/datetime.png\" border=\"0\" title=\"%s %s\">\n",href2
,ltext110
,text
[55]);
473 sprintf(val3
,"%3.2f%%",perc
);
474 sprintf(val4
,"%3.2f%%",inperc
);
475 sprintf(val5
,"%3.2f%%",ouperc
);
476 sprintf(val6
,"%s",buildtime(tnelap
));
477 sprintf(val7
,"%3.2f%%",perc2
);
479 strcpy(hbc1
,"class=\"data\"");
480 strcpy(hbc2
,"class=\"data2\"");
481 strcpy(hbc3
,"class=\"data\"");
482 strcpy(hbc4
,"class=\"data\"");
483 strcpy(hbc5
,"class=\"data\"");
484 strcpy(hbc6
,"class=\"data\"");
485 strcpy(hbc7
,"class=\"data\"");
486 strcpy(hbc8
,"class=\"data\"");
487 strcpy(hbc9
,"class=\"data\"");
488 strcpy(hbc10
,"class=\"data\"");
490 if(strstr(TopUserFields
,"NUM") == 0) {
494 if(strstr(TopUserFields
,"USERID") == 0) {
498 if(strstr(TopUserFields
,"CONNECT") == 0) {
502 if(strstr(TopUserFields
,"BYTES") == 0) {
506 if(strstr(TopUserFields
,"SETYB") == 0) {
510 if(strstr(TopUserFields
,"IN-CACHE-OUT") == 0) {
514 if(strstr(TopUserFields
,"IN-CACHE-OUT") == 0) {
518 if(strstr(TopUserFields
,"USED_TIME") == 0) {
522 if(strstr(TopUserFields
,"MILISEC") == 0) {
526 if(strstr(TopUserFields
,"%TIME") == 0) {
531 if(strstr(ReportType
,"users_sites") == 0)
534 if(dotinuser
&& strstr(name
,"_")) {
535 str2
=(char *)subs(name
,"_",".");
539 sprintf(preg
,"<tr><td %s>%s</td><td %s>%s</td><td %s>%s%s</td><td %s>%s</td><td %s>%s</td><td %s>%s</td><td %s>%s</td><td %s>%s</td><td %s>%s</td><td %s>%s<td %s>%s</td></tr>\n",hbc1
,val1
,hbc2
,val2
,hbc2
,href
,name
,hbc3
,wwork1
,hbc4
,wwork2
,hbc5
,val3
,hbc6
,val4
,hbc7
,val5
,hbc8
,val6
,hbc9
,wwork3
,hbc10
,val7
);
541 if(strstr(user
,"TOTAL") != 0) {
544 inperc
=ttnbytes
/ 100;
545 inperc
=atol(incac
) / inperc
;
549 ouperc
=ttnbytes
/ 100;
550 ouperc
=atol(oucac
) / ouperc
;
553 sprintf(wwork1
,"%s",fixnum(ttnacc
,1));
554 sprintf(wwork2
,"%s",fixnum(ttnbytes
,1));
555 sprintf(wwork3
,"%s",fixnum(ttnelap
,1));
557 strcpy(hbc1
,"class=\"header2\"");
558 strcpy(hbc2
,"class=\"header2\"");
559 strcpy(hbc3
,"class=\"header2\"");
560 strcpy(hbc4
,"class=\"header2\"");
561 strcpy(hbc5
,"class=\"header2\"");
562 strcpy(hbc6
,"class=\"header2\"");
563 strcpy(hbc7
,"class=\"header2\"");
564 strcpy(hbc8
,"class=\"header2\"");
565 strcpy(hbc9
,"class=\"header2\"");
566 strcpy(hbc10
,"class=\"header\"");
568 sprintf(val4
,"%3.2f%%",inperc
);
569 sprintf(val5
,"%3.2f%%",ouperc
);
570 sprintf(val6
,"%s",buildtime(ttnelap
));
571 sprintf(val7
,"%3.2f%%",perc2
);
573 if(strstr(TopUserFields
,"CONNECT") == 0) {
577 if(strstr(TopUserFields
,"BYTES") == 0) {
581 if(strstr(TopUserFields
,"IN-CACHE-OUT") == 0) {
587 if(strstr(TopUserFields
,"USED_TIME") == 0) {
591 if(strstr(TopUserFields
,"MILISEC") == 0) {
596 if(strstr(ReportType
,"date_time") != 0) {
597 if(strstr(TopUserFields
,"TOTAL") != 0)
598 sprintf(preg
,"<tr><td></td><td></td><th %s>%s</th><th %s>%s</th><th %s>%15s</th><td></td><th %s>%s</th><th %s>%s</th><th %s>%s</th><th %s>%s</th></tr>\n",hbc10
,text
[107],hbc1
,wwork1
,hbc2
,wwork2
,hbc3
,val4
,hbc4
,val5
,hbc5
,val6
,hbc6
,wwork3
);
599 } else if(strstr(TopUserFields
,"TOTAL") != 0)
600 sprintf(preg
,"<tr><td></td><td></td><th %s>%s</th><th %s>%s</th><th %s>%15s</th><td></td><th %s>%s</th><th %s>%s</th><th %s>%s</th><th %s>%s</th></tr>\n",hbc10
,text
[107],hbc1
,wwork1
,hbc2
,wwork2
,hbc3
,val4
,hbc4
,val5
,hbc5
,val6
,hbc6
,wwork3
);
607 fscanf(fp_top1
,"%s",user
);
608 fscanf(fp_top1
,"%s",nbytes
);
609 fscanf(fp_top1
,"%s",nacc
);
610 fscanf(fp_top1
,"%s",elap
);
611 fscanf(fp_top1
,"%s",incac
);
612 fscanf(fp_top1
,"%s",oucac
);
616 tnbytes
=ttnbytes
/ totuser
;
619 twork
=ttnacc
/totuser
;
620 twork2
=ttnelap
/totuser
;
621 sprintf(wwork1
,"%s",fixnum(twork
,1));
622 sprintf(wwork2
,"%s",fixnum(tnbytes
,1));
623 sprintf(wwork3
,"%s",fixnum(twork2
,1));
625 if(strstr(TopUserFields
,"CONNECT") == 0) {
629 if(strstr(TopUserFields
,"BYTES") == 0) {
633 if(strstr(TopUserFields
,"IN-CACHE-OUT") == 0) {
639 if(strstr(TopUserFields
,"USED_TIME") == 0) {
643 if(strstr(TopUserFields
,"MILISEC") == 0) {
648 if((strstr(ReportType
,"date_time") != 0 && strstr(TopUserFields
,"AVERAGE") != 0)) {
649 sprintf(preg
,"<tr><td></td><th></th><th %s>%s</th><th %s>%s</th><th %s>%15s</th><td></td><td></td><td></td><th %s>%s</th><th %s>%s</th></tr>\n",hbc10
,text
[96],hbc1
,wwork1
,hbc2
,wwork2
,hbc3
,buildtime(ttnelap
/totuser
),hbc4
,wwork3
);
651 } else if(strstr(TopUserFields
,"AVERAGE") != 0) {
652 sprintf(preg
,"<tr><td></td><th></th><td></td><th %s>%s</th><th %s>%s</th><th %s>%15s</th><td></td><td></td><td></td><th %s>%s</th><th %s>%s</th></tr>\n",hbc10
,text
[96],hbc1
,wwork1
,hbc2
,wwork2
,hbc3
,buildtime(ttnelap
/totuser
),hbc4
,wwork3
);
656 if(strlen(UserAgentLog
) > 0) {
657 fputs("<tr><td></td></tr>\n",fp_top3
);
658 fputs("<tr><td></td></tr>\n",fp_top3
);
659 fputs("<td align=\"left\" colspan=8><font size=-1><a href='useragent.html'>Useragent</a> Report</td>\n",fp_top3
);
662 strcpy(preg
,"</table></center>");
671 if((fp_ou
=fopen(tusr
,"w"))==NULL
) {
672 fprintf(stderr
, "SARG: (topuser) %s: %s\n",text
[45],tusr
);
676 sprintf(preg
,"%d\n",totuser
);
679 fputs("</body>\n</html>\n",fp_top3
);