]>
git.ipfire.org Git - thirdparty/sarg.git/blob - useragent.c
2 * AUTHOR: Pedro Lineu Orso orso@penguintech.com.br
4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
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.
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.
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.
26 #include "include/conf.h"
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
];
39 char user_old
[255]="$#%0a3bc6";
40 char agent_old
[255]="$#%0a3bc6";
43 char idate
[MAXLEN
], fdate
[MAXLEN
];
44 int agentot
=0, agentot2
=0, agentdif
=0, cont
=0;
46 unsigned long totregsl
=0;
57 sprintf(hfile
,"%s/%s/useragent.html", outdir
,periodo
);
59 sprintf(tmp
,"%s/squagent.unsort",TempDir
);
60 sprintf(tmp2
,"%s/squagent.log",TempDir
);
62 if((fp_in
=fopen(UserAgentLog
,"r"))==NULL
) {
63 fprintf(stderr
, "SARG: (useragent) %s: %s\n",text
[45],UserAgentLog
);
67 if((fp_ou
=fopen(tmp
,"w"))==NULL
) {
68 fprintf(stderr
, "SARG: (email) %s: %s\n",text
[45],tmp
);
73 sprintf(msg
,"%s: %s",text
[66],UserAgentLog
);
77 while(fgets(buf
,sizeof(buf
),fp_in
)!=NULL
) {
80 getword(data
,buf
,'[');
81 getword(data
,buf
,' ');
84 getword(agent
,buf
,'"');
85 getword(agent
,buf
,'"');
88 getword(user
,buf
,' ');
89 getword(user
,buf
,'\n');
97 sprintf(buf
,"%s\\%s\\%s\\%s\\\n",ip
,data
,agent
,user
);
105 sprintf(msg
, " %s: %ld",text
[10],totregsl
);
117 sprintf(msg
,"%s: %s",text
[54],tmp2
);
121 sprintf(csort
,"sort -n -t '\\' -k 4,4 -k 3,3 -k 2,2 -k 1,1 -o '%s' '%s'",tmp2
,tmp
);
126 if((fp_in
=fopen(tmp2
,"r"))==NULL
) {
127 fprintf(stderr
, "SARG: (useragent) %s: %s\n",text
[45],tmp2
);
131 if((fp_ht
=fopen(hfile
,"w"))==NULL
) {
132 fprintf(stderr
, "SARG: (useragent) %s: %s\n",text
[45],hfile
);
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
);
145 fputs("</head>\n",fp_ht
);
147 if(strlen(FontFace
) > 0) {
148 sprintf(html
,"<font face=%s>\n",FontFace
);
152 sprintf(html
,"<body bgcolor=%s text=%s background='%s'>\n",BgColor
,TxColor
,BgImage
);
155 if(strlen(LogoImage
) > 0) {
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
);
159 fputs("<tr><td height=\"5\"></td></tr>\n",fp_ht
);
160 fputs("</table>\n",fp_ht
);
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]);
168 fputs("<tr><td></td></tr><tr><td></td></tr>",fp_ht
);
169 fputs("</table></center>\n",fp_ht
);
171 fputs("<center><table cellpadding=0 cellspacing=0>\n",fp_ht
);
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
);
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
);
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]);
183 while(fgets(buf
,sizeof(buf
),fp_in
)!=NULL
) {
184 getword(ip
,buf
,'\\');
186 if(strcmp(Ip2Name
,"yes") == 0) {
187 if(strcmp(ip
,ipantes
) != 0) {
190 strcpy(nameantes
,ip
);
191 } else strcpy(ip
,nameantes
);
194 getword(data
,buf
,'\\');
195 getword(agent
,buf
,'\\');
196 getword(user
,buf
,'\\');
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
);
201 strcpy(user_old
,user
);
202 strcpy(agent_old
,agent
);
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
);
207 strcpy(agent_old
,agent
);
212 fputs("</table>\n",fp_ht
);
216 sprintf(csort
,"sort -n -t '\\' -k 3,3 -o '%s' '%s'",tmp
,tmp2
);
221 if((fp_in
=fopen(tmp
,"r"))==NULL
) {
222 fprintf(stderr
, "SARG: (useragent) %s: %s\n",text
[45],tmp
);
226 if((fp_ou
=fopen(tmp2
,"w"))==NULL
) {
227 fprintf(stderr
, "SARG: (useragent) %s: %s\n",text
[45],tmp2
);
233 while(fgets(buf
,sizeof(buf
),fp_in
)!=NULL
) {
234 getword(ip
,buf
,'\\');
235 getword(data
,buf
,'\\');
236 getword(agent
,buf
,'\\');
240 strcpy(agent_old
,agent
);
245 if(strcmp(agent
,agent_old
) != 0) {
247 sprintf(html
,"%06d %s\n",agentot
,agent_old
);
249 strcpy(agent_old
,agent
);
255 sprintf(html
,"%06d %s\n",agentot
,agent
);
264 sprintf(csort
,"sort -n -r -k 1,1 -o '%s' '%s'",tmp
,tmp2
);
269 if((fp_in
=fopen(tmp
,"r"))==NULL
) {
270 fprintf(stderr
, "SARG: (useragent) %s: %s\n",text
[45],tmp
);
274 if((fp_ht
=fopen(hfile
,"a"))==NULL
) {
275 fprintf(stderr
, "SARG: (useragent) %s: %s\n",text
[45],hfile
);
279 fputs("<br><br>\n",fp_ht
);
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
);
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
;
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
);
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
);
302 fputs("</table>\n</body>\n</html>\n",fp_ht
);