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