]>
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 int agentot
= 0 , agentot2
= 0 , agentdif
= 0 , cont
= 0 , nagent
;
43 unsigned long totregsl
= 0 ;
46 struct getwordstruct gwarea
;
57 sprintf ( tmp3
, "%s/squagent.unsort" , TempDir
);
58 sprintf ( tmp2
, "%s/squagent.log" , TempDir
);
60 if (( fp_in
= fopen ( UserAgentLog
, "r" ))== NULL
) {
61 fprintf ( stderr
, "SARG: (useragent) %s: %s \n " , text
[ 45 ], UserAgentLog
);
65 if (( fp_ou
= fopen ( tmp3
, "w" ))== NULL
) {
66 fprintf ( stderr
, "SARG: (email) %s: %s \n " , text
[ 45 ], tmp3
);
71 debuga ( "%s: %s" , text
[ 66 ], UserAgentLog
);
74 while ( fgets ( buf
, sizeof ( buf
), fp_in
)!= NULL
) {
76 getword_start (& gwarea
, buf
);
77 if ( getword ( ip
, sizeof ( ip
),& gwarea
, ' ' )< 0 || getword_skip ( MAXLEN
,& gwarea
, '[' )< 0 ||
78 getword ( data
, sizeof ( data
),& gwarea
, ' ' )< 0 ) {
79 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , UserAgentLog
);
84 if ( getword_skip ( MAXLEN
,& gwarea
, '"' )< 0 || getword ( agent
, sizeof ( agent
),& gwarea
, '"' )< 0 ) {
85 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , UserAgentLog
);
90 if ( strstr ( warea
, "SCRIPT" ) != 0 || strstr ( warea
, "ONLOAD" ) != 0 )
93 if ( gwarea
. current
[ 0 ]!= '\0' ) {
94 if ( getword_skip ( MAXLEN
,& gwarea
, ' ' )< 0 || getword ( user
, sizeof ( user
),& gwarea
, ' \n ' )< 0 ) {
95 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , UserAgentLog
);
105 fprintf ( fp_ou
, "%s \t %s \t %s \n " , ip
, agent
, user
);
112 debuga ( " %s: %ld" , text
[ 10 ], totregsl
);
119 debuga ( "%s: %s" , text
[ 54 ], tmp2
);
122 sprintf ( csort
, "sort -n -t \"\t\" -k 3,3 -k 2,2 -k 1,1 -o \" %s \" \" %s \" " , tmp2
, tmp3
);
123 cstatus
= system ( csort
);
124 if (! WIFEXITED ( cstatus
) || WEXITSTATUS ( cstatus
)) {
125 fprintf ( stderr
, "SARG: sort command return status %d \n " , WEXITSTATUS ( cstatus
));
126 fprintf ( stderr
, "SARG: sort command: %s \n " , csort
);
129 if (( fp_in
= fopen ( tmp2
, "r" ))== NULL
) {
130 fprintf ( stderr
, "SARG: (useragent) %s: %s \n " , text
[ 45 ], tmp2
);
131 fprintf ( stderr
, "SARG: sort command: %s \n " , csort
);
137 sprintf ( hfile
, "%s/%s/useragent.html" , outdir
, period
);
138 if (( fp_ht
= fopen ( hfile
, "w" ))== NULL
) {
139 fprintf ( stderr
, "SARG: (useragent) %s: %s \n " , text
[ 45 ], hfile
);
144 debuga ( "%s" , text
[ 72 ]);
146 fprintf ( fp_ht
, "<!DOCTYPE HTML PUBLIC \" -//W3C//DTD HTML 4.01 Transitional//EN \" \" http://www.w3.org/TR/html4/loose.dtd \" > \n <html> \n <head> \n <meta http-equiv= \" Content-Type \" content= \" text/html; charset=%s \" > \n " , CharSet
);
148 fputs ( "</head> \n " , fp_ht
);
149 //if(FontFace[0] != '\0') fprintf(fp_ht,"<font face=\"%s\">\n",FontFace);
150 fprintf ( fp_ht
, "<body bgcolor= \" %s \" text= \" %s \" background= \" %s \" > \n " , BgColor
, TxColor
, BgImage
);
151 write_logo_image ( fp_ht
);
153 if ( IndexTree
== INDEX_TREE_DATE
)
154 show_sarg ( fp_ht
, "../../.." );
156 show_sarg ( fp_ht
, ".." );
157 fputs ( "<div align= \" center \" ><table cellpadding= \" 0 \" cellspacing= \" 0 \" > \n " , fp_ht
);
158 fprintf ( fp_ht
, "<tr><th align= \" center \" colspan= \" 2 \" ><b><font color= \" %s \" size= \" +1 \" >%s</font></b></th></tr> \n " , TiColor
, text
[ 105 ]);
160 fputs ( "<tr><td></td></tr><tr><td></td></tr>" , fp_ht
);
161 fputs ( "</table></div> \n " , fp_ht
);
163 fputs ( "<div align= \" center \" ><table cellpadding= \" 0 \" cellspacing= \" 0 \" > \n " , fp_ht
);
165 fprintf ( fp_ht
, "<tr><td align= \" right \" bgcolor= \" %s \" >%s:<td align= \" left \" bgcolor= \" %s \" >%s - %s</td></td></tr> \n " , HeaderBgColor
, text
[ 89 ], TxBgColor
, idate
, fdate
);
167 fputs ( "</table></div> \n " , fp_ht
);
168 fputs ( "<div align= \" center \" ><table cellpadding= \" 0 \" cellspacing= \" 0 \" > \n " , fp_ht
);
169 fputs ( "<tr><td></td><td></td></tr>" , fp_ht
);
171 fprintf ( fp_ou
, "<tr><th align= \" left \" bgcolor= \" %s \" ><font color= \" %s \" >%s</font></th><th bgcolor= \" %s \" align= \" left \" ><font color= \" %s \" >%s</font></th></tr> \n " , HeaderBgColor
, HeaderColor
, text
[ 98 ], HeaderBgColor
, HeaderColor
, text
[ 106 ]);
173 while ( fgets ( buf
, sizeof ( buf
), fp_in
)!= NULL
) {
174 getword_start (& gwarea
, buf
);
175 if ( getword ( ip
, sizeof ( ip
),& gwarea
, ' \t ' )< 0 ) {
176 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , tmp2
);
181 if ( strcmp ( ip
, ipbefore
) != 0 ) {
183 ip2name ( ip
, sizeof ( ip
));
184 strcpy ( namebefore
, ip
);
185 } else strcpy ( ip
, namebefore
);
188 if ( getword ( agent
, sizeof ( agent
),& gwarea
, ' \t ' )< 0 ||
189 getword ( user
, sizeof ( user
),& gwarea
, ' \t ' )< 0 ) {
190 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , tmp2
);
194 if ( strcmp ( user
, user_old
) != 0 ) {
195 fprintf ( fp_ht
, "<tr><td align= \" left \" bgcolor= \" %s \" >%s</td><td align= \" left \" bgcolor= \" %s \" >%s</td></tr> \n " , TxBgColor
, user
, TxBgColor
, agent
);
196 strcpy ( user_old
, user
);
197 strcpy ( agent_old
, agent
);
199 if ( strcmp ( agent
, agent_old
) != 0 ) {
200 fprintf ( fp_ht
, "<tr><td></td><td align= \" left \" bgcolor= \" %s \" >%s</td></tr> \n " , TxBgColor
, agent
);
201 strcpy ( agent_old
, agent
);
206 fputs ( "</table> \n " , fp_ht
);
210 sprintf ( csort
, "sort -t \"\t\" -k 2,2 -o \" %s \" \" %s \" " , tmp3
, tmp2
);
211 cstatus
= system ( csort
);
212 if (! WIFEXITED ( cstatus
) || WEXITSTATUS ( cstatus
)) {
213 fprintf ( stderr
, "SARG: sort command return status %d \n " , WEXITSTATUS ( cstatus
));
214 fprintf ( stderr
, "SARG: sort command: %s \n " , csort
);
217 if (( fp_in
= fopen ( tmp3
, "r" ))== NULL
) {
218 fprintf ( stderr
, "SARG: (useragent) %s: %s \n " , text
[ 45 ], tmp3
);
219 fprintf ( stderr
, "SARG: sort command: %s \n " , csort
);
225 if (( fp_ou
= fopen ( tmp2
, "w" ))== NULL
) {
226 fprintf ( stderr
, "SARG: (useragent) %s: %s \n " , text
[ 45 ], tmp2
);
233 while ( fgets ( buf
, sizeof ( buf
), fp_in
)!= NULL
) {
234 getword_start (& gwarea
, buf
);
235 if ( getword ( ip
, sizeof ( ip
),& gwarea
, ' \t ' )< 0 ||
236 getword ( agent
, sizeof ( agent
),& gwarea
, ' \t ' )< 0 ) {
237 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , tmp
);
243 strcpy ( agent_old
, agent
);
246 if ( strcmp ( agent
, agent_old
) != 0 ) {
248 fprintf ( fp_ou
, "%06d %s \n " , agentot
, agent_old
);
249 strcpy ( agent_old
, agent
);
256 fprintf ( fp_ou
, "%06d %s \n " , agentot
, agent
);
264 sprintf ( csort
, "sort -n -r -k 1,1 -o \" %s \" \" %s \" " , tmp3
, tmp2
);
265 cstatus
= system ( csort
);
266 if (! WIFEXITED ( cstatus
) || WEXITSTATUS ( cstatus
)) {
267 fprintf ( stderr
, "SARG: sort command return status %d \n " , WEXITSTATUS ( cstatus
));
268 fprintf ( stderr
, "SARG: sort command: %s \n " , csort
);
271 if (( fp_in
= fopen ( tmp3
, "r" ))== NULL
) {
272 fprintf ( stderr
, "SARG: (useragent) %s: %s \n " , text
[ 45 ], tmp3
);
273 fprintf ( stderr
, "SARG: sort command: %s \n " , csort
);
279 if (( fp_ht
= fopen ( hfile
, "a" ))== NULL
) {
280 fprintf ( stderr
, "SARG: (useragent) %s: %s \n " , text
[ 45 ], hfile
);
284 fputs ( "<br><br> \n " , fp_ht
);
286 fputs ( "<div align= \" center \" ><table cellpadding= \" 0 \" cellspacing= \" 0 \" > \n " , fp_ht
);
287 fprintf ( fp_ht
, "<tr><th align= \" left \" bgcolor= \" %s \" ><font color= \" %s \" >%s</font></th><th bgcolor= \" %s \" align= \" left \" ><font color= \" %s \" >%s</font></th><th align= \" center \" bgcolor= \" %s \" ><font color= \" %s \" >%%</font></th></tr> \n " , HeaderBgColor
, HeaderColor
, text
[ 106 ], HeaderBgColor
, HeaderColor
, text
[ 107 ], HeaderBgColor
, HeaderColor
);
290 while ( fgets ( buf
, sizeof ( buf
), fp_in
)!= NULL
) {
292 getword_start (& gwarea
, buf
);
293 if ( getword ( tagent
, sizeof ( tagent
),& gwarea
, ' ' )< 0 ) {
294 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , tmp
);
300 perc
= perc
/ agentot2
;
303 fprintf ( fp_ht
, "<tr><td align= \" left \" bgcolor= \" %s \" >%s</td><td align= \" right \" bgcolor= \" %s \" >%d</td><td align= \" right \" bgcolor= \" %s \" >%3.2f</td></tr> \n " , TxBgColor
, gwarea
. current
, TxBgColor
, nagent
, TxBgColor
, perc
);
306 fputs ( "</table></html> \n " , fp_ht
);
308 zdate ( ftime
, sizeof ( ftime
), DateFormat
);
309 fprintf ( fp_ht
, "<br><br><div align= \" center \" ><font size= \" -2 \" >%s <a href= \" %s \" >%s-%s</a> %s %s</font></div> \n " , text
[ 108 ], URL
, PGM
, VERSION
, text
[ 109 ], ftime
);
312 fputs ( "</table> \n </body> \n </html> \n " , fp_ht
);