]> git.ipfire.org Git - thirdparty/sarg.git/blob - datafile.c
f8533ddc4dded90a241e5c54a44669af5ef35cf2
[thirdparty/sarg.git] / datafile.c
1 /*
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
3 * 1998, 2008
4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
5 *
6 * SARG donations:
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
23 *
24 */
25
26 #include "include/conf.h"
27
28 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);
29 void grava_SmartFilter(char *dirname, char *user, char *ip, char *data, char *hora, char *url, char *smart);
30
31 void data_file(char *tmp)
32 {
33
34 FILE *fp_in;
35
36 char accdia[11], acchora[9], accuser[MAXLEN], accip[MAXLEN], accurl[MAXLEN], accbytes[12], accelap[10];
37 char oldaccdia[11], oldacchora[9], oldaccip[MAXLEN];
38 char dirname[MAXLEN], wdirname[MAXLEN], oldurl[MAXLEN], oldaccuser[MAXLEN];
39 char olduser[MAXLEN], oldmsg[50], acccode[50], oldaccelap[10], oldacccode[50];
40 char ipantes[MAXLEN], nameantes[MAXLEN];
41 char accsmart[MAXLEN];
42 char Msg[MAXLEN];
43 char wcrc[50];
44 char crc2[50];
45 char wdname[MAXLEN];
46 char wname2[MAXLEN];
47 DIR *dirp;
48 struct dirent *direntp;
49 long long int nbytes=0;
50 long long int nelap=0;
51 long long int nacc=0;
52 long long int rtotal=0;
53 long long int incache=0;
54 long long int oucache=0;
55
56 ipantes[0]='\0';
57 nameantes[0]='\0';
58
59 olduser[0]='\0';
60 strncat(tmp,"/sarg",5);
61
62 dirp = opendir(tmp);
63 while ( (direntp = readdir( dirp )) != NULL ) {
64 if(strstr(direntp->d_name,".log") == 0)
65 continue;
66 sprintf(tmp3,"%s/%s",tmp,direntp->d_name);
67
68 if((fp_in=fopen(tmp3,"r"))==NULL){
69 fprintf(stderr, "SARG: (datafile) %s: %s\n",text[45],tmp);
70 exit(1);
71 }
72 strcpy(wdname,direntp->d_name);
73 strip_prefix:
74 getword(wname2,wdname,'.');
75 strcat(user,wname2);
76
77 ttopen=0;
78 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
79 getword(accdia,buf,' ');
80 getword(acchora,buf,' ');
81 getword(accuser,buf,' ');
82 getword(accip,buf,' ');
83 getword(accurl,buf,' ');
84 getword(accbytes,buf,' ');
85 getword(acccode,buf,' ');
86 getword(accelap,buf,' ');
87 getword(accsmart,buf,' ');
88 getword(accsmart,buf,'"');
89
90 if(strcmp(Ip2Name,"yes") == 0) {
91 if(strcmp(accip,ipantes) != 0) {
92 strcpy(ipantes,accip);
93 ip2name(accip);
94 strcpy(nameantes,accip);
95 }
96 else strcpy(accip,nameantes);
97 }
98
99 if(!rtotal){
100 strcpy(oldurl,accurl);
101 strcpy(oldacccode,acccode);
102 strcpy(oldaccelap,accelap);
103 strcpy(oldaccuser,accuser);
104 strcpy(oldaccip,accip);
105 strcpy(oldaccdia,accdia);
106 strcpy(oldacchora,acchora);
107 rtotal++;
108 }
109
110 if(strcmp(oldurl,accurl) != 0 || strcmp(oldaccuser,accuser) != 0){
111 strcpy(oldmsg,"OK");
112 if(strstr(oldacccode,"DENIED") != 0) strcpy(oldmsg,text[46]);
113 strcpy(wdirname,dirname);
114 gravatmp(oldaccuser,wdirname,oldurl,nacc,nbytes,oldmsg,nelap,indexonly,incache,oucache);
115 strcpy(wdirname,dirname);
116 saverecs(wdirname,oldaccuser,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
117 nacc=0;
118 nbytes=0;
119 nelap=0;
120 incache=0;
121 oucache=0;
122 if(strcmp(oldaccuser,accuser) != 0) ind2=0;
123 }
124
125 nacc++;
126 nbytes+=my_atoll(accbytes);
127 nelap+=my_atoll(accelap);
128
129 strcpy(wcrc,acccode);
130 getword(crc2,wcrc,'/');
131
132 if(strstr(crc2,"MISS") != 0) oucache+=my_atoll(accbytes);
133 else incache+=my_atoll(accbytes);
134
135 strcpy(oldurl,accurl);
136 strcpy(oldaccuser,accuser);
137 strcpy(oldacccode,acccode);
138 strcpy(oldaccelap,accelap);
139 strcpy(oldaccip,accip);
140 strcpy(oldaccdia,accdia);
141 strcpy(oldacchora,acchora);
142 }
143
144 fclose(fp_in);
145 }
146
147 (void)closedir( dirp );
148 if(debug) {
149 sprintf(Msg,"Datafile %s successfully",DataFile);
150 debuga(Msg);
151 }
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
157 FILE *fp_ou;
158 char reg[MAXLEN];
159
160 if((fp_ou=fopen(DataFile,"a"))==NULL){
161 fprintf(stderr, "SARG: (datafile) %s: %s\n",text[45],DataFile);
162 exit(1);
163 }
164
165 my_lltoa(nacc,val1,0);
166 my_lltoa(nbytes,val2,0);
167 my_lltoa(nelap,val3,0);
168 my_lltoa(incache,val4,0);
169 my_lltoa(oucache,val5,0);
170
171 if(strstr(DataFileFields,"user") != 0) {
172 strcpy(reg,user);
173 strncat(reg,DataFileDelimiter,1);
174 }
175 if(strstr(DataFileFields,"date") != 0) {
176 strncat(reg,dia,strlen(dia));
177 strncat(reg,DataFileDelimiter,1);
178 }
179 if(strstr(DataFileFields,"time") != 0) {
180 strncat(reg,hora,strlen(hora));
181 strncat(reg,DataFileDelimiter,1);
182 }
183 if(strstr(DataFileFields,"url") != 0) {
184 strcpy(name,url);
185 if (strcmp(DataFileUrl,"ip") == 0) name2ip(name);
186 strncat(reg,name,strlen(name));
187 strncat(reg,DataFileDelimiter,1);
188 }
189 if(strstr(DataFileFields,"connect") != 0) {
190 strncat(reg,val1,strlen(val1));
191 strncat(reg,DataFileDelimiter,1);
192 }
193 if(strstr(DataFileFields,"bytes") != 0) {
194 strncat(reg,val2,strlen(val2));
195 strncat(reg,DataFileDelimiter,1);
196 }
197 if(strstr(DataFileFields,"in_cache") != 0) {
198 strncat(reg,val4,strlen(val4));
199 strncat(reg,DataFileDelimiter,1);
200 }
201 if(strstr(DataFileFields,"out_cache") != 0) {
202 strncat(reg,val5,strlen(val5));
203 strncat(reg,DataFileDelimiter,1);
204 }
205 if(strstr(DataFileFields,"elapsed") != 0) {
206 strncat(reg,val3,strlen(val3));
207 strncat(reg,DataFileDelimiter,1);
208 }
209
210 reg[strlen(reg)-1]='\n';
211 fputs(reg,fp_ou);
212
213 fclose(fp_ou);
214 }