From ebdb4ec129e6cf31292489a2930dbba2baa16b51 Mon Sep 17 00:00:00 2001 From: Shaunak Kashyap Date: Tue, 25 Jun 2019 09:33:37 -0400 Subject: [PATCH] Check validity of logstash module configuration when `xpack.enabled` is set to `true`. When the `logstash` metricbeat module is enabled and the `xpack.enabled` flag in the module configuration is set to `true`, an exact set of metricsets is required to be configured for Stack Monitoring to work correctly. This PR implements the necessary validation. --- metricbeat/module/logstash/logstash.go | 44 ++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) 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"