]> git.ipfire.org Git - thirdparty/sarg.git/blame - useragent.c
Indent the configure script for more readability.
[thirdparty/sarg.git] / useragent.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
110ce984 3 * 1998, 2015
25697a35
GS
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
1164c474
FM
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
25697a35
GS
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"
5f3cfd1d 28#include "include/defs.h"
137eb63d
FM
29#include "include/filelist.h"
30
31FileListObject UserAgentLog=NULL;
25697a35 32
32e71fa4 33void useragent(void)
25697a35 34{
976c5bb7 35 FileObject *fp_log;
9bd92830 36 FILE *fp_in = NULL, *fp_ou = NULL, *fp_ht = NULL;
976c5bb7 37 char *ptr;
9bd92830
FM
38 char buf[MAXLEN];
39 char ip[MAXLEN], data[MAXLEN], agent[MAXLEN], user[MAXLEN];
40 char ipbefore[MAXLEN], namebefore[MAXLEN];
41 char tagent[MAXLEN];
42 char user_old[MAXLEN]="$#%0a3bc6";
43 char agent_old[MAXLEN]="$#%0a3bc6";
44 char hfile[MAXLEN];
45 char idate[MAXLEN], fdate[MAXLEN];
46 char tmp2[MAXLEN];
47 char tmp3[MAXLEN];
48 char day[4],month[5],year[5], wdate[20];
49 char csort[MAXLEN];
137eb63d 50 const char *FileName;
9bd92830
FM
51 int agentot=0, agentot2=0, agentdif=0, cont=0, nagent;
52 unsigned long totregsl=0;
53 int cstatus;
54 int ndate;
55 double perc;
56 struct getwordstruct gwarea, gwarea1;
976c5bb7 57 longline line;
137eb63d 58 FileListIterator FIter;
9bd92830
FM
59
60 ip[0]='\0';
61 data[0]='\0';
62 agent[0]='\0';
63 user[0]='\0';
64 user_old[0]='\0';
65 agent_old[0]='\0';
66 ipbefore[0]='\0';
67 namebefore[0]='\0';
68
20d8ad97
FM
69 snprintf(tmp3,sizeof(tmp3),"%s/squagent.int_unsort",tmp);
70 snprintf(tmp2,sizeof(tmp2),"%s/squagent.int_log",tmp);
9bd92830
FM
71
72 if((fp_ou=fopen(tmp3,"w"))==NULL) {
af961877 73 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),tmp3,strerror(errno));
9bd92830
FM
74 exit(EXIT_FAILURE);
75 }
76
976c5bb7
FM
77 if ((line=longline_create())==NULL) {
78 debuga(__FILE__,__LINE__,_("Not enough memory to read useragent log\n"));
79 exit(EXIT_FAILURE);
80 }
81
137eb63d
FM
82 FIter=FileListIter_Open(UserAgentLog);
83 while ((FileName=FileListIter_Next(FIter))!=NULL)
84 {
976c5bb7
FM
85 longline_reset(line);
86 if((fp_log=decomp(FileName))==NULL) {
87 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),FileName,FileObject_GetLastOpenError());
9bd92830
FM
88 exit(EXIT_FAILURE);
89 }
137eb63d
FM
90
91 if(debug) {
92 debuga(__FILE__,__LINE__,_("Reading useragent log \"%s\"\n"),FileName);
9bd92830
FM
93 }
94
976c5bb7 95 while((ptr=longline_read(fp_log,line))!=NULL) {
137eb63d 96 totregsl++;
976c5bb7 97 getword_start(&gwarea,ptr);
137eb63d
FM
98 if (getword(ip,sizeof(ip),&gwarea,' ')<0 || getword_skip(MAXLEN,&gwarea,'[')<0 ||
99 getword(data,sizeof(data),&gwarea,' ')<0) {
100 debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),FileName);
9bd92830
FM
101 exit(EXIT_FAILURE);
102 }
137eb63d
FM
103 getword_start(&gwarea1,data);
104 if (getword(day,sizeof(day),&gwarea1,'/')<0 || getword(month,sizeof(month),&gwarea1,'/')<0 ||
105 getword(year,sizeof(year),&gwarea1,':')<0) {
106 debuga(__FILE__,__LINE__,_("Invalid date in file \"%s\"\n"),FileName);
107 exit(EXIT_FAILURE);
108 }
109 if (dfrom!=0 || duntil!=0){
110 buildymd(day,month,year,wdate,sizeof(wdate));
111 ndate=atoi(wdate);
112 if (ndate<dfrom) continue;
113 if (ndate>duntil) break;
114 }
115 if(totregsl == 1)
116 strcpy(idate,data);
117 strcpy(fdate,data);
118 if (getword_skip(MAXLEN,&gwarea,'"')<0 || getword(agent,sizeof(agent),&gwarea,'"')<0) {
119 debuga(__FILE__,__LINE__,_("Invalid useragent in file \"%s\"\n"),FileName);
120 exit(EXIT_FAILURE);
121 }
122
123 if(gwarea.current[0]!='\0') {
124 if (getword_skip(MAXLEN,&gwarea,' ')<0 || getword(user,sizeof(user),&gwarea,'\n')<0) {
125 debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),FileName);
126 exit(EXIT_FAILURE);
127 }
128 if(user[0] == '-')
129 strcpy(user,ip);
130 if(user[0] == '\0')
131 strcpy(user,ip);
132 } else {
9bd92830 133 strcpy(user,ip);
137eb63d
FM
134 }
135
136 fprintf(fp_ou,"%s\t%s\t%s\n",ip,agent,user);
137 useragent_count++;
9bd92830
FM
138 }
139
976c5bb7
FM
140 if (FileObject_Close(fp_log)==EOF) {
141 debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),FileName,FileObject_GetLastCloseError());
137eb63d
FM
142 exit(EXIT_FAILURE);
143 }
9bd92830 144 }
137eb63d 145 FileListIter_Close(FIter);
976c5bb7 146 longline_destroy(&line);
9bd92830
FM
147
148 if(debug) {
af961877 149 debuga(__FILE__,__LINE__,_(" Records read: %ld\n"),totregsl);
9bd92830
FM
150 }
151
9bd92830 152 if (fclose(fp_ou)==EOF) {
af961877 153 debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),tmp3,strerror(errno));
9bd92830
FM
154 exit(EXIT_FAILURE);
155 }
9bd92830 156 if(debug) {
af961877 157 debuga(__FILE__,__LINE__,_("Sorting file \"%s\"\n"),tmp2);
9bd92830
FM
158 }
159
78eeb33f 160 if (snprintf(csort,sizeof(csort),"sort -n -t \"\t\" -k 3,3 -k 2,2 -k 1,1 -o \"%s\" \"%s\"",tmp2,tmp3)>=sizeof(csort)) {
af961877 161 debuga(__FILE__,__LINE__,_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),tmp2,tmp3);
78eeb33f
FM
162 exit(EXIT_FAILURE);
163 }
9bd92830
FM
164 cstatus=system(csort);
165 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
af961877
FM
166 debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEXITSTATUS(cstatus));
167 debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
9bd92830
FM
168 exit(EXIT_FAILURE);
169 }
170 if((fp_in=fopen(tmp2,"r"))==NULL) {
af961877
FM
171 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),tmp2,strerror(errno));
172 debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
9bd92830
FM
173 exit(EXIT_FAILURE);
174 }
175
11767c6a 176 if (!KeepTempLog && unlink(tmp3)) {
af961877 177 debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),tmp3,strerror(errno));
08f9b029
FM
178 exit(EXIT_FAILURE);
179 }
9bd92830
FM
180
181 snprintf(hfile,sizeof(hfile),"%s/useragent.html", outdirname);
182 if((fp_ht=fopen(hfile,"w"))==NULL) {
af961877 183 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),hfile,strerror(errno));
9bd92830
FM
184 exit(EXIT_FAILURE);
185 }
186
187 if(debug)
af961877 188 debuga(__FILE__,__LINE__,_("Making Useragent report\n"));
9bd92830
FM
189
190 write_html_header(fp_ht,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Squid Useragent's Report"),HTML_JS_NONE);
191 fprintf(fp_ht,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Squid Useragent's Report"));
192 fprintf(fp_ht,"<tr><td class=\"header_c\">%s: %s - %s</td></tr>\n",_("Period"),idate,fdate);
193 close_html_header(fp_ht);
194
195 fputs("<br><br>\n",fp_ht);
196
197 fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht);
198 fputs("<tr><td>&nbsp;</td><td>&nbsp;</td></tr>",fp_ht);
199
200 fprintf(fp_ht,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",_("USERID"),_("AGENT"));
201
202 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
203 getword_start(&gwarea,buf);
204 if (getword(ip,sizeof(ip),&gwarea,'\t')<0) {
af961877 205 debuga(__FILE__,__LINE__,_("Invalid IP address in file \"%s\"\n"),tmp2);
9bd92830
FM
206 exit(EXIT_FAILURE);
207 }
208
209 if(Ip2Name) {
210 if(strcmp(ip,ipbefore) != 0) {
211 strcpy(ipbefore,ip);
212 ip2name(ip,sizeof(ip));
213 strcpy(namebefore,ip);
214 } else strcpy(ip,namebefore);
215 }
216
217 if (getword(agent,sizeof(agent),&gwarea,'\t')<0) {
af961877 218 debuga(__FILE__,__LINE__,_("Invalid useragent in file \"%s\"\n"),tmp2);
9bd92830
FM
219 exit(EXIT_FAILURE);
220 }
221 if (getword(user,sizeof(user),&gwarea,'\t')<0) {
af961877 222 debuga(__FILE__,__LINE__,_("Invalid user ID in file \"%s\"\n"),tmp2);
9bd92830
FM
223 exit(EXIT_FAILURE);
224 }
225
226 if(strcmp(user,user_old) != 0) {
227 fprintf(fp_ht,"<tr><td class=\"data2\">%s</td><td class=\"data2\">",user);
228 output_html_string(fp_ht,agent,250);
229 fputs("</td></tr>\n",fp_ht);
230 strcpy(user_old,user);
231 strcpy(agent_old,agent);
232 } else if(strcmp(agent,agent_old) != 0) {
233 fputs("<tr><td></td><td class=\"data2\">",fp_ht);
234 output_html_string(fp_ht,agent,250);
235 fputs("</td></tr>\n",fp_ht);
236 strcpy(agent_old,agent);
237 }
238 }
239
240 fputs("</table>\n",fp_ht);
204781f4 241 if (fclose(fp_in)==EOF) {
af961877 242 debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),tmp2,strerror(errno));
204781f4
FM
243 exit(EXIT_FAILURE);
244 }
9bd92830 245
78eeb33f 246 if (snprintf(csort,sizeof(csort),"sort -t \"\t\" -k 2,2 -o \"%s\" \"%s\"",tmp3,tmp2)>=sizeof(csort)) {
af961877 247 debuga(__FILE__,__LINE__,_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),tmp2,tmp3);
78eeb33f
FM
248 exit(EXIT_FAILURE);
249 }
9bd92830
FM
250 cstatus=system(csort);
251 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
af961877
FM
252 debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEXITSTATUS(cstatus));
253 debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
9bd92830
FM
254 exit(EXIT_FAILURE);
255 }
256 if((fp_in=fopen(tmp3,"r"))==NULL) {
af961877
FM
257 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),tmp3,strerror(errno));
258 debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
9bd92830
FM
259 exit(EXIT_FAILURE);
260 }
261
11767c6a 262 if (!KeepTempLog && unlink(tmp2)) {
af961877 263 debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),tmp2,strerror(errno));
08f9b029
FM
264 exit(EXIT_FAILURE);
265 }
9bd92830
FM
266
267 if((fp_ou=fopen(tmp2,"w"))==NULL) {
af961877 268 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),tmp2,strerror(errno));
9bd92830
FM
269 exit(EXIT_FAILURE);
270 }
271
272 agent_old[0]='\0';
273 cont=0;
274
275 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
276 getword_start(&gwarea,buf);
277 if (getword(ip,sizeof(ip),&gwarea,'\t')<0) {
af961877 278 debuga(__FILE__,__LINE__,_("Invalid IP address in file \"%s\"\n"),tmp3);
9bd92830
FM
279 exit(EXIT_FAILURE);
280 }
281 if (getword(agent,sizeof(agent),&gwarea,'\t')<0) {
af961877 282 debuga(__FILE__,__LINE__,_("Invalid useragent in file \"%s\"\n"),tmp3);
9bd92830
FM
283 exit(EXIT_FAILURE);
284 }
285
286 if(!cont) {
287 cont++;
288 strcpy(agent_old,agent);
289 }
290
291 if(strcmp(agent,agent_old) != 0) {
292 agentdif++;
293 fprintf(fp_ou,"%06d %s\n",agentot,agent_old);
294 strcpy(agent_old,agent);
295 agentot2+=agentot;
296 agentot=0;
297 }
298 agentot++;
299 }
300 agentdif++;
301 fprintf(fp_ou,"%06d %s\n",agentot,agent);
302 agentot2+=agentot;
303
9bd92830 304 if (fclose(fp_ou)==EOF) {
af961877 305 debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),tmp2,strerror(errno));
9bd92830
FM
306 exit(EXIT_FAILURE);
307 }
204781f4 308 if (fclose(fp_in)==EOF) {
af961877 309 debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),tmp3,strerror(errno));
204781f4
FM
310 exit(EXIT_FAILURE);
311 }
9bd92830 312
11767c6a 313 if (!KeepTempLog && unlink(tmp3)) {
af961877 314 debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),tmp3,strerror(errno));
08f9b029
FM
315 exit(EXIT_FAILURE);
316 }
9bd92830 317
78eeb33f 318 if (snprintf(csort,sizeof(csort),"sort -n -r -k 1,1 -o \"%s\" \"%s\"",tmp3,tmp2)>=sizeof(csort)) {
af961877 319 debuga(__FILE__,__LINE__,_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),tmp2,tmp3);
78eeb33f
FM
320 exit(EXIT_FAILURE);
321 }
9bd92830
FM
322 cstatus=system(csort);
323 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
af961877
FM
324 debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEXITSTATUS(cstatus));
325 debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
9bd92830
FM
326 exit(EXIT_FAILURE);
327 }
328 if((fp_in=fopen(tmp3,"r"))==NULL) {
af961877
FM
329 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),tmp3,strerror(errno));
330 debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
9bd92830
FM
331 exit(EXIT_FAILURE);
332 }
333
11767c6a 334 if (!KeepTempLog && unlink(tmp2)) {
af961877 335 debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),tmp2,strerror(errno));
08f9b029
FM
336 exit(EXIT_FAILURE);
337 }
9bd92830
FM
338
339 fputs("<br><br>\n",fp_ht);
340
341 fputs("<table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht);
342 fprintf(fp_ht,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_c\">%%</th></tr>\n",_("AGENT"),_("TOTAL"));
343
344 perc=0.;
345 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
346 fixendofline(buf);
347 getword_start(&gwarea,buf);
348 if (getword(tagent,sizeof(tagent),&gwarea,' ')<0) {
af961877 349 debuga(__FILE__,__LINE__,_("Invalid useragent in file \"%s\"\n"),tmp3);
9bd92830
FM
350 exit(EXIT_FAILURE);
351 }
352 nagent=atoi(tagent);
353 perc=(agentot2>0) ? nagent * 100. / agentot2 : 0.;
354
355 fputs("<tr><td class=\"data2\">",fp_ht);
356 output_html_string(fp_ht,gwarea.current,250);
357 fprintf(fp_ht,"</td><td class=\"data\">%d</td><td class=\"data\">%3.2lf</td></tr>\n",nagent,perc);
358 }
204781f4 359 if (fclose(fp_in)==EOF) {
af961877 360 debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),tmp3,strerror(errno));
204781f4
FM
361 exit(EXIT_FAILURE);
362 }
9bd92830
FM
363
364 fputs("</table></div>\n",fp_ht);
342bd723 365 write_html_trailer(fp_ht);
507460ae 366 if (fclose(fp_ht)==EOF) {
af961877 367 debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),hfile,strerror(errno));
507460ae
FM
368 exit(EXIT_FAILURE);
369 }
9bd92830 370
11767c6a 371 if (!KeepTempLog && unlink(tmp3)) {
af961877 372 debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),tmp3,strerror(errno));
08f9b029
FM
373 exit(EXIT_FAILURE);
374 }
9bd92830
FM
375
376 return;
25697a35 377}