]> git.ipfire.org Git - thirdparty/sarg.git/blob - datafile.c
Merge messages about IP addresses.
[thirdparty/sarg.git] / datafile.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2015
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 FILE *fp_in, *fp_ou=NULL;
35
36 char *buf;
37 char accdia[11], acchora[9], accip[MAXLEN], *accurl;
38 char oldaccdia[11], oldacchora[9], oldaccip[MAXLEN];
39 char *oldurl;
40 char acccode[50], oldacccode[50];
41 char ipantes[MAXLEN], nameantes[MAXLEN];
42 char accsmart[MAXLEN];
43 char crc2[50];
44 char *str;
45 char tmp3[MAXLEN];
46 char u2[MAX_USER_LEN];
47 char userlabel[MAX_USER_LEN];
48 userscan uscan;
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;
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;
64
65 ipantes[0]='\0';
66 nameantes[0]='\0';
67
68 oldurl=NULL;
69 ourl_size=0;
70
71 uscan=userinfo_startscan();
72 if (uscan == NULL) {
73 debuga(_("Cannot enumerate the user list\n"));
74 exit(EXIT_FAILURE);
75 }
76 while ( (uinfo = userinfo_advancescan(uscan)) != NULL ) {
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 }
83 user_find(userlabel,MAX_USER_LEN, u2);
84 userinfo_label(uinfo,userlabel);
85 if(debug) debuga(_("Reading user file: %s/%s\n"),tmp,uinfo->filename);
86
87 sort_users_log(tmp,debug,uinfo);
88 if (snprintf(tmp3,sizeof(tmp3),"%s/%s.user_log",tmp,uinfo->filename)>=sizeof(tmp3)) {
89 debuga(_("Path too long: "));
90 debuga_more("%s/%s.user_log\n",tmp,uinfo->filename);
91 exit(EXIT_FAILURE);
92 }
93
94 if((fp_in=MY_FOPEN(tmp3,"r"))==NULL){
95 debuga(_("Cannot open file \"%s\": %s\n"),tmp3,strerror(errno));
96 exit(EXIT_FAILURE);
97 }
98
99 if ((line=longline_create())==NULL) {
100 debuga(_("Not enough memory to read the downloaded files.\n"));
101 exit(EXIT_FAILURE);
102 }
103
104 ttopen=0;
105 new_user=1;
106 while((buf=longline_read(fp_in,line))!=NULL) {
107 getword_start(&gwarea,buf);
108 if (getword(accdia,sizeof(accdia),&gwarea,'\t')<0 || getword(acchora,sizeof(acchora),&gwarea,'\t')<0 ||
109 getword(accip,sizeof(accip),&gwarea,'\t')<0 ||
110 getword_ptr(buf,&accurl,&gwarea,'\t')<0 || getword_atoll(&accbytes,&gwarea,'\t')<0 ||
111 getword(acccode,sizeof(acccode),&gwarea,'\t')<0 || getword_atoll(&accelap,&gwarea,'\t')<0) {
112 debuga(_("Invalid record in file \"%s\"\n"),tmp3);
113 exit(EXIT_FAILURE);
114 }
115 if (getword_skip(20000,&gwarea,'"')<0 || getword(accsmart,sizeof(accsmart),&gwarea,'"')<0) {
116 debuga(_("Invalid smart info in file \"%s\"\n"),tmp3);
117 exit(EXIT_FAILURE);
118 }
119
120 if(Ip2Name) {
121 if(strcmp(accip,ipantes) != 0) {
122 strcpy(ipantes,accip);
123 ip2name(accip,sizeof(accip));
124 strcpy(nameantes,accip);
125 }
126 else strcpy(accip,nameantes);
127 }
128
129 if(!rtotal){
130 url_len=strlen(accurl);
131 if (!oldurl || url_len>=ourl_size) {
132 ourl_size=url_len+1;
133 oldurl=realloc(oldurl,ourl_size);
134 if (!oldurl) {
135 debuga(_("Not enough memory to store the url\n"));
136 exit(EXIT_FAILURE);
137 }
138 }
139 strcpy(oldurl,accurl);
140 strcpy(oldacccode,acccode);
141 strcpy(oldaccip,accip);
142 strcpy(oldaccdia,accdia);
143 strcpy(oldacchora,acchora);
144 new_user=0;
145 rtotal++;
146 }
147 same_url=(strcmp(oldurl,accurl) == 0);
148
149 if(!same_url || new_user){
150 if(!fp_ou && (fp_ou=MY_FOPEN(DataFile,"w"))==NULL){
151 debuga(_("Cannot open file \"%s\": %s\n"),DataFile,strerror(errno));
152 exit(EXIT_FAILURE);
153 }
154 saverecs(fp_ou,uinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
155 nacc=0;
156 nbytes=0;
157 nelap=0;
158 incache=0;
159 oucache=0;
160 new_user=0;
161 }
162
163 nacc++;
164 nbytes+=accbytes;
165 nelap+=accelap;
166
167 strcpy(crc2,acccode);
168 str=strchr(crc2,'/');
169 if (str) *str='\0';
170
171 if(strstr(crc2,"MISS") != 0) oucache+=accbytes;
172 else incache+=accbytes;
173
174 if (!same_url) {
175 url_len=strlen(accurl);
176 if (url_len>=ourl_size) {
177 ourl_size=url_len+1;
178 oldurl=realloc(oldurl,ourl_size);
179 if (!oldurl) {
180 debuga(_("Not enough memory to store the url\n"));
181 exit(EXIT_FAILURE);
182 }
183 }
184 strcpy(oldurl,accurl);
185 }
186 new_user=0;
187 strcpy(oldacccode,acccode);
188 strcpy(oldaccip,accip);
189 strcpy(oldaccdia,accdia);
190 strcpy(oldacchora,acchora);
191 }
192
193 if (fclose(fp_in)==EOF) {
194 debuga(_("Read error in \"%s\": %s\n"),tmp3,strerror(errno));
195 exit(EXIT_FAILURE);
196 }
197 longline_destroy(&line);
198 }
199 userinfo_stopscan(uscan);
200 if (oldurl) free(oldurl);
201 if (fp_ou && fclose(fp_ou)==EOF) {
202 debuga(_("Write error in \"%s\": %s\n"),DataFile,strerror(errno));
203 exit(EXIT_FAILURE);
204 }
205
206 if(debug)
207 debuga(_("Datafile %s written successfully\n"),DataFile);
208 }
209
210 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)
211 {
212 char val[20];
213 char name[512];
214
215 if((DataFileFields & DATA_FIELD_USER) != 0) {
216 fputs(uinfo->label,fp_ou);
217 fputc(DataFileDelimiter[0],fp_ou);
218 }
219 if((DataFileFields & DATA_FIELD_DATE) != 0) {
220 fputs(dia,fp_ou);
221 fputc(DataFileDelimiter[0],fp_ou);
222 }
223 if((DataFileFields & DATA_FIELD_TIME) != 0) {
224 fputs(hora,fp_ou);
225 fputc(DataFileDelimiter[0],fp_ou);
226 }
227 if((DataFileFields & DATA_FIELD_URL) != 0) {
228 url_hostname(url,name,sizeof(name));
229 if (DataFileUrl == DATAFILEURL_IP) name2ip(name,sizeof(name));
230 fputs(name,fp_ou);
231 fputc(DataFileDelimiter[0],fp_ou);
232 }
233 if((DataFileFields & DATA_FIELD_CONNECT) != 0) {
234 my_lltoa(nacc,val,sizeof(val),0);
235 fputs(val,fp_ou);
236 fputc(DataFileDelimiter[0],fp_ou);
237 }
238 if((DataFileFields & DATA_FIELD_BYTES) != 0) {
239 my_lltoa(nbytes,val,sizeof(val),0);
240 fputs(val,fp_ou);
241 fputc(DataFileDelimiter[0],fp_ou);
242 }
243 if((DataFileFields & DATA_FIELD_IN_CACHE) != 0) {
244 my_lltoa(incache,val,sizeof(val),0);
245 fputs(val,fp_ou);
246 fputc(DataFileDelimiter[0],fp_ou);
247 }
248 if((DataFileFields & DATA_FIELD_OUT_CACHE) != 0) {
249 my_lltoa(oucache,val,sizeof(val),0);
250 fputs(val,fp_ou);
251 fputc(DataFileDelimiter[0],fp_ou);
252 }
253 if((DataFileFields & DATA_FIELD_ELAPSED) != 0) {
254 my_lltoa(nelap,val,sizeof(val),0);
255 fputs(val,fp_ou);
256 fputc(DataFileDelimiter[0],fp_ou);
257 }
258
259 fputc('\n',fp_ou);
260 }