]>
Commit | Line | Data |
---|---|---|
25697a35 | 1 | /* |
c37945ed FM |
2 | * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com |
3 | * 1998, 2008 | |
94ff9470 | 4 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net |
25697a35 GS |
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 | ||
28 | void useragent() | |
29 | { | |
30 | ||
31 | FILE *fp_in = NULL, *fp_ou = NULL, *fp_ht = NULL; | |
32 | char tmp[MAXLEN], tmp2[MAXLEN]; | |
33 | char ip[MAXLEN], data[255], agent[255], user[255]; | |
34 | char ipantes[MAXLEN], nameantes[MAXLEN]; | |
35 | char tagent[7]; | |
36 | char csort[120]; | |
37 | char msg[255]; | |
38 | char ftime[128]; | |
39 | char user_old[255]="$#%0a3bc6"; | |
40 | char agent_old[255]="$#%0a3bc6"; | |
41 | char html[255]; | |
42 | char hfile[MAXLEN]; | |
43 | char idate[MAXLEN], fdate[MAXLEN]; | |
44 | int agentot=0, agentot2=0, agentdif=0, cont=0; | |
45 | float perc=0; | |
46 | unsigned long totregsl=0; | |
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 | ipantes[0]='\0'; | |
55 | nameantes[0]='\0'; | |
56 | ||
57 | sprintf(hfile,"%s/%s/useragent.html", outdir,periodo); | |
58 | ||
59 | sprintf(tmp,"%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(tmp,"w"))==NULL) { | |
68 | fprintf(stderr, "SARG: (email) %s: %s\n",text[45],tmp); | |
69 | exit(1); | |
70 | } | |
71 | ||
72 | if(debug) { | |
73 | sprintf(msg,"%s: %s",text[66],UserAgentLog); | |
74 | debuga(msg); | |
75 | } | |
76 | ||
77 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
78 | totregsl++; | |
79 | getword(ip,buf,' '); | |
80 | getword(data,buf,'['); | |
81 | getword(data,buf,' '); | |
82 | if(totregsl == 1) | |
83 | strcpy(idate,data); | |
84 | getword(agent,buf,'"'); | |
85 | getword(agent,buf,'"'); | |
86 | ||
87 | if(strlen(buf)) { | |
88 | getword(user,buf,' '); | |
89 | getword(user,buf,'\n'); | |
90 | } | |
91 | ||
92 | if(user[0] == '-') | |
93 | strcpy(user,ip); | |
94 | if(strlen(user) == 0) | |
95 | strcpy(user,ip); | |
96 | ||
97 | sprintf(buf,"%s\\%s\\%s\\%s\\\n",ip,data,agent,user); | |
98 | fputs(buf,fp_ou); | |
99 | user[0]='\0'; | |
100 | } | |
101 | ||
102 | strcpy(fdate,data); | |
103 | ||
104 | if(debug) { | |
105 | sprintf(msg, " %s: %ld",text[10],totregsl); | |
106 | debuga(msg); | |
107 | } | |
108 | ||
109 | fclose(fp_in); | |
110 | fclose(fp_ou); | |
111 | ||
112 | if (fp_ht) { | |
113 | fclose(fp_ht); | |
114 | } | |
115 | ||
116 | if(debug) { | |
117 | sprintf(msg,"%s: %s",text[54],tmp2); | |
118 | debuga(msg); | |
119 | } | |
120 | ||
94ff9470 | 121 | sprintf(csort,"sort -n -t '\\' -k 4,4 -k 3,3 -k 2,2 -k 1,1 -o '%s' '%s'",tmp2,tmp); |
25697a35 GS |
122 | system(csort); |
123 | ||
124 | unlink(tmp); | |
125 | ||
126 | if((fp_in=fopen(tmp2,"r"))==NULL) { | |
127 | fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],tmp2); | |
128 | exit(1); | |
129 | } | |
130 | ||
131 | if((fp_ht=fopen(hfile,"w"))==NULL) { | |
132 | fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],hfile); | |
133 | exit(1); | |
134 | } | |
135 | ||
136 | if(debug) | |
137 | debuga(text[72]); | |
138 | ||
139 | fputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"",fp_ht); | |
140 | fputs(" \"http://www.w3.org/TR/html4/loose.dtd\">\n",fp_ht); | |
141 | fputs("<html>\n",fp_ht); | |
142 | fputs("<head>\n",fp_ht); | |
143 | sprintf(html," <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet); | |
144 | fputs(html,fp_ht); | |
145 | fputs("</head>\n",fp_ht); | |
146 | ||
147 | if(strlen(FontFace) > 0) { | |
148 | sprintf(html,"<font face=%s>\n",FontFace); | |
149 | fputs(html,fp_ht); | |
150 | } | |
151 | ||
152 | sprintf(html,"<body bgcolor=%s text=%s background='%s'>\n",BgColor,TxColor,BgImage); | |
153 | fputs(html,fp_ht); | |
154 | ||
25697a35 | 155 | if(strlen(LogoImage) > 0) { |
94ff9470 GS |
156 | fputs("<center><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht); |
157 | sprintf(html,"<tr><th class=\"logo\"><img src='%s' border=0 align=absmiddle width=%s height=%s> %s</th></tr>\n",LogoImage,Width,Height,LogoText); | |
25697a35 | 158 | fputs(html,fp_ht); |
94ff9470 | 159 | fputs("<tr><td height=\"5\"></td></tr>\n",fp_ht); |
25697a35 GS |
160 | fputs("</table>\n",fp_ht); |
161 | } | |
162 | ||
491b862f GS |
163 | if(strcmp(IndexTree,"date") == 0) |
164 | show_sarg(fp_ht, "../../.."); | |
165 | else | |
166 | show_sarg(fp_ht,".."); | |
25697a35 GS |
167 | fputs("<center><table cellpadding=0 cellspacing=0>\n",fp_ht); |
168 | sprintf(html,"<tr><th align=center colspan=2><b><font color=%s size=+1>%s</font></b></th></tr>\n",TiColor,text[105]); | |
169 | fputs(html,fp_ht); | |
170 | ||
171 | fputs("<tr><td></td></tr><tr><td></td></tr>",fp_ht); | |
172 | fputs("</table></center>\n",fp_ht); | |
173 | ||
174 | fputs("<center><table cellpadding=0 cellspacing=0>\n",fp_ht); | |
175 | ||
176 | sprintf(html,"<tr><td align=right bgcolor=%s><font size=%s>%s:</font><td align=left bgcolor=%s><font size=%s>%s - %s</font></td></td></tr>\n",HeaderBgColor,FontSize,text[89],TxBgColor,FontSize,idate,fdate); | |
177 | fputs(html,fp_ht); | |
178 | ||
179 | fputs("</table></center>\n",fp_ht); | |
180 | fputs("<center><table cellpadding=0 cellspacing=0>\n",fp_ht); | |
181 | fputs("<tr><td></td><td></td></tr>",fp_ht); | |
182 | ||
183 | sprintf(html,"<tr><th align=left bgcolor=%s><font size=%s color=%s>%s</font></th><th bgcolor=%s align=left><font size=%s color=%s>%s</font></th></tr>\n",HeaderBgColor,FontSize,HeaderColor,text[98],HeaderBgColor,FontSize,HeaderColor,text[106]); | |
184 | fputs(html,fp_ou); | |
185 | ||
186 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
187 | getword(ip,buf,'\\'); | |
188 | ||
189 | if(strcmp(Ip2Name,"yes") == 0) { | |
190 | if(strcmp(ip,ipantes) != 0) { | |
191 | strcpy(ipantes,ip); | |
192 | ip2name(ip); | |
193 | strcpy(nameantes,ip); | |
194 | } else strcpy(ip,nameantes); | |
195 | } | |
196 | ||
197 | getword(data,buf,'\\'); | |
198 | getword(agent,buf,'\\'); | |
199 | getword(user,buf,'\\'); | |
200 | ||
201 | if(strcmp(user,user_old) != 0) { | |
202 | sprintf(html,"<tr><td align=left bgcolor=%s><font size=%s>%s</td><td align=left bgcolor=%s><font size=%s>%s</td></tr>\n",TxBgColor,FontSize,user,TxBgColor,FontSize,agent); | |
203 | fputs(html,fp_ht); | |
204 | strcpy(user_old,user); | |
205 | strcpy(agent_old,agent); | |
206 | } else { | |
207 | if(strcmp(agent,agent_old) != 0) { | |
208 | sprintf(html,"<tr><td></td><td align=left bgcolor=%s><font size=%s>%s</td></tr>\n",TxBgColor,FontSize,agent); | |
209 | fputs(html,fp_ht); | |
210 | strcpy(agent_old,agent); | |
211 | } | |
212 | } | |
213 | } | |
214 | ||
215 | fputs("</table>\n",fp_ht); | |
216 | fclose(fp_in); | |
217 | fclose(fp_ou); | |
218 | ||
94ff9470 | 219 | sprintf(csort,"sort -n -t '\\' -k 3,3 -o '%s' '%s'",tmp,tmp2); |
25697a35 GS |
220 | system(csort); |
221 | ||
222 | unlink(tmp2); | |
223 | ||
224 | if((fp_in=fopen(tmp,"r"))==NULL) { | |
225 | fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],tmp); | |
226 | exit(1); | |
227 | } | |
228 | ||
229 | if((fp_ou=fopen(tmp2,"w"))==NULL) { | |
230 | fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],tmp2); | |
231 | exit(1); | |
232 | } | |
233 | ||
234 | agent_old[0]='\0'; | |
235 | ||
236 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
237 | getword(ip,buf,'\\'); | |
238 | getword(data,buf,'\\'); | |
239 | getword(agent,buf,'\\'); | |
240 | ||
241 | if(!cont) { | |
242 | cont++; | |
243 | strcpy(agent_old,agent); | |
244 | } | |
245 | ||
246 | agentot++; | |
247 | ||
248 | if(strcmp(agent,agent_old) != 0) { | |
249 | agentdif++; | |
250 | sprintf(html,"%06d %s\n",agentot,agent_old); | |
251 | fputs(html,fp_ou); | |
252 | strcpy(agent_old,agent); | |
253 | agentot2+=agentot; | |
254 | agentot=1; | |
255 | } | |
256 | } | |
257 | agentdif++; | |
258 | sprintf(html,"%06d %s\n",agentot,agent); | |
259 | fputs(html,fp_ou); | |
260 | agentot2+=agentot; | |
261 | ||
262 | fclose(fp_in); | |
263 | fclose(fp_ou); | |
264 | ||
265 | unlink(tmp); | |
266 | ||
94ff9470 | 267 | sprintf(csort,"sort -n -r -k 1,1 -o '%s' '%s'",tmp,tmp2); |
25697a35 GS |
268 | system(csort); |
269 | ||
270 | unlink(tmp2); | |
271 | ||
272 | if((fp_in=fopen(tmp,"r"))==NULL) { | |
273 | fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],tmp); | |
274 | exit(1); | |
275 | } | |
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("<center><table cellpadding=0 cellspacing=0>\n",fp_ht); | |
285 | sprintf(html,"<tr><th align=left bgcolor=%s><font size=%s color=%s>%s</font></th><th bgcolor=%s align=left><font size=%s color=%s>%s</font></th><th align=center bgcolor=%s><font size=%s color=%s>%%</font></th></tr>\n",HeaderBgColor,FontSize,HeaderColor,text[106],HeaderBgColor,FontSize,HeaderColor,text[107],HeaderBgColor,FontSize,HeaderColor); | |
286 | fputs(html,fp_ht); | |
287 | ||
288 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
289 | buf[strlen(buf)-1]='\0'; | |
290 | getword(tagent,buf,' '); | |
291 | perc=atoi(tagent) * 100; | |
292 | perc=perc / agentot2; | |
293 | ||
294 | sprintf(html,"<tr><td align=left bgcolor=%s><font size=%s>%s</td><td align=right bgcolor=%s><font size=%s>%d</td><td align=right bgcolor=%s><font size=%s>%3.2f</td></tr>\n",TxBgColor,FontSize,buf,TxBgColor,FontSize,atoi(tagent),TxBgColor,FontSize,perc); | |
295 | fputs(html,fp_ht); | |
296 | } | |
297 | ||
298 | fputs("</table></html>\n",fp_ht); | |
299 | if(strcmp(ShowSargInfo,"yes") == 0) { | |
300 | zdate(ftime, DateFormat); | |
301 | sprintf(html,"<br><br><center><font size=-2>%s <a href='%s'>%s-%s</a> %s %s</font></center>\n",text[108],URL,PGM,VERSION,text[109],ftime); | |
302 | fputs(html,fp_ht); | |
303 | } | |
304 | ||
305 | fputs("</table>\n</body>\n</html>\n",fp_ht); | |
306 | fclose(fp_in); | |
307 | fclose(fp_ht); | |
308 | ||
309 | unlink(tmp); | |
310 | ||
311 | return; | |
312 | ||
313 | } |