Skip to content

Commit

Permalink
Add start of elastic-agent diagnostics command (#28265)
Browse files Browse the repository at this point in the history
This PR starts the elastic-agent diagnostics command.
The beats info ("/") HTTP endpoint has been changed to add more data about the running beat including git commit and ephemeral ID.
Currently the diagnostics command will gather beats metadata information from the endpoint and display them along with agent version information.

(cherry picked from commit 887e40a)

# Conflicts:
#	libbeat/cmd/instance/beat.go
#	x-pack/elastic-agent/pkg/agent/cmd/diagnostics.go
  • Loading branch information
michel-laterman authored and mergify-bot committed Nov 10, 2021
1 parent a048c82 commit 37a00da
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
10 changes: 10 additions & 0 deletions libbeat/cmd/instance/beat.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func Run(settings Settings, bt beat.Creator) error {
monitoring.NewTimestamp(registry, "build_time").Set(version.BuildTime())
monitoring.NewBool(registry, "elastic_licensed").Set(b.Info.ElasticLicensed)

<<<<<<< HEAD
if u, err := user.Current(); err != nil {
if _, ok := err.(user.UnknownUserIdError); ok {
// This usually happens if the user UID does not exist in /etc/passwd. It might be the case on K8S
Expand All @@ -206,6 +207,15 @@ func Run(settings Settings, bt beat.Creator) error {
monitoring.NewString(registry, "uid").Set(u.Uid)
monitoring.NewString(registry, "gid").Set(u.Gid)
}
=======
u, err := user.Current()
if err != nil {
return err
}
monitoring.NewString(registry, "username").Set(u.Username)
monitoring.NewString(registry, "uid").Set(u.Uid)
monitoring.NewString(registry, "gid").Set(u.Gid)
>>>>>>> 887e40a182 (Add start of elastic-agent diagnostics command (#28265))

// Add additional info to state registry. This is also reported to monitoring
stateRegistry := monitoring.GetNamespace("state").GetRegistry()
Expand Down
39 changes: 39 additions & 0 deletions x-pack/elastic-agent/pkg/agent/cmd/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package cmd

import (
<<<<<<< HEAD
"archive/zip"
"context"
"encoding/json"
Expand All @@ -28,6 +29,20 @@ import (
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/cli"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config/operations"
=======
"context"
"fmt"
"io"
"os"
"text/tabwriter"
"time"

"github.com/spf13/cobra"

"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/control/client"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/cli"
>>>>>>> 887e40a182 (Add start of elastic-agent diagnostics command (#28265))
)

var diagOutputs = map[string]outputter{
Expand All @@ -36,6 +51,7 @@ var diagOutputs = map[string]outputter{
"yaml": yamlOutput,
}

<<<<<<< HEAD
// DiagnosticsInfo a struct to track all information related to diagnostics for the agent.
type DiagnosticsInfo struct {
ProcMeta []client.ProcMeta
Expand All @@ -49,6 +65,9 @@ type AgentConfig struct {
}

func newDiagnosticsCommand(s []string, streams *cli.IOStreams) *cobra.Command {
=======
func newDiagnosticsCommand(_ []string, streams *cli.IOStreams) *cobra.Command {
>>>>>>> 887e40a182 (Add start of elastic-agent diagnostics command (#28265))
cmd := &cobra.Command{
Use: "diagnostics",
Short: "Gather diagnostics information from the elastic-agent and running processes.",
Expand All @@ -62,11 +81,15 @@ func newDiagnosticsCommand(s []string, streams *cli.IOStreams) *cobra.Command {
}

cmd.Flags().String("output", "human", "Output the diagnostics information in either human, json, or yaml (default: human)")
<<<<<<< HEAD
cmd.AddCommand(newDiagnosticsCollectCommandWithArgs(s, streams))
=======
>>>>>>> 887e40a182 (Add start of elastic-agent diagnostics command (#28265))

return cmd
}

<<<<<<< HEAD
func newDiagnosticsCollectCommandWithArgs(_ []string, streams *cli.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: "collect",
Expand Down Expand Up @@ -97,6 +120,12 @@ func newDiagnosticsCollectCommandWithArgs(_ []string, streams *cli.IOStreams) *c
cmd.Flags().String("output", "yaml", "Output the collected information in either json, or yaml (default: yaml)") // replace output flag with different options

return cmd
=======
// DiagnosticsInfo a struct to track all inforation related to diagnostics for the agent.
type DiagnosticsInfo struct {
ProcMeta []client.ProcMeta
AgentVersion client.Version
>>>>>>> 887e40a182 (Add start of elastic-agent diagnostics command (#28265))
}

func diagnosticCmd(streams *cli.IOStreams, cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -127,6 +156,7 @@ func diagnosticCmd(streams *cli.IOStreams, cmd *cobra.Command, args []string) er
return outputFunc(streams.Out, diag)
}

<<<<<<< HEAD
func diagnosticsCollectCmd(streams *cli.IOStreams, fileName, outputFormat string) error {
err := tryContainerLoadPaths()
if err != nil {
Expand Down Expand Up @@ -160,6 +190,8 @@ func diagnosticsCollectCmd(streams *cli.IOStreams, fileName, outputFormat string
return nil
}

=======
>>>>>>> 887e40a182 (Add start of elastic-agent diagnostics command (#28265))
func getDiagnostics(ctx context.Context) (DiagnosticsInfo, error) {
daemon := client.New()
diag := DiagnosticsInfo{}
Expand All @@ -177,7 +209,11 @@ func getDiagnostics(ctx context.Context) (DiagnosticsInfo, error) {

version, err := daemon.Version(ctx)
if err != nil {
<<<<<<< HEAD
return diag, err
=======
return DiagnosticsInfo{}, err
>>>>>>> 887e40a182 (Add start of elastic-agent diagnostics command (#28265))
}
diag.AgentVersion = version

Expand Down Expand Up @@ -215,6 +251,7 @@ func outputDiagnostics(w io.Writer, d DiagnosticsInfo) error {
tw.Flush()
return nil
}
<<<<<<< HEAD

func gatherConfig() (AgentConfig, error) {
cfg := AgentConfig{}
Expand Down Expand Up @@ -371,3 +408,5 @@ func closeHandlers(err error, closers ...io.Closer) error {
}
return mErr.ErrorOrNil()
}
=======
>>>>>>> 887e40a182 (Add start of elastic-agent diagnostics command (#28265))

0 comments on commit 37a00da

Please sign in to comment.