]>
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"
32 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
];
43 char day
[4],month
[5],year
[5], wdate
[20];
45 int agentot
=0, agentot2
=0, agentdif
=0, cont
=0, nagent
;
46 unsigned long totregsl
=0;
50 struct getwordstruct gwarea
, gwarea1
;
61 sprintf(tmp3
,"%s/squagent.int_unsort",tmp
);
62 sprintf(tmp2
,"%s/squagent.int_log",tmp
);
64 if((fp_in
=fopen(UserAgentLog
,"r"))==NULL
) {
65 debuga(_("(useragent) Cannot open file %s\n"),UserAgentLog
);
69 if((fp_ou
=fopen(tmp3
,"w"))==NULL
) {
70 debuga(_("(useragent) Cannot open file %s\n"),tmp3
);
75 debuga(_("Reading useragent log: %s\n"),UserAgentLog
);
78 while(fgets(buf
,sizeof(buf
),fp_in
)!=NULL
) {
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 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),UserAgentLog
);
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 debuga(_("Maybe you have a broken date in your %s file\n"),UserAgentLog
);
92 buildymd(day
,month
,year
,wdate
);
94 if (ndate
<dfrom
) continue;
95 if (ndate
>duntil
) break;
99 if (getword_skip(MAXLEN
,&gwarea
,'"')<0 || getword(agent
,sizeof(agent
),&gwarea
,'"')<0) {
100 debuga(_("Maybe you have a broken useragent entry in your %s file\n"),UserAgentLog
);
104 if(gwarea
.current
[0]!='\0') {
105 if (getword_skip(MAXLEN
,&gwarea
,' ')<0 || getword(user
,sizeof(user
),&gwarea
,'\n')<0) {
106 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),UserAgentLog
);
117 fprintf(fp_ou
,"%s\t%s\t%s\n",ip
,agent
,user
);
122 debuga(_(" Records read: %ld\n"),totregsl
);
126 if (fclose(fp_ou
)==EOF
) {
127 debuga(_("Failed to close file %s - %s\n"),tmp3
,strerror(errno
));
132 debuga(_("Sorting file: %s\n"),tmp2
);
135 if (snprintf(csort
,sizeof(csort
),"sort -n -t \"\t\" -k 3,3 -k 2,2 -k 1,1 -o \"%s\" \"%s\"",tmp2
,tmp3
)>=sizeof(csort
)) {
136 debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),tmp2
,tmp3
);
139 cstatus
=system(csort
);
140 if (!WIFEXITED(cstatus
) || WEXITSTATUS(cstatus
)) {
141 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus
));
142 debuga(_("sort command: %s\n"),csort
);
145 if((fp_in
=fopen(tmp2
,"r"))==NULL
) {
146 debuga(_("(useragent) Cannot open file %s\n"),tmp2
);
147 debuga(_("sort command: %s\n"),csort
);
151 if (!KeepTempLog
&& unlink(tmp3
)) {
152 debuga(_("Cannot delete \"%s\": %s\n"),tmp3
,strerror(errno
));
156 snprintf(hfile
,sizeof(hfile
),"%s/useragent.html", outdirname
);
157 if((fp_ht
=fopen(hfile
,"w"))==NULL
) {
158 debuga(_("(useragent) Cannot open file %s\n"),hfile
);
163 debuga(_("Making Useragent report\n"));
165 write_html_header(fp_ht
,(IndexTree
== INDEX_TREE_DATE
) ? 3 : 1,_("Squid Useragent's Report"),HTML_JS_NONE
);
166 fprintf(fp_ht
,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Squid Useragent's Report"));
167 fprintf(fp_ht
,"<tr><td class=\"header_c\">%s: %s - %s</td></tr>\n",_("Period"),idate
,fdate
);
168 close_html_header(fp_ht
);
170 fputs("<br><br>\n",fp_ht
);
172 fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht
);
173 fputs("<tr><td> </td><td> </td></tr>",fp_ht
);
175 fprintf(fp_ht
,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",_("USERID"),_("AGENT"));
177 while(fgets(buf
,sizeof(buf
),fp_in
)!=NULL
) {
178 getword_start(&gwarea
,buf
);
179 if (getword(ip
,sizeof(ip
),&gwarea
,'\t')<0) {
180 debuga(_("There is an invalid IP address in file %s\n"),tmp2
);
185 if(strcmp(ip
,ipbefore
) != 0) {
187 ip2name(ip
,sizeof(ip
));
188 strcpy(namebefore
,ip
);
189 } else strcpy(ip
,namebefore
);
192 if (getword(agent
,sizeof(agent
),&gwarea
,'\t')<0) {
193 debuga(_("There is an invalid useragent in file %s\n"),tmp2
);
196 if (getword(user
,sizeof(user
),&gwarea
,'\t')<0) {
197 debuga(_("There is an invalid user ID in file %s\n"),tmp2
);
201 if(strcmp(user
,user_old
) != 0) {
202 fprintf(fp_ht
,"<tr><td class=\"data2\">%s</td><td class=\"data2\">",user
);
203 output_html_string(fp_ht
,agent
,250);
204 fputs("</td></tr>\n",fp_ht
);
205 strcpy(user_old
,user
);
206 strcpy(agent_old
,agent
);
207 } else if(strcmp(agent
,agent_old
) != 0) {
208 fputs("<tr><td></td><td class=\"data2\">",fp_ht
);
209 output_html_string(fp_ht
,agent
,250);
210 fputs("</td></tr>\n",fp_ht
);
211 strcpy(agent_old
,agent
);
215 fputs("</table>\n",fp_ht
);
218 if (snprintf(csort
,sizeof(csort
),"sort -t \"\t\" -k 2,2 -o \"%s\" \"%s\"",tmp3
,tmp2
)>=sizeof(csort
)) {
219 debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),tmp2
,tmp3
);
222 cstatus
=system(csort
);
223 if (!WIFEXITED(cstatus
) || WEXITSTATUS(cstatus
)) {
224 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus
));
225 debuga(_("sort command: %s\n"),csort
);
228 if((fp_in
=fopen(tmp3
,"r"))==NULL
) {
229 debuga(_("(useragent) Cannot open file %s\n"),tmp3
);
230 debuga(_("sort command: %s\n"),csort
);
234 if (!KeepTempLog
&& unlink(tmp2
)) {
235 debuga(_("Cannot delete \"%s\": %s\n"),tmp2
,strerror(errno
));
239 if((fp_ou
=fopen(tmp2
,"w"))==NULL
) {
240 debuga(_("(useragent) Cannot open file %s\n"),tmp2
);
247 while(fgets(buf
,sizeof(buf
),fp_in
)!=NULL
) {
248 getword_start(&gwarea
,buf
);
249 if (getword(ip
,sizeof(ip
),&gwarea
,'\t')<0) {
250 debuga(_("There is an invalid IP address in file %s\n"),tmp3
);
253 if (getword(agent
,sizeof(agent
),&gwarea
,'\t')<0) {
254 debuga(_("There is an invalid useragent in file %s\n"),tmp3
);
260 strcpy(agent_old
,agent
);
263 if(strcmp(agent
,agent_old
) != 0) {
265 fprintf(fp_ou
,"%06d %s\n",agentot
,agent_old
);
266 strcpy(agent_old
,agent
);
273 fprintf(fp_ou
,"%06d %s\n",agentot
,agent
);
277 if (fclose(fp_ou
)==EOF
) {
278 debuga(_("Failed to close file %s - %s\n"),tmp3
,strerror(errno
));
282 if (!KeepTempLog
&& unlink(tmp3
)) {
283 debuga(_("Cannot delete \"%s\": %s\n"),tmp3
,strerror(errno
));
287 if (snprintf(csort
,sizeof(csort
),"sort -n -r -k 1,1 -o \"%s\" \"%s\"",tmp3
,tmp2
)>=sizeof(csort
)) {
288 debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),tmp2
,tmp3
);
291 cstatus
=system(csort
);
292 if (!WIFEXITED(cstatus
) || WEXITSTATUS(cstatus
)) {
293 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus
));
294 debuga(_("sort command: %s\n"),csort
);
297 if((fp_in
=fopen(tmp3
,"r"))==NULL
) {
298 debuga(_("(useragent) Cannot open file %s\n"),tmp3
);
299 debuga(_("sort command: %s\n"),csort
);
303 if (!KeepTempLog
&& unlink(tmp2
)) {
304 debuga(_("Cannot delete \"%s\": %s\n"),tmp2
,strerror(errno
));
308 fputs("<br><br>\n",fp_ht
);
310 fputs("<table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht
);
311 fprintf(fp_ht
,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_c\">%%</th></tr>\n",_("AGENT"),_("TOTAL"));
314 while(fgets(buf
,sizeof(buf
),fp_in
)!=NULL
) {
316 getword_start(&gwarea
,buf
);
317 if (getword(tagent
,sizeof(tagent
),&gwarea
,' ')<0) {
318 debuga(_("There is an invalid useragent in file %s\n"),tmp3
);
322 perc
=(agentot2
>0) ? nagent
* 100. / agentot2
: 0.;
324 fputs("<tr><td class=\"data2\">",fp_ht
);
325 output_html_string(fp_ht
,gwarea
.current
,250);
326 fprintf(fp_ht
,"</td><td class=\"data\">%d</td><td class=\"data\">%3.2lf</td></tr>\n",nagent
,perc
);
330 fputs("</table></div>\n",fp_ht
);
331 if (write_html_trailer(fp_ht
)<0)
332 debuga(_("Write error in file %s\n"),hfile
);
333 if (fclose(fp_ht
)==EOF
)
334 debuga(_("Failed to close file %s - %s\n"),hfile
,strerror(errno
));
336 if (!KeepTempLog
&& unlink(tmp3
)) {
337 debuga(_("Cannot delete \"%s\": %s\n"),tmp3
,strerror(errno
));