]>
Commit | Line | Data |
---|---|---|
25697a35 | 1 | /* |
94ff9470 | 2 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net |
67302a9e | 3 | * 1998, 2013 |
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 | 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); |
25697a35 GS |
31 | |
32 | void 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) { | |
73 | debuga(_("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); | |
93551487 | 85 | if(debug) debuga(_("Reading user file: %s/%s\n"),tmp,uinfo->filename); |
f2ec8c75 | 86 | |
461b479d | 87 | sort_users_log(tmp,debug,uinfo); |
170a77ea FM |
88 | if (snprintf(tmp3,sizeof(tmp3),"%s/%s.user_log",tmp,uinfo->filename)>=sizeof(tmp3)) { |
89 | debuga(_("(datafile) directory path too long: %s/%s.user_log\n"),tmp,uinfo->filename); | |
9bd92830 FM |
90 | exit(EXIT_FAILURE); |
91 | } | |
f2ec8c75 | 92 | |
9bd92830 | 93 | if((fp_in=MY_FOPEN(tmp3,"r"))==NULL){ |
d6f0349d | 94 | debuga(_("(datafile) Cannot open file %s: %s\n"),tmp3,strerror(errno)); |
9bd92830 FM |
95 | exit(EXIT_FAILURE); |
96 | } | |
25697a35 | 97 | |
9bd92830 FM |
98 | if ((line=longline_create())==NULL) { |
99 | debuga(_("Not enough memory to read the downloaded files.\n")); | |
100 | exit(EXIT_FAILURE); | |
101 | } | |
f84a35a3 | 102 | |
9bd92830 FM |
103 | ttopen=0; |
104 | new_user=1; | |
105 | while((buf=longline_read(fp_in,line))!=NULL) { | |
106 | getword_start(&gwarea,buf); | |
107 | if (getword(accdia,sizeof(accdia),&gwarea,'\t')<0 || getword(acchora,sizeof(acchora),&gwarea,'\t')<0 || | |
007905af FM |
108 | getword(accip,sizeof(accip),&gwarea,'\t')<0 || |
109 | getword_ptr(buf,&accurl,&gwarea,'\t')<0 || getword_atoll(&accbytes,&gwarea,'\t')<0 || | |
110 | getword(acccode,sizeof(acccode),&gwarea,'\t')<0 || getword_atoll(&accelap,&gwarea,'\t')<0) { | |
9bd92830 FM |
111 | debuga(_("There is a broken record or garbage in file %s\n"),tmp3); |
112 | exit(EXIT_FAILURE); | |
113 | } | |
114 | if (getword_skip(20000,&gwarea,'"')<0 || getword(accsmart,sizeof(accsmart),&gwarea,'"')<0) { | |
115 | debuga(_("There is an invalid smart info in file %s\n"),tmp3); | |
116 | exit(EXIT_FAILURE); | |
117 | } | |
25697a35 | 118 | |
9bd92830 FM |
119 | if(Ip2Name) { |
120 | if(strcmp(accip,ipantes) != 0) { | |
121 | strcpy(ipantes,accip); | |
122 | ip2name(accip,sizeof(accip)); | |
123 | strcpy(nameantes,accip); | |
124 | } | |
125 | else strcpy(accip,nameantes); | |
126 | } | |
5542cb59 | 127 | |
9bd92830 FM |
128 | if(!rtotal){ |
129 | url_len=strlen(accurl); | |
130 | if (!oldurl || url_len>=ourl_size) { | |
131 | ourl_size=url_len+1; | |
132 | oldurl=realloc(oldurl,ourl_size); | |
133 | if (!oldurl) { | |
134 | debuga(_("Not enough memory to store the url\n")); | |
135 | exit(EXIT_FAILURE); | |
136 | } | |
137 | } | |
138 | strcpy(oldurl,accurl); | |
139 | strcpy(oldacccode,acccode); | |
140 | strcpy(oldaccip,accip); | |
141 | strcpy(oldaccdia,accdia); | |
142 | strcpy(oldacchora,acchora); | |
143 | new_user=0; | |
144 | rtotal++; | |
145 | } | |
146 | same_url=(strcmp(oldurl,accurl) == 0); | |
5542cb59 | 147 | |
9bd92830 FM |
148 | if(!same_url || new_user){ |
149 | if(!fp_ou && (fp_ou=MY_FOPEN(DataFile,"w"))==NULL){ | |
d6f0349d | 150 | debuga(_("(datafile) Cannot open file %s: %s\n"),DataFile,strerror(errno)); |
9bd92830 FM |
151 | exit(EXIT_FAILURE); |
152 | } | |
153 | saverecs(fp_ou,uinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache); | |
154 | nacc=0; | |
155 | nbytes=0; | |
156 | nelap=0; | |
157 | incache=0; | |
158 | oucache=0; | |
159 | new_user=0; | |
160 | } | |
5542cb59 | 161 | |
9bd92830 FM |
162 | nacc++; |
163 | nbytes+=accbytes; | |
164 | nelap+=accelap; | |
25697a35 | 165 | |
9bd92830 FM |
166 | strcpy(crc2,acccode); |
167 | str=strchr(crc2,'/'); | |
168 | if (str) *str='\0'; | |
5542cb59 | 169 | |
9bd92830 FM |
170 | if(strstr(crc2,"MISS") != 0) oucache+=accbytes; |
171 | else incache+=accbytes; | |
25697a35 | 172 | |
9bd92830 FM |
173 | if (!same_url) { |
174 | url_len=strlen(accurl); | |
175 | if (url_len>=ourl_size) { | |
176 | ourl_size=url_len+1; | |
177 | oldurl=realloc(oldurl,ourl_size); | |
178 | if (!oldurl) { | |
179 | debuga(_("Not enough memory to store the url\n")); | |
180 | exit(EXIT_FAILURE); | |
181 | } | |
182 | } | |
183 | strcpy(oldurl,accurl); | |
184 | } | |
185 | new_user=0; | |
186 | strcpy(oldacccode,acccode); | |
187 | strcpy(oldaccip,accip); | |
188 | strcpy(oldaccdia,accdia); | |
189 | strcpy(oldacchora,acchora); | |
190 | } | |
5542cb59 | 191 | |
9bd92830 FM |
192 | fclose(fp_in); |
193 | longline_destroy(&line); | |
194 | } | |
93551487 | 195 | userinfo_stopscan(uscan); |
9bd92830 | 196 | if (oldurl) free(oldurl); |
507460ae FM |
197 | if (fp_ou && fclose(fp_ou)==EOF) { |
198 | debuga(_("Write error in %s: %s\n"),DataFile,strerror(errno)); | |
199 | exit(EXIT_FAILURE); | |
200 | } | |
5542cb59 | 201 | |
9bd92830 FM |
202 | if(debug) |
203 | debuga(_("Datafile %s written successfully\n"),DataFile); | |
25697a35 GS |
204 | } |
205 | ||
f84a35a3 | 206 | 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) |
25697a35 | 207 | { |
9bd92830 FM |
208 | char val[20]; |
209 | char name[512]; | |
25697a35 | 210 | |
9bd92830 FM |
211 | if((DataFileFields & DATA_FIELD_USER) != 0) { |
212 | fputs(uinfo->label,fp_ou); | |
213 | fputc(DataFileDelimiter[0],fp_ou); | |
214 | } | |
215 | if((DataFileFields & DATA_FIELD_DATE) != 0) { | |
216 | fputs(dia,fp_ou); | |
217 | fputc(DataFileDelimiter[0],fp_ou); | |
218 | } | |
219 | if((DataFileFields & DATA_FIELD_TIME) != 0) { | |
220 | fputs(hora,fp_ou); | |
221 | fputc(DataFileDelimiter[0],fp_ou); | |
222 | } | |
223 | if((DataFileFields & DATA_FIELD_URL) != 0) { | |
224 | url_hostname(url,name,sizeof(name)); | |
4afbb7a5 | 225 | if (DataFileUrl == DATAFILEURL_IP) name2ip(name,sizeof(name)); |
9bd92830 FM |
226 | fputs(name,fp_ou); |
227 | fputc(DataFileDelimiter[0],fp_ou); | |
228 | } | |
229 | if((DataFileFields & DATA_FIELD_CONNECT) != 0) { | |
230 | my_lltoa(nacc,val,sizeof(val),0); | |
231 | fputs(val,fp_ou); | |
232 | fputc(DataFileDelimiter[0],fp_ou); | |
233 | } | |
234 | if((DataFileFields & DATA_FIELD_BYTES) != 0) { | |
235 | my_lltoa(nbytes,val,sizeof(val),0); | |
236 | fputs(val,fp_ou); | |
237 | fputc(DataFileDelimiter[0],fp_ou); | |
238 | } | |
239 | if((DataFileFields & DATA_FIELD_IN_CACHE) != 0) { | |
240 | my_lltoa(incache,val,sizeof(val),0); | |
241 | fputs(val,fp_ou); | |
242 | fputc(DataFileDelimiter[0],fp_ou); | |
243 | } | |
244 | if((DataFileFields & DATA_FIELD_OUT_CACHE) != 0) { | |
245 | my_lltoa(oucache,val,sizeof(val),0); | |
246 | fputs(val,fp_ou); | |
247 | fputc(DataFileDelimiter[0],fp_ou); | |
248 | } | |
249 | if((DataFileFields & DATA_FIELD_ELAPSED) != 0) { | |
250 | my_lltoa(nelap,val,sizeof(val),0); | |
251 | fputs(val,fp_ou); | |
252 | fputc(DataFileDelimiter[0],fp_ou); | |
253 | } | |
25697a35 | 254 | |
9bd92830 | 255 | fputc('\n',fp_ou); |
25697a35 | 256 | } |