2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
6 * please look at http://sarg.sourceforge.net/donations.php
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
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.
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.
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.
27 #include "include/conf.h"
28 #include "include/defs.h"
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;
39 long long int elap
, incac
, oucac
;
42 double inperc
=0.00, ouperc
=0.00;
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";
58 struct getwordstruct gwarea
;
66 strcpy(arqper
,dirname
);
67 strcat(arqper
,"/sarg-period");
68 if ((fp_in
= fopen(arqper
, "r")) == 0) {
69 fprintf(stderr
, "SARG: (topuser) %s: %s\n",text
[45],arqper
);
72 if (!fgets(period
,sizeof(period
),fp_in
)) {
73 fprintf(stderr
,"SARG: (topuser) Read error in %s\n",arqper
);
83 strcat(wger
,"/sarg-general");
85 strcat(top2
,"/top.tmp");
86 strcat(tusr
,"/sarg-users");
87 strcat(top3
,"/index.html");
90 if((fp_in
=fopen(wger
,"r"))==NULL
) {
91 fprintf(stderr
, "SARG: (topuser) %s: %s\n",text
[45],wger
);
95 if((fp_top2
=fopen(top2
,"w"))==NULL
) {
96 fprintf(stderr
, "SARG: (topuser) %s: %s\n",text
[45],top2
);
103 while(fgets(warea
,sizeof(warea
),fp_in
)) {
104 getword_start(&gwarea
,warea
);
105 if (getword(user
,sizeof(user
),&gwarea
,'\t')<0) {
106 printf("SARG: Maybe you have a broken user in your %s file.\n",wger
);
109 if(strcmp(user
,"TOTAL") == 0) {
112 if (getword_atoll(&nacc
,&gwarea
,'\t')<0) {
113 printf("SARG: Maybe you have a broken number of access in your %s file.\n",wger
);
116 if (getword_atoll(&nbytes
,&gwarea
,'\t')<0) {
117 printf("SARG: Maybe you have a broken number of bytes in your %s file.\n",wger
);
120 if (getword(url
,sizeof(url
),&gwarea
,'\t')<0) {
121 printf("SARG: Maybe you have a broken url in your %s file.\n",wger
);
124 if (getword(ip
,sizeof(ip
),&gwarea
,'\t')<0) {
125 printf("SARG: Maybe you have a broken user's IP in your %s file.\n",wger
);
128 if (getword(time
,sizeof(time
),&gwarea
,'\t')<0) {
129 printf("SARG: Maybe you have a broken time in your %s file.\n",wger
);
132 if (getword(date
,sizeof(date
),&gwarea
,'\t')<0) {
133 printf("SARG: Maybe you have a broken date in your %s file.\n",wger
);
136 if (getword_atoll(&elap
,&gwarea
,'\t')<0) {
137 printf("SARG: Maybe you have a broken download duration in your %s file.\n",wger
);
140 if (getword_atoll(&incac
,&gwarea
,'\t')<0) {
141 printf("SARG: Maybe you have a broken in cache download in your %s file.\n",wger
);
144 if (getword_atoll(&oucac
,&gwarea
,'\n')<0) {
145 printf("SARG: Maybe you have a broken not in cache download in your %s file.\n",wger
);
148 if(strcmp(olduser
,user
) != 0) {
151 if (olduser
[0] != '\0') {
152 my_lltoa(tnbytes
,val1
,15);
153 my_lltoa(tnacc
,val2
,15);
154 my_lltoa(tnelap
,val3
,15);
155 my_lltoa(tnincache
,val4
,15);
156 my_lltoa(tnoucache
,val5
,15);
157 fprintf(fp_top2
,"%s\t%s\t%s\t%s\t%s\t%s\n",olduser
,val1
,val2
,val3
,val4
,val5
);
162 ttnincache
+=tnincache
;
163 ttnoucache
+=tnoucache
;
165 strcpy(olduser
,user
);
181 if (olduser
[0] != '\0') {
182 my_lltoa(tnbytes
,val1
,15);
183 my_lltoa(tnacc
,val2
,15);
184 my_lltoa(tnelap
,val3
,15);
185 my_lltoa(tnincache
,val4
,15);
186 my_lltoa(tnoucache
,val5
,15);
187 fprintf(fp_top2
,"%s\t%s\t%s\t%s\t%s\t%s\n",olduser
,val1
,val2
,val3
,val4
,val5
);
192 ttnincache
+=tnincache
;
193 ttnoucache
+=tnoucache
;
197 strup(TopuserSortField
);
198 strlow(TopuserSortOrder
);
200 if(strcmp(TopuserSortField
,"USER") == 0)
201 strcpy(sfield
,"1,1");
203 if(strcmp(TopuserSortField
,"CONNECT") == 0)
204 strcpy(sfield
,"3,3");
206 if(strcmp(TopuserSortField
,"TIME") == 0)
207 strcpy(sfield
,"4,4");
209 if(strcmp(TopuserSortOrder
,"normal") == 0)
212 sprintf(csort
,"sort -n -T \"%s\" %s -k %s -o \"%s\" \"%s\"", TempDir
, order
, sfield
, top1
, top2
);
213 cstatus
=system(csort
);
214 if (!WIFEXITED(cstatus
) || WEXITSTATUS(cstatus
)) {
215 fprintf(stderr
, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus
));
216 fprintf(stderr
, "SARG: sort command: %s\n",csort
);
220 if((fp_top1
=fopen(top1
,"r"))==NULL
) {
221 fprintf(stderr
, "SARG: (topuser) %s: %s\n",text
[45],top1
);
227 if((fp_top3
=fopen(top3
,"w"))==NULL
) {
228 fprintf(stderr
, "SARG: (topuser) %s: %s\n",text
[45],top3
);
232 fprintf(fp_top3
, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n<html>\n<head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet
);
234 fputs("</head>\n<body class=\"body\">",fp_top3
);
235 write_logo_image(fp_top3
);
237 if(IndexTree
== INDEX_TREE_DATE
)
238 show_sarg(fp_top3
, "../../..");
240 show_sarg(fp_top3
, "..");
241 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_top3
);
242 fprintf(fp_top3
,"<tr><th align=\"center\" class=\"title\">%s</th></tr>\n",Title
);
243 fprintf(fp_top3
,"<tr><td class=\"header3\">%s: %s</td></tr>\n",text
[89],period
);
244 fprintf(fp_top3
,"<tr><td class=\"header3\">%s: %s, %s</td></tr>\n",text
[104],TopuserSortField
,TopuserSortOrder
);
245 fprintf(fp_top3
,"<tr><th class=\"header3\">%s</th></tr>\n",text
[137]);
246 fputs("</table></div>\n",fp_top3
);
248 fputs("<div align=\"center\"><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_top3
);
249 fputs("<tr><td> </td><td> </td></tr>\n",fp_top3
);
251 if((ReportType
& REPORT_TYPE_TOPSITES
) != 0 && !Privacy
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"11\"><a href=\"topsites.html\"><font class=\"link\">%s</font></a><font class=\"text\"></font></td></tr>\n",text
[119]);
252 if((ReportType
& REPORT_TYPE_SITES_USERS
) != 0 && !Privacy
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"11\"><a href=\"siteuser.html\"><font class=\"link\">%s</font></a><font class=\"text\"></font></td></tr>\n",text
[85]);
253 if(dansguardian_count
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"11\"><a href=\"dansguardian.html\"><font class=\"link\">%s</font></a><font class=\"text\"></font></td></tr>\n",text
[128]);
254 if(squidguard_count
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=11><a href=\"squidguard.html\"><font class=\"link\">%s</font></a><font class=\"text\"></font></td></tr>\n",text
[122]);
255 if ((ReportType
& REPORT_TYPE_DOWNLOADS
) != 0 && download_count
&& !Privacy
&& ndownload
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"11\"><a href=\"download.html\"><font class=\"link\">%s</font></a><font class=\"text\"></font></td></tr>\n",text
[125]);
256 if ((ReportType
& REPORT_TYPE_DENIED
) != 0 && denied_count
&& !Privacy
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"11\"><a href=\"denied.html\"><font class=\"link\">%s</font></a><font class=\"text\"></font></td></tr>\n",text
[118]);
257 if ((ReportType
& REPORT_TYPE_AUTH_FAILURES
) != 0 && authfail_count
&& !Privacy
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"11\"><a href=\"authfail.html\"><font class=\"link\">%s</font></a><font class=\"text\"></font></td></tr>\n",text
[117]);
258 if(smartfilter
) fprintf(fp_top3
,"<tr><td class=\"link\" colspan=\"11\"><a href=\"smartfilter.html\"><font class=\"link\">%s</font></a><font class=\"text\"></font></td></tr>\n",text
[116]);
259 fputs("<tr><td></td></tr>\n",fp_top3
);
261 if ((ReportType
& REPORT_TYPE_TOPUSERS
) == 0) {
262 fputs("</body>\n</html>\n",fp_top3
);
267 fputs("<tr>",fp_top3
);
269 if((TopUserFields
& TOPUSERFIELDS_NUM
) != 0)
270 fprintf(fp_top3
,"<th class=\"header\">%s</th>",text
[100]);
271 if((TopUserFields
& TOPUSERFIELDS_DATE_TIME
) !=0 && (ReportType
& REPORT_TYPE_DATE_TIME
) != 0)
272 fputs("<th class=\"header\"></th>",fp_top3
);
273 if((TopUserFields
& TOPUSERFIELDS_USERID
) != 0)
274 fprintf(fp_top3
,"<th class=\"header\">%s</th>",text
[98]);
275 if((TopUserFields
& TOPUSERFIELDS_CONNECT
) != 0)
276 fprintf(fp_top3
,"<th class=\"header\">%s</th>",text
[92]);
277 if((TopUserFields
& TOPUSERFIELDS_BYTES
) != 0)
278 fprintf(fp_top3
,"<th class=\"header\">%s</th>",text
[93]);
279 if((TopUserFields
& TOPUSERFIELDS_SETYB
) != 0)
280 fprintf(fp_top3
,"<th class=\"header\">%%%s</th>",text
[93]);
281 if((TopUserFields
& TOPUSERFIELDS_IN_CACHE_OUT
) != 0)
282 fprintf(fp_top3
,"<th class=\"header3\" colspan=\"2\">%s-%s-%s</th>",text
[113],text
[114],text
[112]);
283 if((TopUserFields
& TOPUSERFIELDS_USED_TIME
) != 0)
284 fprintf(fp_top3
,"<th class=\"header\">%s</th>",text
[94]);
285 if((TopUserFields
& TOPUSERFIELDS_MILISEC
) != 0)
286 fprintf(fp_top3
,"<th class=\"header\">%s</th>",text
[95]);
287 if((TopUserFields
& TOPUSERFIELDS_PTIME
) != 0)
288 fprintf(fp_top3
,"<th class=\"header\">%%%s</th>",text
[99]);
290 fputs("</tr>\n",fp_top3
);
294 while(fgets(warea
,sizeof(warea
),fp_top1
)) {
295 getword_start(&gwarea
,warea
);
296 if (getword(user
,sizeof(user
),&gwarea
,'\t')<0) {
297 printf("SARG: Maybe you have a broken user in your %s file.\n",top1
);
300 if (getword_atoll(&nbytes
,&gwarea
,'\t')<0) {
301 printf("SARG: Maybe you have a broken number of bytes in your %s file.\n",top1
);
304 if (getword_atoll(&nacc
,&gwarea
,'\t')<0) {
305 printf("SARG: Maybe you have a broken number of access in your %s file.\n",top1
);
308 if (getword_atoll(&elap
,&gwarea
,'\t')<0) {
309 printf("SARG: Maybe you have a broken elpased time in your %s file.\n",top1
);
312 if (getword_atoll(&incac
,&gwarea
,'\t')<0) {
313 printf("SARG: Maybe you have a broken in-cache size in your %s file.\n",top1
);
316 if (getword_atoll(&oucac
,&gwarea
,'\n')<0) {
317 printf("SARG: Maybe you have a broken out-of-cache size in your %s file.\n",top1
);
323 if(TopUsersNum
> 0 && topcount
>= TopUsersNum
) goto final
;
331 if(strcmp(user2
,ipantes
) != 0) {
332 strcpy(ipantes
,user2
);
333 ip2name(user2
,sizeof(user2
));
334 strcpy(nameantes
,user2
);
335 } else strcpy(user2
,nameantes
);
339 fputs("<tr>",fp_top3
);
342 if((TopUserFields
& TOPUSERFIELDS_NUM
) != 0)
343 fprintf(fp_top3
,"<td class=\"data\">%d</td>",posicao
);
345 user_find(name
, sizeof(name
), user2
);
347 ((str
=(char *) strstr(name
, ".")) != (char *) NULL
) &&
348 ((str
=(char *) strstr(str
+1, ".")) != (char *) NULL
))
349 ip2name(name
,sizeof(name
));
350 if(dotinuser
&& strchr(name
,'_')) {
351 subs(name
,sizeof(name
),"_",".");
354 if((TopUserFields
& TOPUSERFIELDS_DATE_TIME
) !=0 && (ReportType
& REPORT_TYPE_DATE_TIME
) != 0) {
355 sprintf(ltext110
,"%s",text
[110]);
356 for(s
=ltext110
; *s
; ++s
)
358 fputs("<td class=\"data2\">",fp_top3
);
361 fprintf(fp_top3
,"<a href=\"%s/graph_day.png\"><img src=\"%s/graph.png\" border=\"0\" title=\"%s\" alt=\"G\"></a> ",user
,ImageFile
,text
[126]);
364 fprintf(fp_top3
,"<a href=\"%s/d%s.html\"><img src=\"%s/datetime.png\" border=\"0\" title=\"%s",user
,user
,ImageFile
,ltext110
);
366 fprintf(fp_top3
," %s",text
[55]);
368 fputs("\" alt=\"T\"></a></td>",fp_top3
);
370 sprintf(val1
,"%s/d%s.html",dirname
,user
);
373 if((TopUserFields
& TOPUSERFIELDS_USERID
) != 0) {
374 if((ReportType
& REPORT_TYPE_USERS_SITES
) == 0)
375 fprintf(fp_top3
,"<td class=\"data2\">%s</td>",name
);
377 fprintf(fp_top3
,"<td class=\"data2\"><a href=\"%s/%s.html\">%s</a></td>",user
,user
,name
);
379 if((TopUserFields
& TOPUSERFIELDS_CONNECT
) != 0)
380 fprintf(fp_top3
,"<td class=\"data\">%s</td>",fixnum(nacc
,1));
381 if((TopUserFields
& TOPUSERFIELDS_BYTES
) != 0)
382 fprintf(fp_top3
,"<td class=\"data\">%s</td>",fixnum(tnbytes
,1));
383 if((TopUserFields
& TOPUSERFIELDS_SETYB
) != 0) {
384 perc
=(ttnbytes
) ? tnbytes
* 100. / ttnbytes
: 0.;
385 fprintf(fp_top3
,"<td class=\"data\">%3.2lf%%</td>",perc
);
387 if((TopUserFields
& TOPUSERFIELDS_IN_CACHE_OUT
) != 0) {
388 inperc
=(tnbytes
) ? incac
* 100. / tnbytes
: 0.;
389 ouperc
=(tnbytes
) ? oucac
* 100. / tnbytes
: 0.;
390 fprintf(fp_top3
,"<td class=\"data\">%3.2lf%%</td><td class=\"data\">%3.2lf%%</td>",inperc
,ouperc
);
392 if((TopUserFields
& TOPUSERFIELDS_USED_TIME
) != 0)
393 fprintf(fp_top3
,"<td class=\"data\">%s</td>",buildtime(tnelap
));
394 if((TopUserFields
& TOPUSERFIELDS_MILISEC
) != 0)
395 fprintf(fp_top3
,"<td class=\"data\">%s</td>",fixnum2(tnelap
,1));
396 if((TopUserFields
& TOPUSERFIELDS_PTIME
) != 0) {
397 perc2
=(ttnelap
) ? elap
* 100. / ttnelap
: 0.;
398 fprintf(fp_top3
,"<td class=\"data\">%3.2lf%%</td>",perc2
);
401 fputs("</tr>\n",fp_top3
);
406 if((TopUserFields
& TOPUSERFIELDS_TOTAL
) != 0) {
408 fputs("<tr>",fp_top3
);
409 if((TopUserFields
& TOPUSERFIELDS_NUM
) != 0)
410 fputs("<td></td>",fp_top3
);
411 if((TopUserFields
& TOPUSERFIELDS_DATE_TIME
) !=0 && (ReportType
& REPORT_TYPE_DATE_TIME
) != 0)
412 fputs("<td></td>",fp_top3
);
413 fprintf(fp_top3
,"<th class=\"header\">%s</th>",text
[107]);
415 if((TopUserFields
& TOPUSERFIELDS_CONNECT
) != 0)
416 fprintf(fp_top3
,"<th class=\"header2\">%s</th>",fixnum(ttnacc
,1));
417 if((TopUserFields
& TOPUSERFIELDS_BYTES
) != 0)
418 fprintf(fp_top3
,"<th class=\"header2\">%15s</th>",fixnum(ttnbytes
,1));
419 if((TopUserFields
& TOPUSERFIELDS_SETYB
) != 0)
420 fputs("<td></td>",fp_top3
);
421 if((TopUserFields
& TOPUSERFIELDS_IN_CACHE_OUT
) != 0)
423 inperc
=(ttnbytes
) ? ttnincache
* 100. / ttnbytes
: 0.;
424 ouperc
=(ttnbytes
) ? ttnoucache
*100. / ttnbytes
: 0.;
425 fprintf(fp_top3
,"<th class=\"header2\">%3.2lf%%</th><th class=\"header2\">%3.2lf%%</th>",inperc
,ouperc
);
427 if((TopUserFields
& TOPUSERFIELDS_USED_TIME
) != 0)
428 fprintf(fp_top3
,"<th class=\"header2\">%s</th>",buildtime(ttnelap
));
429 if((TopUserFields
& TOPUSERFIELDS_MILISEC
) != 0)
430 fprintf(fp_top3
,"<th class=\"header2\">%s</th>",fixnum2(ttnelap
,1));
432 fputs("</tr>\n",fp_top3
);
435 if(ntopuser
&& (TopUserFields
& TOPUSERFIELDS_AVERAGE
) != 0) {
436 fputs("<tr>",fp_top3
);
437 if((TopUserFields
& TOPUSERFIELDS_NUM
) != 0)
438 fputs("<td></td>",fp_top3
);
439 if((TopUserFields
& TOPUSERFIELDS_DATE_TIME
) !=0 && (ReportType
& REPORT_TYPE_DATE_TIME
) != 0)
440 fputs("<td></td>",fp_top3
);
441 fprintf(fp_top3
,"<th class=\"header\">%s</th>",text
[96]);
443 if((TopUserFields
& TOPUSERFIELDS_CONNECT
) != 0)
444 fprintf(fp_top3
,"<th class=\"header2\">%s</th>",fixnum(ttnacc
/totuser
,1));
445 if((TopUserFields
& TOPUSERFIELDS_BYTES
) != 0) {
446 tnbytes
=(totuser
) ? ttnbytes
/ totuser
: 0;
447 fprintf(fp_top3
,"<th class=\"header2\">%15s</th>",fixnum(tnbytes
,1));
449 if((TopUserFields
& TOPUSERFIELDS_SETYB
) != 0)
450 fputs("<td></td>",fp_top3
);
451 if((TopUserFields
& TOPUSERFIELDS_IN_CACHE_OUT
) != 0)
452 fputs("<td></td><td></td>",fp_top3
);
453 if((TopUserFields
& TOPUSERFIELDS_USED_TIME
) != 0)
454 fprintf(fp_top3
,"<th class=\"header2\">%s</th>",buildtime(ttnelap
/totuser
));
455 if((TopUserFields
& TOPUSERFIELDS_MILISEC
) != 0)
456 fprintf(fp_top3
,"<th class=\"header2\">%s</th>",fixnum2(ttnelap
/totuser
,1));
457 fputs("</tr>\n",fp_top3
);
460 if(UserAgentLog
[0] != '\0') {
461 fputs("<tr><td></td></tr>\n",fp_top3
);
462 fputs("<tr><td></td></tr>\n",fp_top3
);
463 fputs("<td align=\"left\" colspan=\"8\"><font size=-1><a href=\"useragent.html\">Useragent</a> Report</td>\n",fp_top3
);
466 fputs("</table></div>",fp_top3
);
474 if((fp_ou
=fopen(tusr
,"w"))==NULL
) {
475 fprintf(stderr
, "SARG: (topuser) %s: %s\n",text
[45],tusr
);
479 fprintf(fp_ou
,"%d\n",totuser
);
481 fputs("</body>\n</html>\n",fp_top3
);