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