Skip to content

Commit 18e7d07

Browse files
committed
Refactor eventlog to only query delta of timestamps per log #58
1 parent 229d0b9 commit 18e7d07

File tree

1 file changed

+51
-16
lines changed

1 file changed

+51
-16
lines changed

checks/eventlog_windows.go

+51-16
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ type JsonEventLog struct {
7373
}
7474

7575
type CheckWindowsEventLog struct {
76-
age time.Duration
77-
logfiles []string
76+
age int64
77+
buffer map[string]map[int64]*resultEvent // Stores the latest event log entries
78+
bufferTimestamps map[string]time.Time // Stores the last read possition of the event log as time
79+
logfiles []string // Name of the Windows Event Logs to query
7880
}
7981

8082
type EventlogCheckOptions struct {
@@ -97,16 +99,13 @@ func (c *CheckWindowsEventLog) Name() string {
9799
// CheckResult will be serialized after the return and should not change until the next call to Run
98100
func (c *CheckWindowsEventLog) Run(ctx context.Context) (interface{}, error) {
99101

100-
now := time.Now().UTC()
101-
//now = now.Add((3600 * time.Second) * -1)
102-
now = now.Add(c.age * -1)
102+
for _, logfile := range c.logfiles {
103103

104-
// Golang date formate: https://golang.org/src/time/format.go
105-
datetime := now.Format("2006-01-02T15:04:05")
104+
// Golang date formate: https://golang.org/src/time/format.go
105+
datetime := c.bufferTimestamps[logfile].Format("2006-01-02T15:04:05")
106+
107+
fmt.Printf("Query logfile %v from %v", logfile, datetime)
106108

107-
//var eventBuffer map[string][]*Win32_NTLogEvent
108-
eventBuffer := make(map[string][]*resultEvent)
109-
for _, logfile := range c.logfiles {
110109
timeout := time.Duration(30 * time.Second)
111110

112111
// Command for testing
@@ -136,7 +135,7 @@ func (c *CheckWindowsEventLog) Run(ctx context.Context) (interface{}, error) {
136135
}
137136

138137
// Add empty array to result
139-
eventBuffer[logfile] = make([]*resultEvent, 0)
138+
//eventBuffer[logfile] = make([]*resultEvent, 0)
140139
continue
141140
}
142141

@@ -176,20 +175,26 @@ func (c *CheckWindowsEventLog) Run(ctx context.Context) (interface{}, error) {
176175
}
177176
} else {
178177
// Empty event log
179-
eventBuffer[logfile] = make([]*resultEvent, 0)
178+
//eventBuffer[logfile] = make([]*resultEvent, 0)
180179
}
181180

182181
if jsonError != nil {
183182
return nil, jsonError
184183
}
185184

185+
// This is the last timestamp we have a log record for
186+
var latestTimestamp = c.bufferTimestamps[logfile]
186187
for _, event := range dst {
187188
// Resolve Memory Leak
188189

189190
TimeGenerated, _ := time.Parse("2006-01-02T15:04:05-07", event.TimeGenerated)
190191
TimeWritten, _ := time.Parse("2006-01-02T15:04:05-07", event.TimeWritten)
191192

192-
eventBuffer[logfile] = append(eventBuffer[logfile], &resultEvent{
193+
if TimeGenerated.After(latestTimestamp) {
194+
latestTimestamp = TimeGenerated
195+
}
196+
197+
c.buffer[logfile][event.Index] = &resultEvent{
193198
MachineName: event.MachineName,
194199
Category: event.Category,
195200
CategoryNumber: event.CategoryNumber,
@@ -200,11 +205,27 @@ func (c *CheckWindowsEventLog) Run(ctx context.Context) (interface{}, error) {
200205
TimeGenerated: TimeGenerated.Unix(),
201206
TimeWritten: TimeWritten.Unix(),
202207
Index: event.Index,
203-
})
208+
}
209+
}
210+
211+
// Store the new timestamp of the newest log record
212+
c.bufferTimestamps[logfile] = latestTimestamp
213+
214+
// Remove logentires that are older than wineventlog-age from config.ini
215+
maxAgeTime := time.Now().UTC()
216+
maxAgeTime = maxAgeTime.Add((time.Duration(c.age) * time.Second) * -1)
217+
218+
for index, record := range c.buffer[logfile] {
219+
recordTime := time.Unix(record.TimeGenerated, 0)
220+
221+
if recordTime.Before(maxAgeTime) {
222+
// Record is to olde - drop it from buffer
223+
delete(c.buffer[logfile], index)
224+
}
204225
}
205226
}
206227

207-
return eventBuffer, nil
228+
return c.buffer, nil
208229
}
209230

210231
// Configure the command or return false if the command was disabled
@@ -218,7 +239,21 @@ func (c *CheckWindowsEventLog) Configure(cfg *config.Configuration) (bool, error
218239
ageSec = uint64(cfg.WindowsEventLogAge)
219240
}
220241

221-
c.age = time.Second * time.Duration(ageSec)
242+
c.age = int64(ageSec)
243+
244+
now := time.Now().UTC()
245+
//now = now.Add((3600 * time.Second) * -1)
246+
now = now.Add((time.Duration(c.age) * time.Second) * -1)
247+
248+
//Create round robbin buffer for eventlog records
249+
c.buffer = make(map[string]map[int64]*resultEvent)
250+
c.bufferTimestamps = make(map[string]time.Time)
251+
for _, logfile := range c.logfiles {
252+
c.buffer[logfile] = make(map[int64]*resultEvent)
253+
254+
//Set the initial logfile start date
255+
c.bufferTimestamps[logfile] = now
256+
}
222257

223258
return true, nil
224259
}

0 commit comments

Comments
 (0)