]> git.ipfire.org Git - thirdparty/sarg.git/blob - topuser.c
2338cda02c80d004c64f0586ecf717825c89e2b2
[thirdparty/sarg.git] / topuser.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 topuser(void)
31 {
32
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;
37 long long int nbytes;
38 long long int nacc;
39 long long int elap, incac, oucac;
40 double perc=0.00;
41 double perc2=0.00;
42 double inperc=0.00, ouperc=0.00;
43 int posicao=0;
44 char olduser[MAX_USER_LEN], csort[MAXLEN];
45 char wger[MAXLEN], top1[MAXLEN], top2[MAXLEN], top3[MAXLEN];
46 char user[MAX_USER_LEN], tusr[MAXLEN];
47 char ipantes[MAXLEN], nameantes[MAXLEN];
48 const char *sfield="-n -k 2,2";
49 const char *order="-r";
50 char title[80];
51 char *warea;
52 int totuser=0;
53 int topcount=0;
54 int cstatus;
55 struct getwordstruct gwarea;
56 longline line;
57 struct generalitemstruct item;
58 const struct userinfostruct *uinfo;
59
60 ipantes[0]='\0';
61 nameantes[0]='\0';
62
63 ntopuser = 0;
64 snprintf(wger,sizeof(wger),"%s/sarg-general",outdirname);
65 if((fp_in=fopen(wger,"r"))==NULL) {
66 debuga(_("(topuser) Cannot open file %s\n"),wger);
67 exit(EXIT_FAILURE);
68 }
69
70 snprintf(top2,sizeof(top2),"%s/top.tmp",outdirname);
71 if((fp_top2=fopen(top2,"w"))==NULL) {
72 debuga(_("(topuser) Cannot open file %s\n"),top2);
73 exit(EXIT_FAILURE);
74 }
75
76 olduser[0]='\0';
77 totuser=0;
78
79 if ((line=longline_create())==NULL) {
80 debuga(_("Not enough memory to read the file %s\n"),wger);
81 exit(EXIT_FAILURE);
82 }
83
84 while((warea=longline_read(fp_in,line))!=NULL) {
85 ger_read(warea,&item,wger);
86 if(item.total) continue;
87 if(strcmp(olduser,item.user) != 0) {
88 totuser++;
89
90 if (olduser[0] != '\0') {
91 fprintf(fp_top2,"%s\t%lld\t%lld\t%lld\t%lld\t%lld\n",olduser,tnbytes,tnacc,tnelap,tnincache,tnoucache);
92
93 ttnbytes+=tnbytes;
94 ttnacc+=tnacc;
95 ttnelap+=tnelap;
96 ttnincache+=tnincache;
97 ttnoucache+=tnoucache;
98 }
99 strcpy(olduser,item.user);
100 tnbytes=0;
101 tnacc=0;
102 tnelap=0;
103 tnincache=0;
104 tnoucache=0;
105 }
106
107 tnbytes+=item.nbytes;
108 tnacc+=item.nacc;
109 tnelap+=item.nelap;
110 tnincache+=item.incache;
111 tnoucache+=item.oucache;
112 }
113 fclose(fp_in);
114 longline_destroy(&line);
115
116 if (olduser[0] != '\0') {
117 fprintf(fp_top2,"%s\t%lld\t%lld\t%lld\t%lld\t%lld\n",olduser,tnbytes,tnacc,tnelap,tnincache,tnoucache);
118
119 ttnbytes+=tnbytes;
120 ttnacc+=tnacc;
121 ttnelap+=tnelap;
122 ttnincache+=tnincache;
123 ttnoucache+=tnoucache;
124 }
125 fclose(fp_top2);
126
127 strup(TopuserSortField);
128 strlow(TopuserSortOrder);
129
130 if(strcmp(TopuserSortField,"USER") == 0)
131 sfield="-k 1,1";
132
133 if(strcmp(TopuserSortField,"CONNECT") == 0)
134 sfield="-n -k 3,3";
135
136 if(strcmp(TopuserSortField,"TIME") == 0)
137 sfield="-n -k 4,4";
138
139 if(strcmp(TopuserSortOrder,"normal") == 0)
140 order="";
141
142 snprintf(top1,sizeof(top1),"%s/top",outdirname);
143 sprintf(csort,"sort -T \"%s\" %s %s -o \"%s\" \"%s\"", TempDir, order, sfield, top1, top2);
144 cstatus=system(csort);
145 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
146 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
147 debuga(_("sort command: %s\n"),csort);
148 exit(EXIT_FAILURE);
149 }
150
151 if((fp_top1=fopen(top1,"r"))==NULL) {
152 debuga(_("(topuser) Cannot open file %s\n"),top1);
153 exit(EXIT_FAILURE);
154 }
155
156 unlink(top2);
157
158 snprintf(top3,sizeof(top3),"%s/index.html",outdirname);
159 if((fp_top3=fopen(top3,"w"))==NULL) {
160 debuga(_("(topuser) Cannot open file %s\n"),top3);
161 exit(EXIT_FAILURE);
162 }
163
164 snprintf(title,sizeof(title),_("SARG report for %s"),period.text);
165 write_html_header(fp_top3,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,title);
166 fputs("<tr><td class=\"header_c\">",fp_top3);
167 fprintf(fp_top3,_("Period: %s"),period.html);
168 fputs("</td></tr>\n",fp_top3);
169 fprintf(fp_top3,"<tr><td class=\"header_c\">%s: %s, %s</td></tr>\n",_("Sort"),TopuserSortField,TopuserSortOrder);
170 fprintf(fp_top3,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Top users"));
171 close_html_header(fp_top3);
172
173 fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_top3);
174
175 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",_("Top sites"));
176 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",_("Sites & Users"));
177 if(dansguardian_count) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"dansguardian.html\">%s</a></td></tr>\n",_("DansGuardian"));
178 if(squidguard_count) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"redirector.html\">%s</a></td></tr>\n",_("Redirector"));
179 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",_("Downloads"));
180 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",_("Denied accesses"));
181 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",_("Authentication Failures"));
182 if(smartfilter) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"smartfilter.html\">%s</a></td></tr>\n",_("SmartFilter"));
183 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"));
184 fputs("<tr><td></td></tr>\n</table></div>\n",fp_top3);
185
186 if ((ReportType & REPORT_TYPE_TOPUSERS) == 0) {
187 fputs("</body>\n</html>\n",fp_top3);
188 fclose (fp_top3);
189 return;
190 }
191
192 fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_top3);
193 fputs("<tr>",fp_top3);
194
195 if((TopUserFields & TOPUSERFIELDS_NUM) != 0)
196 fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("NUM"));
197 if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0)
198 fputs("<th class=\"header_l\"></th>",fp_top3);
199 if((TopUserFields & TOPUSERFIELDS_USERID) != 0)
200 fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("USERID"));
201 if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0)
202 fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("CONNECT"));
203 if((TopUserFields & TOPUSERFIELDS_BYTES) != 0)
204 fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("BYTES"));
205 if((TopUserFields & TOPUSERFIELDS_SETYB) != 0)
206 fprintf(fp_top3,"<th class=\"header_l\">%%%s</th>",_("BYTES"));
207 if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0)
208 fprintf(fp_top3,"<th class=\"header_c\" colspan=\"2\">%s</th>",_("IN-CACHE-OUT"));
209 if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0)
210 fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("ELAPSED TIME"));
211 if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0)
212 fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("MILLISEC"));
213 if((TopUserFields & TOPUSERFIELDS_PTIME) != 0)
214 fprintf(fp_top3,"<th class=\"header_l\">%%%s</th>",_("TIME"));
215
216 fputs("</tr>\n",fp_top3);
217
218 ntopuser = 0;
219
220 if ((line=longline_create())==NULL) {
221 debuga(_("Not enough memory to read the downloaded files\n"));
222 exit(EXIT_FAILURE);
223 }
224
225 while((warea=longline_read(fp_top1,line))!=NULL) {
226 getword_start(&gwarea,warea);
227 if (getword(user,sizeof(user),&gwarea,'\t')<0) {
228 debuga(_("There is a broken user in file %s\n"),top1);
229 exit(EXIT_FAILURE);
230 }
231 if (getword_atoll(&nbytes,&gwarea,'\t')<0) {
232 debuga(_("There is a broken number of bytes in file %s\n"),top1);
233 exit(EXIT_FAILURE);
234 }
235 if (getword_atoll(&nacc,&gwarea,'\t')<0) {
236 debuga(_("There is a broken number of access in file %s\n"),top1);
237 exit(EXIT_FAILURE);
238 }
239 if (getword_atoll(&elap,&gwarea,'\t')<0) {
240 debuga(_("There is a broken elpased time in file %s\n"),top1);
241 exit(EXIT_FAILURE);
242 }
243 if (getword_atoll(&incac,&gwarea,'\t')<0) {
244 debuga(_("There is a broken in-cache size in file %s\n"),top1);
245 exit(EXIT_FAILURE);
246 }
247 if (getword_atoll(&oucac,&gwarea,'\n')<0) {
248 debuga(_("There is a broken out-of-cache size in file %s\n"),top1);
249 exit(EXIT_FAILURE);
250 }
251 if(nacc < 1)
252 continue;
253 ntopuser = 1;
254 if(TopUsersNum > 0 && topcount >= TopUsersNum) break;
255 tnbytes=nbytes;
256 tnelap=elap;
257
258 uinfo=userinfo_find_from_id(user);
259 if (!uinfo) {
260 debuga(_("Unknown user ID %s in file %s\n"),user,top1);
261 exit(EXIT_FAILURE);
262 }
263
264 fputs("<tr>",fp_top3);
265
266 posicao++;
267 if((TopUserFields & TOPUSERFIELDS_NUM) != 0)
268 fprintf(fp_top3,"<td class=\"data\">%d</td>",posicao);
269
270 if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0) {
271 fputs("<td class=\"data2\">",fp_top3);
272 #ifdef HAVE_GD
273 if(Graphs && GraphFont[0]!='\0') {
274 fprintf(fp_top3,"<a href=\"%s/graph_day.png\"><img src=\"%s/graph.png\" title=\"%s\" alt=\"G\"></a>&nbsp;",uinfo->filename,ImageFile,_("Graphic"));
275 }
276 #endif
277 fprintf(fp_top3,"<a href=\"%s/d%s.html\"><img src=\"%s/datetime.png\" title=\"%s",uinfo->filename,uinfo->filename,ImageFile,_("date/time"));
278 #ifdef HAVE_GD
279 fprintf(fp_top3," %s",_("Report"));
280 #endif
281 fputs("\" alt=\"T\"></a></td>",fp_top3);
282 } else {
283 sprintf(val1,"%s/d%s.html",outdirname,uinfo->filename);
284 unlink(val1);
285 }
286 if((TopUserFields & TOPUSERFIELDS_USERID) != 0) {
287 if((ReportType & REPORT_TYPE_USERS_SITES) == 0)
288 fprintf(fp_top3,"<td class=\"data2\">%s</td>",name);
289 else
290 fprintf(fp_top3,"<td class=\"data2\"><a href=\"%s/%s.html\">%s</a></td>",uinfo->filename,uinfo->filename,uinfo->label);
291 }
292 if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0)
293 fprintf(fp_top3,"<td class=\"data\">%s</td>",fixnum(nacc,1));
294 if((TopUserFields & TOPUSERFIELDS_BYTES) != 0)
295 fprintf(fp_top3,"<td class=\"data\">%s</td>",fixnum(tnbytes,1));
296 if((TopUserFields & TOPUSERFIELDS_SETYB) != 0) {
297 perc=(ttnbytes) ? tnbytes * 100. / ttnbytes : 0.;
298 fprintf(fp_top3,"<td class=\"data\">%3.2lf%%</td>",perc);
299 }
300 if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0) {
301 inperc=(tnbytes) ? incac * 100. / tnbytes : 0.;
302 ouperc=(tnbytes) ? oucac * 100. / tnbytes : 0.;
303 fprintf(fp_top3,"<td class=\"data\">%3.2lf%%</td><td class=\"data\">%3.2lf%%</td>",inperc,ouperc);
304 }
305 if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0)
306 fprintf(fp_top3,"<td class=\"data\">%s</td>",buildtime(tnelap));
307 if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0)
308 fprintf(fp_top3,"<td class=\"data\">%s</td>",fixnum2(tnelap,1));
309 if((TopUserFields & TOPUSERFIELDS_PTIME) != 0) {
310 perc2=(ttnelap) ? elap * 100. / ttnelap : 0.;
311 fprintf(fp_top3,"<td class=\"data\">%3.2lf%%</td>",perc2);
312 }
313
314 fputs("</tr>\n",fp_top3);
315
316 topcount++;
317 }
318 fclose(fp_top1);
319 unlink(top1);
320 longline_destroy(&line);
321
322 if((TopUserFields & TOPUSERFIELDS_TOTAL) != 0) {
323 fputs("<tr>",fp_top3);
324 if((TopUserFields & TOPUSERFIELDS_NUM) != 0)
325 fputs("<td></td>",fp_top3);
326 if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0)
327 fputs("<td></td>",fp_top3);
328 fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("TOTAL"));
329
330 if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0)
331 fprintf(fp_top3,"<th class=\"header_r\">%s</th>",fixnum(ttnacc,1));
332 if((TopUserFields & TOPUSERFIELDS_BYTES) != 0)
333 fprintf(fp_top3,"<th class=\"header_r\">%15s</th>",fixnum(ttnbytes,1));
334 if((TopUserFields & TOPUSERFIELDS_SETYB) != 0)
335 fputs("<td></td>",fp_top3);
336 if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0)
337 {
338 inperc=(ttnbytes) ? ttnincache * 100. / ttnbytes : 0.;
339 ouperc=(ttnbytes) ? ttnoucache *100. / ttnbytes : 0.;
340 fprintf(fp_top3,"<th class=\"header_r\">%3.2lf%%</th><th class=\"header_r\">%3.2lf%%</th>",inperc,ouperc);
341 }
342 if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0)
343 fprintf(fp_top3,"<th class=\"header_r\">%s</th>",buildtime(ttnelap));
344 if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0)
345 fprintf(fp_top3,"<th class=\"header_r\">%s</th>",fixnum2(ttnelap,1));
346
347 fputs("</tr>\n",fp_top3);
348 }
349
350 if(ntopuser && (TopUserFields & TOPUSERFIELDS_AVERAGE) != 0) {
351 fputs("<tr>",fp_top3);
352 if((TopUserFields & TOPUSERFIELDS_NUM) != 0)
353 fputs("<td></td>",fp_top3);
354 if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0)
355 fputs("<td></td>",fp_top3);
356 fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("AVERAGE"));
357
358 if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0)
359 fprintf(fp_top3,"<th class=\"header_r\">%s</th>",fixnum(ttnacc/totuser,1));
360 if((TopUserFields & TOPUSERFIELDS_BYTES) != 0) {
361 tnbytes=(totuser) ? ttnbytes / totuser : 0;
362 fprintf(fp_top3,"<th class=\"header_r\">%15s</th>",fixnum(tnbytes,1));
363 }
364 if((TopUserFields & TOPUSERFIELDS_SETYB) != 0)
365 fputs("<td></td>",fp_top3);
366 if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0)
367 fputs("<td></td><td></td>",fp_top3);
368 if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0)
369 fprintf(fp_top3,"<th class=\"header_r\">%s</th>",buildtime(ttnelap/totuser));
370 if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0)
371 fprintf(fp_top3,"<th class=\"header_r\">%s</th>",fixnum2(ttnelap/totuser,1));
372 fputs("</tr>\n",fp_top3);
373 }
374
375 fputs("</table></div>\n",fp_top3);
376 if (write_html_trailer(fp_top3)<0)
377 debuga(_("Write error in top user list %s\n"),top3);
378 if (fclose(fp_top3)==EOF)
379 debuga(_("Failed to close the top user list %s - %s\n"),top3,strerror(errno));
380
381 snprintf(tusr,sizeof(tusr),"%s/sarg-users",outdirname);
382 if((fp_ou=fopen(tusr,"w"))==NULL) {
383 debuga(_("(topuser) Cannot open file %s\n"),tusr);
384 exit(EXIT_FAILURE);
385 }
386 fprintf(fp_ou,"%d\n",totuser);
387 if (fclose(fp_ou)==EOF)
388 debuga(_("Failed to close file %s - %s\n"),tusr,strerror(errno));
389
390 return;
391 }