2020-10-06 02:30:01 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
|
|
|
"github.com/posener/complete"
|
|
|
|
)
|
|
|
|
|
|
|
|
type OperatorMetricsCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OperatorMetricsCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: nomad operator metrics [options]
|
|
|
|
|
|
|
|
Get Nomad metrics
|
2020-11-23 13:10:10 +00:00
|
|
|
|
2020-10-06 02:30:01 +00:00
|
|
|
General Options:
|
|
|
|
|
2020-11-19 16:15:23 +00:00
|
|
|
` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + `
|
2020-10-06 02:30:01 +00:00
|
|
|
|
|
|
|
Metrics Specific Options
|
|
|
|
|
|
|
|
-pretty
|
|
|
|
Pretty prints the JSON output
|
|
|
|
|
|
|
|
-format <format>
|
|
|
|
Specify output format (prometheus)
|
2020-10-14 19:16:10 +00:00
|
|
|
|
|
|
|
-json
|
|
|
|
Output the allocation in its JSON format.
|
|
|
|
|
|
|
|
-t
|
|
|
|
Format and display allocation using a Go template.
|
|
|
|
|
2020-10-06 02:30:01 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OperatorMetricsCommand) Synopsis() string {
|
|
|
|
return "Retrieve Nomad metrics"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OperatorMetricsCommand) AutocompleteFlags() complete.Flags {
|
|
|
|
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
|
|
|
|
complete.Flags{
|
|
|
|
"-pretty": complete.PredictAnything,
|
|
|
|
"-format": complete.PredictAnything,
|
2020-10-14 19:16:10 +00:00
|
|
|
"-json": complete.PredictNothing,
|
|
|
|
"-t": complete.PredictAnything,
|
2020-10-06 02:30:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OperatorMetricsCommand) Name() string { return "metrics" }
|
|
|
|
|
|
|
|
func (c *OperatorMetricsCommand) Run(args []string) int {
|
2020-10-14 19:16:10 +00:00
|
|
|
var pretty, json bool
|
|
|
|
var format, tmpl string
|
2020-10-06 02:30:01 +00:00
|
|
|
|
|
|
|
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
|
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
|
|
|
flags.BoolVar(&pretty, "pretty", false, "")
|
|
|
|
flags.StringVar(&format, "format", "", "")
|
2020-10-14 19:16:10 +00:00
|
|
|
flags.BoolVar(&json, "json", false, "")
|
|
|
|
flags.StringVar(&tmpl, "t", "", "")
|
2020-10-06 02:30:01 +00:00
|
|
|
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error parsing flags: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
args = flags.Args()
|
|
|
|
if l := len(args); l != 0 {
|
|
|
|
c.Ui.Error("This command takes no arguments")
|
|
|
|
c.Ui.Error(commandErrorText(c))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := c.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
params := map[string]string{}
|
|
|
|
|
|
|
|
if pretty {
|
|
|
|
params["pretty"] = "1"
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(format) > 0 {
|
|
|
|
params["format"] = format
|
|
|
|
}
|
|
|
|
|
|
|
|
query := &api.QueryOptions{
|
|
|
|
Params: params,
|
|
|
|
}
|
|
|
|
|
2020-10-14 19:16:10 +00:00
|
|
|
if json || len(tmpl) > 0 {
|
|
|
|
metrics, _, err := client.Operator().MetricsSummary(query)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error querying metrics: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := Format(json, tmpl, metrics)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(out)
|
|
|
|
return 0
|
|
|
|
} else {
|
|
|
|
bs, err := client.Operator().Metrics(query)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error getting metrics: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2020-12-08 20:47:04 +00:00
|
|
|
resp := string(bs)
|
2020-10-14 19:16:10 +00:00
|
|
|
c.Ui.Output(resp)
|
2020-10-06 02:30:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|