github.com/huiliang/nomad@v0.2.1-0.20151124023127-7a8b664699ff/command/node_status.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"strings"
     7  )
     8  
     9  type NodeStatusCommand struct {
    10  	Meta
    11  }
    12  
    13  func (c *NodeStatusCommand) Help() string {
    14  	helpText := `
    15  Usage: nomad node-status [options] [node]
    16  
    17    Display status information about a given node. The list of nodes
    18    returned includes only nodes which jobs may be scheduled to, and
    19    includes status and other high-level information.
    20  
    21    If a node ID is passed, information for that specific node will
    22    be displayed. If no node ID's are passed, then a short-hand
    23    list of all nodes will be displayed.
    24  
    25  General Options:
    26  
    27    ` + generalOptionsUsage() + `
    28  
    29  Node Status Options:
    30  
    31    -short
    32      Display short output. Used only when a single node is being
    33      queried, and drops verbose output about node allocations.
    34  `
    35  	return strings.TrimSpace(helpText)
    36  }
    37  
    38  func (c *NodeStatusCommand) Synopsis() string {
    39  	return "Display status information about nodes"
    40  }
    41  
    42  func (c *NodeStatusCommand) Run(args []string) int {
    43  	var short bool
    44  
    45  	flags := c.Meta.FlagSet("node-status", FlagSetClient)
    46  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    47  	flags.BoolVar(&short, "short", false, "")
    48  
    49  	if err := flags.Parse(args); err != nil {
    50  		return 1
    51  	}
    52  
    53  	// Check that we got either a single node or none
    54  	args = flags.Args()
    55  	if len(args) > 1 {
    56  		c.Ui.Error(c.Help())
    57  		return 1
    58  	}
    59  
    60  	// Get the HTTP client
    61  	client, err := c.Meta.Client()
    62  	if err != nil {
    63  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    64  		return 1
    65  	}
    66  
    67  	// Use list mode if no node name was provided
    68  	if len(args) == 0 {
    69  		// Query the node info
    70  		nodes, _, err := client.Nodes().List(nil)
    71  		if err != nil {
    72  			c.Ui.Error(fmt.Sprintf("Error querying node status: %s", err))
    73  			return 1
    74  		}
    75  
    76  		// Return nothing if no nodes found
    77  		if len(nodes) == 0 {
    78  			return 0
    79  		}
    80  
    81  		// Format the nodes list
    82  		out := make([]string, len(nodes)+1)
    83  		out[0] = "ID|DC|Name|Class|Drain|Status"
    84  		for i, node := range nodes {
    85  			out[i+1] = fmt.Sprintf("%s|%s|%s|%s|%v|%s",
    86  				node.ID,
    87  				node.Datacenter,
    88  				node.Name,
    89  				node.NodeClass,
    90  				node.Drain,
    91  				node.Status)
    92  		}
    93  
    94  		// Dump the output
    95  		c.Ui.Output(formatList(out))
    96  		return 0
    97  	}
    98  
    99  	// Query the specific node
   100  	nodeID := args[0]
   101  	node, _, err := client.Nodes().Info(nodeID, nil)
   102  	if err != nil {
   103  		c.Ui.Error(fmt.Sprintf("Error querying node info: %s", err))
   104  		return 1
   105  	}
   106  
   107  	m := node.Attributes
   108  	keys := make([]string, len(m))
   109  	for k := range m {
   110  		keys = append(keys, k)
   111  	}
   112  	sort.Strings(keys)
   113  
   114  	var attributes []string
   115  	for _, k := range keys {
   116  		if k != "" {
   117  			attributes = append(attributes, fmt.Sprintf("%s:%s", k, m[k]))
   118  		}
   119  	}
   120  
   121  	// Format the output
   122  	basic := []string{
   123  		fmt.Sprintf("ID|%s", node.ID),
   124  		fmt.Sprintf("Name|%s", node.Name),
   125  		fmt.Sprintf("Class|%s", node.NodeClass),
   126  		fmt.Sprintf("Datacenter|%s", node.Datacenter),
   127  		fmt.Sprintf("Drain|%v", node.Drain),
   128  		fmt.Sprintf("Status|%s", node.Status),
   129  		fmt.Sprintf("Attributes|%s", strings.Join(attributes, ", ")),
   130  	}
   131  
   132  	var allocs []string
   133  	if !short {
   134  		// Query the node allocations
   135  		nodeAllocs, _, err := client.Nodes().Allocations(nodeID, nil)
   136  		if err != nil {
   137  			c.Ui.Error(fmt.Sprintf("Error querying node allocations: %s", err))
   138  			return 1
   139  		}
   140  
   141  		// Format the allocations
   142  		allocs = make([]string, len(nodeAllocs)+1)
   143  		allocs[0] = "ID|EvalID|JobID|TaskGroup|DesiredStatus|ClientStatus"
   144  		for i, alloc := range nodeAllocs {
   145  			allocs[i+1] = fmt.Sprintf("%s|%s|%s|%s|%s|%s",
   146  				alloc.ID,
   147  				alloc.EvalID,
   148  				alloc.JobID,
   149  				alloc.TaskGroup,
   150  				alloc.DesiredStatus,
   151  				alloc.ClientStatus)
   152  		}
   153  	}
   154  
   155  	// Dump the output
   156  	c.Ui.Output(formatKV(basic))
   157  	if !short {
   158  		c.Ui.Output("\n### Allocations")
   159  		c.Ui.Output(formatList(allocs))
   160  	}
   161  	return 0
   162  }