]>
Commit | Line | Data |
---|---|---|
25697a35 | 1 | /* |
94ff9470 | 2 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net |
fbd133bb | 3 | * 1998, 2011 |
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 | ||
61 | sprintf(tmp3,"%s/squagent.unsort",tmp); | |
62 | sprintf(tmp2,"%s/squagent.log",tmp); | |
63 | ||
64 | if((fp_in=fopen(UserAgentLog,"r"))==NULL) { | |
65 | debuga(_("(useragent) Cannot open file %s\n"),UserAgentLog); | |
66 | exit(EXIT_FAILURE); | |
67 | } | |
68 | ||
69 | if((fp_ou=fopen(tmp3,"w"))==NULL) { | |
70 | debuga(_("(useragent) Cannot open file %s\n"),tmp3); | |
71 | exit(EXIT_FAILURE); | |
72 | } | |
73 | ||
74 | if(debug) { | |
75 | debuga(_("Reading useragent log: %s\n"),UserAgentLog); | |
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) { |
9bd92830 FM |
83 | debuga(_("Maybe you have a broken record or garbage in your %s file\n"),UserAgentLog); |
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) { |
9bd92830 FM |
89 | debuga(_("Maybe you have a broken date in your %s file\n"),UserAgentLog); |
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) { | |
100 | debuga(_("Maybe you have a broken useragent entry in your %s file\n"),UserAgentLog); | |
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) { | |
106 | debuga(_("Maybe you have a broken record or garbage in your %s file\n"),UserAgentLog); | |
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) { | |
127 | debuga(_("Failed to close file %s - %s\n"),tmp3,strerror(errno)); | |
128 | exit(EXIT_FAILURE); | |
129 | } | |
130 | ||
131 | if(debug) { | |
132 | debuga(_("Sorting file: %s\n"),tmp2); | |
133 | } | |
134 | ||
135 | sprintf(csort,"sort -n -t \"\t\" -k 3,3 -k 2,2 -k 1,1 -o \"%s\" \"%s\"",tmp2,tmp3); | |
136 | cstatus=system(csort); | |
137 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
138 | debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); | |
139 | debuga(_("sort command: %s\n"),csort); | |
140 | exit(EXIT_FAILURE); | |
141 | } | |
142 | if((fp_in=fopen(tmp2,"r"))==NULL) { | |
143 | debuga(_("(useragent) Cannot open file %s\n"),tmp2); | |
144 | debuga(_("sort command: %s\n"),csort); | |
145 | exit(EXIT_FAILURE); | |
146 | } | |
147 | ||
08f9b029 FM |
148 | if (unlink(tmp3)) { |
149 | debuga(_("Cannot delete %s - %s\n"),tmp3,strerror(errno)); | |
150 | exit(EXIT_FAILURE); | |
151 | } | |
9bd92830 FM |
152 | |
153 | snprintf(hfile,sizeof(hfile),"%s/useragent.html", outdirname); | |
154 | if((fp_ht=fopen(hfile,"w"))==NULL) { | |
155 | debuga(_("(useragent) Cannot open file %s\n"),hfile); | |
156 | exit(EXIT_FAILURE); | |
157 | } | |
158 | ||
159 | if(debug) | |
160 | debuga(_("Making Useragent report\n")); | |
161 | ||
162 | write_html_header(fp_ht,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Squid Useragent's Report"),HTML_JS_NONE); | |
163 | fprintf(fp_ht,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Squid Useragent's Report")); | |
164 | fprintf(fp_ht,"<tr><td class=\"header_c\">%s: %s - %s</td></tr>\n",_("Period"),idate,fdate); | |
165 | close_html_header(fp_ht); | |
166 | ||
167 | fputs("<br><br>\n",fp_ht); | |
168 | ||
169 | fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht); | |
170 | fputs("<tr><td> </td><td> </td></tr>",fp_ht); | |
171 | ||
172 | fprintf(fp_ht,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",_("USERID"),_("AGENT")); | |
173 | ||
174 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
175 | getword_start(&gwarea,buf); | |
176 | if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { | |
177 | debuga(_("There is an invalid IP address in file %s\n"),tmp2); | |
178 | exit(EXIT_FAILURE); | |
179 | } | |
180 | ||
181 | if(Ip2Name) { | |
182 | if(strcmp(ip,ipbefore) != 0) { | |
183 | strcpy(ipbefore,ip); | |
184 | ip2name(ip,sizeof(ip)); | |
185 | strcpy(namebefore,ip); | |
186 | } else strcpy(ip,namebefore); | |
187 | } | |
188 | ||
189 | if (getword(agent,sizeof(agent),&gwarea,'\t')<0) { | |
190 | debuga(_("There is an invalid useragent in file %s\n"),tmp2); | |
191 | exit(EXIT_FAILURE); | |
192 | } | |
193 | if (getword(user,sizeof(user),&gwarea,'\t')<0) { | |
194 | debuga(_("There is an invalid user ID in file %s\n"),tmp2); | |
195 | exit(EXIT_FAILURE); | |
196 | } | |
197 | ||
198 | if(strcmp(user,user_old) != 0) { | |
199 | fprintf(fp_ht,"<tr><td class=\"data2\">%s</td><td class=\"data2\">",user); | |
200 | output_html_string(fp_ht,agent,250); | |
201 | fputs("</td></tr>\n",fp_ht); | |
202 | strcpy(user_old,user); | |
203 | strcpy(agent_old,agent); | |
204 | } else if(strcmp(agent,agent_old) != 0) { | |
205 | fputs("<tr><td></td><td class=\"data2\">",fp_ht); | |
206 | output_html_string(fp_ht,agent,250); | |
207 | fputs("</td></tr>\n",fp_ht); | |
208 | strcpy(agent_old,agent); | |
209 | } | |
210 | } | |
211 | ||
212 | fputs("</table>\n",fp_ht); | |
213 | fclose(fp_in); | |
214 | ||
215 | sprintf(csort,"sort -t \"\t\" -k 2,2 -o \"%s\" \"%s\"",tmp3,tmp2); | |
216 | cstatus=system(csort); | |
217 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
218 | debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); | |
219 | debuga(_("sort command: %s\n"),csort); | |
220 | exit(EXIT_FAILURE); | |
221 | } | |
222 | if((fp_in=fopen(tmp3,"r"))==NULL) { | |
223 | debuga(_("(useragent) Cannot open file %s\n"),tmp3); | |
224 | debuga(_("sort command: %s\n"),csort); | |
225 | exit(EXIT_FAILURE); | |
226 | } | |
227 | ||
08f9b029 FM |
228 | if (unlink(tmp2)) { |
229 | debuga(_("Cannot delete %s - %s\n"),tmp2,strerror(errno)); | |
230 | exit(EXIT_FAILURE); | |
231 | } | |
9bd92830 FM |
232 | |
233 | if((fp_ou=fopen(tmp2,"w"))==NULL) { | |
234 | debuga(_("(useragent) Cannot open file %s\n"),tmp2); | |
235 | exit(EXIT_FAILURE); | |
236 | } | |
237 | ||
238 | agent_old[0]='\0'; | |
239 | cont=0; | |
240 | ||
241 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
242 | getword_start(&gwarea,buf); | |
243 | if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { | |
244 | debuga(_("There is an invalid IP address in file %s\n"),tmp3); | |
245 | exit(EXIT_FAILURE); | |
246 | } | |
247 | if (getword(agent,sizeof(agent),&gwarea,'\t')<0) { | |
248 | debuga(_("There is an invalid useragent in file %s\n"),tmp3); | |
249 | exit(EXIT_FAILURE); | |
250 | } | |
251 | ||
252 | if(!cont) { | |
253 | cont++; | |
254 | strcpy(agent_old,agent); | |
255 | } | |
256 | ||
257 | if(strcmp(agent,agent_old) != 0) { | |
258 | agentdif++; | |
259 | fprintf(fp_ou,"%06d %s\n",agentot,agent_old); | |
260 | strcpy(agent_old,agent); | |
261 | agentot2+=agentot; | |
262 | agentot=0; | |
263 | } | |
264 | agentot++; | |
265 | } | |
266 | agentdif++; | |
267 | fprintf(fp_ou,"%06d %s\n",agentot,agent); | |
268 | agentot2+=agentot; | |
269 | ||
270 | fclose(fp_in); | |
271 | if (fclose(fp_ou)==EOF) { | |
272 | debuga(_("Failed to close file %s - %s\n"),tmp3,strerror(errno)); | |
273 | exit(EXIT_FAILURE); | |
274 | } | |
275 | ||
08f9b029 FM |
276 | if (unlink(tmp3)) { |
277 | debuga(_("Cannot delete %s - %s\n"),tmp3,strerror(errno)); | |
278 | exit(EXIT_FAILURE); | |
279 | } | |
9bd92830 FM |
280 | |
281 | sprintf(csort,"sort -n -r -k 1,1 -o \"%s\" \"%s\"",tmp3,tmp2); | |
282 | cstatus=system(csort); | |
283 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
284 | debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); | |
285 | debuga(_("sort command: %s\n"),csort); | |
286 | exit(EXIT_FAILURE); | |
287 | } | |
288 | if((fp_in=fopen(tmp3,"r"))==NULL) { | |
289 | debuga(_("(useragent) Cannot open file %s\n"),tmp3); | |
290 | debuga(_("sort command: %s\n"),csort); | |
291 | exit(EXIT_FAILURE); | |
292 | } | |
293 | ||
08f9b029 FM |
294 | if (unlink(tmp2)) { |
295 | debuga(_("Cannot delete %s - %s\n"),tmp2,strerror(errno)); | |
296 | exit(EXIT_FAILURE); | |
297 | } | |
9bd92830 FM |
298 | |
299 | fputs("<br><br>\n",fp_ht); | |
300 | ||
301 | fputs("<table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht); | |
302 | 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")); | |
303 | ||
304 | perc=0.; | |
305 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
306 | fixendofline(buf); | |
307 | getword_start(&gwarea,buf); | |
308 | if (getword(tagent,sizeof(tagent),&gwarea,' ')<0) { | |
309 | debuga(_("There is an invalid useragent in file %s\n"),tmp3); | |
310 | exit(EXIT_FAILURE); | |
311 | } | |
312 | nagent=atoi(tagent); | |
313 | perc=(agentot2>0) ? nagent * 100. / agentot2 : 0.; | |
314 | ||
315 | fputs("<tr><td class=\"data2\">",fp_ht); | |
316 | output_html_string(fp_ht,gwarea.current,250); | |
317 | fprintf(fp_ht,"</td><td class=\"data\">%d</td><td class=\"data\">%3.2lf</td></tr>\n",nagent,perc); | |
318 | } | |
319 | fclose(fp_in); | |
320 | ||
321 | fputs("</table></div>\n",fp_ht); | |
322 | if (write_html_trailer(fp_ht)<0) | |
323 | debuga(_("Write error in file %s\n"),hfile); | |
324 | if (fclose(fp_ht)==EOF) | |
325 | debuga(_("Failed to close file %s - %s\n"),hfile,strerror(errno)); | |
326 | ||
08f9b029 FM |
327 | if (unlink(tmp3)) { |
328 | debuga(_("Cannot delete %s - %s\n"),tmp3,strerror(errno)); | |
329 | exit(EXIT_FAILURE); | |
330 | } | |
9bd92830 FM |
331 | |
332 | return; | |
25697a35 | 333 | } |