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