]>
Commit | Line | Data |
---|---|---|
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 | |
2c7e8c23 FM |
30 | /* |
31 | Extract a date range from a squid log file and write it into a separate file. | |
32 | ||
33 | It can optionally convert the date in human readable format. | |
34 | ||
35 | The output can be split by day into separate files. | |
36 | ||
37 | \param arq The squid log file to split. | |
38 | \param df The date format if the date is to be converted in human readable form. Only the first | |
39 | character is taken into account. It can be 'e' for European date format or anything else for | |
40 | US date format. | |
41 | \param dfrom The first date to output in the form (Year*10000+Month*100+Day). | |
42 | \param duntil The last date to output in the form (Year*10000+Month*100+Day). | |
43 | \param convert \c True if the date must be converted into human readable form. | |
44 | \param splitprefix If not empty, the output file is written in separate files (one for each day) and | |
45 | the files are named after the day they contain prefixed with the string contained in this variable. | |
46 | */ | |
81a022d8 | 47 | void splitlog(const char *arq, char df, int dfrom, int duntil, int convert, const char *splitprefix) |
25697a35 | 48 | { |
9bd92830 | 49 | FILE *fp_in; |
2c7e8c23 | 50 | FILE *fp_ou=NULL; |
9bd92830 FM |
51 | char *buf; |
52 | char data[30]; | |
53 | char dia[11]; | |
2c7e8c23 | 54 | char output_file[MAXLEN]; |
9bd92830 | 55 | time_t tt; |
2c7e8c23 FM |
56 | time_t min_tt; |
57 | time_t max_tt=0; | |
9bd92830 | 58 | int idata=0; |
2c7e8c23 FM |
59 | int autosplit=0; |
60 | int output_prefix_len=0; | |
61 | int prev_year=0, prev_month=0, prev_day=0; | |
9bd92830 FM |
62 | struct tm *t; |
63 | struct getwordstruct gwarea; | |
64 | longline line; | |
25697a35 | 65 | |
2c7e8c23 FM |
66 | if (splitprefix[0]!='\0') { |
67 | // '/' + '-YYYY-mm-dd' + '\0' == 13 | |
68 | output_prefix_len=snprintf(output_file,sizeof(output_file)-12,"%s%s",outdir,splitprefix); | |
69 | if (output_prefix_len>=sizeof(output_file)-12) { | |
af961877 | 70 | debuga(__FILE__,__LINE__,_("Path too long: ")); |
041018b6 | 71 | debuga_more("%s%s-YYYY-mm-dd\n",outdir,splitprefix); |
2c7e8c23 FM |
72 | exit(EXIT_FAILURE); |
73 | } | |
74 | autosplit=1; | |
75 | } else { | |
76 | fp_ou=stdout; | |
77 | } | |
78 | ||
9bd92830 FM |
79 | if(arq[0] == '\0') |
80 | arq="/var/log/squid/access.log"; | |
25697a35 | 81 | |
9bd92830 | 82 | if((fp_in=MY_FOPEN(arq,"r"))==NULL) { |
af961877 | 83 | debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),arq,strerror(errno)); |
9bd92830 FM |
84 | exit(EXIT_FAILURE); |
85 | } | |
9b179eb0 | 86 | |
9bd92830 | 87 | if ((line=longline_create())==NULL) { |
af961877 | 88 | debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),arq); |
9bd92830 FM |
89 | exit(EXIT_FAILURE); |
90 | } | |
2c7e8c23 | 91 | time(&min_tt); |
25697a35 | 92 | |
9bd92830 FM |
93 | while((buf=longline_read(fp_in,line))!=NULL) { |
94 | getword_start(&gwarea,buf); | |
95 | if (getword(data,sizeof(data),&gwarea,' ')<0) { | |
af961877 | 96 | debuga(__FILE__,__LINE__,_("Invalid date in file \"%s\"\n"),arq); |
9bd92830 FM |
97 | exit(EXIT_FAILURE); |
98 | } | |
99 | tt=atoi(data); | |
100 | t=localtime(&tt); | |
25697a35 | 101 | |
9bd92830 FM |
102 | if(dfrom) { |
103 | idata=(t->tm_year+1900)*10000+(t->tm_mon+1)*100+t->tm_mday; | |
104 | if(idata < dfrom || idata > duntil) | |
105 | continue; | |
106 | } | |
25697a35 | 107 | |
2c7e8c23 FM |
108 | if (autosplit && (prev_year!=t->tm_year || prev_month!=t->tm_mon || prev_day!=t->tm_mday)) { |
109 | prev_year=t->tm_year; | |
110 | prev_month=t->tm_mon; | |
111 | prev_day=t->tm_mday; | |
112 | if (fp_ou && fclose(fp_ou)==EOF) { | |
af961877 | 113 | debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),output_file,strerror(errno)); |
2c7e8c23 FM |
114 | exit(EXIT_FAILURE); |
115 | } | |
116 | strftime(output_file+output_prefix_len, sizeof(output_file)-output_prefix_len, "-%Y-%m-%d", t); | |
117 | /* | |
118 | The line must be added to a file we have already created. The file must be created if the date | |
119 | is seen for the first time. The idea is to create the files from scratch if the split is started | |
120 | a second time. | |
121 | */ | |
122 | if ((fp_ou=MY_FOPEN(output_file,(tt>=min_tt && tt<=max_tt) ? "a" : "w"))==NULL) { | |
af961877 | 123 | debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),output_file,strerror(errno)); |
2c7e8c23 FM |
124 | exit(EXIT_FAILURE); |
125 | } | |
126 | if (tt<min_tt) min_tt=tt; | |
127 | if (tt>max_tt) max_tt=tt; | |
128 | } | |
129 | ||
9bd92830 | 130 | if(!convert) { |
2c7e8c23 | 131 | fprintf(fp_ou,"%s %s\n",data,gwarea.current); |
9bd92830 | 132 | } else { |
81a022d8 | 133 | if (df=='e') |
9bd92830 FM |
134 | strftime(dia, sizeof(dia), "%d/%m/%Y", t); |
135 | else | |
136 | strftime(dia, sizeof(dia), "%m/%d/%Y", t); | |
25697a35 | 137 | |
2c7e8c23 | 138 | fprintf(fp_ou,"%s %02d:%02d:%02d %s\n",dia,t->tm_hour,t->tm_min,t->tm_sec,gwarea.current); |
9bd92830 FM |
139 | } |
140 | } | |
25697a35 | 141 | |
9bd92830 FM |
142 | longline_destroy(&line); |
143 | if (fclose(fp_in)==EOF) { | |
af961877 | 144 | debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),arq,strerror(errno)); |
204781f4 | 145 | exit(EXIT_FAILURE); |
9bd92830 | 146 | } |
2c7e8c23 FM |
147 | if (autosplit && fp_ou) { |
148 | if (fclose(fp_ou)==EOF) { | |
af961877 | 149 | debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),output_file,strerror(errno)); |
2c7e8c23 FM |
150 | exit(EXIT_FAILURE); |
151 | } | |
152 | } | |
25697a35 | 153 | } |