Skip to content

Commit 22f5dbd

Browse files
andrewkrohruflin
authored andcommitted
Backport: Add batch_read_size config to Winlogbeat (elastic#2642)
This configuration option allows users to control the number of event log records that are read, processed, and published in its event loop. * Update changelog. Backport of elastic#2641
1 parent 9f209b3 commit 22f5dbd

File tree

7 files changed

+83
-6
lines changed

7 files changed

+83
-6
lines changed

CHANGELOG.asciidoc

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ https://github.com/elastic/beats/compare/v1.3.1...1.3[Check the HEAD diff]
4747

4848
*Winlogbeat*
4949

50+
- Add `event_logs.batch_read_size` configuration option. {pull}2642[2642]
51+
5052
==== Deprecated
5153

5254
*Affecting all Beats*

winlogbeat/beat/winlogbeat.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,9 @@ func (eb *Winlogbeat) Run(b *beat.Beat) error {
135135
debugf("Initializing EventLog[%s]", eventLogConfig.Name)
136136

137137
eventLog, err := eventlog.New(eventlog.Config{
138-
Name: eventLogConfig.Name,
139-
API: eventLogConfig.API,
138+
Name: eventLogConfig.Name,
139+
API: eventLogConfig.API,
140+
BatchReadSize: eventLogConfig.BatchReadSize,
140141
})
141142
if err != nil {
142143
return fmt.Errorf("Failed to create new event log for %s. %v",

winlogbeat/config/config.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,10 @@ func (mc MetricsConfig) Validate() error {
105105
// EventLogConfig holds the configuration data that specifies which event logs
106106
// to monitor.
107107
type EventLogConfig struct {
108-
Name string
109-
IgnoreOlder string `yaml:"ignore_older"`
110-
API string
108+
Name string
109+
IgnoreOlder string `yaml:"ignore_older"`
110+
BatchReadSize int `yaml:"batch_read_size"`
111+
API string
111112
}
112113

113114
// Validate validates the EventLogConfig data and returns an error describing

winlogbeat/docs/reference/configuration/winlogbeat-options.asciidoc

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
:vista_and_newer: This option is only available on operating systems +
2+
supporting the Windows Event Log API (Microsoft Windows Vista and newer).
3+
14
[[configuration-winlogbeat-options]]
25
=== Winlogbeat
36

@@ -54,6 +57,17 @@ winlogbeat:
5457
- name: Application
5558
--------------------------------------------------------------------------------
5659

60+
===== event_logs.batch_read_size
61+
62+
The maximum number of event log records to read from the Windows API in a single
63+
batch. The default batch size is 100. *{vista_and_newer}*
64+
65+
Winlogbeat starts a goroutine (a lightweight thread) to read from each
66+
individual event log. The goroutine reads a batch of event log records using the
67+
Windows API, applies any processors to the events, publishes them to the
68+
configured outputs, and waits for an acknowledgement from the outputs before
69+
reading additional event log records.
70+
5771
[[configuration-winlogbeat-options-event_logs-name]]
5872
===== event_logs.name
5973

winlogbeat/eventlog/factory.go

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
type Config struct {
1111
Name string // Name of the event log or channel.
1212
RemoteAddress string // Remote computer to connect to. Optional.
13+
BatchReadSize int // The number of events to read in one batch.
1314

1415
API string // Name of the API to use. Optional.
1516
}

winlogbeat/eventlog/wineventlog.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,14 @@ func (l *winEventLog) Close() error {
138138
// newWinEventLog creates and returns a new EventLog for reading event logs
139139
// using the Windows Event Log.
140140
func newWinEventLog(c Config) (EventLog, error) {
141+
if c.BatchReadSize <= 0 {
142+
c.BatchReadSize = defaultMaxNumRead
143+
}
144+
141145
return &winEventLog{
142146
channelName: c.Name,
143147
remoteServer: c.RemoteAddress,
144-
maxRead: defaultMaxNumRead,
148+
maxRead: c.BatchReadSize,
145149
renderBuf: make([]byte, renderBufferSize),
146150
logPrefix: fmt.Sprintf("WinEventLog[%s]", c.Name),
147151
}, nil
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// +build windows
2+
3+
package eventlog
4+
5+
import (
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestWinEventLogBatchReadSize(t *testing.T) {
12+
configureLogp()
13+
log, err := initLog(providerName, sourceName, eventCreateMsgFile)
14+
if err != nil {
15+
t.Fatal(err)
16+
}
17+
defer func() {
18+
err := uninstallLog(providerName, sourceName, log)
19+
if err != nil {
20+
t.Fatal(err)
21+
}
22+
}()
23+
24+
// Publish test messages:
25+
for k, m := range messages {
26+
err = log.Report(m.eventType, k, []string{m.message})
27+
if err != nil {
28+
t.Fatal(err)
29+
}
30+
}
31+
32+
batchReadSize := 2
33+
eventlog, err := newWinEventLog(Config{Name: providerName, BatchReadSize: 2})
34+
if err != nil {
35+
t.Fatal(err)
36+
}
37+
err = eventlog.Open(0)
38+
if err != nil {
39+
t.Fatal(err)
40+
}
41+
defer func() {
42+
err := eventlog.Close()
43+
if err != nil {
44+
t.Fatal(err)
45+
}
46+
}()
47+
48+
records, err := eventlog.Read()
49+
if err != nil {
50+
t.Fatal(err)
51+
}
52+
53+
assert.Len(t, records, batchReadSize)
54+
}

0 commit comments

Comments
 (0)