diff --git a/command/agent_info.go b/command/agent_info.go new file mode 100644 index 000000000..b01351960 --- /dev/null +++ b/command/agent_info.go @@ -0,0 +1,81 @@ +package command + +import ( + "flag" + "fmt" + "strings" + + "github.com/mitchellh/cli" +) + +type AgentInfoCommand struct { + Ui cli.Ui +} + +func (c *AgentInfoCommand) Help() string { + helpText := ` +Usage: nomad agent-info [options] + + Displays status information about the local agent. + +Options: + + -help + Display this message + + -http-addr + Address of the Nomad API to connect. Can also be specified + using the environment variable NOMAD_HTTP_ADDR. + Default = http://127.0.0.1:4646 +` + return strings.TrimSpace(helpText) +} + +func (c *AgentInfoCommand) Synopsis() string { + return "Displays local agent information and status" +} + +func (c *AgentInfoCommand) Run(args []string) int { + var httpAddr *string + + flags := flag.NewFlagSet("agent-info", flag.ContinueOnError) + flags.Usage = func() { c.Ui.Output(c.Help()) } + httpAddr = httpAddrFlag(flags) + + if err := flags.Parse(args); err != nil { + return 1 + } + + // Check that we either got no jobs or exactly one. + if len(flags.Args()) > 0 { + c.Ui.Error(c.Help()) + return 1 + } + + // Get the HTTP client + client, err := httpClient(*httpAddr) + if err != nil { + c.Ui.Error(fmt.Sprintf("Failed initializing Nomad client: %s", err)) + return 1 + } + + // Query the agent info + info, err := client.Agent().Self() + if err != nil { + c.Ui.Error(fmt.Sprintf("Failed querying agent info: %s", err)) + return 1 + } + + var stats map[string]interface{} + stats, _ = info["stats"] + + for section, data := range stats { + c.Ui.Output(section) + d, _ := data.(map[string]interface{}) + for k, v := range d { + c.Ui.Output(fmt.Sprintf(" %s = %v", k, v)) + } + } + + return 0 +} diff --git a/commands.go b/commands.go index a94e0ba2b..398be1f3c 100644 --- a/commands.go +++ b/commands.go @@ -34,6 +34,12 @@ func Commands(metaPtr *command.Meta) map[string]cli.CommandFactory { }, nil }, + "agent-info": func() (cli.Command, error) { + return &command.AgentInfoCommand{ + Ui: meta.Ui, + }, nil + }, + "status": func() (cli.Command, error) { return &command.StatusCommand{ Ui: meta.Ui,