github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/command/agent_info.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"strings"
     7  )
     8  
     9  type AgentInfoCommand struct {
    10  	Meta
    11  }
    12  
    13  func (c *AgentInfoCommand) Help() string {
    14  	helpText := `
    15  Usage: nomad agent-info [options]
    16  
    17    Display status information about the local agent.
    18  
    19  General Options:
    20  
    21    ` + generalOptionsUsage()
    22  	return strings.TrimSpace(helpText)
    23  }
    24  
    25  func (c *AgentInfoCommand) Synopsis() string {
    26  	return "Display status information about the local agent"
    27  }
    28  
    29  func (c *AgentInfoCommand) Run(args []string) int {
    30  	flags := c.Meta.FlagSet("agent-info", FlagSetClient)
    31  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    32  	if err := flags.Parse(args); err != nil {
    33  		return 1
    34  	}
    35  
    36  	// Check that we either got no jobs or exactly one.
    37  	args = flags.Args()
    38  	if len(args) > 0 {
    39  		c.Ui.Error(c.Help())
    40  		return 1
    41  	}
    42  
    43  	// Get the HTTP client
    44  	client, err := c.Meta.Client()
    45  	if err != nil {
    46  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    47  		return 1
    48  	}
    49  
    50  	// Query the agent info
    51  	info, err := client.Agent().Self()
    52  	if err != nil {
    53  		c.Ui.Error(fmt.Sprintf("Error querying agent info: %s", err))
    54  		return 1
    55  	}
    56  
    57  	// Sort and output agent info
    58  	statsKeys := make([]string, 0, len(info.Stats))
    59  	for key := range info.Stats {
    60  		statsKeys = append(statsKeys, key)
    61  	}
    62  	sort.Strings(statsKeys)
    63  
    64  	for _, key := range statsKeys {
    65  		c.Ui.Output(key)
    66  		statsData, _ := info.Stats[key]
    67  		statsDataKeys := make([]string, len(statsData))
    68  		i := 0
    69  		for key := range statsData {
    70  			statsDataKeys[i] = key
    71  			i++
    72  		}
    73  		sort.Strings(statsDataKeys)
    74  
    75  		for _, key := range statsDataKeys {
    76  			c.Ui.Output(fmt.Sprintf("  %s = %v", key, statsData[key]))
    77  		}
    78  	}
    79  
    80  	return 0
    81  }