]>
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"
31 FILE * fp_in
= NULL
, * fp_ou
= NULL
, * fp_ht
= NULL
;
32 char ip
[ MAXLEN
], data
[ MAXLEN
], agent
[ MAXLEN
], user
[ MAXLEN
];
33 char ipbefore
[ MAXLEN
], namebefore
[ MAXLEN
];
35 char user_old
[ MAXLEN
]= "$#%0a3bc6" ;
36 char agent_old
[ MAXLEN
]= "$#%0a3bc6" ;
38 char idate
[ MAXLEN
], fdate
[ MAXLEN
];
39 int agentot
= 0 , agentot2
= 0 , agentdif
= 0 , cont
= 0 ;
40 unsigned long totregsl
= 0 ;
52 sprintf ( hfile
, "%s/%s/useragent.html" , outdir
, period
);
54 sprintf ( tmp3
, "%s/squagent.unsort" , TempDir
);
55 sprintf ( tmp2
, "%s/squagent.log" , TempDir
);
57 if (( fp_in
= fopen ( UserAgentLog
, "r" ))== NULL
) {
58 fprintf ( stderr
, "SARG: (useragent) %s: %s \n " , text
[ 45 ], UserAgentLog
);
62 if (( fp_ou
= fopen ( tmp3
, "w" ))== NULL
) {
63 fprintf ( stderr
, "SARG: (email) %s: %s \n " , text
[ 45 ], tmp3
);
68 sprintf ( msg
, "%s: %s" , text
[ 66 ], UserAgentLog
);
72 while ( fgets ( buf
, sizeof ( buf
), fp_in
)!= NULL
) {
74 if ( getword ( ip
, sizeof ( ip
), buf
, ' ' )< 0 || getword ( data
, sizeof ( data
), buf
, '[' )< 0 ||
75 getword ( data
, sizeof ( data
), buf
, ' ' )< 0 ) {
76 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , UserAgentLog
);
81 if ( getword ( agent
, sizeof ( agent
), buf
, '"' )< 0 || getword ( agent
, sizeof ( agent
), buf
, '"' )< 0 ) {
82 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , UserAgentLog
);
87 if ( strstr ( warea
, "SCRIPT" ) != 0 || strstr ( warea
, "ONLOAD" ) != 0 )
91 if ( getword ( user
, sizeof ( user
), buf
, ' ' )< 0 || getword ( user
, sizeof ( user
), buf
, ' \n ' )< 0 ) {
92 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , UserAgentLog
);
102 sprintf ( buf
, "%s \\ %s \\ %s \\ %s \\\n " , ip
, data
, agent
, user
);
110 sprintf ( msg
, " %s: %ld" , text
[ 10 ], totregsl
);
122 sprintf ( msg
, "%s: %s" , text
[ 54 ], tmp2
);
126 sprintf ( csort
, "sort -n -t ' \\ ' -k 4,4 -k 3,3 -k 2,2 -k 1,1 -o '%s' '%s'" , tmp2
, tmp3
);
127 cstatus
= system ( csort
);
128 if (! WIFEXITED ( cstatus
) || WEXITSTATUS ( cstatus
)) {
129 fprintf ( stderr
, "SARG: sort command return status %d \n " , WEXITSTATUS ( cstatus
));
130 fprintf ( stderr
, "SARG: sort command: %s \n " , csort
);
133 if (( fp_in
= fopen ( tmp2
, "r" ))== NULL
) {
134 fprintf ( stderr
, "SARG: (useragent) %s: %s \n " , text
[ 45 ], tmp2
);
135 fprintf ( stderr
, "SARG: sort command: %s \n " , csort
);
141 if (( fp_ht
= fopen ( hfile
, "w" ))== NULL
) {
142 fprintf ( stderr
, "SARG: (useragent) %s: %s \n " , text
[ 45 ], hfile
);
149 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
);
150 fputs ( "</head> \n " , fp_ht
);
151 if ( strlen ( FontFace
) > 0 ) fprintf ( fp_ht
, "<font face=%s> \n " , FontFace
);
152 fprintf ( fp_ht
, "<body bgcolor=%s text=%s background='%s'> \n " , BgColor
, TxColor
, BgImage
);
153 if ( strlen ( LogoImage
) > 0 ) fprintf ( fp_ht
, "<center><table cellpadding= \" 0 \" cellspacing= \" 0 \" > \n <tr><th class= \" logo \" ><img src='%s' border=0 align=absmiddle width=%s height=%s> %s</th></tr> \n <tr><td height= \" 5 \" ></td></tr> \n </table> \n " , LogoImage
, Width
, Height
, LogoText
);
155 if ( strcmp ( IndexTree
, "date" ) == 0 )
156 show_sarg ( fp_ht
, "../../.." );
158 show_sarg ( fp_ht
, ".." );
159 fputs ( "<center><table cellpadding=0 cellspacing=0> \n " , fp_ht
);
160 sprintf ( html
, "<tr><th align=center colspan=2><b><font color=%s size=+1>%s</font></b></th></tr> \n " , TiColor
, text
[ 105 ]);
163 fputs ( "<tr><td></td></tr><tr><td></td></tr>" , fp_ht
);
164 fputs ( "</table></center> \n " , fp_ht
);
166 fputs ( "<center><table cellpadding=0 cellspacing=0> \n " , fp_ht
);
168 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
);
171 fputs ( "</table></center> \n " , fp_ht
);
172 fputs ( "<center><table cellpadding=0 cellspacing=0> \n " , fp_ht
);
173 fputs ( "<tr><td></td><td></td></tr>" , fp_ht
);
175 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 ]);
178 while ( fgets ( buf
, sizeof ( buf
), fp_in
)!= NULL
) {
179 if ( getword ( ip
, sizeof ( ip
), buf
, ' \\ ' )< 0 ) {
180 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , tmp2
);
184 if ( strcmp ( Ip2Name
, "yes" ) == 0 ) {
185 if ( strcmp ( ip
, ipbefore
) != 0 ) {
188 strcpy ( namebefore
, ip
);
189 } else strcpy ( ip
, namebefore
);
192 if ( getword ( data
, sizeof ( data
), buf
, ' \\ ' )< 0 || getword ( agent
, sizeof ( agent
), buf
, ' \\ ' )< 0 ||
193 getword ( user
, sizeof ( user
), buf
, ' \\ ' )< 0 ) {
194 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , tmp2
);
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'" , tmp3
, tmp2
);
217 cstatus
= system ( csort
);
218 if (! WIFEXITED ( cstatus
) || WEXITSTATUS ( cstatus
)) {
219 fprintf ( stderr
, "SARG: sort command return status %d \n " , WEXITSTATUS ( cstatus
));
220 fprintf ( stderr
, "SARG: sort command: %s \n " , csort
);
223 if (( fp_in
= fopen ( tmp3
, "r" ))== NULL
) {
224 fprintf ( stderr
, "SARG: (useragent) %s: %s \n " , text
[ 45 ], tmp3
);
225 fprintf ( stderr
, "SARG: sort command: %s \n " , csort
);
231 if (( fp_ou
= fopen ( tmp2
, "w" ))== NULL
) {
232 fprintf ( stderr
, "SARG: (useragent) %s: %s \n " , text
[ 45 ], tmp2
);
238 while ( fgets ( buf
, sizeof ( buf
), fp_in
)!= NULL
) {
239 if ( getword ( ip
, sizeof ( ip
), buf
, ' \\ ' )< 0 || getword ( data
, sizeof ( data
), buf
, ' \\ ' )< 0 ||
240 getword ( agent
, sizeof ( agent
), buf
, ' \\ ' )< 0 ) {
241 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , tmp
);
247 strcpy ( agent_old
, agent
);
252 if ( strcmp ( agent
, agent_old
) != 0 ) {
254 sprintf ( html
, "%06d %s \n " , agentot
, agent_old
);
256 strcpy ( agent_old
, agent
);
262 sprintf ( html
, "%06d %s \n " , agentot
, agent
);
271 sprintf ( csort
, "sort -n -r -k 1,1 -o '%s' '%s'" , tmp3
, tmp2
);
272 cstatus
= system ( csort
);
273 if (! WIFEXITED ( cstatus
) || WEXITSTATUS ( cstatus
)) {
274 fprintf ( stderr
, "SARG: sort command return status %d \n " , WEXITSTATUS ( cstatus
));
275 fprintf ( stderr
, "SARG: sort command: %s \n " , csort
);
278 if (( fp_in
= fopen ( tmp3
, "r" ))== NULL
) {
279 fprintf ( stderr
, "SARG: (useragent) %s: %s \n " , text
[ 45 ], tmp3
);
280 fprintf ( stderr
, "SARG: sort command: %s \n " , csort
);
286 if (( fp_ht
= fopen ( hfile
, "a" ))== NULL
) {
287 fprintf ( stderr
, "SARG: (useragent) %s: %s \n " , text
[ 45 ], hfile
);
291 fputs ( "<br><br> \n " , fp_ht
);
293 fputs ( "<center><table cellpadding=0 cellspacing=0> \n " , fp_ht
);
294 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
);
297 while ( fgets ( buf
, sizeof ( buf
), fp_in
)!= NULL
) {
298 buf
[ strlen ( buf
)- 1 ]= '\0' ;
299 if ( getword ( tagent
, sizeof ( tagent
), buf
, ' ' )< 0 ) {
300 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , tmp
);
303 perc
= atoi ( tagent
) * 100 ;
304 perc
= perc
/ agentot2
;
306 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
);
310 fputs ( "</table></html> \n " , fp_ht
);
311 if ( strcmp ( ShowSargInfo
, "yes" ) == 0 ) {
312 zdate ( ftime
, DateFormat
);
313 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
);
317 fputs ( "</table> \n </body> \n </html> \n " , fp_ht
);