]>
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 | |
d6e703cc | 62 | sprintf(tmp3,"%s/squagent.unsort",TempDir); |
25697a35 GS |
63 | sprintf(tmp2,"%s/squagent.log",TempDir); |
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 FM |
104 | strcpy(warea,agent); |
105 | strup(warea); | |
106 | if(strstr(warea,"SCRIPT") != 0 || strstr(warea,"ONLOAD") != 0) | |
107 | baddata(); | |
108 | ||
0a4d2bda | 109 | if(gwarea.current[0]!='\0') { |
9c7c6346 | 110 | if (getword_skip(MAXLEN,&gwarea,' ')<0 || getword(user,sizeof(user),&gwarea,'\n')<0) { |
9f70c14e | 111 | debuga(_("Maybe you have a broken record or garbage in your %s file\n"),UserAgentLog); |
06b39c87 | 112 | exit(EXIT_FAILURE); |
4bcb77cf | 113 | } |
25697a35 GS |
114 | } |
115 | ||
116 | if(user[0] == '-') | |
117 | strcpy(user,ip); | |
0a4d2bda | 118 | if(user[0] == '\0') |
25697a35 GS |
119 | strcpy(user,ip); |
120 | ||
dcb54d06 | 121 | fprintf(fp_ou,"%s\t%s\t%s\n",ip,agent,user); |
25697a35 | 122 | user[0]='\0'; |
c0ec9cc7 | 123 | useragent_count++; |
25697a35 GS |
124 | } |
125 | ||
25697a35 | 126 | if(debug) { |
10210234 | 127 | debuga(_(" Records read: %ld\n"),totregsl); |
25697a35 GS |
128 | } |
129 | ||
130 | fclose(fp_in); | |
131 | fclose(fp_ou); | |
132 | ||
25697a35 | 133 | if(debug) { |
10210234 | 134 | debuga(_("Sorting file: %s\n"),tmp2); |
25697a35 GS |
135 | } |
136 | ||
dcb54d06 | 137 | sprintf(csort,"sort -n -t \"\t\" -k 3,3 -k 2,2 -k 1,1 -o \"%s\" \"%s\"",tmp2,tmp3); |
456d78a5 FM |
138 | cstatus=system(csort); |
139 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
10210234 FM |
140 | debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); |
141 | debuga(_("sort command: %s\n"),csort); | |
06b39c87 | 142 | exit(EXIT_FAILURE); |
456d78a5 | 143 | } |
25697a35 | 144 | if((fp_in=fopen(tmp2,"r"))==NULL) { |
fcdc0918 | 145 | debuga(_("(useragent) Cannot open file %s\n"),tmp2); |
10210234 | 146 | debuga(_("sort command: %s\n"),csort); |
06b39c87 | 147 | exit(EXIT_FAILURE); |
25697a35 GS |
148 | } |
149 | ||
456d78a5 FM |
150 | unlink(tmp3); |
151 | ||
d5d021c5 | 152 | snprintf(hfile,sizeof(hfile),"%s/useragent.html", outdirname); |
25697a35 | 153 | if((fp_ht=fopen(hfile,"w"))==NULL) { |
9f70c14e | 154 | debuga(_("(useragent) Cannot open file %s\n"),hfile); |
06b39c87 | 155 | exit(EXIT_FAILURE); |
25697a35 | 156 | } |
120d768c | 157 | |
25697a35 | 158 | if(debug) |
10210234 | 159 | debuga(_("Making Useragent report\n")); |
25697a35 | 160 | |
c0ec9cc7 | 161 | write_html_header(fp_ht,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Squid Useragent's Report")); |
c36c7384 FM |
162 | fprintf(fp_ht,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Squid Useragent's Report")); |
163 | fprintf(fp_ht,"<tr><td class=\"header_c\">%s: %s - %s</td></tr>\n",_("Period"),idate,fdate); | |
c0ec9cc7 | 164 | close_html_header(fp_ht); |
25697a35 | 165 | |
c0ec9cc7 | 166 | fputs("<br><br>\n",fp_ht); |
25697a35 | 167 | |
c0ec9cc7 | 168 | fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht); |
012ba254 | 169 | fputs("<tr><td> </td><td> </td></tr>",fp_ht); |
25697a35 | 170 | |
c36c7384 | 171 | fprintf(fp_ht,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",_("USERID"),_("AGENT")); |
25697a35 GS |
172 | |
173 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
9c7c6346 FM |
174 | getword_start(&gwarea,buf); |
175 | if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { | |
10210234 | 176 | debuga(_("There is an invalid IP address in file %s\n"),tmp2); |
06b39c87 | 177 | exit(EXIT_FAILURE); |
4bcb77cf | 178 | } |
25697a35 | 179 | |
246c8489 | 180 | if(Ip2Name) { |
d6e703cc FM |
181 | if(strcmp(ip,ipbefore) != 0) { |
182 | strcpy(ipbefore,ip); | |
a1c55d8c | 183 | ip2name(ip,sizeof(ip)); |
d6e703cc FM |
184 | strcpy(namebefore,ip); |
185 | } else strcpy(ip,namebefore); | |
25697a35 GS |
186 | } |
187 | ||
10210234 FM |
188 | if (getword(agent,sizeof(agent),&gwarea,'\t')<0) { |
189 | debuga(_("There is an invalid useragent in file %s\n"),tmp2); | |
06b39c87 | 190 | exit(EXIT_FAILURE); |
10210234 FM |
191 | } |
192 | if (getword(user,sizeof(user),&gwarea,'\t')<0) { | |
193 | debuga(_("There is an invalid user ID in file %s\n"),tmp2); | |
06b39c87 | 194 | exit(EXIT_FAILURE); |
4bcb77cf | 195 | } |
25697a35 GS |
196 | |
197 | if(strcmp(user,user_old) != 0) { | |
012ba254 | 198 | fprintf(fp_ht,"<tr><td class=\"data2\">%s</td><td class=\"data2\">%s</td></tr>\n",user,agent); |
25697a35 GS |
199 | strcpy(user_old,user); |
200 | strcpy(agent_old,agent); | |
201 | } else { | |
202 | if(strcmp(agent,agent_old) != 0) { | |
012ba254 | 203 | fprintf(fp_ht,"<tr><td></td><td class=\"data2\">%s</td></tr>\n",agent); |
25697a35 GS |
204 | strcpy(agent_old,agent); |
205 | } | |
206 | } | |
207 | } | |
120d768c | 208 | |
25697a35 GS |
209 | fputs("</table>\n",fp_ht); |
210 | fclose(fp_in); | |
25697a35 | 211 | |
dcb54d06 | 212 | sprintf(csort,"sort -t \"\t\" -k 2,2 -o \"%s\" \"%s\"",tmp3,tmp2); |
456d78a5 FM |
213 | cstatus=system(csort); |
214 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
10210234 FM |
215 | debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); |
216 | debuga(_("sort command: %s\n"),csort); | |
06b39c87 | 217 | exit(EXIT_FAILURE); |
456d78a5 | 218 | } |
d6e703cc | 219 | if((fp_in=fopen(tmp3,"r"))==NULL) { |
9f70c14e | 220 | debuga(_("(useragent) Cannot open file %s\n"),tmp3); |
10210234 | 221 | debuga(_("sort command: %s\n"),csort); |
06b39c87 | 222 | exit(EXIT_FAILURE); |
25697a35 GS |
223 | } |
224 | ||
456d78a5 FM |
225 | unlink(tmp2); |
226 | ||
25697a35 | 227 | if((fp_ou=fopen(tmp2,"w"))==NULL) { |
fcdc0918 | 228 | debuga(_("(useragent) Cannot open file %s\n"),tmp2); |
06b39c87 | 229 | exit(EXIT_FAILURE); |
25697a35 GS |
230 | } |
231 | ||
232 | agent_old[0]='\0'; | |
dcb54d06 | 233 | cont=0; |
25697a35 GS |
234 | |
235 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
9c7c6346 | 236 | getword_start(&gwarea,buf); |
10210234 FM |
237 | if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { |
238 | debuga(_("There is an invalid IP address in file %s\n"),tmp); | |
06b39c87 | 239 | exit(EXIT_FAILURE); |
10210234 FM |
240 | } |
241 | if (getword(agent,sizeof(agent),&gwarea,'\t')<0) { | |
242 | debuga(_("There is an invalid useragent in file %s\n"),tmp); | |
06b39c87 | 243 | exit(EXIT_FAILURE); |
4bcb77cf | 244 | } |
25697a35 GS |
245 | |
246 | if(!cont) { | |
247 | cont++; | |
248 | strcpy(agent_old,agent); | |
249 | } | |
120d768c | 250 | |
25697a35 GS |
251 | if(strcmp(agent,agent_old) != 0) { |
252 | agentdif++; | |
354c1a68 | 253 | fprintf(fp_ou,"%06d %s\n",agentot,agent_old); |
25697a35 GS |
254 | strcpy(agent_old,agent); |
255 | agentot2+=agentot; | |
dcb54d06 | 256 | agentot=0; |
25697a35 | 257 | } |
dcb54d06 | 258 | agentot++; |
25697a35 GS |
259 | } |
260 | agentdif++; | |
354c1a68 | 261 | fprintf(fp_ou,"%06d %s\n",agentot,agent); |
25697a35 GS |
262 | agentot2+=agentot; |
263 | ||
264 | fclose(fp_in); | |
265 | fclose(fp_ou); | |
266 | ||
d6e703cc | 267 | unlink(tmp3); |
25697a35 | 268 | |
9a2efbd0 | 269 | sprintf(csort,"sort -n -r -k 1,1 -o \"%s\" \"%s\"",tmp3,tmp2); |
456d78a5 FM |
270 | cstatus=system(csort); |
271 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
10210234 FM |
272 | debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); |
273 | debuga(_("sort command: %s\n"),csort); | |
06b39c87 | 274 | exit(EXIT_FAILURE); |
456d78a5 | 275 | } |
d6e703cc | 276 | if((fp_in=fopen(tmp3,"r"))==NULL) { |
9f70c14e | 277 | debuga(_("(useragent) Cannot open file %s\n"),tmp3); |
10210234 | 278 | debuga(_("sort command: %s\n"),csort); |
06b39c87 | 279 | exit(EXIT_FAILURE); |
25697a35 GS |
280 | } |
281 | ||
456d78a5 FM |
282 | unlink(tmp2); |
283 | ||
c0ec9cc7 | 284 | fputs("<br><br>\n",fp_ht); |
25697a35 | 285 | |
c0ec9cc7 | 286 | fputs("<table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht); |
c36c7384 | 287 | 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 | 288 | |
dcb54d06 | 289 | perc=0.; |
25697a35 | 290 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { |
120d768c | 291 | fixendofline(buf); |
9c7c6346 FM |
292 | getword_start(&gwarea,buf); |
293 | if (getword(tagent,sizeof(tagent),&gwarea,' ')<0) { | |
10210234 | 294 | debuga(_("There is an invalid useragent in file %s\n"),tmp); |
06b39c87 | 295 | exit(EXIT_FAILURE); |
4bcb77cf | 296 | } |
dcb54d06 | 297 | nagent=atoi(tagent); |
012ba254 | 298 | perc=(agentot2>0) ? nagent * 100. / agentot2 : 0.; |
25697a35 | 299 | |
012ba254 | 300 | fprintf(fp_ht,"<tr><td class=\"data2\">%s</td><td class=\"data\">%d</td><td class=\"data\">%3.2lf</td></tr>\n",gwarea.current,nagent,perc); |
25697a35 | 301 | } |
25697a35 | 302 | fclose(fp_in); |
c0ec9cc7 FM |
303 | |
304 | fputs("</table></div>\n",fp_ht); | |
fa6552b0 FM |
305 | if (write_html_trailer(fp_ht)<0) |
306 | debuga(_("Write error in file %s\n"),hfile); | |
307 | if (fclose(fp_ht)==EOF) | |
308 | debuga(_("Failed to close file %s - %s\n"),hfile,strerror(errno)); | |
25697a35 | 309 | |
120d768c | 310 | unlink(tmp3); |
25697a35 GS |
311 | |
312 | return; | |
120d768c | 313 | |
25697a35 | 314 | } |