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