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