]> git.ipfire.org Git - thirdparty/sarg.git/blob - useragent.c
Ported r167 from branches/v2_2_6_1 (several changes).
[thirdparty/sarg.git] / useragent.c
1 /*
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
3 * 1998, 2009
4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
5 *
6 * SARG donations:
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
23 *
24 */
25
26 #include "include/conf.h"
27 #include "include/defs.h"
28
29 void useragent(void)
30 {
31
32 FILE *fp_in = NULL, *fp_ou = NULL, *fp_ht = NULL;
33 char ip[MAXLEN], data[MAXLEN], agent[MAXLEN], user[MAXLEN];
34 char ipbefore[MAXLEN], namebefore[MAXLEN];
35 char tagent[MAXLEN];
36 char user_old[MAXLEN]="$#%0a3bc6";
37 char agent_old[MAXLEN]="$#%0a3bc6";
38 char hfile[MAXLEN];
39 char idate[MAXLEN], fdate[MAXLEN];
40 int agentot=0, agentot2=0, agentdif=0, cont=0, nagent;
41 unsigned long totregsl=0;
42 int cstatus;
43 char ftime[128];
44 struct getwordstruct gwarea;
45
46 ip[0]='\0';
47 data[0]='\0';
48 agent[0]='\0';
49 user[0]='\0';
50 user_old[0]='\0';
51 agent_old[0]='\0';
52 ipbefore[0]='\0';
53 namebefore[0]='\0';
54
55 sprintf(tmp3,"%s/squagent.unsort",TempDir);
56 sprintf(tmp2,"%s/squagent.log",TempDir);
57
58 if((fp_in=fopen(UserAgentLog,"r"))==NULL) {
59 fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],UserAgentLog);
60 exit(1);
61 }
62
63 if((fp_ou=fopen(tmp3,"w"))==NULL) {
64 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],tmp3);
65 exit(1);
66 }
67
68 if(debug) {
69 debuga("%s: %s",text[66],UserAgentLog);
70 }
71
72 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
73 totregsl++;
74 getword_start(&gwarea,buf);
75 if (getword(ip,sizeof(ip),&gwarea,' ')<0 || getword_skip(MAXLEN,&gwarea,'[')<0 ||
76 getword(data,sizeof(data),&gwarea,' ')<0) {
77 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",UserAgentLog);
78 exit(1);
79 }
80 if(totregsl == 1)
81 strcpy(idate,data);
82 if (getword_skip(MAXLEN,&gwarea,'"')<0 || getword(agent,sizeof(agent),&gwarea,'"')<0) {
83 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",UserAgentLog);
84 exit(1);
85 }
86 strcpy(warea,agent);
87 strup(warea);
88 if(strstr(warea,"SCRIPT") != 0 || strstr(warea,"ONLOAD") != 0)
89 baddata();
90
91 if(strlen(gwarea.current)) {
92 if (getword_skip(MAXLEN,&gwarea,' ')<0 || getword(user,sizeof(user),&gwarea,'\n')<0) {
93 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",UserAgentLog);
94 exit(1);
95 }
96 }
97
98 if(user[0] == '-')
99 strcpy(user,ip);
100 if(strlen(user) == 0)
101 strcpy(user,ip);
102
103 fprintf(fp_ou,"%s\t%s\t%s\n",ip,agent,user);
104 user[0]='\0';
105 }
106
107 strcpy(fdate,data);
108
109 if(debug) {
110 debuga(" %s: %ld",text[10],totregsl);
111 }
112
113 fclose(fp_in);
114 fclose(fp_ou);
115
116 if(debug) {
117 debuga("%s: %s",text[54],tmp2);
118 }
119
120 sprintf(csort,"sort -n -t \"\t\" -k 3,3 -k 2,2 -k 1,1 -o \"%s\" \"%s\"",tmp2,tmp3);
121 cstatus=system(csort);
122 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
123 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
124 fprintf(stderr, "SARG: sort command: %s\n",csort);
125 exit(1);
126 }
127 if((fp_in=fopen(tmp2,"r"))==NULL) {
128 fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],tmp2);
129 fprintf(stderr, "SARG: sort command: %s\n",csort);
130 exit(1);
131 }
132
133 unlink(tmp3);
134
135 sprintf(hfile,"%s/%s/useragent.html", outdir,period);
136 if((fp_ht=fopen(hfile,"w"))==NULL) {
137 fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],hfile);
138 exit(1);
139 }
140
141 if(debug)
142 debuga("%s",text[72]);
143
144 fprintf(fp_ht, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n<html>\n<head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet);
145 css(fp_ht);
146 fputs("</head>\n",fp_ht);
147 //if(FontFace[0] != '\0') fprintf(fp_ht,"<font face=\"%s\">\n",FontFace);
148 fprintf(fp_ht,"<body bgcolor=\"%s\" text=\"%s\" background=\"%s\">\n",BgColor,TxColor,BgImage);
149 write_logo_image(fp_ht);
150
151 if(strcmp(IndexTree,"date") == 0)
152 show_sarg(fp_ht, "../../..");
153 else
154 show_sarg(fp_ht,"..");
155 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht);
156 fprintf(fp_ht,"<tr><th align=\"center\" colspan=\"2\"><b><font color=\"%s\" size=\"+1\">%s</font></b></th></tr>\n",TiColor,text[105]);
157
158 fputs("<tr><td></td></tr><tr><td></td></tr>",fp_ht);
159 fputs("</table></div>\n",fp_ht);
160
161 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht);
162
163 fprintf(fp_ht,"<tr><td align=\"right\" bgcolor=\"%s\">%s:<td align=\"left\" bgcolor=\"%s\">%s - %s</td></td></tr>\n",HeaderBgColor,text[89],TxBgColor,idate,fdate);
164
165 fputs("</table></div>\n",fp_ht);
166 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht);
167 fputs("<tr><td></td><td></td></tr>",fp_ht);
168
169 fprintf(fp_ou,"<tr><th align=\"left\" bgcolor=\"%s\"><font color=\"%s\">%s</font></th><th bgcolor=\"%s\" align=\"left\"><font color=\"%s\">%s</font></th></tr>\n",HeaderBgColor,HeaderColor,text[98],HeaderBgColor,HeaderColor,text[106]);
170
171 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
172 getword_start(&gwarea,buf);
173 if (getword(ip,sizeof(ip),&gwarea,'\t')<0) {
174 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp2);
175 exit(1);
176 }
177
178 if(strcmp(Ip2Name,"yes") == 0) {
179 if(strcmp(ip,ipbefore) != 0) {
180 strcpy(ipbefore,ip);
181 ip2name(ip,sizeof(ip));
182 strcpy(namebefore,ip);
183 } else strcpy(ip,namebefore);
184 }
185
186 if (getword(agent,sizeof(agent),&gwarea,'\t')<0 ||
187 getword(user,sizeof(user),&gwarea,'\t')<0) {
188 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp2);
189 exit(1);
190 }
191
192 if(strcmp(user,user_old) != 0) {
193 fprintf(fp_ht,"<tr><td align=\"left\" bgcolor=\"%s\">%s</td><td align=\"left\" bgcolor=\"%s\">%s</td></tr>\n",TxBgColor,user,TxBgColor,agent);
194 strcpy(user_old,user);
195 strcpy(agent_old,agent);
196 } else {
197 if(strcmp(agent,agent_old) != 0) {
198 fprintf(fp_ht,"<tr><td></td><td align=\"left\" bgcolor=\"%s\">%s</td></tr>\n",TxBgColor,agent);
199 strcpy(agent_old,agent);
200 }
201 }
202 }
203
204 fputs("</table>\n",fp_ht);
205 fclose(fp_in);
206 fclose(fp_ou);
207
208 sprintf(csort,"sort -t \"\t\" -k 2,2 -o \"%s\" \"%s\"",tmp3,tmp2);
209 cstatus=system(csort);
210 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
211 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
212 fprintf(stderr, "SARG: sort command: %s\n",csort);
213 exit(1);
214 }
215 if((fp_in=fopen(tmp3,"r"))==NULL) {
216 fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],tmp3);
217 fprintf(stderr, "SARG: sort command: %s\n",csort);
218 exit(1);
219 }
220
221 unlink(tmp2);
222
223 if((fp_ou=fopen(tmp2,"w"))==NULL) {
224 fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],tmp2);
225 exit(1);
226 }
227
228 agent_old[0]='\0';
229 cont=0;
230
231 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
232 getword_start(&gwarea,buf);
233 if (getword(ip,sizeof(ip),&gwarea,'\t')<0 ||
234 getword(agent,sizeof(agent),&gwarea,'\t')<0) {
235 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp);
236 exit(1);
237 }
238
239 if(!cont) {
240 cont++;
241 strcpy(agent_old,agent);
242 }
243
244 if(strcmp(agent,agent_old) != 0) {
245 agentdif++;
246 fprintf(fp_ou,"%06d %s\n",agentot,agent_old);
247 strcpy(agent_old,agent);
248 agentot2+=agentot;
249 agentot=0;
250 }
251 agentot++;
252 }
253 agentdif++;
254 fprintf(fp_ou,"%06d %s\n",agentot,agent);
255 agentot2+=agentot;
256
257 fclose(fp_in);
258 fclose(fp_ou);
259
260 unlink(tmp3);
261
262 sprintf(csort,"sort -n -r -k 1,1 -o \"%s\" \"%s\"",tmp3,tmp2);
263 cstatus=system(csort);
264 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
265 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
266 fprintf(stderr, "SARG: sort command: %s\n",csort);
267 exit(1);
268 }
269 if((fp_in=fopen(tmp3,"r"))==NULL) {
270 fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],tmp3);
271 fprintf(stderr, "SARG: sort command: %s\n",csort);
272 exit(1);
273 }
274
275 unlink(tmp2);
276
277 if((fp_ht=fopen(hfile,"a"))==NULL) {
278 fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],hfile);
279 exit(1);
280 }
281
282 fputs("<br><br>\n",fp_ht);
283
284 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht);
285 fprintf(fp_ht,"<tr><th align=\"left\" bgcolor=\"%s\"><font color=\"%s\">%s</font></th><th bgcolor=\"%s\" align=\"left\"><font color=\"%s\">%s</font></th><th align=\"center\" bgcolor=\"%s\"><font color=\"%s\">%%</font></th></tr>\n",HeaderBgColor,HeaderColor,text[106],HeaderBgColor,HeaderColor,text[107],HeaderBgColor,HeaderColor);
286
287 perc=0.;
288 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
289 fixendofline(buf);
290 getword_start(&gwarea,buf);
291 if (getword(tagent,sizeof(tagent),&gwarea,' ')<0) {
292 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp);
293 exit(1);
294 }
295 nagent=atoi(tagent);
296 if(agentot2>0) {
297 perc=nagent * 100;
298 perc=perc / agentot2;
299 }
300
301 fprintf(fp_ht,"<tr><td align=\"left\" bgcolor=\"%s\">%s</td><td align=\"right\" bgcolor=\"%s\">%d</td><td align=\"right\" bgcolor=\"%s\">%3.2f</td></tr>\n",TxBgColor,gwarea.current,TxBgColor,nagent,TxBgColor,perc);
302 }
303
304 fputs("</table></html>\n",fp_ht);
305 if(strcmp(ShowSargInfo,"yes") == 0) {
306 zdate(ftime, sizeof(ftime), DateFormat);
307 fprintf(fp_ht,"<br><br><div align=\"center\"><font size=\"-2\">%s <a href=\"%s\">%s-%s</a> %s %s</font></div>\n",text[108],URL,PGM,VERSION,text[109],ftime);
308 }
309
310 fputs("</table>\n</body>\n</html>\n",fp_ht);
311 fclose(fp_in);
312 fclose(fp_ht);
313
314 unlink(tmp3);
315
316 return;
317
318 }