Skip to content

Commit

Permalink
command/info: Adding new info command
Browse files Browse the repository at this point in the history
  • Loading branch information
armon committed Feb 24, 2014
1 parent 015670d commit fcf0b89
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 0 deletions.
14 changes: 14 additions & 0 deletions command/agent/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const (
stopCommand = "stop"
monitorCommand = "monitor"
leaveCommand = "leave"
statsCommand = "stats"
)

const (
Expand Down Expand Up @@ -347,6 +348,9 @@ func (i *AgentRPC) handleRequest(client *rpcClient, reqHeader *requestHeader) er
case leaveCommand:
return i.handleLeave(client, seq)

case statsCommand:
return i.handleStats(client, seq)

default:
respHeader := responseHeader{Seq: seq, Error: unsupportedCommand}
client.Send(&respHeader, nil)
Expand Down Expand Up @@ -535,6 +539,16 @@ func (i *AgentRPC) handleLeave(client *rpcClient, seq uint64) error {
return err
}

// handleStats is used to get various statistics
func (i *AgentRPC) handleStats(client *rpcClient, seq uint64) error {
header := responseHeader{
Seq: seq,
Error: "",
}
resp := i.agent.Stats()
return client.Send(&header, resp)
}

// Used to convert an error to a string representation
func errToString(err error) string {
if err == nil {
Expand Down
12 changes: 12 additions & 0 deletions command/agent/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,18 @@ func (c *RPCClient) Leave() error {
return c.genericRPC(&header, nil, nil)
}

// Stats is used to get debugging state information
func (c *RPCClient) Stats() (map[string]map[string]string, error) {
header := requestHeader{
Command: statsCommand,
Seq: c.getSeq(),
}
var resp map[string]map[string]string

err := c.genericRPC(&header, nil, &resp)
return resp, err
}

type monitorHandler struct {
client *RPCClient
closed bool
Expand Down
19 changes: 19 additions & 0 deletions command/agent/rpc_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,22 @@ OUTER2:
t.Fatalf("should log joining")
}
}

func TestRPCClientStats(t *testing.T) {
p1 := testRPCClient(t)
defer p1.Close()
testutil.Yield()

stats, err := p1.client.Stats()
if err != nil {
t.Fatalf("err: %s", err)
}

if _, ok := stats["agent"]; !ok {
t.Fatalf("bad: %#v", stats)
}

if _, ok := stats["consul"]; !ok {
t.Fatalf("bad: %#v", stats)
}
}
81 changes: 81 additions & 0 deletions command/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package command

import (
"flag"
"fmt"
"github.com/mitchellh/cli"
"sort"
"strings"
)

// InfoCommand is a Command implementation that queries a running
// Consul agent for various debugging statistics for operators
type InfoCommand struct {
Ui cli.Ui
}

func (i *InfoCommand) Help() string {
helpText := `
Usage: consul info [options]
Provides debugging information for operators
Options:
-rpc-addr=127.0.0.1:8400 RPC address of the Consul agent.
`
return strings.TrimSpace(helpText)
}

func (i *InfoCommand) Run(args []string) int {
cmdFlags := flag.NewFlagSet("info", flag.ContinueOnError)
cmdFlags.Usage = func() { i.Ui.Output(i.Help()) }
rpcAddr := RPCAddrFlag(cmdFlags)
if err := cmdFlags.Parse(args); err != nil {
return 1
}

client, err := RPCClient(*rpcAddr)
if err != nil {
i.Ui.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
return 1
}
defer client.Close()

stats, err := client.Stats()
if err != nil {
i.Ui.Error(fmt.Sprintf("Error querying agent: %s", err))
return 1
}

// Get the keys in sorted order
keys := make([]string, 0, len(stats))
for key := range stats {
keys = append(keys, key)
}
sort.Strings(keys)

// Iterate over each top-level key
for _, key := range keys {
i.Ui.Output(key)

// Sort the sub-keys
subvals := stats[key]
subkeys := make([]string, 0, len(subvals))
for k := range subvals {
subkeys = append(subkeys, k)
}
sort.Strings(subkeys)

// Iterate over the subkeys
for _, subkey := range subkeys {
val := subvals[subkey]
i.Ui.Output(fmt.Sprintf("\t%s = %s", subkey, val))
}
}
return 0
}

func (i *InfoCommand) Synopsis() string {
return "Provides debugging information for operators"
}
30 changes: 30 additions & 0 deletions command/info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package command

import (
"fmt"
"github.com/mitchellh/cli"
"strings"
"testing"
)

func TestInfoCommand_implements(t *testing.T) {
var _ cli.Command = &InfoCommand{}
}

func TestInfoCommandRun(t *testing.T) {
a1 := testAgent(t)
defer a1.Shutdown()

ui := new(cli.MockUi)
c := &InfoCommand{Ui: ui}
args := []string{"-rpc-addr=" + a1.addr}

code := c.Run(args)
if code != 0 {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
}

if !strings.Contains(ui.OutputWriter.String(), "agent") {
t.Fatalf("bad: %#v", ui.OutputWriter.String())
}
}
6 changes: 6 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ func init() {
}, nil
},

"info": func() (cli.Command, error) {
return &command.InfoCommand{
Ui: ui,
}, nil
},

"version": func() (cli.Command, error) {
return &command.VersionCommand{
Revision: GitCommit,
Expand Down

0 comments on commit fcf0b89

Please sign in to comment.