]>
git.ipfire.org Git - thirdparty/sarg.git/blob - useragent.c
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
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"
27 #include "include/defs.h"
32 FILE * fp_in
= NULL
, * fp_ou
= NULL
, * fp_ht
= NULL
;
33 char ip
[ MAXLEN
], data
[ MAXLEN
], agent
[ MAXLEN
], user
[ MAXLEN
];
34 char ipbefore
[ MAXLEN
], namebefore
[ MAXLEN
];
36 char user_old
[ MAXLEN
]= "$#%0a3bc6" ;
37 char agent_old
[ MAXLEN
]= "$#%0a3bc6" ;
39 char idate
[ MAXLEN
], fdate
[ MAXLEN
];
40 int agentot
= 0 , agentot2
= 0 , agentdif
= 0 , cont
= 0 ;
41 unsigned long totregsl
= 0 ;
54 sprintf ( hfile
, "%s/%s/useragent.html" , outdir
, period
);
56 sprintf ( tmp3
, "%s/squagent.unsort" , TempDir
);
57 sprintf ( tmp2
, "%s/squagent.log" , TempDir
);
59 if (( fp_in
= fopen ( UserAgentLog
, "r" ))== NULL
) {
60 fprintf ( stderr
, "SARG: (useragent) %s: %s \n " , text
[ 45 ], UserAgentLog
);
64 if (( fp_ou
= fopen ( tmp3
, "w" ))== NULL
) {
65 fprintf ( stderr
, "SARG: (email) %s: %s \n " , text
[ 45 ], tmp3
);
70 debuga ( "%s: %s" , text
[ 66 ], UserAgentLog
);
73 while ( fgets ( buf
, sizeof ( buf
), fp_in
)!= NULL
) {
75 if ( getword ( ip
, sizeof ( ip
), buf
, ' ' )< 0 || getword ( data
, sizeof ( data
), buf
, '[' )< 0 ||
76 getword ( data
, sizeof ( data
), buf
, ' ' )< 0 ) {
77 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , UserAgentLog
);
82 if ( getword ( agent
, sizeof ( agent
), buf
, '"' )< 0 || getword ( agent
, sizeof ( agent
), buf
, '"' )< 0 ) {
83 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , UserAgentLog
);
88 if ( strstr ( warea
, "SCRIPT" ) != 0 || strstr ( warea
, "ONLOAD" ) != 0 )
92 if ( getword ( user
, sizeof ( user
), buf
, ' ' )< 0 || getword ( user
, sizeof ( user
), buf
, ' \n ' )< 0 ) {
93 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , UserAgentLog
);
100 if ( strlen ( user
) == 0 )
103 fprintf ( fp_ou
, "%s \\ %s \\ %s \\ %s \\\n " , ip
, data
, agent
, user
);
110 debuga ( " %s: %ld" , text
[ 10 ], totregsl
);
121 debuga ( "%s: %s" , text
[ 54 ], tmp2
);
124 sprintf ( csort
, "sort -n -t \" \/ \" -k 4,4 -k 3,3 -k 2,2 -k 1,1 -o \" %s \" \" %s \" " , tmp2
, tmp3
);
125 cstatus
= system ( csort
);
126 if (! WIFEXITED ( cstatus
) || WEXITSTATUS ( cstatus
)) {
127 fprintf ( stderr
, "SARG: sort command return status %d \n " , WEXITSTATUS ( cstatus
));
128 fprintf ( stderr
, "SARG: sort command: %s \n " , csort
);
131 if (( fp_in
= fopen ( tmp2
, "r" ))== NULL
) {
132 fprintf ( stderr
, "SARG: (useragent) %s: %s \n " , text
[ 45 ], tmp2
);
133 fprintf ( stderr
, "SARG: sort command: %s \n " , csort
);
139 if (( fp_ht
= fopen ( hfile
, "w" ))== NULL
) {
140 fprintf ( stderr
, "SARG: (useragent) %s: %s \n " , text
[ 45 ], hfile
);
145 debuga ( "%s" , text
[ 72 ]);
147 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 ( strlen ( FontFace
) > 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 ( strcmp ( IndexTree
, "date" ) == 0 )
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 \" ><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
);
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 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 ]);
173 while ( fgets ( buf
, sizeof ( buf
), fp_in
)!= NULL
) {
174 if ( getword ( ip
, sizeof ( ip
), buf
, ' \\ ' )< 0 ) {
175 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , tmp2
);
179 if ( strcmp ( Ip2Name
, "yes" ) == 0 ) {
180 if ( strcmp ( ip
, ipbefore
) != 0 ) {
182 ip2name ( ip
, sizeof ( ip
));
183 strcpy ( namebefore
, ip
);
184 } else strcpy ( ip
, namebefore
);
187 if ( getword ( data
, sizeof ( data
), buf
, ' \\ ' )< 0 || getword ( agent
, sizeof ( agent
), buf
, ' \\ ' )< 0 ||
188 getword ( user
, sizeof ( user
), buf
, ' \\ ' )< 0 ) {
189 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , tmp2
);
193 if ( strcmp ( user
, user_old
) != 0 ) {
194 fprintf ( fp_ht
, "<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
);
195 strcpy ( user_old
, user
);
196 strcpy ( agent_old
, agent
);
198 if ( strcmp ( agent
, agent_old
) != 0 ) {
199 fprintf ( fp_ht
, "<tr><td></td><td align=left bgcolor=%s><font size=%s>%s</td></tr> \n " , TxBgColor
, FontSize
, agent
);
200 strcpy ( agent_old
, agent
);
205 fputs ( "</table> \n " , fp_ht
);
209 sprintf ( csort
, "sort -n -t \" \/ \" -k 3,3 -o \" %s \" \" %s \" " , tmp3
, tmp2
);
210 cstatus
= system ( csort
);
211 if (! WIFEXITED ( cstatus
) || WEXITSTATUS ( cstatus
)) {
212 fprintf ( stderr
, "SARG: sort command return status %d \n " , WEXITSTATUS ( cstatus
));
213 fprintf ( stderr
, "SARG: sort command: %s \n " , csort
);
216 if (( fp_in
= fopen ( tmp3
, "r" ))== NULL
) {
217 fprintf ( stderr
, "SARG: (useragent) %s: %s \n " , text
[ 45 ], tmp3
);
218 fprintf ( stderr
, "SARG: sort command: %s \n " , csort
);
224 if (( fp_ou
= fopen ( tmp2
, "w" ))== NULL
) {
225 fprintf ( stderr
, "SARG: (useragent) %s: %s \n " , text
[ 45 ], tmp2
);
231 while ( fgets ( buf
, sizeof ( buf
), fp_in
)!= NULL
) {
232 if ( getword ( ip
, sizeof ( ip
), buf
, ' \\ ' )< 0 || getword ( data
, sizeof ( data
), buf
, ' \\ ' )< 0 ||
233 getword ( agent
, sizeof ( agent
), buf
, ' \\ ' )< 0 ) {
234 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , tmp
);
240 strcpy ( agent_old
, agent
);
245 if ( strcmp ( agent
, agent_old
) != 0 ) {
247 fprintf ( fp_ou
, "%06d %s \n " , agentot
, agent_old
);
248 strcpy ( agent_old
, agent
);
254 fprintf ( fp_ou
, "%06d %s \n " , agentot
, agent
);
262 sprintf ( csort
, "sort -n -r -k 1,1 -o \" %s \" \" %s \" " , tmp3
, tmp2
);
263 cstatus
= system ( csort
);
264 if (! WIFEXITED ( cstatus
) || WEXITSTATUS ( cstatus
)) {
265 fprintf ( stderr
, "SARG: sort command return status %d \n " , WEXITSTATUS ( cstatus
));
266 fprintf ( stderr
, "SARG: sort command: %s \n " , csort
);
269 if (( fp_in
= fopen ( tmp3
, "r" ))== NULL
) {
270 fprintf ( stderr
, "SARG: (useragent) %s: %s \n " , text
[ 45 ], tmp3
);
271 fprintf ( stderr
, "SARG: sort command: %s \n " , csort
);
277 if (( fp_ht
= fopen ( hfile
, "a" ))== NULL
) {
278 fprintf ( stderr
, "SARG: (useragent) %s: %s \n " , text
[ 45 ], hfile
);
282 fputs ( "<br><br> \n " , fp_ht
);
284 fputs ( "<div align= \" center \" ><table cellpadding= \" 0 \" cellspacing= \" 0 \" > \n " , fp_ht
);
285 fprintf ( fp_ht
, "<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
);
287 while ( fgets ( buf
, sizeof ( buf
), fp_in
)!= NULL
) {
289 if ( getword ( tagent
, sizeof ( tagent
), buf
, ' ' )< 0 ) {
290 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , tmp
);
293 perc
= atoi ( tagent
) * 100 ;
294 perc
= perc
/ agentot2
;
296 fprintf ( fp_ht
, "<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
);
299 fputs ( "</table></html> \n " , fp_ht
);
300 if ( strcmp ( ShowSargInfo
, "yes" ) == 0 ) {
301 zdate ( ftime
, sizeof ( ftime
), DateFormat
);
302 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
);
305 fputs ( "</table> \n </body> \n </html> \n " , fp_ht
);