]>
git.ipfire.org Git - thirdparty/sarg.git/blob - datafile.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"
30 void saverecs ( FILE * fp_ou
, const struct userinfostruct
* uinfo
, 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
);
32 void data_file ( char * tmp
)
35 FILE * fp_in
, * fp_ou
= NULL
;
38 char accdia
[ 11 ], acchora
[ 9 ], accip
[ MAXLEN
], * accurl
;
39 char oldaccdia
[ 11 ], oldacchora
[ 9 ], oldaccip
[ MAXLEN
];
41 char olduser
[ MAXLEN
], oldmsg
[ 50 ], acccode
[ 50 ], oldacccode
[ 50 ];
42 char ipantes
[ MAXLEN
], nameantes
[ MAXLEN
];
43 char accsmart
[ MAXLEN
];
47 char user
[ MAX_USER_LEN
];
48 char u2
[ MAX_USER_LEN
];
50 struct dirent
* direntp
;
51 long long int nbytes
= 0 ;
52 long long int nelap
= 0 ;
55 long long int incache
= 0 ;
56 long long int oucache
= 0 ;
57 long long int accbytes
;
58 long long int accelap
;
64 struct getwordstruct gwarea
;
65 struct userinfostruct
* uinfo
;
66 struct longlinestruct line
;
67 const char logext
[]= ".log" ;
78 while ( ( direntp
= readdir ( dirp
)) != NULL
) {
79 dlen
= strlen ( direntp
-> d_name
)-( sizeof ( logext
)- 1 );
80 if ( dlen
<= 0 ) continue ;
81 if ( strcmp ( direntp
-> d_name
+ dlen
, logext
) != 0 )
85 if ( dlen
>= sizeof ( user
)) continue ;
86 strncpy ( user
, direntp
-> d_name
, dlen
);
92 uinfo
= userinfo_find_from_file ( user
);
94 debuga ( _ ( "Ignoring unknown user file %s \n " ), user
);
98 if ( Ip2Name
&& uinfo
-> id_is_ip
) {
100 ip2name ( u2
, sizeof ( u2
));
101 strcpy ( nameantes
, u2
);
103 user_find ( uinfo
-> label
, MAX_USER_LEN
, u2
);
105 if ( snprintf ( tmp3
, sizeof ( tmp3
), "%s/%s" , tmp
, direntp
-> d_name
)>= sizeof ( tmp3
)) {
106 debuga ( _ ( "(datafile) directory path too long: %s/%s \n " ), tmp
, direntp
-> d_name
);
110 if (( fp_in
= MY_FOPEN ( tmp3
, "r" ))== NULL
){
111 debuga ( _ ( "(datafile) Cannot open file %s \n " ), tmp3
);
115 if ( longline_prepare (& line
)< 0 ) {
116 debuga ( _ ( "Not enough memory to read the downloaded files. \n " ));
122 while (( buf
= longline_read ( fp_in
,& line
))!= NULL
) {
123 getword_start (& gwarea
, buf
);
124 if ( getword ( accdia
, sizeof ( accdia
),& gwarea
, ' \t ' )< 0 || getword ( acchora
, sizeof ( acchora
),& gwarea
, ' \t ' )< 0 ||
125 getword ( accip
, sizeof ( accip
),& gwarea
, ' \t ' )< 0 ||
126 getword_ptr ( buf
,& accurl
,& gwarea
, ' \t ' )< 0 || getword_atoll (& accbytes
,& gwarea
, ' \t ' )< 0 ||
127 getword ( acccode
, sizeof ( acccode
),& gwarea
, ' \t ' )< 0 || getword_atoll (& accelap
,& gwarea
, ' \t ' )< 0 ) {
128 debuga ( _ ( "There is a broken record or garbage in file %s \n " ), tmp3
);
131 if ( getword_skip ( 20000 ,& gwarea
, '"' )< 0 || getword ( accsmart
, sizeof ( accsmart
),& gwarea
, '"' )< 0 ) {
132 debuga ( _ ( "There is an invalid smart info in file %s \n " ), tmp3
);
137 if ( strcmp ( accip
, ipantes
) != 0 ) {
138 strcpy ( ipantes
, accip
);
139 ip2name ( accip
, sizeof ( accip
));
140 strcpy ( nameantes
, accip
);
142 else strcpy ( accip
, nameantes
);
146 url_len
= strlen ( accurl
);
147 if (! oldurl
|| url_len
>= ourl_size
) {
149 oldurl
= realloc ( oldurl
, ourl_size
);
151 debuga ( _ ( "Not enough memory to store the url \n " ));
155 strcpy ( oldurl
, accurl
);
156 strcpy ( oldacccode
, acccode
);
157 strcpy ( oldaccip
, accip
);
158 strcpy ( oldaccdia
, accdia
);
159 strcpy ( oldacchora
, acchora
);
163 same_url
=( strcmp ( oldurl
, accurl
) == 0 );
165 if (! same_url
|| new_user
){
166 if ( strstr ( oldacccode
, "DENIED" ) != 0 )
167 strcpy ( oldmsg
, _ ( "DENIED" ));
170 if (! fp_ou
&& ( fp_ou
= MY_FOPEN ( DataFile
, "w" ))== NULL
){
171 debuga ( _ ( "(datafile) Cannot open file %s \n " ), DataFile
);
174 saverecs ( fp_ou
, uinfo
, nacc
, oldurl
, nbytes
, oldaccip
, oldacchora
, oldaccdia
, nelap
, incache
, oucache
);
187 strcpy ( crc2
, acccode
);
188 str
= strchr ( crc2
, '/' );
191 if ( strstr ( crc2
, "MISS" ) != 0 ) oucache
+= accbytes
;
192 else incache
+= accbytes
;
195 url_len
= strlen ( accurl
);
196 if ( url_len
>= ourl_size
) {
198 oldurl
= realloc ( oldurl
, ourl_size
);
200 debuga ( _ ( "Not enough memory to store the url \n " ));
204 strcpy ( oldurl
, accurl
);
207 strcpy ( oldacccode
, acccode
);
208 strcpy ( oldaccip
, accip
);
209 strcpy ( oldaccdia
, accdia
);
210 strcpy ( oldacchora
, acchora
);
214 longline_free (& line
);
216 ( void ) closedir ( dirp
);
217 if ( oldurl
) free ( oldurl
);
218 if ( fp_ou
) fclose ( fp_ou
);
221 debuga ( _ ( "Datafile %s written successfully \n " ), DataFile
);
224 void saverecs ( FILE * fp_ou
, const struct userinfostruct
* uinfo
, 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
)
229 if (( DataFileFields
& DATA_FIELD_USER
) != 0 ) {
230 fputs ( uinfo
-> label
, fp_ou
);
231 fputc ( DataFileDelimiter
[ 0 ], fp_ou
);
233 if (( DataFileFields
& DATA_FIELD_DATE
) != 0 ) {
235 fputc ( DataFileDelimiter
[ 0 ], fp_ou
);
237 if (( DataFileFields
& DATA_FIELD_TIME
) != 0 ) {
239 fputc ( DataFileDelimiter
[ 0 ], fp_ou
);
241 if (( DataFileFields
& DATA_FIELD_URL
) != 0 ) {
242 url_hostname ( url
, name
, sizeof ( name
));
243 if ( DataFileUrl
== DATAFILEURL_IP
) name2ip ( name
);
245 fputc ( DataFileDelimiter
[ 0 ], fp_ou
);
247 if (( DataFileFields
& DATA_FIELD_CONNECT
) != 0 ) {
248 my_lltoa ( nacc
, val
, sizeof ( val
), 0 );
250 fputc ( DataFileDelimiter
[ 0 ], fp_ou
);
252 if (( DataFileFields
& DATA_FIELD_BYTES
) != 0 ) {
253 my_lltoa ( nbytes
, val
, sizeof ( val
), 0 );
255 fputc ( DataFileDelimiter
[ 0 ], fp_ou
);
257 if (( DataFileFields
& DATA_FIELD_IN_CACHE
) != 0 ) {
258 my_lltoa ( incache
, val
, sizeof ( val
), 0 );
260 fputc ( DataFileDelimiter
[ 0 ], fp_ou
);
262 if (( DataFileFields
& DATA_FIELD_OUT_CACHE
) != 0 ) {
263 my_lltoa ( oucache
, val
, sizeof ( val
), 0 );
265 fputc ( DataFileDelimiter
[ 0 ], fp_ou
);
267 if (( DataFileFields
& DATA_FIELD_ELAPSED
) != 0 ) {
268 my_lltoa ( nelap
, val
, sizeof ( val
), 0 );
270 fputc ( DataFileDelimiter
[ 0 ], fp_ou
);