]> git.ipfire.org Git - thirdparty/sarg.git/blob - repday.c
Add a function to look a user up by IP address
[thirdparty/sarg.git] / repday.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2013
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 extern numlist hours;
31
32 void report_day(const struct userinfostruct *uinfo)
33 {
34 FILE *fp_in, *fp_ou;
35
36 char buf[200];
37 char data[20];
38 long long int hour;
39 const char *label;
40 char arqout[MAXLEN];
41 char wdirname[2048];
42 char colsep;
43 int ihour=0;
44 int day,month,year;
45 int daylist[MAX_DATETIME_DAYS];
46 int daysort[MAX_DATETIME_DAYS];
47 int ndaylist;
48 int daynum;
49 int dayidx;
50 long long int bytes;
51 long long int elap;
52 long long int tbytes[MAX_DATETIME_DAYS*24];
53 long long int telap[MAX_DATETIME_DAYS*24];
54 long long int tt;
55 long long int tttime[24];
56 int i, j;
57 struct getwordstruct gwarea;
58 struct tm t;
59
60 if (datetimeby==0) return;
61 snprintf(wdirname,sizeof(wdirname),"%s/%s.day",tmp,uinfo->filename);
62 if(access(wdirname, R_OK) != 0) return;
63
64 if (snprintf(arqout,sizeof(arqout),"%s/%s/d%s.html",outdirname,uinfo->filename,uinfo->filename)>=sizeof(arqout)) {
65 debuga(_("Output file name too long: %s/%s/d%s.html\n"),outdirname,uinfo->filename,uinfo->filename);
66 exit(EXIT_FAILURE);
67 }
68
69 if((fp_in=fopen(wdirname,"r"))==NULL) {
70 debuga(_("(repday) Cannot open log file %s: %s\n"),wdirname,strerror(errno));
71 exit(EXIT_FAILURE);
72 }
73
74 memset(tbytes,0,sizeof(tbytes));
75 memset(telap,0,sizeof(telap));
76 ndaylist=0;
77
78 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
79 fixendofline(buf);
80 getword_start(&gwarea,buf);
81 if (getword(data,sizeof(data),&gwarea,'\t')<0) {
82 debuga(_("Invalid date in file %s\n"),wdirname);
83 exit(EXIT_FAILURE);
84 }
85 if (sscanf(data,"%d/%d/%d",&day,&month,&year)!=3) continue;
86 if (day<1 || day>31 || month<1 || month>12 || year>9999) continue;
87 daynum=(year*10000)+(month*100)+day;
88 for (dayidx=0 ; dayidx<ndaylist && daynum!=daylist[dayidx] ; dayidx++);
89 if (dayidx>=ndaylist) {
90 if (dayidx>=sizeof(daylist)/sizeof(*daylist)) {
91 debuga(_("Too many different dates in %s\n"),wdirname);
92 exit(EXIT_FAILURE);
93 }
94 daylist[ndaylist++]=daynum;
95 }
96
97 if (getword_atoll(&hour,&gwarea,'\t')<0) {
98 debuga(_("Invalid time in file %s\n"),wdirname);
99 exit(EXIT_FAILURE);
100 }
101 ihour=(int)hour;
102 i=dayidx*24+ihour;
103
104 if ((datetimeby & DATETIME_BYTE)!=0) {
105 colsep=((datetimeby & DATETIME_ELAP)!=0) ? '\t' : '\0';
106 if (getword_atoll(&bytes,&gwarea,colsep)<0) {
107 debuga(_("Invalid number of bytes in file %s\n"),wdirname);
108 exit(EXIT_FAILURE);
109 }
110 tbytes[i]+=bytes;
111 }
112 if ((datetimeby & DATETIME_ELAP)!=0) {
113 if (getword_atoll(&elap,&gwarea,'\0')<0) {
114 debuga(_("Invalid elapsed time in file %s\n"),wdirname);
115 exit(EXIT_FAILURE);
116 }
117 telap[i]+=elap;
118 }
119 }
120 fclose(fp_in);
121
122 if((fp_ou=fopen(arqout,"w"))==NULL) {
123 debuga(_("(repday) Cannot open output file %s: %s\n"),arqout,strerror(errno));
124 exit(EXIT_FAILURE);
125 }
126
127 write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Day report"),HTML_JS_NONE);
128 fprintf(fp_ou,"<tr><td class=\"header_c\" colspan=\"2\">%s:&nbsp;%s</td></tr>\n",_("Period"),period.html);
129 fprintf(fp_ou,"<tr><th class=\"header_c\" colspan=\"2\">%s:&nbsp;%s</th></tr>\n",_("User"),uinfo->label);
130 close_html_header(fp_ou);
131
132 dayidx=0;
133 for (i=0 ; i<ndaylist ; i++) {
134 daynum=daylist[i];
135 for (j=dayidx ; j>0 && daynum<daylist[daysort[j-1]] ; j--) daysort[j]=daysort[j-1];
136 daysort[j]=i;
137 dayidx++;
138 }
139
140 if((datetimeby & DATETIME_BYTE)!=0) {
141 label=_("BYTES");
142 fputs("<table class=\"report\" cellpadding=\"0\" cellspacing=\"2\">\n", fp_ou);
143 fputs("<tr><th class=\"header_c\"></th>",fp_ou);
144 for( i = 0; i < hours.len; i++ ) {
145 /* TRANSLATORS: It is an hour in the hourly report */
146 sprintf(data,_("%02dH"),hours.list[ i ]);
147 fprintf(fp_ou, "<td class=\"header_c\">%s<br>%s</td>\n", data, label );
148 }
149 fprintf(fp_ou, "<td class=\"header_c\">%s<br>%s</td></tr>\n", _("TOTAL"), label );
150
151 memset(tttime,0,sizeof(tttime));
152 for (dayidx=0 ; dayidx<ndaylist ; dayidx++) {
153 daynum=daysort[dayidx];
154 day=daylist[daynum]%100;
155 month=(daylist[daynum]/100)%100;
156 year=daylist[daynum]/10000;
157 computedate(year,month,day,&t);
158 strftime(data,sizeof(data),"%x",&t);
159 fprintf(fp_ou, "<tr><td class=\"data\">%s</td>\n", data );
160 tt=0;
161 for( i = 0; i < hours.len; i++ ) {
162 ihour=hours.list[i];
163 if (tbytes[daynum*24+ihour]>0) {
164 fprintf(fp_ou, "<td class=\"data\">%s</td>\n",fixnum(tbytes[daynum*24+ihour],1));
165 tt+=tbytes[daynum*24+ihour];
166 tttime[ihour]+=tbytes[daynum*24+ihour];
167 } else
168 fputs("<td class=\"data\"></td>\n",fp_ou);
169 }
170 fprintf(fp_ou, "<td class=\"data\">%s</td></tr>\n",fixnum(tt,1));
171 }
172
173 fprintf(fp_ou, "<tr><td class=\"header_l\">%s</td>\n", _("TOTAL") );
174 tt=0;
175 for( i = 0; i < hours.len; i++ ) {
176 if (tttime[i]>0) {
177 fprintf(fp_ou, "<td class=\"header_r\">%s</td>\n",fixnum(tttime[i],1));
178 tt+=tttime[i];
179 } else
180 fputs("<td class=\"header_r\"></td>\n",fp_ou);
181 }
182 fprintf(fp_ou, "<td class=\"header_r\">%s</td></tr>\n",fixnum(tt,1));
183 fputs("</table>\n",fp_ou);
184 #ifdef ENABLE_DOUBLE_CHECK_DATA
185 if (tt!=uinfo->nbytes) {
186 debuga(_("Total downloaded bytes is %"PRIi64" instead of %"PRIi64" in the hourly report of user %s\n"),
187 (int64_t)tt,(int64_t)uinfo->nbytes,uinfo->label);
188 exit(EXIT_FAILURE);
189 }
190 #endif
191 }
192
193 if((datetimeby & DATETIME_ELAP)!=0) {
194 label=_("H:M:S");
195 fputs("<table class=\"report\" cellpadding=\"0\" cellspacing=\"2\">\n", fp_ou);
196 fputs("<tr><th class=\"header_c\"></th>",fp_ou);
197 for( i = 0; i < hours.len; i++ ) {
198 sprintf(data,_("%02dH"),hours.list[ i ]);
199 fprintf(fp_ou, "<td class=\"header_c\">%s<br>%s</td>\n", data, label );
200 }
201 fprintf(fp_ou, "<td class=\"header_c\">%s<br>%s</td></tr>\n", _("TOTAL"), label );
202
203 memset(tttime,0,sizeof(tttime));
204 for (dayidx=0 ; dayidx<ndaylist ; dayidx++) {
205 daynum=daysort[dayidx];
206 day=daylist[daynum]%100;
207 month=(daylist[daynum]/100)%100;
208 year=daylist[daynum]/10000;
209 computedate(year,month,day,&t);
210 strftime(data,sizeof(data),"%x",&t);
211 fprintf(fp_ou, "<tr><td class=\"data\">%s</td>\n", data );
212 tt=0;
213 for( i = 0; i < hours.len; i++ ) {
214 ihour=hours.list[i];
215 if (telap[daynum*24+ihour]>0) {
216 fprintf(fp_ou, "<td class=\"data\">%s</td>\n",fixtime(telap[daynum*24+ihour]));
217 tt+=telap[daynum*24+ihour];
218 tttime[ihour]+=telap[daynum*24+ihour];
219 } else
220 fputs("<td class=\"data\"></td>\n",fp_ou);
221 }
222 fprintf(fp_ou, "<td class=\"data\">%s</td></tr>\n",fixtime(tt));
223 }
224
225 fprintf(fp_ou, "<tr><td class=\"header_l\">%s</td>\n", _("TOTAL") );
226 tt=0;
227 for( i = 0; i < hours.len; i++ ) {
228 if (tttime[i]>0) {
229 fprintf(fp_ou, "<td class=\"header_r\">%s</td>\n",fixtime(tttime[i]));
230 tt+=tttime[i];
231 } else
232 fputs("<td class=\"header_r\"></td>\n",fp_ou);
233 }
234 fprintf(fp_ou, "<td class=\"header_r\">%s</td></tr>\n",fixtime(tt));
235 fputs("</table>\n",fp_ou);
236 #ifdef ENABLE_DOUBLE_CHECK_DATA
237 if (tt!=uinfo->elap) {
238 debuga(_("Total elapsed time is %"PRIi64" instead of %"PRIi64" in the hourly report of user %s\n"),
239 (int64_t)tt,(int64_t)uinfo->elap,uinfo->label);
240 exit(EXIT_FAILURE);
241 }
242 #endif
243 }
244
245 if (write_html_trailer(fp_ou)<0)
246 debuga(_("Write error in file %s\n"),arqout);
247 if (fclose(fp_ou)==EOF) {
248 debuga(_("Write error in %s: %s\n"),arqout,strerror(errno));
249 exit(EXIT_FAILURE);
250 }
251 return;
252 }