]>
Commit | Line | Data |
---|---|---|
25697a35 | 1 | /* |
94ff9470 | 2 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net |
e99bf0c0 | 3 | * 1998, 2013 |
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) { | |
4b06d570 | 65 | debugapos("useragent",_("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) { | |
4b06d570 | 70 | debugapos("useragent",_("Cannot open file \"%s\": %s\n"),tmp3,strerror(errno)); |
9bd92830 FM |
71 | exit(EXIT_FAILURE); |
72 | } | |
73 | ||
74 | if(debug) { | |
a968fba4 | 75 | debuga(_("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) { |
a968fba4 | 83 | debuga(_("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) { |
a968fba4 | 89 | debuga(_("Invalid date in file \"%s\"\n"),UserAgentLog); |
9bd92830 FM |
90 | exit(EXIT_FAILURE); |
91 | } | |
92 | buildymd(day,month,year,wdate); | |
93 | ndate=atoi(wdate); | |
94 | if (ndate<dfrom) continue; | |
95 | if (ndate>duntil) break; | |
96 | if(totregsl == 1) | |
97 | strcpy(idate,data); | |
98 | strcpy(fdate,data); | |
99 | if (getword_skip(MAXLEN,&gwarea,'"')<0 || getword(agent,sizeof(agent),&gwarea,'"')<0) { | |
a968fba4 | 100 | debuga(_("Invalid useragent in file \"%s\"\n"),UserAgentLog); |
9bd92830 FM |
101 | exit(EXIT_FAILURE); |
102 | } | |
103 | ||
104 | if(gwarea.current[0]!='\0') { | |
105 | if (getword_skip(MAXLEN,&gwarea,' ')<0 || getword(user,sizeof(user),&gwarea,'\n')<0) { | |
a968fba4 | 106 | debuga(_("Invalid record in file \"%s\"\n"),UserAgentLog); |
9bd92830 FM |
107 | exit(EXIT_FAILURE); |
108 | } | |
109 | if(user[0] == '-') | |
110 | strcpy(user,ip); | |
111 | if(user[0] == '\0') | |
112 | strcpy(user,ip); | |
113 | } else { | |
114 | strcpy(user,ip); | |
115 | } | |
116 | ||
117 | fprintf(fp_ou,"%s\t%s\t%s\n",ip,agent,user); | |
118 | useragent_count++; | |
119 | } | |
120 | ||
121 | if(debug) { | |
122 | debuga(_(" Records read: %ld\n"),totregsl); | |
123 | } | |
124 | ||
125 | fclose(fp_in); | |
126 | if (fclose(fp_ou)==EOF) { | |
4b06d570 | 127 | debuga(_("Failed to close file \"%s\": %s\n"),tmp3,strerror(errno)); |
9bd92830 FM |
128 | exit(EXIT_FAILURE); |
129 | } | |
130 | ||
131 | if(debug) { | |
a968fba4 | 132 | debuga(_("Sorting file \"%s\"\n"),tmp2); |
9bd92830 FM |
133 | } |
134 | ||
78eeb33f | 135 | 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)) { |
b0cf31a8 FM |
136 | debuga(_("Command too long: ")); |
137 | debuga_more("sort -n -t \"\t\" -k 3,3 -k 2,2 -k 1,1 -o \"%s\" \"%s\"",tmp2,tmp3); | |
78eeb33f FM |
138 | exit(EXIT_FAILURE); |
139 | } | |
9bd92830 FM |
140 | cstatus=system(csort); |
141 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
142 | debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); | |
143 | debuga(_("sort command: %s\n"),csort); | |
144 | exit(EXIT_FAILURE); | |
145 | } | |
146 | if((fp_in=fopen(tmp2,"r"))==NULL) { | |
4b06d570 | 147 | debugapos("useragent",_("Cannot open file \"%s\": %s\n"),tmp2,strerror(errno)); |
9bd92830 FM |
148 | debuga(_("sort command: %s\n"),csort); |
149 | exit(EXIT_FAILURE); | |
150 | } | |
151 | ||
11767c6a FM |
152 | if (!KeepTempLog && unlink(tmp3)) { |
153 | debuga(_("Cannot delete \"%s\": %s\n"),tmp3,strerror(errno)); | |
08f9b029 FM |
154 | exit(EXIT_FAILURE); |
155 | } | |
9bd92830 FM |
156 | |
157 | snprintf(hfile,sizeof(hfile),"%s/useragent.html", outdirname); | |
158 | if((fp_ht=fopen(hfile,"w"))==NULL) { | |
4b06d570 | 159 | debugapos("useragent",_("Cannot open file \"%s\": %s\n"),hfile,strerror(errno)); |
9bd92830 FM |
160 | exit(EXIT_FAILURE); |
161 | } | |
162 | ||
163 | if(debug) | |
164 | debuga(_("Making Useragent report\n")); | |
165 | ||
166 | write_html_header(fp_ht,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Squid Useragent's Report"),HTML_JS_NONE); | |
167 | fprintf(fp_ht,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Squid Useragent's Report")); | |
168 | fprintf(fp_ht,"<tr><td class=\"header_c\">%s: %s - %s</td></tr>\n",_("Period"),idate,fdate); | |
169 | close_html_header(fp_ht); | |
170 | ||
171 | fputs("<br><br>\n",fp_ht); | |
172 | ||
173 | fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht); | |
174 | fputs("<tr><td> </td><td> </td></tr>",fp_ht); | |
175 | ||
176 | fprintf(fp_ht,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",_("USERID"),_("AGENT")); | |
177 | ||
178 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
179 | getword_start(&gwarea,buf); | |
180 | if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { | |
a968fba4 | 181 | debuga(_("Invalid IP address in file \"%s\"\n"),tmp2); |
9bd92830 FM |
182 | exit(EXIT_FAILURE); |
183 | } | |
184 | ||
185 | if(Ip2Name) { | |
186 | if(strcmp(ip,ipbefore) != 0) { | |
187 | strcpy(ipbefore,ip); | |
188 | ip2name(ip,sizeof(ip)); | |
189 | strcpy(namebefore,ip); | |
190 | } else strcpy(ip,namebefore); | |
191 | } | |
192 | ||
193 | if (getword(agent,sizeof(agent),&gwarea,'\t')<0) { | |
a968fba4 | 194 | debuga(_("Invalid useragent in file \"%s\"\n"),tmp2); |
9bd92830 FM |
195 | exit(EXIT_FAILURE); |
196 | } | |
197 | if (getword(user,sizeof(user),&gwarea,'\t')<0) { | |
a968fba4 | 198 | debuga(_("Invalid user ID in file \"%s\"\n"),tmp2); |
9bd92830 FM |
199 | exit(EXIT_FAILURE); |
200 | } | |
201 | ||
202 | if(strcmp(user,user_old) != 0) { | |
203 | fprintf(fp_ht,"<tr><td class=\"data2\">%s</td><td class=\"data2\">",user); | |
204 | output_html_string(fp_ht,agent,250); | |
205 | fputs("</td></tr>\n",fp_ht); | |
206 | strcpy(user_old,user); | |
207 | strcpy(agent_old,agent); | |
208 | } else if(strcmp(agent,agent_old) != 0) { | |
209 | fputs("<tr><td></td><td class=\"data2\">",fp_ht); | |
210 | output_html_string(fp_ht,agent,250); | |
211 | fputs("</td></tr>\n",fp_ht); | |
212 | strcpy(agent_old,agent); | |
213 | } | |
214 | } | |
215 | ||
216 | fputs("</table>\n",fp_ht); | |
217 | fclose(fp_in); | |
218 | ||
78eeb33f | 219 | if (snprintf(csort,sizeof(csort),"sort -t \"\t\" -k 2,2 -o \"%s\" \"%s\"",tmp3,tmp2)>=sizeof(csort)) { |
b0cf31a8 FM |
220 | debuga(_("Command too long: ")); |
221 | debuga_more("sort -t \"\t\" -k 2,2 -o \"%s\" \"%s\"",tmp3,tmp2); | |
78eeb33f FM |
222 | exit(EXIT_FAILURE); |
223 | } | |
9bd92830 FM |
224 | cstatus=system(csort); |
225 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
226 | debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); | |
227 | debuga(_("sort command: %s\n"),csort); | |
228 | exit(EXIT_FAILURE); | |
229 | } | |
230 | if((fp_in=fopen(tmp3,"r"))==NULL) { | |
4b06d570 | 231 | debugapos("useragent",_("Cannot open file \"%s\": %s\n"),tmp3,strerror(errno)); |
9bd92830 FM |
232 | debuga(_("sort command: %s\n"),csort); |
233 | exit(EXIT_FAILURE); | |
234 | } | |
235 | ||
11767c6a FM |
236 | if (!KeepTempLog && unlink(tmp2)) { |
237 | debuga(_("Cannot delete \"%s\": %s\n"),tmp2,strerror(errno)); | |
08f9b029 FM |
238 | exit(EXIT_FAILURE); |
239 | } | |
9bd92830 FM |
240 | |
241 | if((fp_ou=fopen(tmp2,"w"))==NULL) { | |
4b06d570 | 242 | debugapos("useragent",_("Cannot open file \"%s\": %s\n"),tmp2,strerror(errno)); |
9bd92830 FM |
243 | exit(EXIT_FAILURE); |
244 | } | |
245 | ||
246 | agent_old[0]='\0'; | |
247 | cont=0; | |
248 | ||
249 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
250 | getword_start(&gwarea,buf); | |
251 | if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { | |
a968fba4 | 252 | debuga(_("Invalid IP address in file \"%s\"\n"),tmp3); |
9bd92830 FM |
253 | exit(EXIT_FAILURE); |
254 | } | |
255 | if (getword(agent,sizeof(agent),&gwarea,'\t')<0) { | |
a968fba4 | 256 | debuga(_("Invalid useragent in file \"%s\"\n"),tmp3); |
9bd92830 FM |
257 | exit(EXIT_FAILURE); |
258 | } | |
259 | ||
260 | if(!cont) { | |
261 | cont++; | |
262 | strcpy(agent_old,agent); | |
263 | } | |
264 | ||
265 | if(strcmp(agent,agent_old) != 0) { | |
266 | agentdif++; | |
267 | fprintf(fp_ou,"%06d %s\n",agentot,agent_old); | |
268 | strcpy(agent_old,agent); | |
269 | agentot2+=agentot; | |
270 | agentot=0; | |
271 | } | |
272 | agentot++; | |
273 | } | |
274 | agentdif++; | |
275 | fprintf(fp_ou,"%06d %s\n",agentot,agent); | |
276 | agentot2+=agentot; | |
277 | ||
278 | fclose(fp_in); | |
279 | if (fclose(fp_ou)==EOF) { | |
4b06d570 | 280 | debuga(_("Failed to close file \"%s\": %s\n"),tmp3,strerror(errno)); |
9bd92830 FM |
281 | exit(EXIT_FAILURE); |
282 | } | |
283 | ||
11767c6a FM |
284 | if (!KeepTempLog && unlink(tmp3)) { |
285 | debuga(_("Cannot delete \"%s\": %s\n"),tmp3,strerror(errno)); | |
08f9b029 FM |
286 | exit(EXIT_FAILURE); |
287 | } | |
9bd92830 | 288 | |
78eeb33f | 289 | if (snprintf(csort,sizeof(csort),"sort -n -r -k 1,1 -o \"%s\" \"%s\"",tmp3,tmp2)>=sizeof(csort)) { |
b0cf31a8 FM |
290 | debuga(_("Command too long: ")); |
291 | debuga_more("sort -n -r -k 1,1 -o \"%s\" \"%s\"",tmp3,tmp2); | |
78eeb33f FM |
292 | exit(EXIT_FAILURE); |
293 | } | |
9bd92830 FM |
294 | cstatus=system(csort); |
295 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
296 | debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); | |
297 | debuga(_("sort command: %s\n"),csort); | |
298 | exit(EXIT_FAILURE); | |
299 | } | |
300 | if((fp_in=fopen(tmp3,"r"))==NULL) { | |
4b06d570 | 301 | debugapos("useragent",_("Cannot open file \"%s\": %s\n"),tmp3,strerror(errno)); |
9bd92830 FM |
302 | debuga(_("sort command: %s\n"),csort); |
303 | exit(EXIT_FAILURE); | |
304 | } | |
305 | ||
11767c6a FM |
306 | if (!KeepTempLog && unlink(tmp2)) { |
307 | debuga(_("Cannot delete \"%s\": %s\n"),tmp2,strerror(errno)); | |
08f9b029 FM |
308 | exit(EXIT_FAILURE); |
309 | } | |
9bd92830 FM |
310 | |
311 | fputs("<br><br>\n",fp_ht); | |
312 | ||
313 | fputs("<table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht); | |
314 | 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")); | |
315 | ||
316 | perc=0.; | |
317 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
318 | fixendofline(buf); | |
319 | getword_start(&gwarea,buf); | |
320 | if (getword(tagent,sizeof(tagent),&gwarea,' ')<0) { | |
a968fba4 | 321 | debuga(_("Invalid useragent in file \"%s\"\n"),tmp3); |
9bd92830 FM |
322 | exit(EXIT_FAILURE); |
323 | } | |
324 | nagent=atoi(tagent); | |
325 | perc=(agentot2>0) ? nagent * 100. / agentot2 : 0.; | |
326 | ||
327 | fputs("<tr><td class=\"data2\">",fp_ht); | |
328 | output_html_string(fp_ht,gwarea.current,250); | |
329 | fprintf(fp_ht,"</td><td class=\"data\">%d</td><td class=\"data\">%3.2lf</td></tr>\n",nagent,perc); | |
330 | } | |
331 | fclose(fp_in); | |
332 | ||
333 | fputs("</table></div>\n",fp_ht); | |
334 | if (write_html_trailer(fp_ht)<0) | |
b2dad7e7 | 335 | debuga(_("Write error in file \"%s\"\n"),hfile); |
9bd92830 | 336 | if (fclose(fp_ht)==EOF) |
4b06d570 | 337 | debuga(_("Failed to close file \"%s\": %s\n"),hfile,strerror(errno)); |
9bd92830 | 338 | |
11767c6a FM |
339 | if (!KeepTempLog && unlink(tmp3)) { |
340 | debuga(_("Cannot delete \"%s\": %s\n"),tmp3,strerror(errno)); | |
08f9b029 FM |
341 | exit(EXIT_FAILURE); |
342 | } | |
9bd92830 FM |
343 | |
344 | return; | |
25697a35 | 345 | } |