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