]>
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 static void saverecs ( FILE * fp_ou
, const struct userinfostruct
* uinfo
, long long int nacc
, const char * url
, long long int nbytes
, const char * ip
, const char * hora
, const char * dia
, long long int nelap
, long long int incache
, long long int oucache
);
32 void data_file ( char * tmp
)
38 char accdia
[ 11 ], acchora
[ 9 ], accip
[ MAXLEN
], * accurl
;
39 char oldaccdia
[ 11 ], oldacchora
[ 9 ], oldaccip
[ MAXLEN
];
41 char acccode
[ 50 ], oldacccode
[ 50 ];
42 char ipantes
[ MAXLEN
], nameantes
[ MAXLEN
];
43 char accsmart
[ MAXLEN
];
47 char u2
[ MAX_USER_LEN
];
48 char userlabel
[ MAX_USER_LEN
];
50 long long int nbytes
= 0 ;
51 long long int nelap
= 0 ;
54 long long int incache
= 0 ;
55 long long int oucache
= 0 ;
56 long long int accbytes
;
57 long long int accelap
;
62 struct getwordstruct gwarea
;
63 struct userinfostruct
* uinfo
;
66 init_usertab ( UserTabFile
);
74 uscan
= userinfo_startscan ();
76 debuga ( __FILE__
, __LINE__
, _ ( "Cannot enumerate the user list \n " ));
79 while ( ( uinfo
= userinfo_advancescan ( uscan
)) != NULL
) {
81 if ( Ip2Name
&& uinfo
-> id_is_ip
) {
83 ip2name ( u2
, sizeof ( u2
));
86 user_find ( userlabel
, MAX_USER_LEN
, u2
);
87 userinfo_label ( uinfo
, userlabel
);
88 if ( debug
) debuga ( __FILE__
, __LINE__
, _ ( "Reading user file \" %s/%s \"\n " ), tmp
, uinfo
-> filename
);
90 sort_users_log ( tmp
, debug
, uinfo
);
91 if ( snprintf ( tmp3
, sizeof ( tmp3
), "%s/%s.user_log" , tmp
, uinfo
-> filename
)>= sizeof ( tmp3
)) {
92 debuga ( __FILE__
, __LINE__
, _ ( "Path too long: " ));
93 debuga_more ( "%s/%s.user_log \n " , tmp
, uinfo
-> filename
);
97 if (( fp_in
= FileObject_Open ( tmp3
))== NULL
){
98 debuga ( __FILE__
, __LINE__
, _ ( "Cannot open file \" %s \" : %s \n " ), tmp3
, FileObject_GetLastOpenError ());
102 if (( line
= longline_create ())== NULL
) {
103 debuga ( __FILE__
, __LINE__
, _ ( "Not enough memory to read file \" %s \"\n " ), tmp3
);
109 while (( buf
= longline_read ( fp_in
, line
))!= NULL
) {
110 getword_start (& gwarea
, buf
);
111 if ( getword ( accdia
, sizeof ( accdia
),& gwarea
, ' \t ' )< 0 || getword ( acchora
, sizeof ( acchora
),& gwarea
, ' \t ' )< 0 ||
112 getword ( accip
, sizeof ( accip
),& gwarea
, ' \t ' )< 0 ||
113 getword_ptr ( buf
,& accurl
,& gwarea
, ' \t ' )< 0 || getword_atoll (& accbytes
,& gwarea
, ' \t ' )< 0 ||
114 getword ( acccode
, sizeof ( acccode
),& gwarea
, ' \t ' )< 0 || getword_atoll (& accelap
,& gwarea
, ' \t ' )< 0 ) {
115 debuga ( __FILE__
, __LINE__
, _ ( "Invalid record in file \" %s \"\n " ), tmp3
);
118 if ( getword_skip ( 20000 ,& gwarea
, '"' )< 0 || getword ( accsmart
, sizeof ( accsmart
),& gwarea
, '"' )< 0 ) {
119 debuga ( __FILE__
, __LINE__
, _ ( "Invalid smart info in file \" %s \"\n " ), tmp3
);
124 if ( strcmp ( accip
, ipantes
) != 0 ) {
125 strcpy ( ipantes
, accip
);
126 ip2name ( accip
, sizeof ( accip
));
127 strcpy ( nameantes
, accip
);
129 else strcpy ( accip
, nameantes
);
133 url_len
= strlen ( accurl
);
134 if (! oldurl
|| url_len
>= ourl_size
) {
136 oldurl
= realloc ( oldurl
, ourl_size
);
138 debuga ( __FILE__
, __LINE__
, _ ( "Not enough memory to store the url \n " ));
142 strcpy ( oldurl
, accurl
);
143 strcpy ( oldacccode
, acccode
);
144 strcpy ( oldaccip
, accip
);
145 strcpy ( oldaccdia
, accdia
);
146 strcpy ( oldacchora
, acchora
);
150 same_url
=( strcmp ( oldurl
, accurl
) == 0 );
152 if (! same_url
|| new_user
){
153 if (! fp_ou
&& ( fp_ou
= MY_FOPEN ( DataFile
, "w" ))== NULL
){
154 debuga ( __FILE__
, __LINE__
, _ ( "Cannot open file \" %s \" : %s \n " ), DataFile
, strerror ( errno
));
157 saverecs ( fp_ou
, uinfo
, nacc
, oldurl
, nbytes
, oldaccip
, oldacchora
, oldaccdia
, nelap
, incache
, oucache
);
170 strcpy ( crc2
, acccode
);
171 str
= strchr ( crc2
, '/' );
174 if ( strstr ( crc2
, "MISS" ) != 0 ) oucache
+= accbytes
;
175 else incache
+= accbytes
;
178 url_len
= strlen ( accurl
);
179 if ( url_len
>= ourl_size
) {
181 oldurl
= realloc ( oldurl
, ourl_size
);
183 debuga ( __FILE__
, __LINE__
, _ ( "Not enough memory to store the url \n " ));
187 strcpy ( oldurl
, accurl
);
190 strcpy ( oldacccode
, acccode
);
191 strcpy ( oldaccip
, accip
);
192 strcpy ( oldaccdia
, accdia
);
193 strcpy ( oldacchora
, acchora
);
196 if ( FileObject_Close ( fp_in
)) {
197 debuga ( __FILE__
, __LINE__
, _ ( "Read error in \" %s \" : %s \n " ), tmp3
, FileObject_GetLastCloseError ());
200 longline_destroy (& line
);
202 userinfo_stopscan ( uscan
);
203 if ( oldurl
) free ( oldurl
);
204 if ( fp_ou
&& fclose ( fp_ou
)== EOF
) {
205 debuga ( __FILE__
, __LINE__
, _ ( "Write error in \" %s \" : %s \n " ), DataFile
, strerror ( errno
));
210 debuga ( __FILE__
, __LINE__
, _ ( "Datafile %s written successfully \n " ), DataFile
);
213 static void saverecs ( FILE * fp_ou
, const struct userinfostruct
* uinfo
, long long int nacc
, const char * url
, long long int nbytes
, const char * ip
, const char * hora
, const char * dia
, long long int nelap
, long long int incache
, long long int oucache
)
218 if (( DataFileFields
& DATA_FIELD_USER
) != 0 ) {
219 fputs ( uinfo
-> label
, fp_ou
);
220 fputc ( DataFileDelimiter
[ 0 ], fp_ou
);
222 if (( DataFileFields
& DATA_FIELD_DATE
) != 0 ) {
224 fputc ( DataFileDelimiter
[ 0 ], fp_ou
);
226 if (( DataFileFields
& DATA_FIELD_TIME
) != 0 ) {
228 fputc ( DataFileDelimiter
[ 0 ], fp_ou
);
230 if (( DataFileFields
& DATA_FIELD_URL
) != 0 ) {
231 url_hostname ( url
, name
, sizeof ( name
));
232 if ( DataFileUrl
== DATAFILEURL_IP
&& name
[ 0 ]!= ALIAS_PREFIX
) name2ip ( name
, sizeof ( name
));
234 fputc ( DataFileDelimiter
[ 0 ], fp_ou
);
236 if (( DataFileFields
& DATA_FIELD_CONNECT
) != 0 ) {
237 my_lltoa ( nacc
, val
, sizeof ( val
), 0 );
239 fputc ( DataFileDelimiter
[ 0 ], fp_ou
);
241 if (( DataFileFields
& DATA_FIELD_BYTES
) != 0 ) {
242 my_lltoa ( nbytes
, val
, sizeof ( val
), 0 );
244 fputc ( DataFileDelimiter
[ 0 ], fp_ou
);
246 if (( DataFileFields
& DATA_FIELD_IN_CACHE
) != 0 ) {
247 my_lltoa ( incache
, val
, sizeof ( val
), 0 );
249 fputc ( DataFileDelimiter
[ 0 ], fp_ou
);
251 if (( DataFileFields
& DATA_FIELD_OUT_CACHE
) != 0 ) {
252 my_lltoa ( oucache
, val
, sizeof ( val
), 0 );
254 fputc ( DataFileDelimiter
[ 0 ], fp_ou
);
256 if (( DataFileFields
& DATA_FIELD_ELAPSED
) != 0 ) {
257 my_lltoa ( nelap
, val
, sizeof ( val
), 0 );
259 fputc ( DataFileDelimiter
[ 0 ], fp_ou
);