]> git.ipfire.org Git - thirdparty/sarg.git/blob - datafile.c
8bab494a2a0d62f63e4139a95783329cca036062
[thirdparty/sarg.git] / datafile.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2010
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
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"
28 #include "include/defs.h"
29
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);
31
32 void data_file(char *tmp)
33 {
34
35 FILE *fp_in, *fp_ou=NULL;
36
37 char *buf;
38 char accdia[11], acchora[9], accip[MAXLEN], *accurl;
39 char oldaccdia[11], oldacchora[9], oldaccip[MAXLEN];
40 char *oldurl;
41 char olduser[MAXLEN], acccode[50], oldacccode[50];
42 char ipantes[MAXLEN], nameantes[MAXLEN];
43 char accsmart[MAXLEN];
44 char crc2[50];
45 char *str;
46 char tmp3[MAXLEN];
47 char user[MAX_USER_LEN];
48 char u2[MAX_USER_LEN];
49 DIR *dirp;
50 struct dirent *direntp;
51 long long int nbytes=0;
52 long long int nelap=0;
53 long long int nacc=0;
54 int rtotal=0;
55 long long int incache=0;
56 long long int oucache=0;
57 long long int accbytes;
58 long long int accelap;
59 int dlen;
60 int new_user;
61 int same_url;
62 int url_len;
63 int ourl_size;
64 struct getwordstruct gwarea;
65 struct userinfostruct *uinfo;
66 longline line;
67 const char logext[]=".log";
68
69 ipantes[0]='\0';
70 nameantes[0]='\0';
71
72 olduser[0]='\0';
73 oldurl=NULL;
74 ourl_size=0;
75
76 if ((dirp = opendir(tmp)) == NULL) {
77 debuga(_("Failed to open directory %s - %s\n"),tmp,strerror(errno));
78 exit(EXIT_FAILURE);
79 }
80 while ( (direntp = readdir( dirp )) != NULL ) {
81 dlen=strlen(direntp->d_name)-(sizeof(logext)-1);
82 if (dlen<=0) continue;
83 if(strcmp(direntp->d_name+dlen,logext) != 0)
84 continue;
85
86 if (dlen>0) {
87 if (dlen>=sizeof(user)) continue;
88 strncpy(user,direntp->d_name,dlen);
89 user[dlen]=0;
90 } else {
91 user[0]='\0';
92 }
93
94 uinfo=userinfo_find_from_file(user);
95 if (!uinfo) {
96 debuga(_("Ignoring unknown user file %s\n"),user);
97 continue;
98 }
99 strcpy(u2,uinfo->id);
100 if(Ip2Name && uinfo->id_is_ip) {
101 strcpy(ipantes,u2);
102 ip2name(u2,sizeof(u2));
103 strcpy(nameantes,u2);
104 }
105 user_find(uinfo->label,MAX_USER_LEN, u2);
106
107 if (snprintf(tmp3,sizeof(tmp3),"%s/%s",tmp,direntp->d_name)>=sizeof(tmp3)) {
108 debuga(_("(datafile) directory path too long: %s/%s\n"),tmp,direntp->d_name);
109 exit(EXIT_FAILURE);
110 }
111
112 if((fp_in=MY_FOPEN(tmp3,"r"))==NULL){
113 debuga(_("(datafile) Cannot open file %s\n"),tmp3);
114 exit(EXIT_FAILURE);
115 }
116
117 if ((line=longline_create())==NULL) {
118 debuga(_("Not enough memory to read the downloaded files.\n"));
119 exit(EXIT_FAILURE);
120 }
121
122 ttopen=0;
123 new_user=1;
124 while((buf=longline_read(fp_in,line))!=NULL) {
125 getword_start(&gwarea,buf);
126 if (getword(accdia,sizeof(accdia),&gwarea,'\t')<0 || getword(acchora,sizeof(acchora),&gwarea,'\t')<0 ||
127 getword(accip,sizeof(accip),&gwarea,'\t')<0 ||
128 getword_ptr(buf,&accurl,&gwarea,'\t')<0 || getword_atoll(&accbytes,&gwarea,'\t')<0 ||
129 getword(acccode,sizeof(acccode),&gwarea,'\t')<0 || getword_atoll(&accelap,&gwarea,'\t')<0) {
130 debuga(_("There is a broken record or garbage in file %s\n"),tmp3);
131 exit(EXIT_FAILURE);
132 }
133 if (getword_skip(20000,&gwarea,'"')<0 || getword(accsmart,sizeof(accsmart),&gwarea,'"')<0) {
134 debuga(_("There is an invalid smart info in file %s\n"),tmp3);
135 exit(EXIT_FAILURE);
136 }
137
138 if(Ip2Name) {
139 if(strcmp(accip,ipantes) != 0) {
140 strcpy(ipantes,accip);
141 ip2name(accip,sizeof(accip));
142 strcpy(nameantes,accip);
143 }
144 else strcpy(accip,nameantes);
145 }
146
147 if(!rtotal){
148 url_len=strlen(accurl);
149 if (!oldurl || url_len>=ourl_size) {
150 ourl_size=url_len+1;
151 oldurl=realloc(oldurl,ourl_size);
152 if (!oldurl) {
153 debuga(_("Not enough memory to store the url\n"));
154 exit(EXIT_FAILURE);
155 }
156 }
157 strcpy(oldurl,accurl);
158 strcpy(oldacccode,acccode);
159 strcpy(oldaccip,accip);
160 strcpy(oldaccdia,accdia);
161 strcpy(oldacchora,acchora);
162 new_user=0;
163 rtotal++;
164 }
165 same_url=(strcmp(oldurl,accurl) == 0);
166
167 if(!same_url || new_user){
168 if(!fp_ou && (fp_ou=MY_FOPEN(DataFile,"w"))==NULL){
169 debuga(_("(datafile) Cannot open file %s\n"),DataFile);
170 exit(EXIT_FAILURE);
171 }
172 saverecs(fp_ou,uinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
173 nacc=0;
174 nbytes=0;
175 nelap=0;
176 incache=0;
177 oucache=0;
178 new_user=0;
179 }
180
181 nacc++;
182 nbytes+=accbytes;
183 nelap+=accelap;
184
185 strcpy(crc2,acccode);
186 str=strchr(crc2,'/');
187 if (str) *str='\0';
188
189 if(strstr(crc2,"MISS") != 0) oucache+=accbytes;
190 else incache+=accbytes;
191
192 if (!same_url) {
193 url_len=strlen(accurl);
194 if (url_len>=ourl_size) {
195 ourl_size=url_len+1;
196 oldurl=realloc(oldurl,ourl_size);
197 if (!oldurl) {
198 debuga(_("Not enough memory to store the url\n"));
199 exit(EXIT_FAILURE);
200 }
201 }
202 strcpy(oldurl,accurl);
203 }
204 new_user=0;
205 strcpy(oldacccode,acccode);
206 strcpy(oldaccip,accip);
207 strcpy(oldaccdia,accdia);
208 strcpy(oldacchora,acchora);
209 }
210
211 fclose(fp_in);
212 longline_destroy(&line);
213 }
214 (void)closedir( dirp );
215 if (oldurl) free(oldurl);
216 if (fp_ou) fclose(fp_ou);
217
218 if(debug)
219 debuga(_("Datafile %s written successfully\n"),DataFile);
220 }
221
222 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)
223 {
224 char val[20];
225 char name[512];
226
227 if((DataFileFields & DATA_FIELD_USER) != 0) {
228 fputs(uinfo->label,fp_ou);
229 fputc(DataFileDelimiter[0],fp_ou);
230 }
231 if((DataFileFields & DATA_FIELD_DATE) != 0) {
232 fputs(dia,fp_ou);
233 fputc(DataFileDelimiter[0],fp_ou);
234 }
235 if((DataFileFields & DATA_FIELD_TIME) != 0) {
236 fputs(hora,fp_ou);
237 fputc(DataFileDelimiter[0],fp_ou);
238 }
239 if((DataFileFields & DATA_FIELD_URL) != 0) {
240 url_hostname(url,name,sizeof(name));
241 if (DataFileUrl == DATAFILEURL_IP) name2ip(name);
242 fputs(name,fp_ou);
243 fputc(DataFileDelimiter[0],fp_ou);
244 }
245 if((DataFileFields & DATA_FIELD_CONNECT) != 0) {
246 my_lltoa(nacc,val,sizeof(val),0);
247 fputs(val,fp_ou);
248 fputc(DataFileDelimiter[0],fp_ou);
249 }
250 if((DataFileFields & DATA_FIELD_BYTES) != 0) {
251 my_lltoa(nbytes,val,sizeof(val),0);
252 fputs(val,fp_ou);
253 fputc(DataFileDelimiter[0],fp_ou);
254 }
255 if((DataFileFields & DATA_FIELD_IN_CACHE) != 0) {
256 my_lltoa(incache,val,sizeof(val),0);
257 fputs(val,fp_ou);
258 fputc(DataFileDelimiter[0],fp_ou);
259 }
260 if((DataFileFields & DATA_FIELD_OUT_CACHE) != 0) {
261 my_lltoa(oucache,val,sizeof(val),0);
262 fputs(val,fp_ou);
263 fputc(DataFileDelimiter[0],fp_ou);
264 }
265 if((DataFileFields & DATA_FIELD_ELAPSED) != 0) {
266 my_lltoa(nelap,val,sizeof(val),0);
267 fputs(val,fp_ou);
268 fputc(DataFileDelimiter[0],fp_ou);
269 }
270
271 fputc('\n',fp_ou);
272 }