]>
git.ipfire.org Git - thirdparty/sarg.git/blob - datafile.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"
28 void saverecs ( char * dirname
, char * user
, long long int nacc
, char * url
, long long int nbytes
, char * ip
, char * hora
, char * dia
, long long int nelap
, long long int incache
, long long int oucache
);
29 void grava_SmartFilter ( char * dirname
, char * user
, char * ip
, char * data
, char * hora
, char * url
, char * smart
);
31 void data_file ( char * tmp
)
36 char accdia
[ 11 ], acchora
[ 9 ], accuser
[ MAXLEN
], accip
[ MAXLEN
], accurl
[ MAXLEN
], accbytes
[ 12 ], accelap
[ 10 ];
37 char oldaccdia
[ 11 ], oldacchora
[ 9 ], oldaccip
[ MAXLEN
];
38 char dirname
[ MAXLEN
], wdirname
[ MAXLEN
], oldurl
[ MAXLEN
], oldaccuser
[ MAXLEN
];
39 char olduser
[ MAXLEN
], oldmsg
[ 50 ], acccode
[ 50 ], oldaccelap
[ 10 ], oldacccode
[ 50 ];
40 char ipantes
[ MAXLEN
], nameantes
[ MAXLEN
];
41 char accsmart
[ MAXLEN
];
48 struct dirent
* direntp
;
49 long long int nbytes
= 0 ;
50 long long int nelap
= 0 ;
52 long long int rtotal
= 0 ;
53 long long int incache
= 0 ;
54 long long int oucache
= 0 ;
60 strncat ( tmp
, "/sarg" , 5 );
63 while ( ( direntp
= readdir ( dirp
)) != NULL
) {
64 if ( strstr ( direntp
-> d_name
, ".log" ) == 0 )
66 sprintf ( tmp3
, "%s/%s" , tmp
, direntp
-> d_name
);
68 if (( fp_in
= fopen ( tmp3
, "r" ))== NULL
){
69 fprintf ( stderr
, "SARG: (datafile) %s: %s \n " , text
[ 45 ], tmp
);
72 strcpy ( wdname
, direntp
-> d_name
);
74 if ( getword ( wname2
, sizeof ( wname2
), wdname
, '.' )< 0 ) {
75 printf ( "SARG: Maybe you have a broken record or garbage in your %s directory. \n " , tmp
);
81 while ( fgets ( buf
, sizeof ( buf
), fp_in
)!= NULL
) {
82 if ( getword ( accdia
, sizeof ( accdia
), buf
, ' ' )< 0 || getword ( acchora
, sizeof ( acchora
), buf
, ' ' )< 0 ||
83 getword ( accuser
, sizeof ( accuser
), buf
, ' ' )< 0 || getword ( accip
, sizeof ( accip
), buf
, ' ' )< 0 ||
84 getword ( accurl
, sizeof ( accurl
), buf
, ' ' )< 0 || getword ( accbytes
, sizeof ( accbytes
), buf
, ' ' )< 0 ||
85 getword ( acccode
, sizeof ( acccode
), buf
, ' ' )< 0 || getword ( accelap
, sizeof ( accelap
), buf
, ' ' )< 0 ||
86 getword ( accsmart
, sizeof ( accsmart
), buf
, ' ' )< 0 || getword ( accsmart
, sizeof ( accsmart
), buf
, '"' )< 0 ) {
87 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , tmp3
);
91 if ( strcmp ( Ip2Name
, "yes" ) == 0 ) {
92 if ( strcmp ( accip
, ipantes
) != 0 ) {
93 strcpy ( ipantes
, accip
);
95 strcpy ( nameantes
, accip
);
97 else strcpy ( accip
, nameantes
);
101 strcpy ( oldurl
, accurl
);
102 strcpy ( oldacccode
, acccode
);
103 strcpy ( oldaccelap
, accelap
);
104 strcpy ( oldaccuser
, accuser
);
105 strcpy ( oldaccip
, accip
);
106 strcpy ( oldaccdia
, accdia
);
107 strcpy ( oldacchora
, acchora
);
111 if ( strcmp ( oldurl
, accurl
) != 0 || strcmp ( oldaccuser
, accuser
) != 0 ){
113 if ( strstr ( oldacccode
, "DENIED" ) != 0 ) strcpy ( oldmsg
, text
[ 46 ]);
114 strcpy ( wdirname
, dirname
);
115 gravatmp ( oldaccuser
, wdirname
, oldurl
, nacc
, nbytes
, oldmsg
, nelap
, indexonly
, incache
, oucache
);
116 strcpy ( wdirname
, dirname
);
117 saverecs ( wdirname
, oldaccuser
, nacc
, oldurl
, nbytes
, oldaccip
, oldacchora
, oldaccdia
, nelap
, incache
, oucache
);
123 if ( strcmp ( oldaccuser
, accuser
) != 0 ) ind2
= 0 ;
127 nbytes
+= my_atoll ( accbytes
);
128 nelap
+= my_atoll ( accelap
);
130 strcpy ( wcrc
, acccode
);
131 if ( getword ( crc2
, sizeof ( crc2
), wcrc
, '/' )< 0 ) {
132 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , tmp3
);
136 if ( strstr ( crc2
, "MISS" ) != 0 ) oucache
+= my_atoll ( accbytes
);
137 else incache
+= my_atoll ( accbytes
);
139 strcpy ( oldurl
, accurl
);
140 strcpy ( oldaccuser
, accuser
);
141 strcpy ( oldacccode
, acccode
);
142 strcpy ( oldaccelap
, accelap
);
143 strcpy ( oldaccip
, accip
);
144 strcpy ( oldaccdia
, accdia
);
145 strcpy ( oldacchora
, acchora
);
151 ( void ) closedir ( dirp
);
153 sprintf ( Msg
, "Datafile %s successfully" , DataFile
);
158 void saverecs ( char * dirname
, char * user
, long long int nacc
, char * url
, long long int nbytes
, char * ip
, char * hora
, char * dia
, long long int nelap
, long long int incache
, long long int oucache
)
164 if (( fp_ou
= fopen ( DataFile
, "a" ))== NULL
){
165 fprintf ( stderr
, "SARG: (datafile) %s: %s \n " , text
[ 45 ], DataFile
);
169 my_lltoa ( nacc
, val1
, 0 );
170 my_lltoa ( nbytes
, val2
, 0 );
171 my_lltoa ( nelap
, val3
, 0 );
172 my_lltoa ( incache
, val4
, 0 );
173 my_lltoa ( oucache
, val5
, 0 );
175 if ( strstr ( DataFileFields
, "user" ) != 0 ) {
177 strncat ( reg
, DataFileDelimiter
, 1 );
179 if ( strstr ( DataFileFields
, "date" ) != 0 ) {
180 strncat ( reg
, dia
, strlen ( dia
));
181 strncat ( reg
, DataFileDelimiter
, 1 );
183 if ( strstr ( DataFileFields
, "time" ) != 0 ) {
184 strncat ( reg
, hora
, strlen ( hora
));
185 strncat ( reg
, DataFileDelimiter
, 1 );
187 if ( strstr ( DataFileFields
, "url" ) != 0 ) {
189 if ( strcmp ( DataFileUrl
, "ip" ) == 0 ) name2ip ( name
);
190 strncat ( reg
, name
, strlen ( name
));
191 strncat ( reg
, DataFileDelimiter
, 1 );
193 if ( strstr ( DataFileFields
, "connect" ) != 0 ) {
194 strncat ( reg
, val1
, strlen ( val1
));
195 strncat ( reg
, DataFileDelimiter
, 1 );
197 if ( strstr ( DataFileFields
, "bytes" ) != 0 ) {
198 strncat ( reg
, val2
, strlen ( val2
));
199 strncat ( reg
, DataFileDelimiter
, 1 );
201 if ( strstr ( DataFileFields
, "in_cache" ) != 0 ) {
202 strncat ( reg
, val4
, strlen ( val4
));
203 strncat ( reg
, DataFileDelimiter
, 1 );
205 if ( strstr ( DataFileFields
, "out_cache" ) != 0 ) {
206 strncat ( reg
, val5
, strlen ( val5
));
207 strncat ( reg
, DataFileDelimiter
, 1 );
209 if ( strstr ( DataFileFields
, "elapsed" ) != 0 ) {
210 strncat ( reg
, val3
, strlen ( val3
));
211 strncat ( reg
, DataFileDelimiter
, 1 );
214 reg
[ strlen ( reg
)- 1 ]= ' \n ' ;