-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move winlogbeat to new publisher pipeline
- Move winlogbeat to publisher pipeline - move fields + tags processing to pipeline client - introduce local `processors` setting for each configured event log. - minor cleanups in tests
- Loading branch information
1 parent
6093150
commit 6dd6290
Showing
15 changed files
with
335 additions
and
298 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package beater | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/elastic/beats/libbeat/logp" | ||
"github.com/elastic/beats/libbeat/processors" | ||
"github.com/elastic/beats/libbeat/publisher/bc/publisher" | ||
"github.com/elastic/beats/libbeat/publisher/beat" | ||
"github.com/elastic/beats/winlogbeat/checkpoint" | ||
"github.com/elastic/beats/winlogbeat/eventlog" | ||
) | ||
|
||
type eventLogger struct { | ||
source eventlog.EventLog | ||
eventMeta common.EventMetadata | ||
processors beat.ProcessorList | ||
} | ||
|
||
type eventLoggerConfig struct { | ||
common.EventMetadata `config:",inline"` // Fields and tags to add to events. | ||
Processors processors.PluginConfig `config:"processors"` | ||
} | ||
|
||
func newEventLogger( | ||
source eventlog.EventLog, | ||
options *common.Config, | ||
) (*eventLogger, error) { | ||
config := eventLoggerConfig{} | ||
if err := options.Unpack(&config); err != nil { | ||
return nil, err | ||
} | ||
|
||
processors, err := processors.New(config.Processors) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &eventLogger{ | ||
source: source, | ||
eventMeta: config.EventMetadata, | ||
processors: processors, | ||
}, nil | ||
} | ||
|
||
func (e *eventLogger) connect(pipeline publisher.Publisher) (beat.Client, error) { | ||
api := e.source.Name() | ||
return pipeline.ConnectX(beat.ClientConfig{ | ||
PublishMode: beat.GuaranteedSend, | ||
EventMetadata: e.eventMeta, | ||
Meta: nil, // TODO: configure modules/ES ingest pipeline? | ||
Processor: e.processors, | ||
ACKCount: func(n int) { | ||
addPublished(api, n) | ||
logp.Info("EventLog[%s] successfully published %d events", api, n) | ||
}, | ||
}) | ||
} | ||
|
||
func (e *eventLogger) run( | ||
done <-chan struct{}, | ||
pipeline publisher.Publisher, | ||
state checkpoint.EventLogState, | ||
) { | ||
api := e.source | ||
|
||
// Initialize per event log metrics. | ||
initMetrics(api.Name()) | ||
|
||
client, err := e.connect(pipeline) | ||
if err != nil { | ||
logp.Warn("EventLog[%s] Pipeline error. Failed to connect to publisher pipeline", | ||
api.Name()) | ||
return | ||
} | ||
|
||
// close client on function return or when `done` is triggered (unblock client) | ||
defer client.Close() | ||
go func() { | ||
<-done | ||
client.Close() | ||
}() | ||
|
||
err = api.Open(state.RecordNumber) | ||
if err != nil { | ||
logp.Warn("EventLog[%s] Open() error. No events will be read from "+ | ||
"this source. %v", api.Name(), err) | ||
return | ||
} | ||
defer func() { | ||
logp.Info("EventLog[%s] Stop processing.", api.Name()) | ||
|
||
if err := api.Close(); err != nil { | ||
logp.Warn("EventLog[%s] Close() error. %v", api.Name(), err) | ||
return | ||
} | ||
}() | ||
|
||
debugf("EventLog[%s] opened successfully", api.Name()) | ||
|
||
for { | ||
select { | ||
case <-done: | ||
return | ||
default: | ||
} | ||
|
||
// Read from the event. | ||
records, err := api.Read() | ||
if err != nil { | ||
logp.Warn("EventLog[%s] Read() error: %v", api.Name(), err) | ||
break | ||
} | ||
|
||
debugf("EventLog[%s] Read() returned %d records", api.Name(), len(records)) | ||
if len(records) == 0 { | ||
// TODO: Consider implementing notifications using | ||
// NotifyChangeEventLog instead of polling. | ||
time.Sleep(time.Second) | ||
continue | ||
} | ||
|
||
for _, lr := range records { | ||
client.Publish(lr.ToEvent()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package beater | ||
|
||
import "expvar" | ||
|
||
// Metrics that can retrieved through the expvar web interface. Metrics must be | ||
// enable through configuration in order for the web service to be started. | ||
var ( | ||
publishedEvents = expvar.NewMap("published_events") | ||
) | ||
|
||
func initMetrics(namespace string) { | ||
// Initialize metrics. | ||
publishedEvents.Add(namespace, 0) | ||
} | ||
|
||
func addPublished(namespace string, n int) { | ||
numEvents := int64(n) | ||
publishedEvents.Add("total", numEvents) | ||
publishedEvents.Add(namespace, numEvents) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.