2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
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
|
2023-10-20 07:35:54 +00:00
|
|
|
|
|
|
|
// Below this point is where CLI flag options are stored.
|
|
|
|
logLevel string
|
|
|
|
nodeID string
|
|
|
|
serverID string
|
|
|
|
logJSON bool
|
|
|
|
logIncludeLocation bool
|
2019-10-07 20:41:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2023-10-20 07:35:54 +00:00
|
|
|
-log-include-location
|
|
|
|
Include file and line information in each log line. The default is false.
|
|
|
|
|
2019-10-25 14:58:54 +00:00
|
|
|
-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
|
|
|
|
|
|
|
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
|
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
2023-10-20 07:35:54 +00:00
|
|
|
flags.StringVar(&c.logLevel, "log-level", "", "")
|
|
|
|
flags.BoolVar(&c.logIncludeLocation, "log-include-location", false, "")
|
|
|
|
flags.StringVar(&c.nodeID, "node-id", "", "")
|
|
|
|
flags.StringVar(&c.serverID, "server-id", "", "")
|
|
|
|
flags.BoolVar(&c.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
|
2023-10-20 07:35:54 +00:00
|
|
|
if c.nodeID != "" {
|
|
|
|
c.nodeID, err = lookupNodeID(client.Nodes(), c.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{
|
2023-10-20 07:35:54 +00:00
|
|
|
"log_level": c.logLevel,
|
|
|
|
"node_id": c.nodeID,
|
|
|
|
"server_id": c.serverID,
|
|
|
|
"log_json": strconv.FormatBool(c.logJSON),
|
|
|
|
"log_include_location": strconv.FormatBool(c.logIncludeLocation),
|
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
|
|
|
}
|