sarg  2.4.0
About: SARG ia a Squid Analysis Report Generator.
  Fossies Dox: sarg-2.4.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

datafile.c
Go to the documentation of this file.
1 /*
2  * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3  * 1998, 2015
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 static void saverecs(FILE *fp_ou, const struct userinfostruct *uinfo, long long int nacc, const char *url, long long int nbytes, const char *ip, const char *hora, const char *dia, long long int nelap, long long int incache, long long int oucache);
31 
32 void data_file(char *tmp)
33 {
34  FileObject *fp_in;
35  FILE *fp_ou=NULL;
36 
37  char *buf;
38  char accdia[11], acchora[9], accip[MAXLEN], *accurl;
39  char oldaccdia[11], oldacchora[9], oldaccip[MAXLEN];
40  char *oldurl;
41  char acccode[50], oldacccode[50];
42  char ipantes[MAXLEN], nameantes[MAXLEN];
43  char accsmart[MAXLEN];
44  char crc2[50];
45  char *str;
46  char tmp3[MAXLEN];
47  char u2[MAX_USER_LEN];
48  char userlabel[MAX_USER_LEN];
49  userscan uscan;
50  long long int nbytes=0;
51  long long int nelap=0;
52  long long int nacc=0;
53  int rtotal=0;
54  long long int incache=0;
55  long long int oucache=0;
56  long long int accbytes;
57  long long int accelap;
58  int new_user;
59  int same_url;
60  int url_len;
61  int ourl_size;
62  struct getwordstruct gwarea;
63  struct userinfostruct *uinfo;
64  longline line;
65 
67 
68  ipantes[0]='\0';
69  nameantes[0]='\0';
70 
71  oldurl=NULL;
72  ourl_size=0;
73 
74  uscan=userinfo_startscan();
75  if (uscan == NULL) {
76  debuga(__FILE__,__LINE__,_("Cannot enumerate the user list\n"));
77  exit(EXIT_FAILURE);
78  }
79  while ( (uinfo = userinfo_advancescan(uscan)) != NULL ) {
80  strcpy(u2,uinfo->id);
81  if (Ip2Name && uinfo->id_is_ip) {
82  strcpy(ipantes,u2);
83  ip2name(u2,sizeof(u2));
84  strcpy(nameantes,u2);
85  }
86  user_find(userlabel,MAX_USER_LEN, u2);
87  userinfo_label(uinfo,userlabel);
88  if (debug) debuga(__FILE__,__LINE__,_("Reading user file \"%s/%s\"\n"),tmp,uinfo->filename);
89 
90  sort_users_log(tmp,debug,uinfo);
91  if (snprintf(tmp3,sizeof(tmp3),"%s/%s.user_log",tmp,uinfo->filename)>=sizeof(tmp3)) {
92  debuga(__FILE__,__LINE__,_("Path too long: "));
93  debuga_more("%s/%s.user_log\n",tmp,uinfo->filename);
94  exit(EXIT_FAILURE);
95  }
96 
97  if ((fp_in=FileObject_Open(tmp3))==NULL){
98  debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),tmp3,FileObject_GetLastOpenError());
99  exit(EXIT_FAILURE);
100  }
101 
102  if ((line=longline_create())==NULL) {
103  debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),tmp3);
104  exit(EXIT_FAILURE);
105  }
106 
107  ttopen=0;
108  new_user=1;
109  while((buf=longline_read(fp_in,line))!=NULL) {
110  getword_start(&gwarea,buf);
111  if (getword(accdia,sizeof(accdia),&gwarea,'\t')<0 || getword(acchora,sizeof(acchora),&gwarea,'\t')<0 ||
112  getword(accip,sizeof(accip),&gwarea,'\t')<0 ||
113  getword_ptr(buf,&accurl,&gwarea,'\t')<0 || getword_atoll(&accbytes,&gwarea,'\t')<0 ||
114  getword(acccode,sizeof(acccode),&gwarea,'\t')<0 || getword_atoll(&accelap,&gwarea,'\t')<0) {
115  debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),tmp3);
116  exit(EXIT_FAILURE);
117  }
118  if (getword_skip(20000,&gwarea,'"')<0 || getword(accsmart,sizeof(accsmart),&gwarea,'"')<0) {
119  debuga(__FILE__,__LINE__,_("Invalid smart info in file \"%s\"\n"),tmp3);
120  exit(EXIT_FAILURE);
121  }
122 
123  if (Ip2Name) {
124  if (strcmp(accip,ipantes) != 0) {
125  strcpy(ipantes,accip);
126  ip2name(accip,sizeof(accip));
127  strcpy(nameantes,accip);
128  }
129  else strcpy(accip,nameantes);
130  }
131 
132  if (!rtotal){
133  url_len=strlen(accurl);
134  if (!oldurl || url_len>=ourl_size) {
135  ourl_size=url_len+1;
136  oldurl=realloc(oldurl,ourl_size);
137  if (!oldurl) {
138  debuga(__FILE__,__LINE__,_("Not enough memory to store the url\n"));
139  exit(EXIT_FAILURE);
140  }
141  }
142  strcpy(oldurl,accurl);
143  strcpy(oldacccode,acccode);
144  strcpy(oldaccip,accip);
145  strcpy(oldaccdia,accdia);
146  strcpy(oldacchora,acchora);
147  new_user=0;
148  rtotal++;
149  }
150  same_url=(strcmp(oldurl,accurl) == 0);
151 
152  if (!same_url || new_user){
153  if (!fp_ou && (fp_ou=MY_FOPEN(DataFile,"w"))==NULL){
154  debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),DataFile,strerror(errno));
155  exit(EXIT_FAILURE);
156  }
157  saverecs(fp_ou,uinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
158  nacc=0;
159  nbytes=0;
160  nelap=0;
161  incache=0;
162  oucache=0;
163  new_user=0;
164  }
165 
166  nacc++;
167  nbytes+=accbytes;
168  nelap+=accelap;
169 
170  strcpy(crc2,acccode);
171  str=strchr(crc2,'/');
172  if (str) *str='\0';
173 
174  if (strstr(crc2,"MISS") != 0) oucache+=accbytes;
175  else incache+=accbytes;
176 
177  if (!same_url) {
178  url_len=strlen(accurl);
179  if (url_len>=ourl_size) {
180  ourl_size=url_len+1;
181  oldurl=realloc(oldurl,ourl_size);
182  if (!oldurl) {
183  debuga(__FILE__,__LINE__,_("Not enough memory to store the url\n"));
184  exit(EXIT_FAILURE);
185  }
186  }
187  strcpy(oldurl,accurl);
188  }
189  new_user=0;
190  strcpy(oldacccode,acccode);
191  strcpy(oldaccip,accip);
192  strcpy(oldaccdia,accdia);
193  strcpy(oldacchora,acchora);
194  }
195 
196  if (FileObject_Close(fp_in)) {
197  debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),tmp3,FileObject_GetLastCloseError());
198  exit(EXIT_FAILURE);
199  }
200  longline_destroy(&line);
201  }
202  userinfo_stopscan(uscan);
203  if (oldurl) free(oldurl);
204  if (fp_ou && fclose(fp_ou)==EOF) {
205  debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),DataFile,strerror(errno));
206  exit(EXIT_FAILURE);
207  }
208 
209  if (debug)
210  debuga(__FILE__,__LINE__,_("Datafile \"%s\" written successfully\n"),DataFile);
211 }
212 
213 static void saverecs(FILE *fp_ou, const struct userinfostruct *uinfo, long long int nacc, const char *url, long long int nbytes, const char *ip, const char *hora, const char *dia, long long int nelap, long long int incache, long long int oucache)
214 {
215  char val[20];
216  char name[512];
217 
218  if ((DataFileFields & DATA_FIELD_USER) != 0) {
219  fputs(uinfo->label,fp_ou);
220  fputc(DataFileDelimiter[0],fp_ou);
221  }
222  if ((DataFileFields & DATA_FIELD_DATE) != 0) {
223  fputs(dia,fp_ou);
224  fputc(DataFileDelimiter[0],fp_ou);
225  }
226  if ((DataFileFields & DATA_FIELD_TIME) != 0) {
227  fputs(hora,fp_ou);
228  fputc(DataFileDelimiter[0],fp_ou);
229  }
230  if ((DataFileFields & DATA_FIELD_URL) != 0) {
231  url_hostname(url,name,sizeof(name));
232  if (DataFileUrl == DATAFILEURL_IP && name[0]!=ALIAS_PREFIX) name2ip(name,sizeof(name));
233  fputs(name,fp_ou);
234  fputc(DataFileDelimiter[0],fp_ou);
235  }
236  if ((DataFileFields & DATA_FIELD_CONNECT) != 0) {
237  my_lltoa(nacc,val,sizeof(val),0);
238  fputs(val,fp_ou);
239  fputc(DataFileDelimiter[0],fp_ou);
240  }
241  if ((DataFileFields & DATA_FIELD_BYTES) != 0) {
242  my_lltoa(nbytes,val,sizeof(val),0);
243  fputs(val,fp_ou);
244  fputc(DataFileDelimiter[0],fp_ou);
245  }
246  if ((DataFileFields & DATA_FIELD_IN_CACHE) != 0) {
247  my_lltoa(incache,val,sizeof(val),0);
248  fputs(val,fp_ou);
249  fputc(DataFileDelimiter[0],fp_ou);
250  }
251  if ((DataFileFields & DATA_FIELD_OUT_CACHE) != 0) {
252  my_lltoa(oucache,val,sizeof(val),0);
253  fputs(val,fp_ou);
254  fputc(DataFileDelimiter[0],fp_ou);
255  }
256  if ((DataFileFields & DATA_FIELD_ELAPSED) != 0) {
257  my_lltoa(nelap,val,sizeof(val),0);
258  fputs(val,fp_ou);
259  fputc(DataFileDelimiter[0],fp_ou);
260  }
261 
262  fputc('\n',fp_ou);
263 }
DATA_FIELD_USER
#define DATA_FIELD_USER
Definition: conf.h:202
userinfo_startscan
userscan userinfo_startscan(void)
Definition: userinfo.c:250
sort_users_log
void sort_users_log(const char *tmp, int debug, struct userinfostruct *uinfo)
Definition: sort.c:112
DataFile
char DataFile[20000]
Definition: conf.h:380
getword_start
void getword_start(struct getwordstruct *gwarea, const char *line)
Definition: util.c:74
debuga
void debuga(const char *File, int Line, const char *msg,...)
Definition: util.c:601
userinfostruct
What is known about a user.
Definition: defs.h:78
UserTabFile
char UserTabFile[255]
Definition: conf.h:333
name2ip
void name2ip(char *name, int name_size)
Definition: ip2name.c:256
longline_read
char * longline_read(FileObject *fp_in, longline line)
Definition: longline.c:97
FileObject_GetLastCloseError
const char * FileObject_GetLastCloseError(void)
Definition: fileobject.c:263
longlinestruct
Definition: longline.c:56
FileObject_GetLastOpenError
const char * FileObject_GetLastOpenError(void)
Definition: fileobject.c:236
data_file
void data_file(char *tmp)
Definition: datafile.c:32
DATAFILEURL_IP
#define DATAFILEURL_IP
Definition: conf.h:252
MY_FOPEN
#define MY_FOPEN
Definition: conf.h:129
userinfostruct::id
const char * id
The ID of the user as found in the input file.
Definition: defs.h:81
_
#define _(String)
Definition: conf.h:155
my_lltoa
void my_lltoa(unsigned long long int n, char *s, int ssize, int len)
Definition: util.c:493
ip2name
void ip2name(char *ip, int ip_len)
Definition: ip2name.c:216
MAXLEN
#define MAXLEN
Definition: conf.h:176
getword_skip
int getword_skip(int limit, struct getwordstruct *gwarea, char stop)
Definition: util.c:160
userscanstruct
Hold pointer to scan through the user list.
Definition: userinfo.c:51
MAX_USER_LEN
#define MAX_USER_LEN
Definition: conf.h:179
DATA_FIELD_ELAPSED
#define DATA_FIELD_ELAPSED
Definition: conf.h:210
DataFileFields
unsigned long int DataFileFields
Definition: conf.h:382
name
char name[20000]
Definition: conf.h:335
userinfostruct::id_is_ip
bool id_is_ip
True if the ID is in fact the IP address from which the user connected.
Definition: defs.h:85
DATA_FIELD_URL
#define DATA_FIELD_URL
Definition: conf.h:205
init_usertab
void init_usertab(const char *UserTabFile)
Definition: usertab.c:426
DATA_FIELD_BYTES
#define DATA_FIELD_BYTES
Definition: conf.h:207
userinfostruct::label
const char * label
The name of the user to display in the report.
Definition: defs.h:89
getword_ptr
int getword_ptr(char *orig_line, char **word, struct getwordstruct *gwarea, char stop)
Definition: util.c:343
DATA_FIELD_OUT_CACHE
#define DATA_FIELD_OUT_CACHE
Definition: conf.h:209
DATA_FIELD_CONNECT
#define DATA_FIELD_CONNECT
Definition: conf.h:206
getword
int getword(char *word, int limit, struct getwordstruct *gwarea, char stop)
Definition: util.c:90
FileObject_Open
FileObject * FileObject_Open(const char *FileName)
Definition: fileobject.c:104
conf.h
Include headers and define global variables. */.
userinfo_stopscan
void userinfo_stopscan(userscan uscan)
Definition: userinfo.c:266
ALIAS_PREFIX
#define ALIAS_PREFIX
The character prefixed in front of the host names that are aliased.
Definition: conf.h:290
userinfo_advancescan
struct userinfostruct * userinfo_advancescan(userscan uscan)
Definition: userinfo.c:280
DATA_FIELD_DATE
#define DATA_FIELD_DATE
Definition: conf.h:203
longline_create
longline longline_create(void)
Definition: longline.c:70
userinfostruct::filename
const char * filename
The mangled name to use in file names of that user.
Definition: defs.h:91
ttopen
int ttopen
Definition: conf.h:482
getwordstruct
Definition: defs.h:26
longline_destroy
void longline_destroy(longline *line_ptr)
Definition: longline.c:168
userinfostruct::ip
const char * ip
The user's IP address.
Definition: defs.h:83
tmp
char tmp[20000]
Definition: conf.h:315
defs.h
Declaration of the structures and functions.
url_hostname
void url_hostname(const char *url, char *hostname, int hostsize)
Definition: url.c:909
DataFileDelimiter
char DataFileDelimiter[3]
Definition: conf.h:381
user_find
void user_find(char *mappedname, int namelen, const char *userlogin)
Definition: usertab.c:460
DataFileUrl
unsigned long int DataFileUrl
Definition: conf.h:383
getword_atoll
int getword_atoll(long long int *number, struct getwordstruct *gwarea, char stop)
Definition: util.c:182
userinfo_label
void userinfo_label(struct userinfostruct *uinfo, const char *label)
Definition: userinfo.c:194
saverecs
static void saverecs(FILE *fp_ou, const struct userinfostruct *uinfo, long long int nacc, const char *url, long long int nbytes, const char *ip, const char *hora, const char *dia, long long int nelap, long long int incache, long long int oucache)
Definition: datafile.c:213
FileObject_Close
int FileObject_Close(FileObject *File)
Definition: fileobject.c:206
debuga_more
void debuga_more(const char *msg,...)
Definition: util.c:631
debug
int debug
Definition: conf.h:489
DATA_FIELD_IN_CACHE
#define DATA_FIELD_IN_CACHE
Definition: conf.h:208
FileObjectStruct
Definition: fileobject.h:4
DATA_FIELD_TIME
#define DATA_FIELD_TIME
Definition: conf.h:204
Ip2Name
bool Ip2Name
Definition: conf.h:337