]> git.ipfire.org Git - thirdparty/sarg.git/blob - totday.c
Report any error while reading the day summary file
[thirdparty/sarg.git] / totday.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2011
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 day_totalize(const char *tmp, const struct userinfostruct *uinfo)
31 {
32 FILE *fp_in, *fp_ou;
33
34 char buf[200];
35 char date[20];
36 int hour;
37 long long int bytes;
38 long long int elap;
39 long long int tbytes[MAX_DATETIME_DAYS*24];
40 long long int telap[MAX_DATETIME_DAYS*24];
41 int day,month,year;
42 int daylist[MAX_DATETIME_DAYS];
43 int ndaylist;
44 int daynum;
45 int dayidx;
46 int i;
47 char wdirname[2048];
48 char arqout[2048];
49 char colsep;
50 struct getwordstruct gwarea;
51
52 if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return;
53 if (datetimeby==0) return;
54
55 if (snprintf(wdirname,sizeof(wdirname),"%s/%s.htmp",tmp,uinfo->filename)>=sizeof(wdirname)) {
56 debuga(_("File name too long: %s/%s%s\n"),tmp,uinfo->filename,".htmp");
57 exit(EXIT_FAILURE);
58 }
59 if (snprintf(arqout,sizeof(arqout),"%s/%s.day",tmp,uinfo->filename)>=sizeof(arqout)) {
60 debuga(_("File name too long: %s/%s%s\n"),tmp,uinfo->filename,".day");
61 exit(EXIT_FAILURE);
62 }
63
64 if((fp_in=fopen(wdirname,"r"))==NULL) {
65 debuga(_("(totday) Cannot open log file %s\n"),wdirname);
66 exit(EXIT_FAILURE);
67 }
68
69 memset(tbytes,0,sizeof(tbytes));
70 memset(telap,0,sizeof(tbytes));
71 ndaylist=0;
72
73 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
74 fixendofline(buf);
75 getword_start(&gwarea,buf);
76 if (getword(date,sizeof(date),&gwarea,'\t')<0 || getword_atoi(&hour,&gwarea,'\t')<0) {
77 debuga(_("There is a broken record or garbage in file %s\n"),wdirname);
78 exit(EXIT_FAILURE);
79 }
80 if (sscanf(date,"%d/%d/%d",&day,&month,&year)!=3) {
81 debuga(_("Invalid date \"%s\" in %s\n"),date,wdirname);
82 exit(EXIT_FAILURE);
83 }
84 if (day<1 || day>31 || month<1 || month>12 || year>9999) {
85 debuga(_("Invalid date component in \"%s\" in %s\n"),date,wdirname);
86 exit(EXIT_FAILURE);
87 }
88 if (hour<0 || hour>=24) {
89 debuga(_("Invalid hour %d in %s\n"),hour,wdirname);
90 exit(EXIT_FAILURE);
91 }
92 daynum=(year*10000)+(month*100)+day;
93 for (dayidx=0 ; dayidx<ndaylist && daynum!=daylist[dayidx] ; dayidx++);
94 if (dayidx>=ndaylist) {
95 if (dayidx>=sizeof(daylist)/sizeof(*daylist)) {
96 debuga(_("Too many different dates in %s\n"),wdirname);
97 exit(EXIT_FAILURE);
98 }
99 daylist[ndaylist++]=daynum;
100 }
101 i=dayidx*24+hour;
102 if ((datetimeby & DATETIME_BYTE)!=0) {
103 colsep=((datetimeby & DATETIME_ELAP)!=0) ? '\t' : '\0';
104 if (getword_atoll(&bytes,&gwarea,colsep)<0) {
105 debuga(_("Invalid number of bytes in file %s\n"),wdirname);
106 exit(EXIT_FAILURE);
107 }
108 tbytes[i]+=bytes;
109 }
110 if ((datetimeby & DATETIME_ELAP)!=0) {
111 if (getword_atoll(&elap,&gwarea,'\0')<0) {
112 debuga(_("Invalid elapsed time in file %s\n"),wdirname);
113 exit(EXIT_FAILURE);
114 }
115 telap[i]+=elap;
116 }
117 }
118 fclose(fp_in);
119
120 if((fp_ou=fopen(arqout,"w"))==NULL) {
121 debuga(_("(totday) Cannot open log file %s\n"),arqout);
122 exit(EXIT_FAILURE);
123 }
124
125 for (i=0 ; i<sizeof(tbytes)/sizeof(*tbytes) ; i++) {
126 if (tbytes[i]==0 && telap[i]==0) continue;
127 dayidx=i/24;
128 if (dayidx>=sizeof(daylist)/sizeof(*daylist)) {
129 debuga(_("Invalid day index found in %s\n"),wdirname);
130 exit(EXIT_FAILURE);
131 }
132 hour=i%24;
133 daynum=daylist[dayidx];
134 day=daynum%100;
135 month=(daynum/100)%100;
136 year=daynum/10000;
137 fprintf(fp_ou,"%d/%d/%d\t%d",day,month,year,hour);
138 if ((datetimeby & DATETIME_BYTE)!=0) fprintf(fp_ou,"\t%"PRIu64"",(uint64_t)tbytes[i]);
139 if ((datetimeby & DATETIME_ELAP)!=0) fprintf(fp_ou,"\t%"PRIu64"",(uint64_t)telap[i]);
140 fputs("\n",fp_ou);
141 }
142
143 if (fclose(fp_ou)==EOF) {
144 debuga(_("Failed to close file %s - %s\n"),arqout,strerror(errno));
145 exit(EXIT_FAILURE);
146 }
147
148 if (unlink(wdirname)==-1) {
149 debuga(_("Cannot delete temporary file %s - %s\n"),wdirname,strerror(errno));
150 exit(EXIT_FAILURE);
151 }
152 return;
153 }