diff --git a/libbeat/cfgfile/cfgfile.go b/libbeat/cfgfile/cfgfile.go index f77325109779..2b88aaad1577 100644 --- a/libbeat/cfgfile/cfgfile.go +++ b/libbeat/cfgfile/cfgfile.go @@ -112,8 +112,6 @@ func GetDefaultCfgfile() string { } // HandleFlags adapts default config settings based on command line flags. -// This also stores if -E management.enabled=true was set on command line -// to determine if running the Beat under agent. func HandleFlags() error { // default for the home path is the binary location home, err := filepath.Abs(filepath.Dir(os.Args[0])) @@ -131,27 +129,6 @@ func HandleFlags() error { common.PrintConfigDebugf(overwrites, "CLI setting overwrites (-E flag):") } - // Enable check to see if beat is running under Agent - // This is stored in a package so the modules which don't have - // access to the config can check this value. - type management struct { - Enabled bool `config:"management.enabled"` - } - var managementSettings management - cfgFlag := flag.Lookup("E") - if cfgFlag == nil { - fleetmode.SetAgentMode(false) - return nil - } - cfgObject, _ := cfgFlag.Value.(*config.SettingsFlag) - cliCfg := cfgObject.Config() - - err = cliCfg.Unpack(&managementSettings) - if err != nil { - fleetmode.SetAgentMode(false) - return nil //nolint:nilerr // unpacking failing isn't an error for this case - } - fleetmode.SetAgentMode(managementSettings.Enabled) return nil } diff --git a/libbeat/common/fleetmode/fleet_mode.go b/libbeat/common/fleetmode/fleet_mode.go index 97a17804f647..af179b887eac 100644 --- a/libbeat/common/fleetmode/fleet_mode.go +++ b/libbeat/common/fleetmode/fleet_mode.go @@ -17,18 +17,33 @@ package fleetmode -var managementEnabled bool - -// SetAgentMode stores if the Beat is running under Elastic Agent. -// Normally this is called when the command line flags are parsed. -// This is stored as a package level variable because some components -// (like filebeat/metricbeat modules) don't have access to the -// configuration information to determine this on their own. -func SetAgentMode(enabled bool) { - managementEnabled = enabled -} +import ( + "flag" + + "github.com/elastic/elastic-agent-libs/config" +) -// Enabled returns true if the Beat is running under Elastic Agent. +// Enabled checks to see if filebeat/metricbeat is running under Agent +// The management setting is stored in the main Beat runtime object, but we can't see that from a module +// So instead we check the CLI flags, since Agent starts filebeat/metricbeat with "-E", "management.enabled=true" func Enabled() bool { - return managementEnabled + type management struct { + Enabled bool `config:"management.enabled"` + } + var managementSettings management + + cfgFlag := flag.Lookup("E") + if cfgFlag == nil { + return false + } + + cfgObject, _ := cfgFlag.Value.(*config.SettingsFlag) + cliCfg := cfgObject.Config() + + err := cliCfg.Unpack(&managementSettings) + if err != nil { + return false + } + + return managementSettings.Enabled }