]> git.ipfire.org Git - thirdparty/sarg.git/blob - useragent.c
Use one function to parse the lines from sarg-general instead of multiple codes scatt...
[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 tmp3[MAXLEN];
43 char day[4],month[5],year[5], wdate[20];
44 char csort[MAXLEN];
45 int agentot=0, agentot2=0, agentdif=0, cont=0, nagent;
46 unsigned long totregsl=0;
47 int cstatus;
48 int ndate;
49 double perc;
50 struct getwordstruct gwarea, gwarea1;
51
52 ip[0]='\0';
53 data[0]='\0';
54 agent[0]='\0';
55 user[0]='\0';
56 user_old[0]='\0';
57 agent_old[0]='\0';
58 ipbefore[0]='\0';
59 namebefore[0]='\0';
60
61 sprintf(tmp3,"%s/squagent.unsort",TempDir);
62 sprintf(tmp2,"%s/squagent.log",TempDir);
63
64 if((fp_in=fopen(UserAgentLog,"r"))==NULL) {
65 fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],UserAgentLog);
66 exit(1);
67 }
68
69 if((fp_ou=fopen(tmp3,"w"))==NULL) {
70 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],tmp3);
71 exit(1);
72 }
73
74 if(debug) {
75 debuga("%s: %s",text[66],UserAgentLog);
76 }
77
78 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
79 totregsl++;
80 getword_start(&gwarea,buf);
81 if (getword(ip,sizeof(ip),&gwarea,' ')<0 || getword_skip(MAXLEN,&gwarea,'[')<0 ||
82 getword(data,sizeof(data),&gwarea,' ')<0) {
83 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",UserAgentLog);
84 exit(1);
85 }
86 getword_start(&gwarea1,data);
87 if (getword(day,sizeof(day),&gwarea1,'/')<0 || getword(month,sizeof(month),&gwarea1,'/')<0 ||
88 getword(year,sizeof(year),&gwarea1,':')<0) {
89 printf("SARG: Maybe you have a broken date in your %s file.\n",UserAgentLog);
90 exit(1);
91 }
92 buildymd(day,month,year,wdate);
93 ndate=atoi(wdate);
94 if (ndate<dfrom) continue;
95 if (ndate>duntil) break;
96 if(totregsl == 1)
97 strcpy(idate,data);
98 strcpy(fdate,data);
99 if (getword_skip(MAXLEN,&gwarea,'"')<0 || getword(agent,sizeof(agent),&gwarea,'"')<0) {
100 printf("SARG: Maybe you have a broken useragent entry in your %s file.\n",UserAgentLog);
101 exit(1);
102 }
103 strcpy(warea,agent);
104 strup(warea);
105 if(strstr(warea,"SCRIPT") != 0 || strstr(warea,"ONLOAD") != 0)
106 baddata();
107
108 if(gwarea.current[0]!='\0') {
109 if (getword_skip(MAXLEN,&gwarea,' ')<0 || getword(user,sizeof(user),&gwarea,'\n')<0) {
110 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",UserAgentLog);
111 exit(1);
112 }
113 }
114
115 if(user[0] == '-')
116 strcpy(user,ip);
117 if(user[0] == '\0')
118 strcpy(user,ip);
119
120 fprintf(fp_ou,"%s\t%s\t%s\n",ip,agent,user);
121 user[0]='\0';
122 useragent_count++;
123 }
124
125 if(debug) {
126 debuga(" %s: %ld",text[10],totregsl);
127 }
128
129 fclose(fp_in);
130 fclose(fp_ou);
131
132 if(debug) {
133 debuga("%s: %s",text[54],tmp2);
134 }
135
136 sprintf(csort,"sort -n -t \"\t\" -k 3,3 -k 2,2 -k 1,1 -o \"%s\" \"%s\"",tmp2,tmp3);
137 cstatus=system(csort);
138 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
139 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
140 fprintf(stderr, "SARG: sort command: %s\n",csort);
141 exit(1);
142 }
143 if((fp_in=fopen(tmp2,"r"))==NULL) {
144 fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],tmp2);
145 fprintf(stderr, "SARG: sort command: %s\n",csort);
146 exit(1);
147 }
148
149 unlink(tmp3);
150
151 sprintf(hfile,"%s/useragent.html", dirname);
152 if((fp_ht=fopen(hfile,"w"))==NULL) {
153 fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],hfile);
154 exit(1);
155 }
156
157 if(debug)
158 debuga("%s",text[72]);
159
160 write_html_header(fp_ht,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Squid Useragent's Report"));
161 fprintf(fp_ht,"<tr><th class=\"header_c\">%s</th></tr>\n",text[105]);
162 fprintf(fp_ht,"<tr><td class=\"header_c\">%s: %s - %s</td></tr>\n",text[89],idate,fdate);
163 close_html_header(fp_ht);
164
165 fputs("<br><br>\n",fp_ht);
166
167 fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht);
168 fputs("<tr><td>&nbsp;</td><td>&nbsp;</td></tr>",fp_ht);
169
170 fprintf(fp_ht,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",text[98],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 class=\"data2\">%s</td><td class=\"data2\">%s</td></tr>\n",user,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 class=\"data2\">%s</td></tr>\n",agent);
200 strcpy(agent_old,agent);
201 }
202 }
203 }
204
205 fputs("</table>\n",fp_ht);
206 fclose(fp_in);
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 fputs("<br><br>\n",fp_ht);
278
279 fputs("<table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht);
280 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]);
281
282 perc=0.;
283 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
284 fixendofline(buf);
285 getword_start(&gwarea,buf);
286 if (getword(tagent,sizeof(tagent),&gwarea,' ')<0) {
287 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp);
288 exit(1);
289 }
290 nagent=atoi(tagent);
291 perc=(agentot2>0) ? nagent * 100. / agentot2 : 0.;
292
293 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);
294 }
295 fclose(fp_in);
296
297 fputs("</table></div>\n",fp_ht);
298 write_html_trailer(fp_ht);
299 fclose(fp_ht);
300
301 unlink(tmp3);
302
303 return;
304
305 }