]> git.ipfire.org Git - thirdparty/sarg.git/blame - datafile.c
Output w3c compliant strict html (almost every output).
[thirdparty/sarg.git] / datafile.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
1164c474 3 * 1998, 2010
25697a35
GS
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
1164c474
FM
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
25697a35
GS
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"
5f3cfd1d 28#include "include/defs.h"
25697a35
GS
29
30void 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
31
32void data_file(char *tmp)
33{
34
35 FILE *fp_in;
36
6e792ade 37 char accdia[11], acchora[9], accuser[MAXLEN], accip[MAXLEN], accurl[MAXLEN];
d6e703cc 38 char oldaccdia[11], oldacchora[9], oldaccip[MAXLEN];
25697a35 39 char dirname[MAXLEN], wdirname[MAXLEN], oldurl[MAXLEN], oldaccuser[MAXLEN];
6e792ade 40 char olduser[MAXLEN], oldmsg[50], acccode[50], oldacccode[50];
5542cb59 41 char ipantes[MAXLEN], nameantes[MAXLEN];
25697a35 42 char accsmart[MAXLEN];
25697a35 43 char crc2[50];
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;
c0ec9cc7 55 int dlen;
9c7c6346 56 struct getwordstruct gwarea;
c0ec9cc7 57 const char logext[]=".log";
25697a35
GS
58
59 ipantes[0]='\0';
60 nameantes[0]='\0';
61
62 olduser[0]='\0';
63 strncat(tmp,"/sarg",5);
64
65 dirp = opendir(tmp);
66 while ( (direntp = readdir( dirp )) != NULL ) {
c0ec9cc7
FM
67 dlen=strlen(direntp->d_name)-(sizeof(logext)-1);
68 if (dlen<=0) continue;
69 if(strcmp(direntp->d_name+dlen,logext) != 0)
25697a35 70 continue;
c0ec9cc7 71
9c7c6346
FM
72 if (snprintf(tmp3,sizeof(tmp3),"%s/%s",tmp,direntp->d_name)>=sizeof(tmp3)) {
73 fprintf(stderr,"SARG: (datafile) directory path too long: %s/%s\n",tmp,direntp->d_name);
74 exit(1);
75 }
25697a35 76
936c9905
FM
77 if((fp_in=MY_FOPEN(tmp3,"r"))==NULL){
78 fprintf(stderr, "SARG: (datafile) %s: %s\n",text[45],tmp);
25697a35
GS
79 exit(1);
80 }
25697a35
GS
81
82 ttopen=0;
83 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
9c7c6346
FM
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 ||
6e792ade
FM
87 getword(accurl,sizeof(accurl),&gwarea,' ')<0 || getword_atoll(&accbytes,&gwarea,' ')<0 ||
88 getword(acccode,sizeof(acccode),&gwarea,' ')<0 || getword_atoll(&accelap,&gwarea,' ')<0 ||
9c7c6346 89 getword_skip(20000,&gwarea,' ')<0 || getword(accsmart,sizeof(accsmart),&gwarea,'"')<0) {
4bcb77cf
FM
90 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp3);
91 exit(1);
92 }
5542cb59 93
246c8489 94 if(Ip2Name) {
25697a35
GS
95 if(strcmp(accip,ipantes) != 0) {
96 strcpy(ipantes,accip);
a1c55d8c 97 ip2name(accip,sizeof(accip));
25697a35 98 strcpy(nameantes,accip);
5542cb59
FM
99 }
100 else strcpy(accip,nameantes);
25697a35 101 }
5542cb59 102
25697a35
GS
103 if(!rtotal){
104 strcpy(oldurl,accurl);
105 strcpy(oldacccode,acccode);
25697a35
GS
106 strcpy(oldaccuser,accuser);
107 strcpy(oldaccip,accip);
108 strcpy(oldaccdia,accdia);
109 strcpy(oldacchora,acchora);
110 rtotal++;
111 }
5542cb59 112
25697a35
GS
113 if(strcmp(oldurl,accurl) != 0 || strcmp(oldaccuser,accuser) != 0){
114 strcpy(oldmsg,"OK");
d6e703cc 115 if(strstr(oldacccode,"DENIED") != 0) strcpy(oldmsg,text[46]);
0a4e18e1 116 gravatmp(oldaccuser,oldurl,nacc,nbytes,oldmsg,nelap,indexonly,incache,oucache);
25697a35 117 strcpy(wdirname,dirname);
14c2eac6 118 saverecs(wdirname,oldaccuser,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
25697a35
GS
119 nacc=0;
120 nbytes=0;
121 nelap=0;
122 incache=0;
123 oucache=0;
d6e703cc 124 if(strcmp(oldaccuser,accuser) != 0) ind2=0;
25697a35
GS
125 }
126
127 nacc++;
6e792ade
FM
128 nbytes+=accbytes;
129 nelap+=accelap;
5542cb59 130
9c7c6346
FM
131 strcpy(crc2,acccode);
132 str=strchr(crc2,'/');
133 if (str) *str='\0';
25697a35 134
6e792ade
FM
135 if(strstr(crc2,"MISS") != 0) oucache+=accbytes;
136 else incache+=accbytes;
5542cb59 137
25697a35
GS
138 strcpy(oldurl,accurl);
139 strcpy(oldaccuser,accuser);
140 strcpy(oldacccode,acccode);
25697a35
GS
141 strcpy(oldaccip,accip);
142 strcpy(oldaccdia,accdia);
143 strcpy(oldacchora,acchora);
144 }
5542cb59 145
25697a35 146 fclose(fp_in);
c0ec9cc7 147 }
5542cb59 148
25697a35 149 (void)closedir( dirp );
d2fe0c32
FM
150 if(debug)
151 debuga("Datafile %s successfully",DataFile);
25697a35
GS
152}
153
14c2eac6 154void 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 155{
25697a35
GS
156 FILE *fp_ou;
157 char reg[MAXLEN];
158
936c9905 159 if((fp_ou=MY_FOPEN(DataFile,"a"))==NULL){
32e71fa4 160 fprintf(stderr, "SARG: (datafile) %s: %s\n",text[45],DataFile);
25697a35
GS
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
085c1e1f 170 if((DataFileFields & DATA_FIELD_USER) != 0) {
25697a35
GS
171 strcpy(reg,user);
172 strncat(reg,DataFileDelimiter,1);
173 }
085c1e1f 174 if((DataFileFields & DATA_FIELD_DATE) != 0) {
25697a35
GS
175 strncat(reg,dia,strlen(dia));
176 strncat(reg,DataFileDelimiter,1);
177 }
085c1e1f 178 if((DataFileFields & DATA_FIELD_TIME) != 0) {
25697a35
GS
179 strncat(reg,hora,strlen(hora));
180 strncat(reg,DataFileDelimiter,1);
181 }
085c1e1f 182 if((DataFileFields & DATA_FIELD_URL) != 0) {
25697a35 183 strcpy(name,url);
d6e703cc 184 if (strcmp(DataFileUrl,"ip") == 0) name2ip(name);
25697a35
GS
185 strncat(reg,name,strlen(name));
186 strncat(reg,DataFileDelimiter,1);
187 }
085c1e1f 188 if((DataFileFields & DATA_FIELD_CONNECT) != 0) {
25697a35
GS
189 strncat(reg,val1,strlen(val1));
190 strncat(reg,DataFileDelimiter,1);
191 }
085c1e1f 192 if((DataFileFields & DATA_FIELD_BYTES) != 0) {
25697a35
GS
193 strncat(reg,val2,strlen(val2));
194 strncat(reg,DataFileDelimiter,1);
195 }
085c1e1f 196 if((DataFileFields & DATA_FIELD_IN_CACHE) != 0) {
25697a35
GS
197 strncat(reg,val4,strlen(val4));
198 strncat(reg,DataFileDelimiter,1);
199 }
085c1e1f 200 if((DataFileFields & DATA_FIELD_OUT_CACHE) != 0) {
25697a35
GS
201 strncat(reg,val5,strlen(val5));
202 strncat(reg,DataFileDelimiter,1);
203 }
085c1e1f 204 if((DataFileFields & DATA_FIELD_ELAPSED) != 0) {
25697a35
GS
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);
25697a35 213}