]> git.ipfire.org Git - thirdparty/sarg.git/blob - datafile.c
Removed Pedro Orso's e-mail as per his request
[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(char *dirname, char *user, 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;
36
37 char accdia[11], acchora[9], accuser[MAXLEN], accip[MAXLEN], accurl[MAXLEN];
38 char oldaccdia[11], oldacchora[9], oldaccip[MAXLEN];
39 char dirname[MAXLEN], wdirname[MAXLEN], oldurl[MAXLEN], oldaccuser[MAXLEN];
40 char olduser[MAXLEN], oldmsg[50], acccode[50], oldacccode[50];
41 char ipantes[MAXLEN], nameantes[MAXLEN];
42 char accsmart[MAXLEN];
43 char crc2[50];
44 char wdname[MAXLEN];
45 char *str;
46 DIR *dirp;
47 struct dirent *direntp;
48 long long int nbytes=0;
49 long long int nelap=0;
50 long long int nacc=0;
51 long long 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 struct getwordstruct gwarea;
57
58 ipantes[0]='\0';
59 nameantes[0]='\0';
60
61 olduser[0]='\0';
62 strncat(tmp,"/sarg",5);
63
64 dirp = opendir(tmp);
65 while ( (direntp = readdir( dirp )) != NULL ) {
66 if(strstr(direntp->d_name,".log") == 0)
67 continue;
68 if (snprintf(tmp3,sizeof(tmp3),"%s/%s",tmp,direntp->d_name)>=sizeof(tmp3)) {
69 fprintf(stderr,"SARG: (datafile) directory path too long: %s/%s\n",tmp,direntp->d_name);
70 exit(1);
71 }
72
73 if((fp_in=MY_FOPEN(tmp3,"r"))==NULL){
74 fprintf(stderr, "SARG: (datafile) %s: %s\n",text[45],tmp);
75 exit(1);
76 }
77 strcpy(wdname,direntp->d_name);
78 str=strchr(wdname,'.');
79 if (str) *str='\0';
80 strcat(user,wdname);
81
82 ttopen=0;
83 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
84 getword_start(&gwarea,buf);
85 if (getword(accdia,sizeof(accdia),&gwarea,' ')<0 || getword(acchora,sizeof(acchora),&gwarea,' ')<0 ||
86 getword(accuser,sizeof(accuser),&gwarea,' ')<0 || getword(accip,sizeof(accip),&gwarea,' ')<0 ||
87 getword(accurl,sizeof(accurl),&gwarea,' ')<0 || getword_atoll(&accbytes,&gwarea,' ')<0 ||
88 getword(acccode,sizeof(acccode),&gwarea,' ')<0 || getword_atoll(&accelap,&gwarea,' ')<0 ||
89 getword_skip(20000,&gwarea,' ')<0 || getword(accsmart,sizeof(accsmart),&gwarea,'"')<0) {
90 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp3);
91 exit(1);
92 }
93
94 if(Ip2Name) {
95 if(strcmp(accip,ipantes) != 0) {
96 strcpy(ipantes,accip);
97 ip2name(accip,sizeof(accip));
98 strcpy(nameantes,accip);
99 }
100 else strcpy(accip,nameantes);
101 }
102
103 if(!rtotal){
104 strcpy(oldurl,accurl);
105 strcpy(oldacccode,acccode);
106 strcpy(oldaccuser,accuser);
107 strcpy(oldaccip,accip);
108 strcpy(oldaccdia,accdia);
109 strcpy(oldacchora,acchora);
110 rtotal++;
111 }
112
113 if(strcmp(oldurl,accurl) != 0 || strcmp(oldaccuser,accuser) != 0){
114 strcpy(oldmsg,"OK");
115 if(strstr(oldacccode,"DENIED") != 0) strcpy(oldmsg,text[46]);
116 gravatmp(oldaccuser,oldurl,nacc,nbytes,oldmsg,nelap,indexonly,incache,oucache);
117 strcpy(wdirname,dirname);
118 saverecs(wdirname,oldaccuser,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
119 nacc=0;
120 nbytes=0;
121 nelap=0;
122 incache=0;
123 oucache=0;
124 if(strcmp(oldaccuser,accuser) != 0) ind2=0;
125 }
126
127 nacc++;
128 nbytes+=accbytes;
129 nelap+=accelap;
130
131 strcpy(crc2,acccode);
132 str=strchr(crc2,'/');
133 if (str) *str='\0';
134
135 if(strstr(crc2,"MISS") != 0) oucache+=accbytes;
136 else incache+=accbytes;
137
138 strcpy(oldurl,accurl);
139 strcpy(oldaccuser,accuser);
140 strcpy(oldacccode,acccode);
141 strcpy(oldaccip,accip);
142 strcpy(oldaccdia,accdia);
143 strcpy(oldacchora,acchora);
144 }
145
146 fclose(fp_in);
147 }
148
149 (void)closedir( dirp );
150 if(debug)
151 debuga("Datafile %s successfully",DataFile);
152 }
153
154 void saverecs(char *dirname, char *user, 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)
155 {
156 FILE *fp_ou;
157 char reg[MAXLEN];
158
159 if((fp_ou=MY_FOPEN(DataFile,"a"))==NULL){
160 fprintf(stderr, "SARG: (datafile) %s: %s\n",text[45],DataFile);
161 exit(1);
162 }
163
164 my_lltoa(nacc,val1,0);
165 my_lltoa(nbytes,val2,0);
166 my_lltoa(nelap,val3,0);
167 my_lltoa(incache,val4,0);
168 my_lltoa(oucache,val5,0);
169
170 if(strstr(DataFileFields,"user") != 0) {
171 strcpy(reg,user);
172 strncat(reg,DataFileDelimiter,1);
173 }
174 if(strstr(DataFileFields,"date") != 0) {
175 strncat(reg,dia,strlen(dia));
176 strncat(reg,DataFileDelimiter,1);
177 }
178 if(strstr(DataFileFields,"time") != 0) {
179 strncat(reg,hora,strlen(hora));
180 strncat(reg,DataFileDelimiter,1);
181 }
182 if(strstr(DataFileFields,"url") != 0) {
183 strcpy(name,url);
184 if (strcmp(DataFileUrl,"ip") == 0) name2ip(name);
185 strncat(reg,name,strlen(name));
186 strncat(reg,DataFileDelimiter,1);
187 }
188 if(strstr(DataFileFields,"connect") != 0) {
189 strncat(reg,val1,strlen(val1));
190 strncat(reg,DataFileDelimiter,1);
191 }
192 if(strstr(DataFileFields,"bytes") != 0) {
193 strncat(reg,val2,strlen(val2));
194 strncat(reg,DataFileDelimiter,1);
195 }
196 if(strstr(DataFileFields,"in_cache") != 0) {
197 strncat(reg,val4,strlen(val4));
198 strncat(reg,DataFileDelimiter,1);
199 }
200 if(strstr(DataFileFields,"out_cache") != 0) {
201 strncat(reg,val5,strlen(val5));
202 strncat(reg,DataFileDelimiter,1);
203 }
204 if(strstr(DataFileFields,"elapsed") != 0) {
205 strncat(reg,val3,strlen(val3));
206 strncat(reg,DataFileDelimiter,1);
207 }
208
209 reg[strlen(reg)-1]='\n';
210 fputs(reg,fp_ou);
211
212 fclose(fp_ou);
213 }