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