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