From 3be983b231b781c18c41d0f10c52f837edb360fd Mon Sep 17 00:00:00 2001 From: Michel Laterman <82832767+michel-laterman@users.noreply.github.com> Date: Thu, 20 Jan 2022 14:41:55 -0800 Subject: [PATCH] Add beat/process config to elastic agent diagnostics collect (#29902) * Add beat/process config to elastic agent diagnostics collect Add the configs that are output from elastic-agent inspect output to the bundle generated by elastic-agent diagnostics collect. Files are named as ProcID_RouteKey. * Apply suggestions from code review Co-authored-by: Anderson Queiroz Co-authored-by: Anderson Queiroz --- x-pack/elastic-agent/CHANGELOG.next.asciidoc | 1 + .../pkg/agent/cmd/diagnostics.go | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/x-pack/elastic-agent/CHANGELOG.next.asciidoc b/x-pack/elastic-agent/CHANGELOG.next.asciidoc index 40cecf51b90..37f09cec0cc 100644 --- a/x-pack/elastic-agent/CHANGELOG.next.asciidoc +++ b/x-pack/elastic-agent/CHANGELOG.next.asciidoc @@ -157,3 +157,4 @@ - Allow pprof endpoints for elastic-agent or beats if enabled. {pull}28983[28983] {pull}29155[29155] - Add --fleet-server-es-ca-trusted-fingerprint flag to allow agent/fleet-server to work with elasticsearch clusters using self signed certs. {pull}29128[29128] - Discover changes in Kubernetes nodes metadata as soon as they happen. {pull}23139[23139] +- Add results of inspect output command into archive produced by diagnostics collect. {pull}29902[29902] diff --git a/x-pack/elastic-agent/pkg/agent/cmd/diagnostics.go b/x-pack/elastic-agent/pkg/agent/cmd/diagnostics.go index b32edf6df2d..d5b7a759f72 100644 --- a/x-pack/elastic-agent/pkg/agent/cmd/diagnostics.go +++ b/x-pack/elastic-agent/pkg/agent/cmd/diagnostics.go @@ -22,6 +22,7 @@ import ( "github.com/spf13/cobra" "gopkg.in/yaml.v2" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/info" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/paths" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/configuration" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/control/client" @@ -47,6 +48,7 @@ type DiagnosticsInfo struct { type AgentConfig struct { ConfigLocal *configuration.Configuration ConfigRendered map[string]interface{} + AppConfig map[string]interface{} // map of processName_rk:config } func newDiagnosticsCommand(s []string, streams *cli.IOStreams) *cobra.Command { @@ -361,6 +363,35 @@ func gatherConfig() (AgentConfig, error) { } cfg.ConfigRendered = mapCFG + // Gather vars to render process config + isStandalone, err := isStandalone(renderedCFG) + if err != nil { + return AgentConfig{}, err + } + + agentInfo, err := info.NewAgentInfo(false) + if err != nil { + return AgentConfig{}, err + } + + log, err := newErrorLogger() + if err != nil { + return AgentConfig{}, err + } + + // Get process config - uses same approach as inspect output command. + // Does not contact server process to request configs. + pMap, err := getProgramsFromConfig(log, agentInfo, renderedCFG, isStandalone) + if err != nil { + return AgentConfig{}, err + } + cfg.AppConfig = make(map[string]interface{}, 0) + for rk, programs := range pMap { + for _, p := range programs { + cfg.AppConfig[p.Identifier()+"_"+rk] = p.Configuration() + } + } + return cfg, nil } @@ -419,6 +450,15 @@ func createZip(fileName, outputFormat string, diag DiagnosticsInfo, cfg AgentCon if err := writeFile(zf, outputFormat, cfg.ConfigRendered); err != nil { return closeHandlers(err, zw, f) } + for name, appCfg := range cfg.AppConfig { + zf, err := zw.Create("config/" + name + "." + outputFormat) + if err != nil { + return closeHandlers(err, zw, f) + } + if err := writeFile(zf, outputFormat, appCfg); err != nil { + return closeHandlers(err, zw, f) + } + } if err := zipLogs(zw); err != nil { return closeHandlers(err, zw, f)