]>
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
)
34 FILE * fp_in
, * fp_ou
= NULL
;
37 char accdia
[ 11 ], acchora
[ 9 ], accip
[ MAXLEN
], * accurl
;
38 char oldaccdia
[ 11 ], oldacchora
[ 9 ], oldaccip
[ MAXLEN
];
40 char acccode
[ 50 ], oldacccode
[ 50 ];
41 char ipantes
[ MAXLEN
], nameantes
[ MAXLEN
];
42 char accsmart
[ MAXLEN
];
46 char u2
[ MAX_USER_LEN
];
47 char userlabel
[ MAX_USER_LEN
];
49 long long int nbytes
= 0 ;
50 long long int nelap
= 0 ;
53 long long int incache
= 0 ;
54 long long int oucache
= 0 ;
55 long long int accbytes
;
56 long long int accelap
;
61 struct getwordstruct gwarea
;
62 struct userinfostruct
* uinfo
;
71 uscan
= userinfo_startscan ();
73 debuga ( _ ( "Cannot enumerate the user list \n " ));
76 while ( ( uinfo
= userinfo_advancescan ( uscan
)) != NULL
) {
78 if ( Ip2Name
&& uinfo
-> id_is_ip
) {
80 ip2name ( u2
, sizeof ( u2
));
83 user_find ( userlabel
, MAX_USER_LEN
, u2
);
84 userinfo_label ( uinfo
, userlabel
);
85 if ( debug
) debuga ( _ ( "Reading user file: %s/%s \n " ), tmp
, uinfo
-> filename
);
87 sort_users_log ( tmp
, debug
, uinfo
);
88 if ( snprintf ( tmp3
, sizeof ( tmp3
), "%s/%s.user_log" , tmp
, uinfo
-> filename
)>= sizeof ( tmp3
)) {
89 debuga ( _ ( "Path too long: " ));
90 debuga_more ( "%s/%s.user_log \n " , tmp
, uinfo
-> filename
);
94 if (( fp_in
= MY_FOPEN ( tmp3
, "r" ))== NULL
){
95 debuga ( _ ( "Cannot open file \" %s \" : %s \n " ), tmp3
, strerror ( errno
));
99 if (( line
= longline_create ())== NULL
) {
100 debuga ( _ ( "Not enough memory to read the downloaded files. \n " ));
106 while (( buf
= longline_read ( fp_in
, line
))!= NULL
) {
107 getword_start (& gwarea
, buf
);
108 if ( getword ( accdia
, sizeof ( accdia
),& gwarea
, ' \t ' )< 0 || getword ( acchora
, sizeof ( acchora
),& gwarea
, ' \t ' )< 0 ||
109 getword ( accip
, sizeof ( accip
),& gwarea
, ' \t ' )< 0 ||
110 getword_ptr ( buf
,& accurl
,& gwarea
, ' \t ' )< 0 || getword_atoll (& accbytes
,& gwarea
, ' \t ' )< 0 ||
111 getword ( acccode
, sizeof ( acccode
),& gwarea
, ' \t ' )< 0 || getword_atoll (& accelap
,& gwarea
, ' \t ' )< 0 ) {
112 debuga ( _ ( "There is a broken record or garbage in file %s \n " ), tmp3
);
115 if ( getword_skip ( 20000 ,& gwarea
, '"' )< 0 || getword ( accsmart
, sizeof ( accsmart
),& gwarea
, '"' )< 0 ) {
116 debuga ( _ ( "There is an invalid smart info in file %s \n " ), tmp3
);
121 if ( strcmp ( accip
, ipantes
) != 0 ) {
122 strcpy ( ipantes
, accip
);
123 ip2name ( accip
, sizeof ( accip
));
124 strcpy ( nameantes
, accip
);
126 else strcpy ( accip
, nameantes
);
130 url_len
= strlen ( accurl
);
131 if (! oldurl
|| url_len
>= ourl_size
) {
133 oldurl
= realloc ( oldurl
, ourl_size
);
135 debuga ( _ ( "Not enough memory to store the url \n " ));
139 strcpy ( oldurl
, accurl
);
140 strcpy ( oldacccode
, acccode
);
141 strcpy ( oldaccip
, accip
);
142 strcpy ( oldaccdia
, accdia
);
143 strcpy ( oldacchora
, acchora
);
147 same_url
=( strcmp ( oldurl
, accurl
) == 0 );
149 if (! same_url
|| new_user
){
150 if (! fp_ou
&& ( fp_ou
= MY_FOPEN ( DataFile
, "w" ))== NULL
){
151 debuga ( _ ( "Cannot open file \" %s \" : %s \n " ), DataFile
, strerror ( errno
));
154 saverecs ( fp_ou
, uinfo
, nacc
, oldurl
, nbytes
, oldaccip
, oldacchora
, oldaccdia
, nelap
, incache
, oucache
);
167 strcpy ( crc2
, acccode
);
168 str
= strchr ( crc2
, '/' );
171 if ( strstr ( crc2
, "MISS" ) != 0 ) oucache
+= accbytes
;
172 else incache
+= accbytes
;
175 url_len
= strlen ( accurl
);
176 if ( url_len
>= ourl_size
) {
178 oldurl
= realloc ( oldurl
, ourl_size
);
180 debuga ( _ ( "Not enough memory to store the url \n " ));
184 strcpy ( oldurl
, accurl
);
187 strcpy ( oldacccode
, acccode
);
188 strcpy ( oldaccip
, accip
);
189 strcpy ( oldaccdia
, accdia
);
190 strcpy ( oldacchora
, acchora
);
194 longline_destroy (& line
);
196 userinfo_stopscan ( uscan
);
197 if ( oldurl
) free ( oldurl
);
198 if ( fp_ou
&& fclose ( fp_ou
)== EOF
) {
199 debuga ( _ ( "Write error in \" %s \" : %s \n " ), DataFile
, strerror ( errno
));
204 debuga ( _ ( "Datafile %s written successfully \n " ), DataFile
);
207 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
)
212 if (( DataFileFields
& DATA_FIELD_USER
) != 0 ) {
213 fputs ( uinfo
-> label
, fp_ou
);
214 fputc ( DataFileDelimiter
[ 0 ], fp_ou
);
216 if (( DataFileFields
& DATA_FIELD_DATE
) != 0 ) {
218 fputc ( DataFileDelimiter
[ 0 ], fp_ou
);
220 if (( DataFileFields
& DATA_FIELD_TIME
) != 0 ) {
222 fputc ( DataFileDelimiter
[ 0 ], fp_ou
);
224 if (( DataFileFields
& DATA_FIELD_URL
) != 0 ) {
225 url_hostname ( url
, name
, sizeof ( name
));
226 if ( DataFileUrl
== DATAFILEURL_IP
) name2ip ( name
, sizeof ( name
));
228 fputc ( DataFileDelimiter
[ 0 ], fp_ou
);
230 if (( DataFileFields
& DATA_FIELD_CONNECT
) != 0 ) {
231 my_lltoa ( nacc
, val
, sizeof ( val
), 0 );
233 fputc ( DataFileDelimiter
[ 0 ], fp_ou
);
235 if (( DataFileFields
& DATA_FIELD_BYTES
) != 0 ) {
236 my_lltoa ( nbytes
, val
, sizeof ( val
), 0 );
238 fputc ( DataFileDelimiter
[ 0 ], fp_ou
);
240 if (( DataFileFields
& DATA_FIELD_IN_CACHE
) != 0 ) {
241 my_lltoa ( incache
, val
, sizeof ( val
), 0 );
243 fputc ( DataFileDelimiter
[ 0 ], fp_ou
);
245 if (( DataFileFields
& DATA_FIELD_OUT_CACHE
) != 0 ) {
246 my_lltoa ( oucache
, val
, sizeof ( val
), 0 );
248 fputc ( DataFileDelimiter
[ 0 ], fp_ou
);
250 if (( DataFileFields
& DATA_FIELD_ELAPSED
) != 0 ) {
251 my_lltoa ( nelap
, val
, sizeof ( val
), 0 );
253 fputc ( DataFileDelimiter
[ 0 ], fp_ou
);