]>
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; | |
d6e703cc FM |
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"; | |
25697a35 GS |
39 | char hfile[MAXLEN]; |
40 | char idate[MAXLEN], fdate[MAXLEN]; | |
06ced858 | 41 | char tmp2[MAXLEN]; |
2240dcea | 42 | char tmp3[MAXLEN]; |
012ba254 | 43 | char day[4],month[5],year[5], wdate[20]; |
2240dcea | 44 | char csort[MAXLEN]; |
dcb54d06 | 45 | int agentot=0, agentot2=0, agentdif=0, cont=0, nagent; |
25697a35 | 46 | unsigned long totregsl=0; |
456d78a5 | 47 | int cstatus; |
012ba254 FM |
48 | int ndate; |
49 | double perc; | |
50 | struct getwordstruct gwarea, gwarea1; | |
25697a35 GS |
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'; | |
d6e703cc FM |
58 | ipbefore[0]='\0'; |
59 | namebefore[0]='\0'; | |
25697a35 | 60 | |
d6e703cc | 61 | sprintf(tmp3,"%s/squagent.unsort",TempDir); |
25697a35 GS |
62 | sprintf(tmp2,"%s/squagent.log",TempDir); |
63 | ||
64 | if((fp_in=fopen(UserAgentLog,"r"))==NULL) { | |
9f70c14e | 65 | debuga(_("(useragent) Cannot open file %s\n"),UserAgentLog); |
25697a35 GS |
66 | exit(1); |
67 | } | |
68 | ||
d6e703cc | 69 | if((fp_ou=fopen(tmp3,"w"))==NULL) { |
9f70c14e | 70 | debuga(_("(useragent) Cannot open file %s\n"),tmp3); |
25697a35 GS |
71 | exit(1); |
72 | } | |
73 | ||
74 | if(debug) { | |
10210234 | 75 | debuga(_("Reading useragent log: %s\n"),UserAgentLog); |
25697a35 GS |
76 | } |
77 | ||
78 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
79 | totregsl++; | |
9c7c6346 FM |
80 | getword_start(&gwarea,buf); |
81 | if (getword(ip,sizeof(ip),&gwarea,' ')<0 || getword_skip(MAXLEN,&gwarea,'[')<0 || | |
82 | getword(data,sizeof(data),&gwarea,' ')<0) { | |
9f70c14e | 83 | debuga(_("Maybe you have a broken record or garbage in your %s file\n"),UserAgentLog); |
4bcb77cf FM |
84 | exit(1); |
85 | } | |
012ba254 FM |
86 | getword_start(&gwarea1,data); |
87 | if (getword(day,sizeof(day),&gwarea1,'/')<0 || getword(month,sizeof(month),&gwarea1,'/')<0 || | |
88 | getword(year,sizeof(year),&gwarea1,':')<0) { | |
9f70c14e | 89 | debuga(_("Maybe you have a broken date in your %s file\n"),UserAgentLog); |
012ba254 FM |
90 | exit(1); |
91 | } | |
92 | buildymd(day,month,year,wdate); | |
93 | ndate=atoi(wdate); | |
c0ec9cc7 FM |
94 | if (ndate<dfrom) continue; |
95 | if (ndate>duntil) break; | |
25697a35 GS |
96 | if(totregsl == 1) |
97 | strcpy(idate,data); | |
012ba254 | 98 | strcpy(fdate,data); |
9c7c6346 | 99 | if (getword_skip(MAXLEN,&gwarea,'"')<0 || getword(agent,sizeof(agent),&gwarea,'"')<0) { |
9f70c14e | 100 | debuga(_("Maybe you have a broken useragent entry in your %s file\n"),UserAgentLog); |
4bcb77cf FM |
101 | exit(1); |
102 | } | |
d6e703cc FM |
103 | strcpy(warea,agent); |
104 | strup(warea); | |
105 | if(strstr(warea,"SCRIPT") != 0 || strstr(warea,"ONLOAD") != 0) | |
106 | baddata(); | |
107 | ||
0a4d2bda | 108 | if(gwarea.current[0]!='\0') { |
9c7c6346 | 109 | if (getword_skip(MAXLEN,&gwarea,' ')<0 || getword(user,sizeof(user),&gwarea,'\n')<0) { |
9f70c14e | 110 | debuga(_("Maybe you have a broken record or garbage in your %s file\n"),UserAgentLog); |
4bcb77cf FM |
111 | exit(1); |
112 | } | |
25697a35 GS |
113 | } |
114 | ||
115 | if(user[0] == '-') | |
116 | strcpy(user,ip); | |
0a4d2bda | 117 | if(user[0] == '\0') |
25697a35 GS |
118 | strcpy(user,ip); |
119 | ||
dcb54d06 | 120 | fprintf(fp_ou,"%s\t%s\t%s\n",ip,agent,user); |
25697a35 | 121 | user[0]='\0'; |
c0ec9cc7 | 122 | useragent_count++; |
25697a35 GS |
123 | } |
124 | ||
25697a35 | 125 | if(debug) { |
10210234 | 126 | debuga(_(" Records read: %ld\n"),totregsl); |
25697a35 GS |
127 | } |
128 | ||
129 | fclose(fp_in); | |
130 | fclose(fp_ou); | |
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); | |
456d78a5 FM |
141 | exit(1); |
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); |
25697a35 GS |
146 | exit(1); |
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); |
25697a35 GS |
154 | exit(1); |
155 | } | |
120d768c | 156 | |
25697a35 | 157 | if(debug) |
10210234 | 158 | debuga(_("Making Useragent report\n")); |
25697a35 | 159 | |
c0ec9cc7 | 160 | write_html_header(fp_ht,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Squid Useragent's Report")); |
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); |
4bcb77cf FM |
176 | exit(1); |
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); | |
189 | exit(1); | |
190 | } | |
191 | if (getword(user,sizeof(user),&gwarea,'\t')<0) { | |
192 | debuga(_("There is an invalid user ID in file %s\n"),tmp2); | |
4bcb77cf FM |
193 | exit(1); |
194 | } | |
25697a35 GS |
195 | |
196 | if(strcmp(user,user_old) != 0) { | |
012ba254 | 197 | fprintf(fp_ht,"<tr><td class=\"data2\">%s</td><td class=\"data2\">%s</td></tr>\n",user,agent); |
25697a35 GS |
198 | strcpy(user_old,user); |
199 | strcpy(agent_old,agent); | |
200 | } else { | |
201 | if(strcmp(agent,agent_old) != 0) { | |
012ba254 | 202 | fprintf(fp_ht,"<tr><td></td><td class=\"data2\">%s</td></tr>\n",agent); |
25697a35 GS |
203 | strcpy(agent_old,agent); |
204 | } | |
205 | } | |
206 | } | |
120d768c | 207 | |
25697a35 GS |
208 | fputs("</table>\n",fp_ht); |
209 | fclose(fp_in); | |
25697a35 | 210 | |
dcb54d06 | 211 | sprintf(csort,"sort -t \"\t\" -k 2,2 -o \"%s\" \"%s\"",tmp3,tmp2); |
456d78a5 FM |
212 | cstatus=system(csort); |
213 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
10210234 FM |
214 | debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); |
215 | debuga(_("sort command: %s\n"),csort); | |
456d78a5 FM |
216 | exit(1); |
217 | } | |
d6e703cc | 218 | if((fp_in=fopen(tmp3,"r"))==NULL) { |
9f70c14e | 219 | debuga(_("(useragent) Cannot open file %s\n"),tmp3); |
10210234 | 220 | debuga(_("sort command: %s\n"),csort); |
25697a35 GS |
221 | exit(1); |
222 | } | |
223 | ||
456d78a5 FM |
224 | unlink(tmp2); |
225 | ||
25697a35 | 226 | if((fp_ou=fopen(tmp2,"w"))==NULL) { |
fcdc0918 | 227 | debuga(_("(useragent) Cannot open file %s\n"),tmp2); |
25697a35 GS |
228 | exit(1); |
229 | } | |
230 | ||
231 | agent_old[0]='\0'; | |
dcb54d06 | 232 | cont=0; |
25697a35 GS |
233 | |
234 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
9c7c6346 | 235 | getword_start(&gwarea,buf); |
10210234 FM |
236 | if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { |
237 | debuga(_("There is an invalid IP address in file %s\n"),tmp); | |
238 | exit(1); | |
239 | } | |
240 | if (getword(agent,sizeof(agent),&gwarea,'\t')<0) { | |
241 | debuga(_("There is an invalid useragent in file %s\n"),tmp); | |
4bcb77cf FM |
242 | exit(1); |
243 | } | |
25697a35 GS |
244 | |
245 | if(!cont) { | |
246 | cont++; | |
247 | strcpy(agent_old,agent); | |
248 | } | |
120d768c | 249 | |
25697a35 GS |
250 | if(strcmp(agent,agent_old) != 0) { |
251 | agentdif++; | |
354c1a68 | 252 | fprintf(fp_ou,"%06d %s\n",agentot,agent_old); |
25697a35 GS |
253 | strcpy(agent_old,agent); |
254 | agentot2+=agentot; | |
dcb54d06 | 255 | agentot=0; |
25697a35 | 256 | } |
dcb54d06 | 257 | agentot++; |
25697a35 GS |
258 | } |
259 | agentdif++; | |
354c1a68 | 260 | fprintf(fp_ou,"%06d %s\n",agentot,agent); |
25697a35 GS |
261 | agentot2+=agentot; |
262 | ||
263 | fclose(fp_in); | |
264 | fclose(fp_ou); | |
265 | ||
d6e703cc | 266 | unlink(tmp3); |
25697a35 | 267 | |
9a2efbd0 | 268 | sprintf(csort,"sort -n -r -k 1,1 -o \"%s\" \"%s\"",tmp3,tmp2); |
456d78a5 FM |
269 | cstatus=system(csort); |
270 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
10210234 FM |
271 | debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); |
272 | debuga(_("sort command: %s\n"),csort); | |
456d78a5 FM |
273 | exit(1); |
274 | } | |
d6e703cc | 275 | if((fp_in=fopen(tmp3,"r"))==NULL) { |
9f70c14e | 276 | debuga(_("(useragent) Cannot open file %s\n"),tmp3); |
10210234 | 277 | debuga(_("sort command: %s\n"),csort); |
25697a35 GS |
278 | exit(1); |
279 | } | |
280 | ||
456d78a5 FM |
281 | unlink(tmp2); |
282 | ||
c0ec9cc7 | 283 | fputs("<br><br>\n",fp_ht); |
25697a35 | 284 | |
c0ec9cc7 | 285 | fputs("<table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht); |
c36c7384 | 286 | 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 | 287 | |
dcb54d06 | 288 | perc=0.; |
25697a35 | 289 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { |
120d768c | 290 | fixendofline(buf); |
9c7c6346 FM |
291 | getword_start(&gwarea,buf); |
292 | if (getword(tagent,sizeof(tagent),&gwarea,' ')<0) { | |
10210234 | 293 | debuga(_("There is an invalid useragent in file %s\n"),tmp); |
4bcb77cf FM |
294 | exit(1); |
295 | } | |
dcb54d06 | 296 | nagent=atoi(tagent); |
012ba254 | 297 | perc=(agentot2>0) ? nagent * 100. / agentot2 : 0.; |
25697a35 | 298 | |
012ba254 | 299 | 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 | 300 | } |
25697a35 | 301 | fclose(fp_in); |
c0ec9cc7 FM |
302 | |
303 | fputs("</table></div>\n",fp_ht); | |
304 | write_html_trailer(fp_ht); | |
25697a35 GS |
305 | fclose(fp_ht); |
306 | ||
120d768c | 307 | unlink(tmp3); |
25697a35 GS |
308 | |
309 | return; | |
120d768c | 310 | |
25697a35 | 311 | } |