2023-03-28 19:12:30 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-10-13 15:15:54 +00:00
|
|
|
package monitor
|
2013-12-31 00:09:39 +00:00
|
|
|
|
|
|
|
import (
|
2017-10-13 15:15:54 +00:00
|
|
|
"flag"
|
2013-12-31 00:09:39 +00:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
2017-10-13 15:15:54 +00:00
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2017-10-13 15:15:54 +00:00
|
|
|
"github.com/hashicorp/consul/command/flags"
|
|
|
|
"github.com/mitchellh/cli"
|
2013-12-31 00:09:39 +00:00
|
|
|
)
|
|
|
|
|
2017-10-13 15:15:54 +00:00
|
|
|
// cmd is a Command implementation that queries a running
|
2013-12-31 00:09:39 +00:00
|
|
|
// Consul agent what members are part of the cluster currently.
|
2017-10-13 15:15:54 +00:00
|
|
|
type cmd struct {
|
|
|
|
UI cli.Ui
|
2017-10-17 13:44:20 +00:00
|
|
|
help string
|
2017-10-13 15:15:54 +00:00
|
|
|
flags *flag.FlagSet
|
|
|
|
http *flags.HTTPFlags
|
2017-02-09 22:31:52 +00:00
|
|
|
|
2017-10-13 15:15:54 +00:00
|
|
|
shutdownCh <-chan struct{}
|
2013-12-31 00:09:39 +00:00
|
|
|
|
|
|
|
lock sync.Mutex
|
|
|
|
quitting bool
|
2017-10-11 12:51:18 +00:00
|
|
|
|
|
|
|
// flags
|
|
|
|
logLevel string
|
2020-01-28 23:50:41 +00:00
|
|
|
logJSON bool
|
2017-10-11 12:51:18 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 15:15:54 +00:00
|
|
|
func New(ui cli.Ui, shutdownCh <-chan struct{}) *cmd {
|
|
|
|
c := &cmd{UI: ui, shutdownCh: shutdownCh}
|
|
|
|
c.init()
|
|
|
|
return c
|
2013-12-31 00:09:39 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 15:15:54 +00:00
|
|
|
func (c *cmd) init() {
|
|
|
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
c.flags.StringVar(&c.logLevel, "log-level", "INFO",
|
|
|
|
"Log level of the agent.")
|
2020-01-28 23:50:41 +00:00
|
|
|
c.flags.BoolVar(&c.logJSON, "log-json", false,
|
|
|
|
"Output logs in JSON format.")
|
2017-10-13 15:15:54 +00:00
|
|
|
|
2017-10-18 00:38:26 +00:00
|
|
|
c.http = &flags.HTTPFlags{}
|
|
|
|
flags.Merge(c.flags, c.http.ClientFlags())
|
2017-10-17 22:00:01 +00:00
|
|
|
c.help = flags.Usage(help, c.flags)
|
2013-12-31 00:09:39 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 15:15:54 +00:00
|
|
|
func (c *cmd) Run(args []string) int {
|
2020-01-28 23:50:41 +00:00
|
|
|
var logCh chan string
|
|
|
|
var err error
|
|
|
|
var client *api.Client
|
|
|
|
|
|
|
|
if err = c.flags.Parse(args); err != nil {
|
2013-12-31 00:09:39 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
client, err = c.http.APIClient()
|
2013-12-31 00:09:39 +00:00
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
|
2013-12-31 00:09:39 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-02-09 22:31:52 +00:00
|
|
|
eventDoneCh := make(chan struct{})
|
2020-01-28 23:50:41 +00:00
|
|
|
if c.logJSON {
|
|
|
|
logCh, err = client.Agent().MonitorJSON(c.logLevel, eventDoneCh, nil)
|
2020-02-11 09:57:50 +00:00
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error starting JSON monitor: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
2021-06-07 16:08:48 +00:00
|
|
|
} else {
|
|
|
|
logCh, err = client.Agent().Monitor(c.logLevel, eventDoneCh, nil)
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error starting monitor: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
2013-12-31 00:09:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(eventDoneCh)
|
|
|
|
OUTER:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case log := <-logCh:
|
|
|
|
if log == "" {
|
|
|
|
break OUTER
|
|
|
|
}
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Info(log)
|
2013-12-31 00:09:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
|
|
|
if !c.quitting {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Info("")
|
|
|
|
c.UI.Output("Remote side ended the monitor! This usually means that the\n" +
|
2013-12-31 00:09:39 +00:00
|
|
|
"remote side has exited or crashed.")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-eventDoneCh:
|
|
|
|
return 1
|
2017-10-13 15:15:54 +00:00
|
|
|
case <-c.shutdownCh:
|
2013-12-31 00:09:39 +00:00
|
|
|
c.lock.Lock()
|
|
|
|
c.quitting = true
|
|
|
|
c.lock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-10-13 15:15:54 +00:00
|
|
|
func (c *cmd) Synopsis() string {
|
2017-10-17 13:44:20 +00:00
|
|
|
return synopsis
|
2013-12-31 00:09:39 +00:00
|
|
|
}
|
2017-10-13 15:15:54 +00:00
|
|
|
|
2017-10-17 13:44:20 +00:00
|
|
|
func (c *cmd) Help() string {
|
|
|
|
return c.help
|
|
|
|
}
|
|
|
|
|
|
|
|
const synopsis = "Stream logs from a Consul agent"
|
|
|
|
const help = `
|
2017-10-13 15:15:54 +00:00
|
|
|
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.
|
|
|
|
`
|