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