]>
Commit | Line | Data |
---|---|---|
1c91da07 FM |
1 | /* |
2 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net | |
3 | * 1998, 2012 | |
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 | //! \c True if the current log is known to be a sarg parsed log. | |
31 | static bool InSargLog=false; | |
9427469a FM |
32 | //! \c True if the file name is invalid. |
33 | static bool InvalidFileName=true; | |
cc6af460 FM |
34 | //! The last period extracted from the log file name. |
35 | static struct periodstruct SargPeriod; | |
1c91da07 FM |
36 | |
37 | /*! | |
38 | A new file is being read. The name of the file is \a FileName. | |
39 | */ | |
40 | static void Sarg_NewFile(const char *FileName) | |
41 | { | |
42 | InSargLog=false; | |
cc6af460 | 43 | InvalidFileName=(getperiod_fromsarglog(FileName,&SargPeriod)<0); |
1c91da07 FM |
44 | } |
45 | ||
46 | /*! | |
47 | Read one entry from a sarg generated log. | |
48 | ||
49 | \param Line One line from the input log file. | |
50 | \param Entry Where to store the information parsed from the line. | |
51 | ||
52 | \retval RLRC_NoError One valid entry is parsed. | |
53 | \retval RLRC_Unknown The line is invalid. | |
54 | \retval RLRC_InternalError An internal error was encountered. | |
55 | */ | |
56 | static enum ReadLogReturnCodeEnum Sarg_ReadEntry(char *Line,struct ReadLogStruct *Entry) | |
57 | { | |
58 | const char *Begin; | |
59 | int IpLen; | |
60 | int HttpCodeLen; | |
61 | int UrlLen; | |
62 | int UserLen; | |
63 | int Day; | |
64 | int Month; | |
65 | int Year; | |
66 | int Hour; | |
67 | int Minute; | |
68 | int Second; | |
0c87646f | 69 | |
1c91da07 | 70 | if (strncmp(Line,"*** SARG Log ***",16)==0) { |
9427469a FM |
71 | if (InvalidFileName) { |
72 | debuga(_("The name of the file is invalid for a sarg log\n")); | |
73 | exit(EXIT_FAILURE); | |
74 | } | |
cc6af460 | 75 | getperiod_merge(&period,&SargPeriod); |
1c91da07 FM |
76 | InSargLog=true; |
77 | return(RLRC_Ignore); | |
78 | } | |
79 | if (!InSargLog) return(RLRC_Unknown); | |
0c87646f | 80 | |
1c91da07 FM |
81 | // get the date |
82 | Day=0; | |
83 | while (isdigit(*Line)) Day=Day*10+(*Line++-'0'); | |
84 | if (*Line!='/' || Day<1 || Day>31) return(RLRC_Unknown); | |
85 | ||
86 | ++Line; | |
87 | Month=0; | |
88 | while (isdigit(*Line)) Month=Month*10+(*Line++-'0'); | |
89 | if (*Line!='/') return(RLRC_Unknown); | |
90 | if (Month<=0 || Month>12) return(RLRC_Unknown); | |
91 | ||
92 | ++Line; | |
93 | Year=0; | |
94 | while (isdigit(*Line)) Year=Year*10+(*Line++-'0'); | |
95 | if (*Line!='\t' || Year<1900 || Year>2200) return(RLRC_Unknown); | |
0c87646f | 96 | |
1c91da07 FM |
97 | // get the time |
98 | ++Line; | |
99 | Hour=0; | |
100 | while (isdigit(*Line)) Hour=Hour*10+(*Line++-'0'); | |
101 | if (*Line!=':' || Hour>=24) return(RLRC_Unknown); | |
102 | ++Line; | |
103 | Minute=0; | |
104 | while (isdigit(*Line)) Minute=Minute*10+(*Line++-'0'); | |
105 | if (*Line!=':' || Minute>=60) return(RLRC_Unknown); | |
106 | ++Line; | |
107 | Second=0; | |
108 | while (isdigit(*Line)) Second=Second*10+(*Line++-'0'); | |
109 | if (*Line!='\t' || Second>60) return(RLRC_Unknown); //second can be 60 due to a leap second | |
110 | ||
cb53374b FM |
111 | Entry->EntryTime.tm_year=Year-1900; |
112 | Entry->EntryTime.tm_mon=Month-1; | |
113 | Entry->EntryTime.tm_mday=Day; | |
114 | Entry->EntryTime.tm_hour=Hour; | |
115 | Entry->EntryTime.tm_min=Minute; | |
116 | Entry->EntryTime.tm_sec=Second; | |
7dbbca70 | 117 | Entry->EntryTime.tm_isdst=-1; |
1c91da07 FM |
118 | |
119 | // the ID of the user | |
120 | Entry->User=++Line; | |
121 | for (UserLen=0 ; *Line && *Line!='\t' ; UserLen++) Line++; | |
122 | if (*Line!='\t' || UserLen==0) return(RLRC_Unknown); | |
0c87646f | 123 | |
1c91da07 FM |
124 | // get IP address |
125 | Entry->Ip=++Line; | |
126 | for (IpLen=0 ; *Line && *Line!='\t' ; IpLen++) Line++; | |
127 | if (*Line!='\t' || IpLen==0) return(RLRC_Unknown); | |
128 | ||
129 | // get the URL | |
130 | Entry->Url=++Line; | |
131 | for (UrlLen=0 ; *Line && *Line!='\t' ; UrlLen++) Line++; | |
132 | if (*Line!='\t' || UrlLen==0) return(RLRC_Unknown); | |
0c87646f | 133 | |
1c91da07 FM |
134 | // get the number of transfered bytes. |
135 | Begin=++Line; | |
136 | Entry->DataSize=0LL; | |
137 | while (isdigit(*Line)) Entry->DataSize=Entry->DataSize*10+(*Line++-'0'); | |
138 | if (*Line!='\t' || Begin==Line) return(RLRC_Unknown); | |
0c87646f | 139 | |
1c91da07 FM |
140 | // get the HTTP code. |
141 | Entry->HttpCode=++Line; | |
142 | for (HttpCodeLen=0 ; *Line && *Line!='\t' ; HttpCodeLen++) Line++; | |
143 | if (*Line!='\t' || HttpCodeLen==0) return(RLRC_Unknown); | |
144 | ||
145 | // get the elapsed time. | |
146 | Begin=++Line; | |
147 | Entry->ElapsedTime=0L; | |
148 | while (isdigit(*Line)) Entry->ElapsedTime=Entry->ElapsedTime*10+(*Line++-'0'); | |
149 | if (*Line!='\t' || Line==Begin) return(RLRC_Unknown); | |
150 | ||
151 | // get the smart filter | |
7dbbca70 FM |
152 | //! \bug Smart filter ignored from sarg log format. |
153 | ||
154 | // check the entry time | |
155 | if (mktime(&Entry->EntryTime)==-1) { | |
156 | debuga(_("Invalid date or time found in the common log file\n")); | |
157 | return(RLRC_InternalError); | |
158 | } | |
159 | ||
1c91da07 FM |
160 | // it is safe to alter the line buffer now that we are returning a valid entry |
161 | Entry->Ip[IpLen]='\0'; | |
162 | Entry->HttpCode[HttpCodeLen]='\0'; | |
163 | Entry->Url[UrlLen]='\0'; | |
164 | Entry->User[UserLen]='\0'; | |
0c87646f | 165 | |
1c91da07 FM |
166 | return(RLRC_NoError); |
167 | } | |
168 | ||
169 | //! \brief Object to read a standard squid log format. | |
170 | const struct ReadLogProcessStruct ReadSargLog= | |
171 | { | |
172 | /* TRANSLATORS: This is the name of the log format displayed when this format is detected in an input log file. */ | |
173 | N_("sarg log format"), | |
174 | Sarg_NewFile, | |
175 | Sarg_ReadEntry | |
176 | }; |