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";
59 struct getwordstruct gwarea
;
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
);
73 if (!fgets(period
,sizeof(period
),fp_in
)) {
74 fprintf(stderr
,"SARG: (topuser) Read error in %s\n",arqper
);
84 strcat(wger
,"/sarg-general");
86 strcat(top2
,"/top.tmp");
87 strcat(tusr
,"/sarg-users");
88 strcat(top3
,"/index.html");
91 if((fp_in
=fopen(wger
,"r"))==NULL
) {
92 fprintf(stderr
, "SARG: (topuser) %s: %s\n",text
[45],wger
);
96 if((fp_top2
=fopen(top2
,"w"))==NULL
) {
97 fprintf(stderr
, "SARG: (topuser) %s: %s\n",text
[45],top2
);
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
);
110 if(strcmp(user
,"TOTAL") == 0) {
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
);
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
);
121 if (getword(url
,sizeof(url
),&gwarea
,'\t')<0) {
122 printf("SARG: Maybe you have a broken url in your %s file.\n",wger
);
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
);
129 if (getword(time
,sizeof(time
),&gwarea
,'\t')<0) {
130 printf("SARG: Maybe you have a broken time in your %s file.\n",wger
);
133 if (getword(date
,sizeof(date
),&gwarea
,'\t')<0) {
134 printf("SARG: Maybe you have a broken date in your %s file.\n",wger
);
137 if (getword_atoll(&elap
,&gwarea
,'\t')<0) {
138 printf("SARG: Maybe you have a broken download duration in your %s file.\n",wger
);
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
);
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
);
149 if(strcmp(olduser
,user
) != 0) {
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
);
163 ttnincache
+=tnincache
;
164 ttnoucache
+=tnoucache
;
166 strcpy(olduser
,user
);
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
);
193 ttnincache
+=tnincache
;
194 ttnoucache
+=tnoucache
;
198 strup(TopuserSortField
);
199 strlow(TopuserSortOrder
);
201 if(strcmp(TopuserSortField
,"USER") == 0)
202 strcpy(sfield
,"1,1");
204 if(strcmp(TopuserSortField
,"CONNECT") == 0)
205 strcpy(sfield
,"3,3");
207 if(strcmp(TopuserSortField
,"TIME") == 0)
208 strcpy(sfield
,"4,4");
210 if(strcmp(TopuserSortOrder
,"normal") == 0)
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
);
221 if((fp_top1
=fopen(top1
,"r"))==NULL
) {
222 fprintf(stderr
, "SARG: (topuser) %s: %s\n",text
[45],top1
);
228 if((fp_top3
=fopen(top3
,"w"))==NULL
) {
229 fprintf(stderr
, "SARG: (topuser) %s: %s\n",text
[45],top3
);
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
);
240 fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_top3
);
241 fputs("<tr><td> </td><td> </td></tr>\n",fp_top3
);
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
);
254 if ((ReportType
& REPORT_TYPE_TOPUSERS
) == 0) {
255 fputs("</body>\n</html>\n",fp_top3
);
260 fputs("<tr>",fp_top3
);
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]);
283 fputs("</tr>\n",fp_top3
);
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
);
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
);
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
);
301 if (getword_atoll(&elap
,&gwarea
,'\t')<0) {
302 printf("SARG: Maybe you have a broken elpased time in your %s file.\n",top1
);
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
);
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
);
316 if(TopUsersNum
> 0 && topcount
>= TopUsersNum
) break;
324 if(strcmp(user2
,ipantes
) != 0) {
325 strcpy(ipantes
,user2
);
326 ip2name(user2
,sizeof(user2
));
327 strcpy(nameantes
,user2
);
328 } else strcpy(user2
,nameantes
);
332 fputs("<tr>",fp_top3
);
335 if((TopUserFields
& TOPUSERFIELDS_NUM
) != 0)
336 fprintf(fp_top3
,"<td class=\"data\">%d</td>",posicao
);
338 user_find(name
, sizeof(name
), user2
);
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
),"_",".");
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
)
351 fputs("<td class=\"data2\">",fp_top3
);
354 fprintf(fp_top3
,"<a href=\"%s/graph_day.png\"><img src=\"%s/graph.png\" title=\"%s\" alt=\"G\"></a> ",user
,ImageFile
,text
[126]);
357 fprintf(fp_top3
,"<a href=\"%s/d%s.html\"><img src=\"%s/datetime.png\" title=\"%s",user
,user
,ImageFile
,ltext110
);
359 fprintf(fp_top3
," %s",text
[55]);
361 fputs("\" alt=\"T\"></a></td>",fp_top3
);
363 sprintf(val1
,"%s/d%s.html",dirname
,user
);
366 if((TopUserFields
& TOPUSERFIELDS_USERID
) != 0) {
367 if((ReportType
& REPORT_TYPE_USERS_SITES
) == 0)
368 fprintf(fp_top3
,"<td class=\"data2\">%s</td>",name
);
370 fprintf(fp_top3
,"<td class=\"data2\"><a href=\"%s/%s.html\">%s</a></td>",user
,user
,name
);
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
);
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
);
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
);
394 fputs("</tr>\n",fp_top3
);
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]);
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)
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
);
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));
426 fputs("</tr>\n",fp_top3
);
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]);
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));
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
);
454 fputs("</table></div>\n",fp_top3
);
455 write_html_trailer(fp_top3
);
458 if((fp_ou
=fopen(tusr
,"w"))==NULL
) {
459 fprintf(stderr
, "SARG: (topuser) %s: %s\n",text
[45],tusr
);
462 fprintf(fp_ou
,"%d\n",totuser
);