@@ -73,8 +73,10 @@ type JsonEventLog struct {
73
73
}
74
74
75
75
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
78
80
}
79
81
80
82
type EventlogCheckOptions struct {
@@ -97,16 +99,13 @@ func (c *CheckWindowsEventLog) Name() string {
97
99
// CheckResult will be serialized after the return and should not change until the next call to Run
98
100
func (c * CheckWindowsEventLog ) Run (ctx context.Context ) (interface {}, error ) {
99
101
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 {
103
103
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 )
106
108
107
- //var eventBuffer map[string][]*Win32_NTLogEvent
108
- eventBuffer := make (map [string ][]* resultEvent )
109
- for _ , logfile := range c .logfiles {
110
109
timeout := time .Duration (30 * time .Second )
111
110
112
111
// Command for testing
@@ -136,7 +135,7 @@ func (c *CheckWindowsEventLog) Run(ctx context.Context) (interface{}, error) {
136
135
}
137
136
138
137
// Add empty array to result
139
- eventBuffer [logfile ] = make ([]* resultEvent , 0 )
138
+ // eventBuffer[logfile] = make([]*resultEvent, 0)
140
139
continue
141
140
}
142
141
@@ -176,20 +175,26 @@ func (c *CheckWindowsEventLog) Run(ctx context.Context) (interface{}, error) {
176
175
}
177
176
} else {
178
177
// Empty event log
179
- eventBuffer [logfile ] = make ([]* resultEvent , 0 )
178
+ // eventBuffer[logfile] = make([]*resultEvent, 0)
180
179
}
181
180
182
181
if jsonError != nil {
183
182
return nil , jsonError
184
183
}
185
184
185
+ // This is the last timestamp we have a log record for
186
+ var latestTimestamp = c .bufferTimestamps [logfile ]
186
187
for _ , event := range dst {
187
188
// Resolve Memory Leak
188
189
189
190
TimeGenerated , _ := time .Parse ("2006-01-02T15:04:05-07" , event .TimeGenerated )
190
191
TimeWritten , _ := time .Parse ("2006-01-02T15:04:05-07" , event .TimeWritten )
191
192
192
- eventBuffer [logfile ] = append (eventBuffer [logfile ], & resultEvent {
193
+ if TimeGenerated .After (latestTimestamp ) {
194
+ latestTimestamp = TimeGenerated
195
+ }
196
+
197
+ c.buffer [logfile ][event.Index ] = & resultEvent {
193
198
MachineName : event .MachineName ,
194
199
Category : event .Category ,
195
200
CategoryNumber : event .CategoryNumber ,
@@ -200,11 +205,27 @@ func (c *CheckWindowsEventLog) Run(ctx context.Context) (interface{}, error) {
200
205
TimeGenerated : TimeGenerated .Unix (),
201
206
TimeWritten : TimeWritten .Unix (),
202
207
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
+ }
204
225
}
205
226
}
206
227
207
- return eventBuffer , nil
228
+ return c . buffer , nil
208
229
}
209
230
210
231
// Configure the command or return false if the command was disabled
@@ -218,7 +239,21 @@ func (c *CheckWindowsEventLog) Configure(cfg *config.Configuration) (bool, error
218
239
ageSec = uint64 (cfg .WindowsEventLogAge )
219
240
}
220
241
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
+ }
222
257
223
258
return true , nil
224
259
}
0 commit comments