-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathClfColumnizer.cs
More file actions
194 lines (164 loc) · 6.1 KB
/
ClfColumnizer.cs
File metadata and controls
194 lines (164 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using System.Globalization;
using System.Text.RegularExpressions;
namespace LogExpert.Core.Classes.Columnizer;
public class ClfColumnizer : ILogLineColumnizer
{
private const string DateTimeFormat = "dd/MMM/yyyy:HH:mm:ss zzz";
#region Fields
private readonly Regex _lineRegex = new("(.*) (-) (.*) (\\[.*\\]) (\".*\") (.*) (.*) (\".*\") (\".*\")");
private readonly CultureInfo _cultureInfo = new("en-US");
private int _timeOffset;
#endregion
#region cTor
// anon-212-34-174-126.suchen.de - - [08/Mar/2008:00:41:10 +0100] "GET /wiki/index.php?title=Bild:Poster_small.jpg&printable=yes&printable=yes HTTP/1.1" 304 0 "http://www.captain-kloppi.de/wiki/index.php?title=Bild:Poster_small.jpg&printable=yes" "gonzo1[P] +http://www.suchen.de/faq.html"
public ClfColumnizer ()
{
}
#endregion
#region Public methods
public bool IsTimeshiftImplemented ()
{
return true;
}
public void SetTimeOffset (int msecOffset)
{
_timeOffset = msecOffset;
}
public int GetTimeOffset ()
{
return _timeOffset;
}
public DateTime GetTimestamp (ILogLineColumnizerCallback callback, ILogLine line)
{
var cols = SplitLine(callback, line);
if (cols == null || cols.ColumnValues.Length < 8)
{
return DateTime.MinValue;
}
if (cols.ColumnValues[2].FullValue.Length == 0)
{
return DateTime.MinValue;
}
try
{
var dateTime = DateTime.ParseExact(cols.ColumnValues[2].FullValue, DateTimeFormat, _cultureInfo);
return dateTime;
}
catch (Exception)
{
return DateTime.MinValue;
}
}
public void PushValue (ILogLineColumnizerCallback callback, int column, string value, string oldValue)
{
if (column == 2)
{
try
{
var newDateTime =
DateTime.ParseExact(value, DateTimeFormat, _cultureInfo);
var oldDateTime =
DateTime.ParseExact(oldValue, DateTimeFormat, _cultureInfo);
var mSecsOld = oldDateTime.Ticks / TimeSpan.TicksPerMillisecond;
var mSecsNew = newDateTime.Ticks / TimeSpan.TicksPerMillisecond;
_timeOffset = (int)(mSecsNew - mSecsOld);
}
catch (FormatException)
{
}
}
}
public string GetName ()
{
return "Webserver CLF Columnizer";
}
public string GetDescription ()
{
return "Common Logfile Format used by webservers.";
}
public int GetColumnCount ()
{
return 8;
}
public string[] GetColumnNames ()
{
return ["IP", "User", "Date/Time", "Request", "Status", "Bytes", "Referrer", "User agent"];
}
public IColumnizedLogLine SplitLine (ILogLineColumnizerCallback callback, ILogLine line)
{
ColumnizedLogLine cLogLine = new()
{
LogLine = line
};
var columns = new Column[8]
{
new() {FullValue = "", Parent = cLogLine},
new() {FullValue = "", Parent = cLogLine},
new() {FullValue = "", Parent = cLogLine},
new() {FullValue = "", Parent = cLogLine},
new() {FullValue = "", Parent = cLogLine},
new() {FullValue = "", Parent = cLogLine},
new() {FullValue = "", Parent = cLogLine},
new() {FullValue = "", Parent = cLogLine}
};
cLogLine.ColumnValues = columns.Select(a => a as IColumn).ToArray();
var temp = line.FullLine;
if (temp.Length > 1024)
{
// spam
temp = temp[..1024];
columns[3].FullValue = temp;
return cLogLine;
}
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// anon-212-34-174-126.suchen.de - - [08/Mar/2008:00:41:10 +0100] "GET /wiki/index.php?title=Bild:Poster_small.jpg&printable=yes&printable=yes HTTP/1.1" 304 0 "http://www.captain-kloppi.de/wiki/index.php?title=Bild:Poster_small.jpg&printable=yes" "gonzo1[P] +http://www.suchen.de/faq.html"
if (_lineRegex.IsMatch(temp))
{
var match = _lineRegex.Match(temp);
var groups = match.Groups;
if (groups.Count == 10)
{
columns[0].FullValue = groups[1].Value;
columns[1].FullValue = groups[3].Value;
columns[3].FullValue = groups[5].Value;
columns[4].FullValue = groups[6].Value;
columns[5].FullValue = groups[7].Value;
columns[6].FullValue = groups[8].Value;
columns[7].FullValue = groups[9].Value;
var dateTimeStr = groups[4].Value.Substring(1, 26);
// dirty probing of date/time format (much faster than DateTime.ParseExact()
if (dateTimeStr[2] == '/' && dateTimeStr[6] == '/' && dateTimeStr[11] == ':')
{
if (_timeOffset != 0)
{
try
{
var dateTime = DateTime.ParseExact(dateTimeStr, DateTimeFormat, _cultureInfo);
dateTime = dateTime.Add(new TimeSpan(0, 0, 0, 0, _timeOffset));
var newDate = dateTime.ToString(DateTimeFormat, _cultureInfo);
columns[2].FullValue = newDate;
}
catch (Exception)
{
columns[2].FullValue = "n/a";
}
}
else
{
columns[2].FullValue = dateTimeStr;
}
}
else
{
columns[2].FullValue = dateTimeStr;
}
}
}
else
{
columns[3].FullValue = temp;
}
return cLogLine;
}
#endregion
}