]> git.ipfire.org Git - thirdparty/sarg.git/blob - useragent.c
87bd875ce19d07d94fcc07e88bbca1ace9ef718c
[thirdparty/sarg.git] / useragent.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2010
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
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"
28 #include "include/defs.h"
29
30 void useragent(void)
31 {
32
33 FILE *fp_in = NULL, *fp_ou = NULL, *fp_ht = NULL;
34 char buf[MAXLEN];
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";
40 char hfile[MAXLEN];
41 char idate[MAXLEN], fdate[MAXLEN];
42 char tmp2[MAXLEN];
43 char tmp3[MAXLEN];
44 char day[4],month[5],year[5], wdate[20];
45 char csort[MAXLEN];
46 int agentot=0, agentot2=0, agentdif=0, cont=0, nagent;
47 unsigned long totregsl=0;
48 int cstatus;
49 int ndate;
50 double perc;
51 struct getwordstruct gwarea, gwarea1;
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';
59 ipbefore[0]='\0';
60 namebefore[0]='\0';
61
62 sprintf(tmp3,"%s/squagent.unsort",TempDir);
63 sprintf(tmp2,"%s/squagent.log",TempDir);
64
65 if((fp_in=fopen(UserAgentLog,"r"))==NULL) {
66 debuga(_("(useragent) Cannot open file %s\n"),UserAgentLog);
67 exit(EXIT_FAILURE);
68 }
69
70 if((fp_ou=fopen(tmp3,"w"))==NULL) {
71 debuga(_("(useragent) Cannot open file %s\n"),tmp3);
72 exit(EXIT_FAILURE);
73 }
74
75 if(debug) {
76 debuga(_("Reading useragent log: %s\n"),UserAgentLog);
77 }
78
79 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
80 totregsl++;
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) {
84 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),UserAgentLog);
85 exit(EXIT_FAILURE);
86 }
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) {
90 debuga(_("Maybe you have a broken date in your %s file\n"),UserAgentLog);
91 exit(EXIT_FAILURE);
92 }
93 buildymd(day,month,year,wdate);
94 ndate=atoi(wdate);
95 if (ndate<dfrom) continue;
96 if (ndate>duntil) break;
97 if(totregsl == 1)
98 strcpy(idate,data);
99 strcpy(fdate,data);
100 if (getword_skip(MAXLEN,&gwarea,'"')<0 || getword(agent,sizeof(agent),&gwarea,'"')<0) {
101 debuga(_("Maybe you have a broken useragent entry in your %s file\n"),UserAgentLog);
102 exit(EXIT_FAILURE);
103 }
104 strcpy(warea,agent);
105 strup(warea);
106 if(strstr(warea,"SCRIPT") != 0 || strstr(warea,"ONLOAD") != 0)
107 baddata();
108
109 if(gwarea.current[0]!='\0') {
110 if (getword_skip(MAXLEN,&gwarea,' ')<0 || getword(user,sizeof(user),&gwarea,'\n')<0) {
111 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),UserAgentLog);
112 exit(EXIT_FAILURE);
113 }
114 }
115
116 if(user[0] == '-')
117 strcpy(user,ip);
118 if(user[0] == '\0')
119 strcpy(user,ip);
120
121 fprintf(fp_ou,"%s\t%s\t%s\n",ip,agent,user);
122 user[0]='\0';
123 useragent_count++;
124 }
125
126 if(debug) {
127 debuga(_(" Records read: %ld\n"),totregsl);
128 }
129
130 fclose(fp_in);
131 fclose(fp_ou);
132
133 if(debug) {
134 debuga(_("Sorting file: %s\n"),tmp2);
135 }
136
137 sprintf(csort,"sort -n -t \"\t\" -k 3,3 -k 2,2 -k 1,1 -o \"%s\" \"%s\"",tmp2,tmp3);
138 cstatus=system(csort);
139 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
140 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
141 debuga(_("sort command: %s\n"),csort);
142 exit(EXIT_FAILURE);
143 }
144 if((fp_in=fopen(tmp2,"r"))==NULL) {
145 debuga(_("(useragent) Cannot open file %s\n"),tmp2);
146 debuga(_("sort command: %s\n"),csort);
147 exit(EXIT_FAILURE);
148 }
149
150 unlink(tmp3);
151
152 snprintf(hfile,sizeof(hfile),"%s/useragent.html", outdirname);
153 if((fp_ht=fopen(hfile,"w"))==NULL) {
154 debuga(_("(useragent) Cannot open file %s\n"),hfile);
155 exit(EXIT_FAILURE);
156 }
157
158 if(debug)
159 debuga(_("Making Useragent report\n"));
160
161 write_html_header(fp_ht,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Squid Useragent's Report"));
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);
164 close_html_header(fp_ht);
165
166 fputs("<br><br>\n",fp_ht);
167
168 fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht);
169 fputs("<tr><td>&nbsp;</td><td>&nbsp;</td></tr>",fp_ht);
170
171 fprintf(fp_ht,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",_("USERID"),_("AGENT"));
172
173 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
174 getword_start(&gwarea,buf);
175 if (getword(ip,sizeof(ip),&gwarea,'\t')<0) {
176 debuga(_("There is an invalid IP address in file %s\n"),tmp2);
177 exit(EXIT_FAILURE);
178 }
179
180 if(Ip2Name) {
181 if(strcmp(ip,ipbefore) != 0) {
182 strcpy(ipbefore,ip);
183 ip2name(ip,sizeof(ip));
184 strcpy(namebefore,ip);
185 } else strcpy(ip,namebefore);
186 }
187
188 if (getword(agent,sizeof(agent),&gwarea,'\t')<0) {
189 debuga(_("There is an invalid useragent in file %s\n"),tmp2);
190 exit(EXIT_FAILURE);
191 }
192 if (getword(user,sizeof(user),&gwarea,'\t')<0) {
193 debuga(_("There is an invalid user ID in file %s\n"),tmp2);
194 exit(EXIT_FAILURE);
195 }
196
197 if(strcmp(user,user_old) != 0) {
198 fprintf(fp_ht,"<tr><td class=\"data2\">%s</td><td class=\"data2\">%s</td></tr>\n",user,agent);
199 strcpy(user_old,user);
200 strcpy(agent_old,agent);
201 } else {
202 if(strcmp(agent,agent_old) != 0) {
203 fprintf(fp_ht,"<tr><td></td><td class=\"data2\">%s</td></tr>\n",agent);
204 strcpy(agent_old,agent);
205 }
206 }
207 }
208
209 fputs("</table>\n",fp_ht);
210 fclose(fp_in);
211
212 sprintf(csort,"sort -t \"\t\" -k 2,2 -o \"%s\" \"%s\"",tmp3,tmp2);
213 cstatus=system(csort);
214 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
215 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
216 debuga(_("sort command: %s\n"),csort);
217 exit(EXIT_FAILURE);
218 }
219 if((fp_in=fopen(tmp3,"r"))==NULL) {
220 debuga(_("(useragent) Cannot open file %s\n"),tmp3);
221 debuga(_("sort command: %s\n"),csort);
222 exit(EXIT_FAILURE);
223 }
224
225 unlink(tmp2);
226
227 if((fp_ou=fopen(tmp2,"w"))==NULL) {
228 debuga(_("(useragent) Cannot open file %s\n"),tmp2);
229 exit(EXIT_FAILURE);
230 }
231
232 agent_old[0]='\0';
233 cont=0;
234
235 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
236 getword_start(&gwarea,buf);
237 if (getword(ip,sizeof(ip),&gwarea,'\t')<0) {
238 debuga(_("There is an invalid IP address in file %s\n"),tmp);
239 exit(EXIT_FAILURE);
240 }
241 if (getword(agent,sizeof(agent),&gwarea,'\t')<0) {
242 debuga(_("There is an invalid useragent in file %s\n"),tmp);
243 exit(EXIT_FAILURE);
244 }
245
246 if(!cont) {
247 cont++;
248 strcpy(agent_old,agent);
249 }
250
251 if(strcmp(agent,agent_old) != 0) {
252 agentdif++;
253 fprintf(fp_ou,"%06d %s\n",agentot,agent_old);
254 strcpy(agent_old,agent);
255 agentot2+=agentot;
256 agentot=0;
257 }
258 agentot++;
259 }
260 agentdif++;
261 fprintf(fp_ou,"%06d %s\n",agentot,agent);
262 agentot2+=agentot;
263
264 fclose(fp_in);
265 fclose(fp_ou);
266
267 unlink(tmp3);
268
269 sprintf(csort,"sort -n -r -k 1,1 -o \"%s\" \"%s\"",tmp3,tmp2);
270 cstatus=system(csort);
271 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
272 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
273 debuga(_("sort command: %s\n"),csort);
274 exit(EXIT_FAILURE);
275 }
276 if((fp_in=fopen(tmp3,"r"))==NULL) {
277 debuga(_("(useragent) Cannot open file %s\n"),tmp3);
278 debuga(_("sort command: %s\n"),csort);
279 exit(EXIT_FAILURE);
280 }
281
282 unlink(tmp2);
283
284 fputs("<br><br>\n",fp_ht);
285
286 fputs("<table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht);
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"));
288
289 perc=0.;
290 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
291 fixendofline(buf);
292 getword_start(&gwarea,buf);
293 if (getword(tagent,sizeof(tagent),&gwarea,' ')<0) {
294 debuga(_("There is an invalid useragent in file %s\n"),tmp);
295 exit(EXIT_FAILURE);
296 }
297 nagent=atoi(tagent);
298 perc=(agentot2>0) ? nagent * 100. / agentot2 : 0.;
299
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);
301 }
302 fclose(fp_in);
303
304 fputs("</table></div>\n",fp_ht);
305 write_html_trailer(fp_ht);
306 fclose(fp_ht);
307
308 unlink(tmp3);
309
310 return;
311
312 }