]> git.ipfire.org Git - thirdparty/sarg.git/blob - datafile.c
Messages ported to gettext. Only French translation exists
[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], oldmsg[50], 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 struct longlinestruct line;
67 const char logext[]=".log";
68
69 ipantes[0]='\0';
70 nameantes[0]='\0';
71
72 olduser[0]='\0';
73 strcat(tmp,"/sarg");
74 oldurl=NULL;
75 ourl_size=0;
76
77 dirp = opendir(tmp);
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)
82 continue;
83
84 if (dlen>0) {
85 if (dlen>=sizeof(user)) continue;
86 strncpy(user,direntp->d_name,dlen);
87 user[dlen]=0;
88 } else {
89 user[0]='\0';
90 }
91
92 uinfo=userinfo_find_from_file(user);
93 if (!uinfo) {
94 debuga(_("Ignoring unknown user file %s\n"),user);
95 continue;
96 }
97 strcpy(u2,uinfo->id);
98 if(Ip2Name && uinfo->id_is_ip) {
99 strcpy(ipantes,u2);
100 ip2name(u2,sizeof(u2));
101 strcpy(nameantes,u2);
102 }
103 user_find(uinfo->label,MAX_USER_LEN, u2);
104
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);
107 exit(1);
108 }
109
110 if((fp_in=MY_FOPEN(tmp3,"r"))==NULL){
111 debuga(_("(datafile) Cannot open file %s\n"),tmp3);
112 exit(1);
113 }
114
115 if (longline_prepare(&line)<0) {
116 debuga(_("Not enough memory to read the downloaded files.\n"));
117 exit(1);
118 }
119
120 ttopen=0;
121 new_user=1;
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);
129 exit(1);
130 }
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);
133 exit(1);
134 }
135
136 if(Ip2Name) {
137 if(strcmp(accip,ipantes) != 0) {
138 strcpy(ipantes,accip);
139 ip2name(accip,sizeof(accip));
140 strcpy(nameantes,accip);
141 }
142 else strcpy(accip,nameantes);
143 }
144
145 if(!rtotal){
146 url_len=strlen(accurl);
147 if (!oldurl || url_len>=ourl_size) {
148 ourl_size=url_len+1;
149 oldurl=realloc(oldurl,ourl_size);
150 if (!oldurl) {
151 debuga(_("Not enough memory to store the url\n"));
152 exit(1);
153 }
154 }
155 strcpy(oldurl,accurl);
156 strcpy(oldacccode,acccode);
157 strcpy(oldaccip,accip);
158 strcpy(oldaccdia,accdia);
159 strcpy(oldacchora,acchora);
160 new_user=0;
161 rtotal++;
162 }
163 same_url=(strcmp(oldurl,accurl) == 0);
164
165 if(!same_url || new_user){
166 if(strstr(oldacccode,"DENIED") != 0)
167 strcpy(oldmsg,_("DENIED"));
168 else
169 strcpy(oldmsg,"OK");
170 if(!fp_ou && (fp_ou=MY_FOPEN(DataFile,"w"))==NULL){
171 debuga(_("(datafile) Cannot open file %s\n"),DataFile);
172 exit(1);
173 }
174 saverecs(fp_ou,uinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
175 nacc=0;
176 nbytes=0;
177 nelap=0;
178 incache=0;
179 oucache=0;
180 new_user=0;
181 }
182
183 nacc++;
184 nbytes+=accbytes;
185 nelap+=accelap;
186
187 strcpy(crc2,acccode);
188 str=strchr(crc2,'/');
189 if (str) *str='\0';
190
191 if(strstr(crc2,"MISS") != 0) oucache+=accbytes;
192 else incache+=accbytes;
193
194 if (!same_url) {
195 url_len=strlen(accurl);
196 if (url_len>=ourl_size) {
197 ourl_size=url_len+1;
198 oldurl=realloc(oldurl,ourl_size);
199 if (!oldurl) {
200 debuga(_("Not enough memory to store the url\n"));
201 exit(1);
202 }
203 }
204 strcpy(oldurl,accurl);
205 }
206 new_user=0;
207 strcpy(oldacccode,acccode);
208 strcpy(oldaccip,accip);
209 strcpy(oldaccdia,accdia);
210 strcpy(oldacchora,acchora);
211 }
212
213 fclose(fp_in);
214 longline_free(&line);
215 }
216 (void)closedir( dirp );
217 if (oldurl) free(oldurl);
218 if (fp_ou) fclose(fp_ou);
219
220 if(debug)
221 debuga(_("Datafile %s written successfully\n"),DataFile);
222 }
223
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)
225 {
226 char val[20];
227 char name[512];
228
229 if((DataFileFields & DATA_FIELD_USER) != 0) {
230 fputs(uinfo->label,fp_ou);
231 fputc(DataFileDelimiter[0],fp_ou);
232 }
233 if((DataFileFields & DATA_FIELD_DATE) != 0) {
234 fputs(dia,fp_ou);
235 fputc(DataFileDelimiter[0],fp_ou);
236 }
237 if((DataFileFields & DATA_FIELD_TIME) != 0) {
238 fputs(hora,fp_ou);
239 fputc(DataFileDelimiter[0],fp_ou);
240 }
241 if((DataFileFields & DATA_FIELD_URL) != 0) {
242 url_hostname(url,name,sizeof(name));
243 if (DataFileUrl == DATAFILEURL_IP) name2ip(name);
244 fputs(name,fp_ou);
245 fputc(DataFileDelimiter[0],fp_ou);
246 }
247 if((DataFileFields & DATA_FIELD_CONNECT) != 0) {
248 my_lltoa(nacc,val,sizeof(val),0);
249 fputs(val,fp_ou);
250 fputc(DataFileDelimiter[0],fp_ou);
251 }
252 if((DataFileFields & DATA_FIELD_BYTES) != 0) {
253 my_lltoa(nbytes,val,sizeof(val),0);
254 fputs(val,fp_ou);
255 fputc(DataFileDelimiter[0],fp_ou);
256 }
257 if((DataFileFields & DATA_FIELD_IN_CACHE) != 0) {
258 my_lltoa(incache,val,sizeof(val),0);
259 fputs(val,fp_ou);
260 fputc(DataFileDelimiter[0],fp_ou);
261 }
262 if((DataFileFields & DATA_FIELD_OUT_CACHE) != 0) {
263 my_lltoa(oucache,val,sizeof(val),0);
264 fputs(val,fp_ou);
265 fputc(DataFileDelimiter[0],fp_ou);
266 }
267 if((DataFileFields & DATA_FIELD_ELAPSED) != 0) {
268 my_lltoa(nelap,val,sizeof(val),0);
269 fputs(val,fp_ou);
270 fputc(DataFileDelimiter[0],fp_ou);
271 }
272
273 fputc('\n',fp_ou);
274 }