]>
Commit | Line | Data |
---|---|---|
25697a35 | 1 | /* |
94ff9470 | 2 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net |
1164c474 | 3 | * 1998, 2010 |
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 GS |
31 | { |
32 | ||
33 | FILE *fp_in = NULL, *fp_ou = NULL, *fp_ht = NULL; | |
06b39c87 | 34 | char buf[MAXLEN]; |
d6e703cc FM |
35 | char ip[MAXLEN], data[MAXLEN], agent[MAXLEN], user[MAXLEN]; |
36 | char ipbefore[MAXLEN], namebefore[MAXLEN]; | |
37 | char tagent[MAXLEN]; | |
38 | char user_old[MAXLEN]="$#%0a3bc6"; | |
39 | char agent_old[MAXLEN]="$#%0a3bc6"; | |
25697a35 GS |
40 | char hfile[MAXLEN]; |
41 | char idate[MAXLEN], fdate[MAXLEN]; | |
06ced858 | 42 | char tmp2[MAXLEN]; |
2240dcea | 43 | char tmp3[MAXLEN]; |
012ba254 | 44 | char day[4],month[5],year[5], wdate[20]; |
2240dcea | 45 | char csort[MAXLEN]; |
dcb54d06 | 46 | int agentot=0, agentot2=0, agentdif=0, cont=0, nagent; |
25697a35 | 47 | unsigned long totregsl=0; |
456d78a5 | 48 | int cstatus; |
012ba254 FM |
49 | int ndate; |
50 | double perc; | |
51 | struct getwordstruct gwarea, gwarea1; | |
25697a35 GS |
52 | |
53 | ip[0]='\0'; | |
54 | data[0]='\0'; | |
55 | agent[0]='\0'; | |
56 | user[0]='\0'; | |
57 | user_old[0]='\0'; | |
58 | agent_old[0]='\0'; | |
d6e703cc FM |
59 | ipbefore[0]='\0'; |
60 | namebefore[0]='\0'; | |
25697a35 | 61 | |
4e302ff5 FM |
62 | sprintf(tmp3,"%s/squagent.unsort",tmp); |
63 | sprintf(tmp2,"%s/squagent.log",tmp); | |
25697a35 GS |
64 | |
65 | if((fp_in=fopen(UserAgentLog,"r"))==NULL) { | |
9f70c14e | 66 | debuga(_("(useragent) Cannot open file %s\n"),UserAgentLog); |
06b39c87 | 67 | exit(EXIT_FAILURE); |
25697a35 GS |
68 | } |
69 | ||
d6e703cc | 70 | if((fp_ou=fopen(tmp3,"w"))==NULL) { |
9f70c14e | 71 | debuga(_("(useragent) Cannot open file %s\n"),tmp3); |
06b39c87 | 72 | exit(EXIT_FAILURE); |
25697a35 GS |
73 | } |
74 | ||
75 | if(debug) { | |
10210234 | 76 | debuga(_("Reading useragent log: %s\n"),UserAgentLog); |
25697a35 GS |
77 | } |
78 | ||
79 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
80 | totregsl++; | |
9c7c6346 FM |
81 | getword_start(&gwarea,buf); |
82 | if (getword(ip,sizeof(ip),&gwarea,' ')<0 || getword_skip(MAXLEN,&gwarea,'[')<0 || | |
83 | getword(data,sizeof(data),&gwarea,' ')<0) { | |
9f70c14e | 84 | debuga(_("Maybe you have a broken record or garbage in your %s file\n"),UserAgentLog); |
06b39c87 | 85 | exit(EXIT_FAILURE); |
4bcb77cf | 86 | } |
012ba254 FM |
87 | getword_start(&gwarea1,data); |
88 | if (getword(day,sizeof(day),&gwarea1,'/')<0 || getword(month,sizeof(month),&gwarea1,'/')<0 || | |
89 | getword(year,sizeof(year),&gwarea1,':')<0) { | |
9f70c14e | 90 | debuga(_("Maybe you have a broken date in your %s file\n"),UserAgentLog); |
06b39c87 | 91 | exit(EXIT_FAILURE); |
012ba254 FM |
92 | } |
93 | buildymd(day,month,year,wdate); | |
94 | ndate=atoi(wdate); | |
c0ec9cc7 FM |
95 | if (ndate<dfrom) continue; |
96 | if (ndate>duntil) break; | |
25697a35 GS |
97 | if(totregsl == 1) |
98 | strcpy(idate,data); | |
012ba254 | 99 | strcpy(fdate,data); |
9c7c6346 | 100 | if (getword_skip(MAXLEN,&gwarea,'"')<0 || getword(agent,sizeof(agent),&gwarea,'"')<0) { |
9f70c14e | 101 | debuga(_("Maybe you have a broken useragent entry in your %s file\n"),UserAgentLog); |
06b39c87 | 102 | exit(EXIT_FAILURE); |
4bcb77cf | 103 | } |
d6e703cc | 104 | |
0a4d2bda | 105 | if(gwarea.current[0]!='\0') { |
9c7c6346 | 106 | if (getword_skip(MAXLEN,&gwarea,' ')<0 || getword(user,sizeof(user),&gwarea,'\n')<0) { |
9f70c14e | 107 | debuga(_("Maybe you have a broken record or garbage in your %s file\n"),UserAgentLog); |
06b39c87 | 108 | exit(EXIT_FAILURE); |
4bcb77cf | 109 | } |
d82e8117 FM |
110 | if(user[0] == '-') |
111 | strcpy(user,ip); | |
112 | if(user[0] == '\0') | |
113 | strcpy(user,ip); | |
114 | } else { | |
25697a35 | 115 | strcpy(user,ip); |
d82e8117 | 116 | } |
25697a35 | 117 | |
dcb54d06 | 118 | fprintf(fp_ou,"%s\t%s\t%s\n",ip,agent,user); |
c0ec9cc7 | 119 | useragent_count++; |
25697a35 GS |
120 | } |
121 | ||
25697a35 | 122 | if(debug) { |
10210234 | 123 | debuga(_(" Records read: %ld\n"),totregsl); |
25697a35 GS |
124 | } |
125 | ||
126 | fclose(fp_in); | |
d82e8117 FM |
127 | if (fclose(fp_ou)==EOF) { |
128 | debuga(_("Failed to close file %s - %s\n"),tmp3,strerror(errno)); | |
129 | exit(EXIT_FAILURE); | |
130 | } | |
25697a35 | 131 | |
25697a35 | 132 | if(debug) { |
10210234 | 133 | debuga(_("Sorting file: %s\n"),tmp2); |
25697a35 GS |
134 | } |
135 | ||
dcb54d06 | 136 | sprintf(csort,"sort -n -t \"\t\" -k 3,3 -k 2,2 -k 1,1 -o \"%s\" \"%s\"",tmp2,tmp3); |
456d78a5 FM |
137 | cstatus=system(csort); |
138 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
10210234 FM |
139 | debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); |
140 | debuga(_("sort command: %s\n"),csort); | |
06b39c87 | 141 | exit(EXIT_FAILURE); |
456d78a5 | 142 | } |
25697a35 | 143 | if((fp_in=fopen(tmp2,"r"))==NULL) { |
fcdc0918 | 144 | debuga(_("(useragent) Cannot open file %s\n"),tmp2); |
10210234 | 145 | debuga(_("sort command: %s\n"),csort); |
06b39c87 | 146 | exit(EXIT_FAILURE); |
25697a35 GS |
147 | } |
148 | ||
456d78a5 FM |
149 | unlink(tmp3); |
150 | ||
d5d021c5 | 151 | snprintf(hfile,sizeof(hfile),"%s/useragent.html", outdirname); |
25697a35 | 152 | if((fp_ht=fopen(hfile,"w"))==NULL) { |
9f70c14e | 153 | debuga(_("(useragent) Cannot open file %s\n"),hfile); |
06b39c87 | 154 | exit(EXIT_FAILURE); |
25697a35 | 155 | } |
120d768c | 156 | |
25697a35 | 157 | if(debug) |
10210234 | 158 | debuga(_("Making Useragent report\n")); |
25697a35 | 159 | |
2e96438d | 160 | write_html_header(fp_ht,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Squid Useragent's Report"),HTML_JS_NONE); |
c36c7384 FM |
161 | fprintf(fp_ht,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Squid Useragent's Report")); |
162 | fprintf(fp_ht,"<tr><td class=\"header_c\">%s: %s - %s</td></tr>\n",_("Period"),idate,fdate); | |
c0ec9cc7 | 163 | close_html_header(fp_ht); |
25697a35 | 164 | |
c0ec9cc7 | 165 | fputs("<br><br>\n",fp_ht); |
25697a35 | 166 | |
c0ec9cc7 | 167 | fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht); |
012ba254 | 168 | fputs("<tr><td> </td><td> </td></tr>",fp_ht); |
25697a35 | 169 | |
c36c7384 | 170 | fprintf(fp_ht,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",_("USERID"),_("AGENT")); |
25697a35 GS |
171 | |
172 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
9c7c6346 FM |
173 | getword_start(&gwarea,buf); |
174 | if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { | |
10210234 | 175 | debuga(_("There is an invalid IP address in file %s\n"),tmp2); |
06b39c87 | 176 | exit(EXIT_FAILURE); |
4bcb77cf | 177 | } |
25697a35 | 178 | |
246c8489 | 179 | if(Ip2Name) { |
d6e703cc FM |
180 | if(strcmp(ip,ipbefore) != 0) { |
181 | strcpy(ipbefore,ip); | |
a1c55d8c | 182 | ip2name(ip,sizeof(ip)); |
d6e703cc FM |
183 | strcpy(namebefore,ip); |
184 | } else strcpy(ip,namebefore); | |
25697a35 GS |
185 | } |
186 | ||
10210234 FM |
187 | if (getword(agent,sizeof(agent),&gwarea,'\t')<0) { |
188 | debuga(_("There is an invalid useragent in file %s\n"),tmp2); | |
06b39c87 | 189 | exit(EXIT_FAILURE); |
10210234 FM |
190 | } |
191 | if (getword(user,sizeof(user),&gwarea,'\t')<0) { | |
192 | debuga(_("There is an invalid user ID in file %s\n"),tmp2); | |
06b39c87 | 193 | exit(EXIT_FAILURE); |
4bcb77cf | 194 | } |
25697a35 GS |
195 | |
196 | if(strcmp(user,user_old) != 0) { | |
d82e8117 FM |
197 | fprintf(fp_ht,"<tr><td class=\"data2\">%s</td><td class=\"data2\">",user); |
198 | output_html_string(fp_ht,agent,250); | |
199 | fputs("</td></tr>\n",fp_ht); | |
25697a35 GS |
200 | strcpy(user_old,user); |
201 | strcpy(agent_old,agent); | |
d82e8117 FM |
202 | } else if(strcmp(agent,agent_old) != 0) { |
203 | fputs("<tr><td></td><td class=\"data2\">",fp_ht); | |
204 | output_html_string(fp_ht,agent,250); | |
205 | fputs("</td></tr>\n",fp_ht); | |
206 | strcpy(agent_old,agent); | |
25697a35 GS |
207 | } |
208 | } | |
120d768c | 209 | |
25697a35 GS |
210 | fputs("</table>\n",fp_ht); |
211 | fclose(fp_in); | |
25697a35 | 212 | |
dcb54d06 | 213 | sprintf(csort,"sort -t \"\t\" -k 2,2 -o \"%s\" \"%s\"",tmp3,tmp2); |
456d78a5 FM |
214 | cstatus=system(csort); |
215 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
10210234 FM |
216 | debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); |
217 | debuga(_("sort command: %s\n"),csort); | |
06b39c87 | 218 | exit(EXIT_FAILURE); |
456d78a5 | 219 | } |
d6e703cc | 220 | if((fp_in=fopen(tmp3,"r"))==NULL) { |
9f70c14e | 221 | debuga(_("(useragent) Cannot open file %s\n"),tmp3); |
10210234 | 222 | debuga(_("sort command: %s\n"),csort); |
06b39c87 | 223 | exit(EXIT_FAILURE); |
25697a35 GS |
224 | } |
225 | ||
456d78a5 FM |
226 | unlink(tmp2); |
227 | ||
25697a35 | 228 | if((fp_ou=fopen(tmp2,"w"))==NULL) { |
fcdc0918 | 229 | debuga(_("(useragent) Cannot open file %s\n"),tmp2); |
06b39c87 | 230 | exit(EXIT_FAILURE); |
25697a35 GS |
231 | } |
232 | ||
233 | agent_old[0]='\0'; | |
dcb54d06 | 234 | cont=0; |
25697a35 GS |
235 | |
236 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
9c7c6346 | 237 | getword_start(&gwarea,buf); |
10210234 | 238 | if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { |
d82e8117 | 239 | debuga(_("There is an invalid IP address in file %s\n"),tmp3); |
06b39c87 | 240 | exit(EXIT_FAILURE); |
10210234 FM |
241 | } |
242 | if (getword(agent,sizeof(agent),&gwarea,'\t')<0) { | |
d82e8117 | 243 | debuga(_("There is an invalid useragent in file %s\n"),tmp3); |
06b39c87 | 244 | exit(EXIT_FAILURE); |
4bcb77cf | 245 | } |
25697a35 GS |
246 | |
247 | if(!cont) { | |
248 | cont++; | |
249 | strcpy(agent_old,agent); | |
250 | } | |
120d768c | 251 | |
25697a35 GS |
252 | if(strcmp(agent,agent_old) != 0) { |
253 | agentdif++; | |
354c1a68 | 254 | fprintf(fp_ou,"%06d %s\n",agentot,agent_old); |
25697a35 GS |
255 | strcpy(agent_old,agent); |
256 | agentot2+=agentot; | |
dcb54d06 | 257 | agentot=0; |
25697a35 | 258 | } |
dcb54d06 | 259 | agentot++; |
25697a35 GS |
260 | } |
261 | agentdif++; | |
354c1a68 | 262 | fprintf(fp_ou,"%06d %s\n",agentot,agent); |
25697a35 GS |
263 | agentot2+=agentot; |
264 | ||
265 | fclose(fp_in); | |
d82e8117 FM |
266 | if (fclose(fp_ou)==EOF) { |
267 | debuga(_("Failed to close file %s - %s\n"),tmp3,strerror(errno)); | |
268 | exit(EXIT_FAILURE); | |
269 | } | |
25697a35 | 270 | |
d6e703cc | 271 | unlink(tmp3); |
25697a35 | 272 | |
9a2efbd0 | 273 | sprintf(csort,"sort -n -r -k 1,1 -o \"%s\" \"%s\"",tmp3,tmp2); |
456d78a5 FM |
274 | cstatus=system(csort); |
275 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
10210234 FM |
276 | debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); |
277 | debuga(_("sort command: %s\n"),csort); | |
06b39c87 | 278 | exit(EXIT_FAILURE); |
456d78a5 | 279 | } |
d6e703cc | 280 | if((fp_in=fopen(tmp3,"r"))==NULL) { |
9f70c14e | 281 | debuga(_("(useragent) Cannot open file %s\n"),tmp3); |
10210234 | 282 | debuga(_("sort command: %s\n"),csort); |
06b39c87 | 283 | exit(EXIT_FAILURE); |
25697a35 GS |
284 | } |
285 | ||
456d78a5 FM |
286 | unlink(tmp2); |
287 | ||
c0ec9cc7 | 288 | fputs("<br><br>\n",fp_ht); |
25697a35 | 289 | |
c0ec9cc7 | 290 | fputs("<table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht); |
c36c7384 | 291 | 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")); |
25697a35 | 292 | |
dcb54d06 | 293 | perc=0.; |
25697a35 | 294 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { |
120d768c | 295 | fixendofline(buf); |
9c7c6346 FM |
296 | getword_start(&gwarea,buf); |
297 | if (getword(tagent,sizeof(tagent),&gwarea,' ')<0) { | |
d82e8117 | 298 | debuga(_("There is an invalid useragent in file %s\n"),tmp3); |
06b39c87 | 299 | exit(EXIT_FAILURE); |
4bcb77cf | 300 | } |
dcb54d06 | 301 | nagent=atoi(tagent); |
012ba254 | 302 | perc=(agentot2>0) ? nagent * 100. / agentot2 : 0.; |
25697a35 | 303 | |
d82e8117 FM |
304 | fputs("<tr><td class=\"data2\">",fp_ht); |
305 | output_html_string(fp_ht,gwarea.current,250); | |
306 | fprintf(fp_ht,"</td><td class=\"data\">%d</td><td class=\"data\">%3.2lf</td></tr>\n",nagent,perc); | |
25697a35 | 307 | } |
25697a35 | 308 | fclose(fp_in); |
c0ec9cc7 FM |
309 | |
310 | fputs("</table></div>\n",fp_ht); | |
fa6552b0 FM |
311 | if (write_html_trailer(fp_ht)<0) |
312 | debuga(_("Write error in file %s\n"),hfile); | |
313 | if (fclose(fp_ht)==EOF) | |
314 | debuga(_("Failed to close file %s - %s\n"),hfile,strerror(errno)); | |
25697a35 | 315 | |
120d768c | 316 | unlink(tmp3); |
25697a35 GS |
317 | |
318 | return; | |
120d768c | 319 | |
25697a35 | 320 | } |