2019-10-07 20:41:52 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-10-31 13:59:24 +00:00
|
|
|
"io"
|
2019-10-07 20:41:52 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2019-10-24 16:47:46 +00:00
|
|
|
"strconv"
|
2019-10-07 20:41:52 +00:00
|
|
|
"strings"
|
|
|
|
"syscall"
|
2019-10-31 13:59:24 +00:00
|
|
|
"time"
|
2019-10-10 19:30:37 +00:00
|
|
|
|
2019-10-24 16:47:46 +00:00
|
|
|
"github.com/hashicorp/nomad/api"
|
2019-10-10 19:30:37 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2019-10-07 20:41:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MonitorCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *MonitorCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: nomad monitor [options]
|
|
|
|
|
2020-09-14 19:08:30 +00:00
|
|
|
Stream log messages of a nomad agent. The monitor command lets you
|
|
|
|
listen for log levels that may be filtered out of the Nomad agent. For
|
|
|
|
example your agent may only be logging at INFO level, but with the monitor
|
|
|
|
command you can set -log-level DEBUG
|
2019-10-07 20:41:52 +00:00
|
|
|
|
2020-11-19 21:38:08 +00:00
|
|
|
When ACLs are enabled, this command requires a token with the 'agent:read'
|
|
|
|
capability.
|
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
General Options:
|
|
|
|
|
2020-11-19 16:15:23 +00:00
|
|
|
` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + `
|
2019-11-21 21:01:09 +00:00
|
|
|
|
2019-10-25 14:58:54 +00:00
|
|
|
Monitor Specific Options:
|
|
|
|
|
|
|
|
-log-level <level>
|
|
|
|
Sets the log level to monitor (default: INFO)
|
|
|
|
|
|
|
|
-node-id <node-id>
|
|
|
|
Sets the specific node to monitor
|
|
|
|
|
2019-11-21 21:01:09 +00:00
|
|
|
-server-id <server-id>
|
|
|
|
Sets the specific server to monitor
|
|
|
|
|
2019-11-04 19:32:53 +00:00
|
|
|
-json
|
2019-10-25 14:58:54 +00:00
|
|
|
Sets log output to JSON format
|
|
|
|
`
|
2019-10-07 20:41:52 +00:00
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *MonitorCommand) Synopsis() string {
|
2020-01-08 15:44:00 +00:00
|
|
|
return "Stream logs from a Nomad agent"
|
2019-10-07 20:41:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *MonitorCommand) Name() string { return "monitor" }
|
|
|
|
|
|
|
|
func (c *MonitorCommand) Run(args []string) int {
|
2019-10-10 19:30:37 +00:00
|
|
|
c.Ui = &cli.PrefixedUi{
|
|
|
|
OutputPrefix: " ",
|
|
|
|
InfoPrefix: " ",
|
|
|
|
ErrorPrefix: "==> ",
|
|
|
|
Ui: c.Ui,
|
|
|
|
}
|
2019-10-07 20:41:52 +00:00
|
|
|
|
2019-10-10 19:30:37 +00:00
|
|
|
var logLevel string
|
|
|
|
var nodeID string
|
2019-11-12 21:23:39 +00:00
|
|
|
var serverID string
|
2019-10-24 16:47:46 +00:00
|
|
|
var logJSON bool
|
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
|
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
|
|
|
flags.StringVar(&logLevel, "log-level", "", "")
|
2019-10-10 19:30:37 +00:00
|
|
|
flags.StringVar(&nodeID, "node-id", "", "")
|
2019-11-12 21:23:39 +00:00
|
|
|
flags.StringVar(&serverID, "server-id", "", "")
|
2019-11-04 14:04:47 +00:00
|
|
|
flags.BoolVar(&logJSON, "json", false, "")
|
2019-10-07 20:41:52 +00:00
|
|
|
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2019-11-01 15:06:49 +00:00
|
|
|
args = flags.Args()
|
|
|
|
if l := len(args); l != 0 {
|
|
|
|
c.Ui.Error("This command takes no arguments")
|
|
|
|
c.Ui.Error(commandErrorText(c))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
client, err := c.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
|
|
|
|
c.Ui.Error(commandErrorText(c))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2019-11-21 19:51:48 +00:00
|
|
|
// Query the node info and lookup prefix
|
|
|
|
if nodeID != "" {
|
2023-02-07 22:42:25 +00:00
|
|
|
nodeID, err = lookupNodeID(client.Nodes(), nodeID)
|
2019-11-21 19:51:48 +00:00
|
|
|
if err != nil {
|
2023-02-07 22:42:25 +00:00
|
|
|
c.Ui.Error(err.Error())
|
2019-11-21 19:51:48 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 16:47:46 +00:00
|
|
|
params := map[string]string{
|
2019-10-30 13:36:39 +00:00
|
|
|
"log_level": logLevel,
|
|
|
|
"node_id": nodeID,
|
2019-11-12 21:23:39 +00:00
|
|
|
"server_id": serverID,
|
2019-10-30 13:36:39 +00:00
|
|
|
"log_json": strconv.FormatBool(logJSON),
|
2019-10-24 16:47:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
query := &api.QueryOptions{
|
|
|
|
Params: params,
|
|
|
|
}
|
2019-10-31 13:59:24 +00:00
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
eventDoneCh := make(chan struct{})
|
2019-10-31 13:59:24 +00:00
|
|
|
frames, errCh := client.Agent().Monitor(eventDoneCh, query)
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
2019-10-07 20:41:52 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error starting monitor: %s", err))
|
|
|
|
c.Ui.Error(commandErrorText(c))
|
|
|
|
return 1
|
2019-10-31 13:59:24 +00:00
|
|
|
default:
|
2019-10-07 20:41:52 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 13:59:24 +00:00
|
|
|
// Create a reader
|
|
|
|
var r io.ReadCloser
|
|
|
|
frameReader := api.NewFrameReader(frames, errCh, eventDoneCh)
|
|
|
|
frameReader.SetUnblockTime(500 * time.Millisecond)
|
|
|
|
r = frameReader
|
2019-10-07 20:41:52 +00:00
|
|
|
|
2019-10-31 13:59:24 +00:00
|
|
|
defer r.Close()
|
2019-10-07 20:41:52 +00:00
|
|
|
|
|
|
|
signalCh := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
|
|
|
|
|
2019-10-31 13:59:24 +00:00
|
|
|
go func() {
|
|
|
|
<-signalCh
|
|
|
|
// End the streaming
|
|
|
|
r.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
_, err = io.Copy(os.Stdout, r)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("error monitoring logs: %s", err))
|
2019-10-09 15:17:29 +00:00
|
|
|
return 1
|
2019-10-07 20:41:52 +00:00
|
|
|
}
|
2019-10-31 13:59:24 +00:00
|
|
|
|
|
|
|
return 0
|
2019-10-07 20:41:52 +00:00
|
|
|
}
|