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