Skip to content

Commit

Permalink
Move monitor command to its own package
Browse files Browse the repository at this point in the history
  • Loading branch information
Preetha Appan committed Oct 13, 2017
1 parent 6770b64 commit 9541818
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 32 deletions.
9 changes: 2 additions & 7 deletions command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/hashicorp/consul/command/lock"
"github.com/hashicorp/consul/command/maint"
"github.com/hashicorp/consul/command/members"
"github.com/hashicorp/consul/command/monitor"
"github.com/hashicorp/consul/command/validate"
"github.com/hashicorp/consul/version"
"github.com/mitchellh/cli"
Expand Down Expand Up @@ -137,13 +138,7 @@ func init() {
},

"monitor": func() (cli.Command, error) {
return &MonitorCommand{
ShutdownCh: makeShutdownCh(),
BaseCommand: BaseCommand{
Flags: FlagSetClientHTTP,
UI: ui,
},
}, nil
return monitor.New(ui, makeShutdownCh()), nil
},

"operator": func() (cli.Command, error) {
Expand Down
68 changes: 43 additions & 25 deletions command/monitor.go → command/monitor/monitor.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package command
package monitor

import (
"flag"
"fmt"
"sync"

"github.com/hashicorp/consul/command/flags"
"github.com/mitchellh/cli"
)

// MonitorCommand is a Command implementation that queries a running
// cmd is a Command implementation that queries a running
// Consul agent what members are part of the cluster currently.
type MonitorCommand struct {
BaseCommand
type cmd struct {
UI cli.Ui
usage string
flags *flag.FlagSet
http *flags.HTTPFlags

ShutdownCh <-chan struct{}
shutdownCh <-chan struct{}

lock sync.Mutex
quitting bool
Expand All @@ -19,33 +26,33 @@ type MonitorCommand struct {
logLevel string
}

func (c *MonitorCommand) initFlags() {
c.InitFlagSet()
c.FlagSet.StringVar(&c.logLevel, "log-level", "INFO",
"Log level of the agent.")
func New(ui cli.Ui, shutdownCh <-chan struct{}) *cmd {
c := &cmd{UI: ui, shutdownCh: shutdownCh}
c.init()
return c
}

func (c *MonitorCommand) Help() string {
c.initFlags()
return c.HelpCommand(`
Usage: consul monitor [options]
func (c *cmd) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())

Shows recent log messages of a Consul agent, and attaches to the agent,
outputting log messages as they occur in real time. The monitor lets you
listen for log levels that may be filtered out of the Consul agent. For
example your agent may only be logging at INFO level, but with the monitor
you can see the DEBUG level logs.
c.flags.StringVar(&c.logLevel, "log-level", "INFO",
"Log level of the agent.")

c.usage = flags.Usage(usage, c.flags, c.http.ClientFlags(), nil)
}

`)
func (c *cmd) Help() string {
return c.usage
}

func (c *MonitorCommand) Run(args []string) int {
c.initFlags()
if err := c.FlagSet.Parse(args); err != nil {
func (c *cmd) Run(args []string) int {
if err := c.flags.Parse(args); err != nil {
return 1
}

client, err := c.HTTPClient()
client, err := c.http.APIClient()
if err != nil {
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
return 1
Expand Down Expand Up @@ -83,7 +90,7 @@ func (c *MonitorCommand) Run(args []string) int {
select {
case <-eventDoneCh:
return 1
case <-c.ShutdownCh:
case <-c.shutdownCh:
c.lock.Lock()
c.quitting = true
c.lock.Unlock()
Expand All @@ -92,6 +99,17 @@ func (c *MonitorCommand) Run(args []string) int {
return 0
}

func (c *MonitorCommand) Synopsis() string {
func (c *cmd) Synopsis() string {
return "Stream logs from a Consul agent"
}

const usage = `
Usage: consul monitor [options]
Shows recent log messages of a Consul agent, and attaches to the agent,
outputting log messages as they occur in real time. The monitor lets you
listen for log levels that may be filtered out of the Consul agent. For
example your agent may only be logging at INFO level, but with the monitor
you can see the DEBUG level logs.
`

0 comments on commit 9541818

Please sign in to comment.