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