]>
Commit | Line | Data |
---|---|---|
25697a35 | 1 | /* |
94ff9470 | 2 | * AUTHOR: Pedro Lineu Orso orso@penguintech.com.br |
25697a35 | 3 | * 1998, 2005 |
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 | ||
163 | show_sarg(fp_ht,".."); | |
164 | fputs("<center><table cellpadding=0 cellspacing=0>\n",fp_ht); | |
165 | sprintf(html,"<tr><th align=center colspan=2><b><font color=%s size=+1>%s</font></b></th></tr>\n",TiColor,text[105]); | |
166 | fputs(html,fp_ht); | |
167 | ||
168 | fputs("<tr><td></td></tr><tr><td></td></tr>",fp_ht); | |
169 | fputs("</table></center>\n",fp_ht); | |
170 | ||
171 | fputs("<center><table cellpadding=0 cellspacing=0>\n",fp_ht); | |
172 | ||
173 | 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); | |
174 | fputs(html,fp_ht); | |
175 | ||
176 | fputs("</table></center>\n",fp_ht); | |
177 | fputs("<center><table cellpadding=0 cellspacing=0>\n",fp_ht); | |
178 | fputs("<tr><td></td><td></td></tr>",fp_ht); | |
179 | ||
180 | 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]); | |
181 | fputs(html,fp_ou); | |
182 | ||
183 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
184 | getword(ip,buf,'\\'); | |
185 | ||
186 | if(strcmp(Ip2Name,"yes") == 0) { | |
187 | if(strcmp(ip,ipantes) != 0) { | |
188 | strcpy(ipantes,ip); | |
189 | ip2name(ip); | |
190 | strcpy(nameantes,ip); | |
191 | } else strcpy(ip,nameantes); | |
192 | } | |
193 | ||
194 | getword(data,buf,'\\'); | |
195 | getword(agent,buf,'\\'); | |
196 | getword(user,buf,'\\'); | |
197 | ||
198 | if(strcmp(user,user_old) != 0) { | |
199 | 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); | |
200 | fputs(html,fp_ht); | |
201 | strcpy(user_old,user); | |
202 | strcpy(agent_old,agent); | |
203 | } else { | |
204 | if(strcmp(agent,agent_old) != 0) { | |
205 | sprintf(html,"<tr><td></td><td align=left bgcolor=%s><font size=%s>%s</td></tr>\n",TxBgColor,FontSize,agent); | |
206 | fputs(html,fp_ht); | |
207 | strcpy(agent_old,agent); | |
208 | } | |
209 | } | |
210 | } | |
211 | ||
212 | fputs("</table>\n",fp_ht); | |
213 | fclose(fp_in); | |
214 | fclose(fp_ou); | |
215 | ||
94ff9470 | 216 | sprintf(csort,"sort -n -t '\\' -k 3,3 -o '%s' '%s'",tmp,tmp2); |
25697a35 GS |
217 | system(csort); |
218 | ||
219 | unlink(tmp2); | |
220 | ||
221 | if((fp_in=fopen(tmp,"r"))==NULL) { | |
222 | fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],tmp); | |
223 | exit(1); | |
224 | } | |
225 | ||
226 | if((fp_ou=fopen(tmp2,"w"))==NULL) { | |
227 | fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],tmp2); | |
228 | exit(1); | |
229 | } | |
230 | ||
231 | agent_old[0]='\0'; | |
232 | ||
233 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
234 | getword(ip,buf,'\\'); | |
235 | getword(data,buf,'\\'); | |
236 | getword(agent,buf,'\\'); | |
237 | ||
238 | if(!cont) { | |
239 | cont++; | |
240 | strcpy(agent_old,agent); | |
241 | } | |
242 | ||
243 | agentot++; | |
244 | ||
245 | if(strcmp(agent,agent_old) != 0) { | |
246 | agentdif++; | |
247 | sprintf(html,"%06d %s\n",agentot,agent_old); | |
248 | fputs(html,fp_ou); | |
249 | strcpy(agent_old,agent); | |
250 | agentot2+=agentot; | |
251 | agentot=1; | |
252 | } | |
253 | } | |
254 | agentdif++; | |
255 | sprintf(html,"%06d %s\n",agentot,agent); | |
256 | fputs(html,fp_ou); | |
257 | agentot2+=agentot; | |
258 | ||
259 | fclose(fp_in); | |
260 | fclose(fp_ou); | |
261 | ||
262 | unlink(tmp); | |
263 | ||
94ff9470 | 264 | sprintf(csort,"sort -n -r -k 1,1 -o '%s' '%s'",tmp,tmp2); |
25697a35 GS |
265 | system(csort); |
266 | ||
267 | unlink(tmp2); | |
268 | ||
269 | if((fp_in=fopen(tmp,"r"))==NULL) { | |
270 | fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],tmp); | |
271 | exit(1); | |
272 | } | |
273 | ||
274 | if((fp_ht=fopen(hfile,"a"))==NULL) { | |
275 | fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],hfile); | |
276 | exit(1); | |
277 | } | |
278 | ||
279 | fputs("<br><br>\n",fp_ht); | |
280 | ||
281 | fputs("<center><table cellpadding=0 cellspacing=0>\n",fp_ht); | |
282 | 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); | |
283 | fputs(html,fp_ht); | |
284 | ||
285 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
286 | buf[strlen(buf)-1]='\0'; | |
287 | getword(tagent,buf,' '); | |
288 | perc=atoi(tagent) * 100; | |
289 | perc=perc / agentot2; | |
290 | ||
291 | 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); | |
292 | fputs(html,fp_ht); | |
293 | } | |
294 | ||
295 | fputs("</table></html>\n",fp_ht); | |
296 | if(strcmp(ShowSargInfo,"yes") == 0) { | |
297 | zdate(ftime, DateFormat); | |
298 | 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); | |
299 | fputs(html,fp_ht); | |
300 | } | |
301 | ||
302 | fputs("</table>\n</body>\n</html>\n",fp_ht); | |
303 | fclose(fp_in); | |
304 | fclose(fp_ht); | |
305 | ||
306 | unlink(tmp); | |
307 | ||
308 | return; | |
309 | ||
310 | } |