]> git.ipfire.org Git - thirdparty/sarg.git/blob - topsites.c
Make the reports title consistent
[thirdparty/sarg.git] / topsites.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 topsites(void)
31 {
32
33 FILE *fp_in, *fp_ou;
34
35 char *buf;
36 char *url;
37 char *ourl=NULL;
38 char csort[255];
39 char general[MAXLEN];
40 char general2[MAXLEN];
41 char general3[MAXLEN];
42 char sites[MAXLEN];
43 char report[MAXLEN];
44 const char *sortf;
45 const char *sortt;
46 long long int nacc;
47 long long int nbytes;
48 long long int ntime;
49 long long int tnacc=0;
50 long long int tnbytes=0;
51 long long int tntime=0;
52 long long int twork1=0, twork2=0, twork3=0;
53 int regs=0;
54 int cstatus;
55 int url_len;
56 int ourl_size=0;
57 struct getwordstruct gwarea;
58 longline line;
59 struct generalitemstruct item;
60
61 if(Privacy)
62 return;
63
64 sprintf(general,"%s/sarg-general",outdirname);
65 sprintf(sites,"%s/sarg-sites",outdirname);
66 sprintf(general2,"%s/sarg-general2",outdirname);
67 sprintf(general3,"%s/sarg-general3",outdirname);
68
69 if ((ReportType & REPORT_TYPE_TOPUSERS) == 0)
70 sprintf(report,"%s/index.html",outdirname);
71 else
72 sprintf(report,"%s/topsites.html",outdirname);
73
74 sprintf(csort,"sort -k 4,4 -o \"%s\" \"%s\"",general2,general);
75 cstatus=system(csort);
76 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
77 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
78 debuga(_("sort command: %s\n"),csort);
79 exit(EXIT_FAILURE);
80 }
81
82 if((fp_in=fopen(general2,"r"))==NULL) {
83 debuga(_("(topsites) Cannot open log file %s\n"),general2);
84 debuga(_("sort command: %s\n"),csort);
85 exit(EXIT_FAILURE);
86 }
87
88 if((fp_ou=fopen(general3,"w"))==NULL) {
89 debuga(_("(topsites) Cannot open log file %s\n"),general3);
90 exit(EXIT_FAILURE);
91 }
92
93 if ((line=longline_create())==NULL) {
94 debuga(_("Not enough memory to read file %s\n"),general2);
95 exit(EXIT_FAILURE);
96 }
97
98 while((buf=longline_read(fp_in,line))!=NULL) {
99 ger_read(buf,&item,general2);
100 if(item.total) continue;
101
102 if(!regs) {
103 url_len=strlen(item.url);
104 if (!ourl || url_len>=ourl_size) {
105 ourl_size=url_len+1;
106 ourl=realloc(ourl,ourl_size);
107 if (!ourl) {
108 debuga(_("Not enough memory to store the url\n"));
109 exit(EXIT_FAILURE);
110 }
111 }
112 strcpy(ourl,item.url);
113 regs++;
114 }
115
116 if(strcmp(item.url,ourl) != 0) {
117 fprintf(fp_ou,"%lld\t%lld\t%lld\t%s\n",tnacc,tnbytes,tntime,ourl);
118 url_len=strlen(item.url);
119 if (url_len>=ourl_size) {
120 ourl_size=url_len+1;
121 ourl=realloc(ourl,ourl_size);
122 if (!ourl) {
123 debuga(_("Not enough memory to store the url\n"));
124 exit(EXIT_FAILURE);
125 }
126 }
127 strcpy(ourl,item.url);
128 tnacc=0;
129 tnbytes=0;
130 tntime=0;
131 }
132
133 tnacc+=item.nacc;
134 tnbytes+=item.nbytes;
135 tntime+=item.nelap;
136 }
137 fclose(fp_in);
138 unlink(general2);
139 longline_destroy(&line);
140
141 if (ourl) {
142 fprintf(fp_ou,"%lld\t%lld\t%lld\t%s\n",tnacc,tnbytes,tntime,ourl);
143 free(ourl);
144 }
145
146 fclose(fp_ou);
147
148 strlow(TopsitesSortField);
149 strlow(TopsitesSortType);
150
151 if(strcmp(TopsitesSortField,"connect") == 0)
152 sortf="-k 1,1 -k 2,2";
153 else if(strcmp(TopsitesSortField,"bytes") == 0)
154 sortf="-k 2,2 -k 1,1";
155 else
156 sortf="";
157 if(strcmp(TopsitesSortType,"a") == 0)
158 sortt="";
159 else if(strcmp(TopsitesSortType,"d") == 0)
160 sortt="-r";
161 else
162 sortt="";
163
164 sprintf(csort,"sort %s -n %s -o \"%s\" \"%s\"",sortt,sortf,sites,general3);
165 cstatus=system(csort);
166 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
167 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
168 debuga(_("sort command: %s\n"),csort);
169 exit(EXIT_FAILURE);
170 }
171 if((fp_in=fopen(sites,"r"))==NULL) {
172 debuga(_("(topsites) Cannot open log file %s\n"),sites);
173 debuga(_("sort command: %s\n"),csort);
174 exit(EXIT_FAILURE);
175 }
176
177 unlink(general2);
178 unlink(general3);
179
180 if((fp_ou=fopen(report,"w"))==NULL) {
181 debuga(_("(topsites) Cannot open log file %s\n"),report);
182 exit(EXIT_FAILURE);
183 }
184
185 write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Top sites"));
186 fputs("<tr><td class=\"header_c\">",fp_ou);
187 fprintf(fp_ou,_("Period: %s"),period.html);
188 fputs("</td></tr>\n",fp_ou);
189 fputs("<tr><th class=\"header_c\">",fp_ou);
190 fprintf(fp_ou,_("Top %d sites"),TopSitesNum);
191 fputs("</th></tr>\n",fp_ou);
192 close_html_header(fp_ou);
193
194 fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_ou);
195 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"));
196
197 regs=0;
198 ntopsites = 0;
199
200 if ((line=longline_create())==NULL) {
201 debuga(_("Not enough memory to read file %s\n"),sites);
202 exit(EXIT_FAILURE);
203 }
204
205 while(regs<TopSitesNum && (buf=longline_read(fp_in,line))!=NULL) {
206 getword_start(&gwarea,buf);
207 if (getword_atoll(&nacc,&gwarea,'\t')<0) {
208 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),sites);
209 exit(EXIT_FAILURE);
210 }
211 if (nacc == 0) continue;
212 if (getword_atoll(&nbytes,&gwarea,'\t')<0 || getword_atoll(&ntime,&gwarea,'\t')<0) {
213 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),sites);
214 exit(EXIT_FAILURE);
215 }
216 if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
217 debuga(_("The url is invalid in file %s\n"),sites);
218 exit(EXIT_FAILURE);
219 }
220
221 twork1=nacc;
222 twork2=nbytes;
223 twork3=ntime;
224
225 strcpy(wwork1,fixnum(twork1,1));
226 strcpy(wwork2,fixnum(twork2,1));
227 strcpy(wwork3,fixtime(twork3));
228
229 fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2 link\">",++regs);
230
231 if(BlockIt[0] != '\0') {
232 fprintf(fp_ou,"<a href=\"%s%s?url=\"",wwwDocumentRoot,BlockIt);
233 output_html_url(fp_ou,url);
234 fputs("\"><img src=\"../images/sarg-squidguard-block.png\"></a>&nbsp;",fp_ou);
235 }
236
237 fputs("<a href=\"http://",fp_ou);
238 output_html_url(fp_ou,url);
239 fputs("\">",fp_ou);
240 output_html_string(fp_ou,url,100);
241 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);
242 }
243 fclose(fp_in);
244 longline_destroy(&line);
245
246 fputs("</table></div>\n",fp_ou);
247 if (write_html_trailer(fp_ou)<0)
248 debuga(_("Write error in file %s\n"),report);
249 if (fclose(fp_ou)==EOF)
250 debuga(_("Failed to close file %s - %s\n"),report,strerror(errno));
251
252 return;
253
254 }