]>
git.ipfire.org Git - thirdparty/sarg.git/blob - topsites.c
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"
40 char general2
[MAXLEN
];
41 char general3
[MAXLEN
];
51 long long int tnacc
=0;
52 long long int tnbytes
=0;
53 long long int tntime
=0;
54 long long int twork1
=0, twork2
=0, twork3
=0;
59 struct getwordstruct gwarea
;
60 struct longlinestruct line
;
61 struct generalitemstruct item
;
66 sprintf(general
,"%s/sarg-general",dirname
);
67 sprintf(sites
,"%s/sarg-sites",dirname
);
68 sprintf(general2
,"%s/sarg-general2",dirname
);
69 sprintf(general3
,"%s/sarg-general3",dirname
);
70 sprintf(per
,"%s/sarg-period",dirname
);
72 if ((ReportType
& REPORT_TYPE_TOPUSERS
) == 0)
73 sprintf(report
,"%s/index.html",dirname
);
75 sprintf(report
,"%s/topsites.html",dirname
);
77 if ((fp_in
= fopen(per
, "r")) == 0) {
78 fprintf(stderr
, "SARG: (topuser) %s: %s\n",_("Cannot open file"),per
);
82 if (!fgets(period
,sizeof(period
),fp_in
)) {
83 fprintf(stderr
,"SARG: (topuser) read error in %s\n",per
);
88 sprintf(csort
,"sort -k 4,4 -o \"%s\" \"%s\"",general2
,general
);
89 cstatus
=system(csort
);
90 if (!WIFEXITED(cstatus
) || WEXITSTATUS(cstatus
)) {
91 fprintf(stderr
, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus
));
92 fprintf(stderr
, "SARG: sort command: %s\n",csort
);
96 if((fp_in
=fopen(general2
,"r"))==NULL
) {
97 fprintf(stderr
, "SARG: (topsite) %s: %s\n",_("Cannot open log file"),general2
);
98 fprintf(stderr
, "SARG: sort command: %s\n",csort
);
102 if((fp_ou
=fopen(general3
,"w"))==NULL
) {
103 fprintf(stderr
, "SARG: (topsite) %s: %s\n",_("Cannot open log file"),general3
);
107 if (longline_prepare(&line
)<0) {
108 debuga(_("Not enough memory to read file %s"),general2
);
112 while((buf
=longline_read(fp_in
,&line
))!=NULL
) {
113 ger_read(buf
,&item
,general2
);
114 if(item
.total
) continue;
117 url_len
=strlen(item
.url
);
118 if (!ourl
|| url_len
>=ourl_size
) {
120 ourl
=realloc(ourl
,ourl_size
);
122 debuga(_("Not enough memory to store the url"));
126 strcpy(ourl
,item
.url
);
130 if(strcmp(item
.url
,ourl
) != 0) {
131 fprintf(fp_ou
,"%lld\t%lld\t%lld\t%s\n",tnacc
,tnbytes
,tntime
,ourl
);
132 url_len
=strlen(item
.url
);
133 if (url_len
>=ourl_size
) {
135 ourl
=realloc(ourl
,ourl_size
);
137 debuga(_("Not enough memory to store the url"));
141 strcpy(ourl
,item
.url
);
148 tnbytes
+=item
.nbytes
;
153 longline_free(&line
);
156 fprintf(fp_ou
,"%lld\t%lld\t%lld\t%s\n",tnacc
,tnbytes
,tntime
,ourl
);
162 strlow(TopsitesSortField
);
163 strlow(TopsitesSortType
);
165 if(strcmp(TopsitesSortField
,"connect") == 0)
166 sortf
="-k 1,1 -k 2,2";
167 else if(strcmp(TopsitesSortField
,"bytes") == 0)
168 sortf
="-k 2,2 -k 1,1";
171 if(strcmp(TopsitesSortType
,"a") == 0)
173 else if(strcmp(TopsitesSortType
,"d") == 0)
178 sprintf(csort
,"sort %s -n %s -o \"%s\" \"%s\"",sortt
,sortf
,sites
,general3
);
179 cstatus
=system(csort
);
180 if (!WIFEXITED(cstatus
) || WEXITSTATUS(cstatus
)) {
181 fprintf(stderr
, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus
));
182 fprintf(stderr
, "SARG: sort command: %s\n",csort
);
185 if((fp_in
=fopen(sites
,"r"))==NULL
) {
186 fprintf(stderr
, "SARG: (topsite) %s: %s\n",_("Cannot open log file"),sites
);
187 fprintf(stderr
, "SARG: sort command: %s\n",csort
);
194 if((fp_ou
=fopen(report
,"w"))==NULL
) {
195 fprintf(stderr
, "SARG: (topsite) %s: %s\n",_("Cannot open log file"),report
);
199 write_html_header(fp_ou
,(IndexTree
== INDEX_TREE_DATE
) ? 3 : 1,_("Top sites"));
200 fprintf(fp_ou
,"<tr><td class=\"header_c\">%s: %s</td></tr>\n",_("Period"),period
);
201 fprintf(fp_ou
,"<tr><th class=\"header_c\">%s %d %s</th></tr>\n",_("Top"),TopSitesNum
,_("sites"));
202 close_html_header(fp_ou
);
204 fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_ou
);
205 fputs("<tr><td></td></tr>\n",fp_ou
);
206 fprintf(fp_ou
,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",_("NUM"),_("ACCESSED SITE"),_("CONNECT"),_("BYTES"),_("TIME"));
211 if (longline_prepare(&line
)<0) {
212 debuga(_("Not enough memory to read file %s"),sites
);
216 while(regs
<TopSitesNum
&& (buf
=longline_read(fp_in
,&line
))!=NULL
) {
217 getword_start(&gwarea
,buf
);
218 if (getword_atoll(&nacc
,&gwarea
,'\t')<0) {
219 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",sites
);
222 if (nacc
== 0) continue;
223 if (getword_atoll(&nbytes
,&gwarea
,'\t')<0 || getword_atoll(&ntime
,&gwarea
,'\t')<0) {
224 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",sites
);
227 if (getword_ptr(buf
,&url
,&gwarea
,'\t')<0) {
228 debuga(_("Maybe you have a broken url in your %s file"),sites
);
236 strcpy(wwork1
,fixnum(twork1
,1));
237 strcpy(wwork2
,fixnum(twork2
,1));
238 strcpy(wwork3
,fixtime(twork3
));
240 fprintf(fp_ou
,"<tr><td class=\"data\">%d</td><td class=\"data2 link\">",++regs
);
242 if(BlockIt
[0] != '\0') {
243 fprintf(fp_ou
,"<a href=\"%s%s?url=\"",wwwDocumentRoot
,BlockIt
);
244 output_html_url(fp_ou
,url
);
245 fputs("\"><img src=\"../images/sarg-squidguard-block.png\"></a> ",fp_ou
);
248 fputs("<a href=\"http://",fp_ou
);
249 output_html_url(fp_ou
,url
);
251 output_html_string(fp_ou
,url
,100);
252 fprintf(fp_ou
,"</a></td><td class=\"data\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s</td></tr>\n",wwork1
,wwork2
,wwork3
);
255 longline_free(&line
);
257 fputs("</table></div>\n",fp_ou
);
258 write_html_trailer(fp_ou
);