]> git.ipfire.org Git - thirdparty/sarg.git/blame - totday.c
Add support to decompress xz files
[thirdparty/sarg.git] / totday.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
110ce984 3 * 1998, 2015
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 29
026ddd8b
FM
30//! The daily statistics for one user.
31struct DayStruct
25697a35 32{
026ddd8b
FM
33 int ndaylist;
34 int maxindex;
35 int daylist[MAX_DATETIME_DAYS];
36 long long int bytes[MAX_DATETIME_DAYS*24];
37 long long int elap[MAX_DATETIME_DAYS*24];
38};
39
40/*!
41Prepare the object to store the daily statistics of one user.
42
43\return The object to pass to other functions in this module.
44The object must be freed with a call to day_cleanup().
45*/
46DayObject day_prepare(void)
47{
48 DayObject ddata;
bd43d81f 49
026ddd8b
FM
50 ddata=(DayObject)malloc(sizeof(*ddata));
51 if (!ddata)
52 {
af961877 53 debuga(__FILE__,__LINE__,_("Not enough memory to store the daily statistics\n"));
026ddd8b
FM
54 exit(EXIT_FAILURE);
55 }
bd43d81f 56
026ddd8b
FM
57 return(ddata);
58}
25697a35 59
026ddd8b
FM
60/*!
61Free the memory allocated by day_prepare().
62
63\param ddata The object returned by day_prepare().
64*/
65void day_cleanup(DayObject ddata)
66{
67 if (ddata) free(ddata);
68}
69
70/*!
71Prepare the object for a new user.
72
73\param ddata The object created by day_prepare().
74*/
75void day_newuser(DayObject ddata)
76{
77 if (ddata)
78 {
79 ddata->ndaylist=0;
80 ddata->maxindex=0;
81 memset(ddata->bytes,0,sizeof(ddata->bytes));
82 memset(ddata->elap,0,sizeof(ddata->elap));
83 }
84}
85
86/*!
87Store one data point in the statistics.
88
89\param ddata The object to store the statistics.
90\param date The date of the data point formated as day/month/year.
91\param time The time of the data point.
92\param elap The time spent processing the user's request on the proxy.
93\param bytes The number of bytes transfered by the user.
94*/
95void day_addpoint(DayObject ddata,const char *date, const char *time, long long int elap, long long int bytes)
96{
44bdeb60 97 int hour;
9bd92830 98 int day,month,year;
9bd92830
FM
99 int daynum;
100 int dayidx;
101 int i;
120d768c 102
94b6a694 103 if (!ddata) return;
026ddd8b 104 if (sscanf(date,"%d/%d/%d",&day,&month,&year)!=3) {
af961877 105 debuga(__FILE__,__LINE__,_("Invalid date \"%s\" for the hourly statistics\n"),date);
9bd92830
FM
106 exit(EXIT_FAILURE);
107 }
026ddd8b 108 if (day<1 || day>31 || month<1 || month>12 || year>9999) {
af961877 109 debuga(__FILE__,__LINE__,_("Invalid date component in \"%s\" for the hourly statistics\n"),date);
9bd92830
FM
110 exit(EXIT_FAILURE);
111 }
026ddd8b
FM
112 hour=atoi(time);
113 if (hour<0 || hour>=24) {
af961877 114 debuga(__FILE__,__LINE__,_("Invalid hour %d for the hourly statistics\n"),hour);
9bd92830
FM
115 exit(EXIT_FAILURE);
116 }
026ddd8b
FM
117 daynum=(year*10000)+(month*100)+day;
118 for (dayidx=ddata->ndaylist-1 ; dayidx>=0 && daynum!=ddata->daylist[dayidx] ; dayidx--);
119 if (dayidx<0) {
120 dayidx=ddata->ndaylist++;
121 if (dayidx>=sizeof(ddata->daylist)/sizeof(*ddata->daylist)) {
af961877 122 debuga(__FILE__,__LINE__,_("Too many different dates for the hourly statistics\n"));
9bd92830
FM
123 exit(EXIT_FAILURE);
124 }
026ddd8b
FM
125 ddata->daylist[dayidx]=daynum;
126 }
127 i=dayidx*24+hour;
128 if (i>=ddata->maxindex) ddata->maxindex=i+1;
129 ddata->bytes[i]+=bytes;
130 ddata->elap[i]+=elap;
131}
132
133/*!
134Store the dayly statistics in the file.
135
136\param ddata The object containing the statistics.
137\param tmp The temporary directory to store the file into.
138\param uinfo The user's data.
139*/
140void day_totalize(DayObject ddata,const char *tmp, const struct userinfostruct *uinfo)
141{
142 FILE *fp_ou;
143 int hour;
144 int day,month,year;
145 int i;
146 int daynum;
147 int dayidx;
148 char arqout[2048];
149
150 if (datetimeby==0) return;
151 if (!ddata) return;
152
153 if (snprintf(arqout,sizeof(arqout),"%s/%s.day",tmp,uinfo->filename)>=sizeof(arqout)) {
af961877 154 debuga(__FILE__,__LINE__,_("Path too long: "));
041018b6 155 debuga_more("%s/%s%s\n",tmp,uinfo->filename,".day");
026ddd8b 156 exit(EXIT_FAILURE);
9bd92830 157 }
25697a35 158
9bd92830 159 if((fp_ou=fopen(arqout,"w"))==NULL) {
af961877 160 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),arqout,strerror(errno));
007905af 161 exit(EXIT_FAILURE);
9bd92830 162 }
25697a35 163
026ddd8b
FM
164 for (i=0 ; i<ddata->maxindex ; i++) {
165 if (ddata->bytes[i]==0 && ddata->elap[i]==0) continue;
9bd92830 166 dayidx=i/24;
026ddd8b 167 if (dayidx>=sizeof(ddata->daylist)/sizeof(*ddata->daylist)) {
af961877 168 debuga(__FILE__,__LINE__,_("Invalid day index found in the hourly statistics\n"));
44bdeb60
FM
169 exit(EXIT_FAILURE);
170 }
9bd92830 171 hour=i%24;
026ddd8b 172 daynum=ddata->daylist[dayidx];
9bd92830
FM
173 day=daynum%100;
174 month=(daynum/100)%100;
175 year=daynum/10000;
44bdeb60 176 fprintf(fp_ou,"%d/%d/%d\t%d",day,month,year,hour);
026ddd8b
FM
177 if ((datetimeby & DATETIME_BYTE)!=0) fprintf(fp_ou,"\t%"PRIu64"",(uint64_t)ddata->bytes[i]);
178 if ((datetimeby & DATETIME_ELAP)!=0) fprintf(fp_ou,"\t%"PRIu64"",(uint64_t)ddata->elap[i]);
9bd92830
FM
179 fputs("\n",fp_ou);
180 }
25697a35 181
9bd92830 182 if (fclose(fp_ou)==EOF) {
af961877 183 debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),arqout,strerror(errno));
9bd92830
FM
184 exit(EXIT_FAILURE);
185 }
9bd92830 186 return;
25697a35 187}
e3e6aef4
FM
188
189/*!
190Delete the temporary file generated by day_totalize().
191
192\param uinfo The user whose daily statistics are to be deleted.
193*/
194void day_deletefile(const struct userinfostruct *uinfo)
195{
196 char arqout[2048];
197
198 if (KeepTempLog) return;
199
200 if (snprintf(arqout,sizeof(arqout),"%s/%s.day",tmp,uinfo->filename)>=sizeof(arqout)) {
af961877 201 debuga(__FILE__,__LINE__,_("Path too long: "));
041018b6 202 debuga_more("%s/%s%s\n",tmp,uinfo->filename,".day");
e3e6aef4
FM
203 exit(EXIT_FAILURE);
204 }
205
206 if (unlink(arqout))
af961877 207 debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),arqout,strerror(errno));
e3e6aef4
FM
208}
209