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