diff --git a/metricbeat/module/logstash/logstash.go b/metricbeat/module/logstash/logstash.go index f33d1396069..79021f329cb 100644 --- a/metricbeat/module/logstash/logstash.go +++ b/metricbeat/module/logstash/logstash.go @@ -23,10 +23,54 @@ import ( "github.com/pkg/errors" + "github.com/elastic/beats/libbeat/common" "github.com/elastic/beats/metricbeat/helper" "github.com/elastic/beats/metricbeat/mb" ) +func init() { + // Register the ModuleFactory function for this module. + if err := mb.Registry.AddModule(ModuleName, NewModule); err != nil { + panic(err) + } +} + +// NewModule creates a new module after performing validation. +func NewModule(base mb.BaseModule) (mb.Module, error) { + if err := validateXPackMetricsets(base); err != nil { + return nil, err + } + + return &base, nil +} + +// Validate that correct metricsets have been specified if xpack.enabled = true. +func validateXPackMetricsets(base mb.BaseModule) error { + config := struct { + Metricsets []string `config:"metricsets"` + XPackEnabled bool `config:"xpack.enabled"` + }{} + if err := base.UnpackConfig(&config); err != nil { + return err + } + + // Nothing to validate if xpack.enabled != true + if !config.XPackEnabled { + return nil + } + + expectedXPackMetricsets := []string{ + "node", + "node_stats", + } + + if !common.MakeStringSet(config.Metricsets...).Equals(common.MakeStringSet(expectedXPackMetricsets...)) { + return errors.Errorf("The %v module with xpack.enabled: true must have metricsets: %v", ModuleName, expectedXPackMetricsets) + } + + return nil +} + // ModuleName is the name of this module. const ModuleName = "logstash"