]> git.ipfire.org Git - thirdparty/sarg.git/blame - datafile.c
Display the source of the message displayed on stderr
[thirdparty/sarg.git] / datafile.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
110ce984 3 * 1998, 2015
25697a35
GS
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
1164c474
FM
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
25697a35
GS
9 * ---------------------------------------------------------------------
10 *
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.
15 *
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.
20 *
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.
24 *
25 */
26
27#include "include/conf.h"
5f3cfd1d 28#include "include/defs.h"
25697a35 29
f84a35a3 30void 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);
25697a35
GS
31
32void data_file(char *tmp)
33{
9bd92830 34 FILE *fp_in, *fp_ou=NULL;
25697a35 35
9bd92830
FM
36 char *buf;
37 char accdia[11], acchora[9], accip[MAXLEN], *accurl;
38 char oldaccdia[11], oldacchora[9], oldaccip[MAXLEN];
39 char *oldurl;
5025f299 40 char acccode[50], oldacccode[50];
9bd92830
FM
41 char ipantes[MAXLEN], nameantes[MAXLEN];
42 char accsmart[MAXLEN];
43 char crc2[50];
44 char *str;
45 char tmp3[MAXLEN];
9bd92830 46 char u2[MAX_USER_LEN];
b6b6cb8c 47 char userlabel[MAX_USER_LEN];
93551487 48 userscan uscan;
9bd92830
FM
49 long long int nbytes=0;
50 long long int nelap=0;
51 long long int nacc=0;
52 int rtotal=0;
53 long long int incache=0;
54 long long int oucache=0;
55 long long int accbytes;
56 long long int accelap;
9bd92830
FM
57 int new_user;
58 int same_url;
59 int url_len;
60 int ourl_size;
61 struct getwordstruct gwarea;
62 struct userinfostruct *uinfo;
63 longline line;
25697a35 64
9bd92830
FM
65 ipantes[0]='\0';
66 nameantes[0]='\0';
25697a35 67
9bd92830
FM
68 oldurl=NULL;
69 ourl_size=0;
25697a35 70
93551487
FM
71 uscan=userinfo_startscan();
72 if (uscan == NULL) {
af961877 73 debuga(__FILE__,__LINE__,_("Cannot enumerate the user list\n"));
9bd92830
FM
74 exit(EXIT_FAILURE);
75 }
93551487 76 while ( (uinfo = userinfo_advancescan(uscan)) != NULL ) {
9bd92830
FM
77 strcpy(u2,uinfo->id);
78 if(Ip2Name && uinfo->id_is_ip) {
79 strcpy(ipantes,u2);
80 ip2name(u2,sizeof(u2));
81 strcpy(nameantes,u2);
82 }
b6b6cb8c
FM
83 user_find(userlabel,MAX_USER_LEN, u2);
84 userinfo_label(uinfo,userlabel);
af961877 85 if(debug) debuga(__FILE__,__LINE__,_("Reading user file \"%s/%s\"\n"),tmp,uinfo->filename);
f2ec8c75 86
461b479d 87 sort_users_log(tmp,debug,uinfo);
170a77ea 88 if (snprintf(tmp3,sizeof(tmp3),"%s/%s.user_log",tmp,uinfo->filename)>=sizeof(tmp3)) {
af961877 89 debuga(__FILE__,__LINE__,_("Path too long: "));
041018b6 90 debuga_more("%s/%s.user_log\n",tmp,uinfo->filename);
9bd92830
FM
91 exit(EXIT_FAILURE);
92 }
f2ec8c75 93
9bd92830 94 if((fp_in=MY_FOPEN(tmp3,"r"))==NULL){
af961877 95 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),tmp3,strerror(errno));
9bd92830
FM
96 exit(EXIT_FAILURE);
97 }
25697a35 98
9bd92830 99 if ((line=longline_create())==NULL) {
af961877 100 debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),tmp3);
9bd92830
FM
101 exit(EXIT_FAILURE);
102 }
f84a35a3 103
9bd92830
FM
104 ttopen=0;
105 new_user=1;
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 ||
007905af
FM
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) {
af961877 112 debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),tmp3);
9bd92830
FM
113 exit(EXIT_FAILURE);
114 }
115 if (getword_skip(20000,&gwarea,'"')<0 || getword(accsmart,sizeof(accsmart),&gwarea,'"')<0) {
af961877 116 debuga(__FILE__,__LINE__,_("Invalid smart info in file \"%s\"\n"),tmp3);
9bd92830
FM
117 exit(EXIT_FAILURE);
118 }
25697a35 119
9bd92830
FM
120 if(Ip2Name) {
121 if(strcmp(accip,ipantes) != 0) {
122 strcpy(ipantes,accip);
123 ip2name(accip,sizeof(accip));
124 strcpy(nameantes,accip);
125 }
126 else strcpy(accip,nameantes);
127 }
5542cb59 128
9bd92830
FM
129 if(!rtotal){
130 url_len=strlen(accurl);
131 if (!oldurl || url_len>=ourl_size) {
132 ourl_size=url_len+1;
133 oldurl=realloc(oldurl,ourl_size);
134 if (!oldurl) {
af961877 135 debuga(__FILE__,__LINE__,_("Not enough memory to store the url\n"));
9bd92830
FM
136 exit(EXIT_FAILURE);
137 }
138 }
139 strcpy(oldurl,accurl);
140 strcpy(oldacccode,acccode);
141 strcpy(oldaccip,accip);
142 strcpy(oldaccdia,accdia);
143 strcpy(oldacchora,acchora);
144 new_user=0;
145 rtotal++;
146 }
147 same_url=(strcmp(oldurl,accurl) == 0);
5542cb59 148
9bd92830
FM
149 if(!same_url || new_user){
150 if(!fp_ou && (fp_ou=MY_FOPEN(DataFile,"w"))==NULL){
af961877 151 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),DataFile,strerror(errno));
9bd92830
FM
152 exit(EXIT_FAILURE);
153 }
154 saverecs(fp_ou,uinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
155 nacc=0;
156 nbytes=0;
157 nelap=0;
158 incache=0;
159 oucache=0;
160 new_user=0;
161 }
5542cb59 162
9bd92830
FM
163 nacc++;
164 nbytes+=accbytes;
165 nelap+=accelap;
25697a35 166
9bd92830
FM
167 strcpy(crc2,acccode);
168 str=strchr(crc2,'/');
169 if (str) *str='\0';
5542cb59 170
9bd92830
FM
171 if(strstr(crc2,"MISS") != 0) oucache+=accbytes;
172 else incache+=accbytes;
25697a35 173
9bd92830
FM
174 if (!same_url) {
175 url_len=strlen(accurl);
176 if (url_len>=ourl_size) {
177 ourl_size=url_len+1;
178 oldurl=realloc(oldurl,ourl_size);
179 if (!oldurl) {
af961877 180 debuga(__FILE__,__LINE__,_("Not enough memory to store the url\n"));
9bd92830
FM
181 exit(EXIT_FAILURE);
182 }
183 }
184 strcpy(oldurl,accurl);
185 }
186 new_user=0;
187 strcpy(oldacccode,acccode);
188 strcpy(oldaccip,accip);
189 strcpy(oldaccdia,accdia);
190 strcpy(oldacchora,acchora);
191 }
5542cb59 192
204781f4 193 if (fclose(fp_in)==EOF) {
af961877 194 debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),tmp3,strerror(errno));
204781f4
FM
195 exit(EXIT_FAILURE);
196 }
9bd92830
FM
197 longline_destroy(&line);
198 }
93551487 199 userinfo_stopscan(uscan);
9bd92830 200 if (oldurl) free(oldurl);
507460ae 201 if (fp_ou && fclose(fp_ou)==EOF) {
af961877 202 debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),DataFile,strerror(errno));
507460ae
FM
203 exit(EXIT_FAILURE);
204 }
5542cb59 205
9bd92830 206 if(debug)
af961877 207 debuga(__FILE__,__LINE__,_("Datafile %s written successfully\n"),DataFile);
25697a35
GS
208}
209
f84a35a3 210void 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)
25697a35 211{
9bd92830
FM
212 char val[20];
213 char name[512];
25697a35 214
9bd92830
FM
215 if((DataFileFields & DATA_FIELD_USER) != 0) {
216 fputs(uinfo->label,fp_ou);
217 fputc(DataFileDelimiter[0],fp_ou);
218 }
219 if((DataFileFields & DATA_FIELD_DATE) != 0) {
220 fputs(dia,fp_ou);
221 fputc(DataFileDelimiter[0],fp_ou);
222 }
223 if((DataFileFields & DATA_FIELD_TIME) != 0) {
224 fputs(hora,fp_ou);
225 fputc(DataFileDelimiter[0],fp_ou);
226 }
227 if((DataFileFields & DATA_FIELD_URL) != 0) {
228 url_hostname(url,name,sizeof(name));
4afbb7a5 229 if (DataFileUrl == DATAFILEURL_IP) name2ip(name,sizeof(name));
9bd92830
FM
230 fputs(name,fp_ou);
231 fputc(DataFileDelimiter[0],fp_ou);
232 }
233 if((DataFileFields & DATA_FIELD_CONNECT) != 0) {
234 my_lltoa(nacc,val,sizeof(val),0);
235 fputs(val,fp_ou);
236 fputc(DataFileDelimiter[0],fp_ou);
237 }
238 if((DataFileFields & DATA_FIELD_BYTES) != 0) {
239 my_lltoa(nbytes,val,sizeof(val),0);
240 fputs(val,fp_ou);
241 fputc(DataFileDelimiter[0],fp_ou);
242 }
243 if((DataFileFields & DATA_FIELD_IN_CACHE) != 0) {
244 my_lltoa(incache,val,sizeof(val),0);
245 fputs(val,fp_ou);
246 fputc(DataFileDelimiter[0],fp_ou);
247 }
248 if((DataFileFields & DATA_FIELD_OUT_CACHE) != 0) {
249 my_lltoa(oucache,val,sizeof(val),0);
250 fputs(val,fp_ou);
251 fputc(DataFileDelimiter[0],fp_ou);
252 }
253 if((DataFileFields & DATA_FIELD_ELAPSED) != 0) {
254 my_lltoa(nelap,val,sizeof(val),0);
255 fputs(val,fp_ou);
256 fputc(DataFileDelimiter[0],fp_ou);
257 }
25697a35 258
9bd92830 259 fputc('\n',fp_ou);
25697a35 260}