]>
git.ipfire.org Git - thirdparty/sarg.git/blob - useragent.c
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
6 * please look at http://sarg.sourceforge.net/donations.php
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
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.
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.
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.
27 #include "include/conf.h"
28 #include "include/defs.h"
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
];
37 char user_old
[MAXLEN
]="$#%0a3bc6";
38 char agent_old
[MAXLEN
]="$#%0a3bc6";
40 char idate
[MAXLEN
], fdate
[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;
48 struct getwordstruct gwarea
, gwarea1
;
59 sprintf(tmp3
,"%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(tmp3
,"w"))==NULL
) {
68 fprintf(stderr
, "SARG: (email) %s: %s\n",text
[45],tmp3
);
73 debuga("%s: %s",text
[66],UserAgentLog
);
76 while(fgets(buf
,sizeof(buf
),fp_in
)!=NULL
) {
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
);
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
);
90 buildymd(day
,month
,year
,wdate
);
92 if (ndate
<dfrom
) continue;
93 if (ndate
>duntil
) break;
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
);
103 if(strstr(warea
,"SCRIPT") != 0 || strstr(warea
,"ONLOAD") != 0)
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
);
118 fprintf(fp_ou
,"%s\t%s\t%s\n",ip
,agent
,user
);
124 debuga(" %s: %ld",text
[10],totregsl
);
131 debuga("%s: %s",text
[54],tmp2
);
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
);
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
);
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
);
156 debuga("%s",text
[72]);
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
);
163 fputs("<br><br>\n",fp_ht
);
165 fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht
);
166 fputs("<tr><td> </td><td> </td></tr>",fp_ht
);
168 fprintf(fp_ht
,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",text
[98],text
[106]);
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
);
178 if(strcmp(ip
,ipbefore
) != 0) {
180 ip2name(ip
,sizeof(ip
));
181 strcpy(namebefore
,ip
);
182 } else strcpy(ip
,namebefore
);
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
);
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
);
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
);
203 fputs("</table>\n",fp_ht
);
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
);
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
);
221 if((fp_ou
=fopen(tmp2
,"w"))==NULL
) {
222 fprintf(stderr
, "SARG: (useragent) %s: %s\n",text
[45],tmp2
);
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
);
239 strcpy(agent_old
,agent
);
242 if(strcmp(agent
,agent_old
) != 0) {
244 fprintf(fp_ou
,"%06d %s\n",agentot
,agent_old
);
245 strcpy(agent_old
,agent
);
252 fprintf(fp_ou
,"%06d %s\n",agentot
,agent
);
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
);
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
);
275 fputs("<br><br>\n",fp_ht
);
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]);
281 while(fgets(buf
,sizeof(buf
),fp_in
)!=NULL
) {
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
);
289 perc
=(agentot2
>0) ? nagent
* 100. / agentot2
: 0.;
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
);
295 fputs("</table></div>\n",fp_ht
);
296 write_html_trailer(fp_ht
);