]> git.ipfire.org Git - thirdparty/sarg.git/blob - useragent.c
Ported r225 from branches/v2_2_7 (remove Pedro Orso's mail from headers)
[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 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 int agentot=0, agentot2=0, agentdif=0, cont=0, nagent;
43 unsigned long totregsl=0;
44 int cstatus;
45 char ftime[128];
46 struct getwordstruct gwarea;
47
48 ip[0]='\0';
49 data[0]='\0';
50 agent[0]='\0';
51 user[0]='\0';
52 user_old[0]='\0';
53 agent_old[0]='\0';
54 ipbefore[0]='\0';
55 namebefore[0]='\0';
56
57 sprintf(tmp3,"%s/squagent.unsort",TempDir);
58 sprintf(tmp2,"%s/squagent.log",TempDir);
59
60 if((fp_in=fopen(UserAgentLog,"r"))==NULL) {
61 fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],UserAgentLog);
62 exit(1);
63 }
64
65 if((fp_ou=fopen(tmp3,"w"))==NULL) {
66 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],tmp3);
67 exit(1);
68 }
69
70 if(debug) {
71 debuga("%s: %s",text[66],UserAgentLog);
72 }
73
74 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
75 totregsl++;
76 getword_start(&gwarea,buf);
77 if (getword(ip,sizeof(ip),&gwarea,' ')<0 || getword_skip(MAXLEN,&gwarea,'[')<0 ||
78 getword(data,sizeof(data),&gwarea,' ')<0) {
79 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",UserAgentLog);
80 exit(1);
81 }
82 if(totregsl == 1)
83 strcpy(idate,data);
84 if (getword_skip(MAXLEN,&gwarea,'"')<0 || getword(agent,sizeof(agent),&gwarea,'"')<0) {
85 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",UserAgentLog);
86 exit(1);
87 }
88 strcpy(warea,agent);
89 strup(warea);
90 if(strstr(warea,"SCRIPT") != 0 || strstr(warea,"ONLOAD") != 0)
91 baddata();
92
93 if(gwarea.current[0]!='\0') {
94 if (getword_skip(MAXLEN,&gwarea,' ')<0 || getword(user,sizeof(user),&gwarea,'\n')<0) {
95 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",UserAgentLog);
96 exit(1);
97 }
98 }
99
100 if(user[0] == '-')
101 strcpy(user,ip);
102 if(user[0] == '\0')
103 strcpy(user,ip);
104
105 fprintf(fp_ou,"%s\t%s\t%s\n",ip,agent,user);
106 user[0]='\0';
107 }
108
109 strcpy(fdate,data);
110
111 if(debug) {
112 debuga(" %s: %ld",text[10],totregsl);
113 }
114
115 fclose(fp_in);
116 fclose(fp_ou);
117
118 if(debug) {
119 debuga("%s: %s",text[54],tmp2);
120 }
121
122 sprintf(csort,"sort -n -t \"\t\" -k 3,3 -k 2,2 -k 1,1 -o \"%s\" \"%s\"",tmp2,tmp3);
123 cstatus=system(csort);
124 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
125 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
126 fprintf(stderr, "SARG: sort command: %s\n",csort);
127 exit(1);
128 }
129 if((fp_in=fopen(tmp2,"r"))==NULL) {
130 fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],tmp2);
131 fprintf(stderr, "SARG: sort command: %s\n",csort);
132 exit(1);
133 }
134
135 unlink(tmp3);
136
137 sprintf(hfile,"%s/%s/useragent.html", outdir,period);
138 if((fp_ht=fopen(hfile,"w"))==NULL) {
139 fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],hfile);
140 exit(1);
141 }
142
143 if(debug)
144 debuga("%s",text[72]);
145
146 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);
147 css(fp_ht);
148 fputs("</head>\n",fp_ht);
149 //if(FontFace[0] != '\0') fprintf(fp_ht,"<font face=\"%s\">\n",FontFace);
150 fprintf(fp_ht,"<body bgcolor=\"%s\" text=\"%s\" background=\"%s\">\n",BgColor,TxColor,BgImage);
151 write_logo_image(fp_ht);
152
153 if(IndexTree == INDEX_TREE_DATE)
154 show_sarg(fp_ht, "../../..");
155 else
156 show_sarg(fp_ht,"..");
157 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht);
158 fprintf(fp_ht,"<tr><th align=\"center\" colspan=\"2\"><b><font color=\"%s\" size=\"+1\">%s</font></b></th></tr>\n",TiColor,text[105]);
159
160 fputs("<tr><td></td></tr><tr><td></td></tr>",fp_ht);
161 fputs("</table></div>\n",fp_ht);
162
163 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht);
164
165 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);
166
167 fputs("</table></div>\n",fp_ht);
168 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht);
169 fputs("<tr><td></td><td></td></tr>",fp_ht);
170
171 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]);
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 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp2);
177 exit(1);
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 getword(user,sizeof(user),&gwarea,'\t')<0) {
190 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp2);
191 exit(1);
192 }
193
194 if(strcmp(user,user_old) != 0) {
195 fprintf(fp_ht,"<tr><td align=\"left\" bgcolor=\"%s\">%s</td><td align=\"left\" bgcolor=\"%s\">%s</td></tr>\n",TxBgColor,user,TxBgColor,agent);
196 strcpy(user_old,user);
197 strcpy(agent_old,agent);
198 } else {
199 if(strcmp(agent,agent_old) != 0) {
200 fprintf(fp_ht,"<tr><td></td><td align=\"left\" bgcolor=\"%s\">%s</td></tr>\n",TxBgColor,agent);
201 strcpy(agent_old,agent);
202 }
203 }
204 }
205
206 fputs("</table>\n",fp_ht);
207 fclose(fp_in);
208 fclose(fp_ou);
209
210 sprintf(csort,"sort -t \"\t\" -k 2,2 -o \"%s\" \"%s\"",tmp3,tmp2);
211 cstatus=system(csort);
212 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
213 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
214 fprintf(stderr, "SARG: sort command: %s\n",csort);
215 exit(1);
216 }
217 if((fp_in=fopen(tmp3,"r"))==NULL) {
218 fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],tmp3);
219 fprintf(stderr, "SARG: sort command: %s\n",csort);
220 exit(1);
221 }
222
223 unlink(tmp2);
224
225 if((fp_ou=fopen(tmp2,"w"))==NULL) {
226 fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],tmp2);
227 exit(1);
228 }
229
230 agent_old[0]='\0';
231 cont=0;
232
233 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
234 getword_start(&gwarea,buf);
235 if (getword(ip,sizeof(ip),&gwarea,'\t')<0 ||
236 getword(agent,sizeof(agent),&gwarea,'\t')<0) {
237 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp);
238 exit(1);
239 }
240
241 if(!cont) {
242 cont++;
243 strcpy(agent_old,agent);
244 }
245
246 if(strcmp(agent,agent_old) != 0) {
247 agentdif++;
248 fprintf(fp_ou,"%06d %s\n",agentot,agent_old);
249 strcpy(agent_old,agent);
250 agentot2+=agentot;
251 agentot=0;
252 }
253 agentot++;
254 }
255 agentdif++;
256 fprintf(fp_ou,"%06d %s\n",agentot,agent);
257 agentot2+=agentot;
258
259 fclose(fp_in);
260 fclose(fp_ou);
261
262 unlink(tmp3);
263
264 sprintf(csort,"sort -n -r -k 1,1 -o \"%s\" \"%s\"",tmp3,tmp2);
265 cstatus=system(csort);
266 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
267 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
268 fprintf(stderr, "SARG: sort command: %s\n",csort);
269 exit(1);
270 }
271 if((fp_in=fopen(tmp3,"r"))==NULL) {
272 fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],tmp3);
273 fprintf(stderr, "SARG: sort command: %s\n",csort);
274 exit(1);
275 }
276
277 unlink(tmp2);
278
279 if((fp_ht=fopen(hfile,"a"))==NULL) {
280 fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],hfile);
281 exit(1);
282 }
283
284 fputs("<br><br>\n",fp_ht);
285
286 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht);
287 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);
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 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp);
295 exit(1);
296 }
297 nagent=atoi(tagent);
298 if(agentot2>0) {
299 perc=nagent * 100;
300 perc=perc / agentot2;
301 }
302
303 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);
304 }
305
306 fputs("</table></html>\n",fp_ht);
307 if(ShowSargInfo) {
308 zdate(ftime, sizeof(ftime), DateFormat);
309 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);
310 }
311
312 fputs("</table>\n</body>\n</html>\n",fp_ht);
313 fclose(fp_in);
314 fclose(fp_ht);
315
316 unlink(tmp3);
317
318 return;
319
320 }