]> git.ipfire.org Git - thirdparty/sarg.git/blob - readlog_squid.c
Store the entry time in a structure instead of a pointer
[thirdparty/sarg.git] / readlog_squid.c
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 /*!
31 A new file is being read. The name of the file is \a FileName.
32 */
33 static void Squid_NewFile(const char *FileName)
34 {
35 }
36
37 /*!
38 Read one entry from a standard squid log format.
39
40 \param Line One line from the input log file.
41 \param Entry Where to store the information parsed from the line.
42
43 \retval RLRC_NoError One valid entry is parsed.
44 \retval RLRC_Unknown The line is invalid.
45 \retval RLRC_InternalError An internal error was encountered.
46 */
47 static enum ReadLogReturnCodeEnum Squid_ReadEntry(char *Line,struct ReadLogStruct *Entry)
48 {
49 const char *Begin;
50 time_t log_time;
51 int IpLen;
52 int HttpCodeLen;
53 int UrlLen;
54 int UserLen;
55 struct tm *tt;
56
57 // get log time.
58 Begin=Line;
59 log_time=0;
60 while (isdigit(*Line)) log_time=log_time*10+(*Line++-'0');
61 if (*Line!='.' || Line==Begin) return(RLRC_Unknown);
62
63 // ignore decimal part to log time.
64 Begin=++Line;
65 while (isdigit(*Line)) Line++;
66 if (*Line!=' ' || Line==Begin) return(RLRC_Unknown);
67
68 // skip spaces before the elapsed time.
69 while (*Line==' ') Line++;
70 if (!isdigit(*Line)) return(RLRC_Unknown);
71
72 // get the elapsed time.
73 Begin=Line;
74 Entry->ElapsedTime=0L;
75 while (isdigit(*Line)) Entry->ElapsedTime=Entry->ElapsedTime*10+(*Line++-'0');
76 if (*Line!=' ' || Line==Begin) return(RLRC_Unknown);
77
78 // get IP address. It can be a fqdn if that option is enabled in squid.
79 Entry->Ip=++Line;
80 for (IpLen=0 ; *Line && *Line!=' ' ; IpLen++) Line++;
81 if (*Line!=' ' || IpLen==0) return(RLRC_Unknown);
82
83 // get the HTTP code.
84 Entry->HttpCode=++Line;
85 for (HttpCodeLen=0 ; *Line && *Line!=' ' ; HttpCodeLen++) Line++;
86 if (*Line!=' ' || HttpCodeLen==0) return(RLRC_Unknown);
87
88 // get the number of transfered bytes.
89 Begin=++Line;
90 Entry->DataSize=0LL;
91 while (isdigit(*Line)) Entry->DataSize=Entry->DataSize*10+(*Line++-'0');
92 if (*Line!=' ' || Begin==Line) return(RLRC_Unknown);
93
94 // skip the HTTP function
95 Begin=++Line;
96 while (*Line && *Line!=' ') Line++;
97 if (*Line!=' '|| Line==Begin) return(RLRC_Unknown);
98
99 // the url
100 Entry->Url=++Line;
101 for (UrlLen=0 ; *Line && *Line!=' ' ; UrlLen++) Line++;
102 if (*Line!=' ' || UrlLen==0) return(RLRC_Unknown);
103
104 // the ID of the user or - if the user is unidentified
105 Entry->User=++Line;
106 for (UserLen=0 ; *Line && *Line!=' ' ; UserLen++) Line++;
107 if (*Line!=' ' || UserLen==0) return(RLRC_Unknown);
108
109 // now, the format is known with a good confidence. If the time doesn't decode, it is an error.
110 tt=localtime(&log_time);
111 if (tt==NULL) {
112 debuga(_("Cannot convert the timestamp from the squid log file\n"));
113 return(RLRC_InternalError);
114 }
115 memcpy(&Entry->EntryTime,tt,sizeof(struct tm));
116
117 // it is safe to alter the line buffer now that we are returning a valid entry
118 Entry->Ip[IpLen]='\0';
119 Entry->HttpCode[HttpCodeLen]='\0';
120 Entry->Url[UrlLen]='\0';
121 Entry->User[UserLen]='\0';
122
123 return(RLRC_NoError);
124 }
125
126 //! \brief Object to read a standard squid log format.
127 const struct ReadLogProcessStruct ReadSquidLog=
128 {
129 /* TRANSLATORS: This is the name of the log format displayed when this format is detected in an input log file. */
130 N_("squid log format"),
131 Squid_NewFile,
132 Squid_ReadEntry
133 };