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