2018-05-17 22:54:34 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/briandowns/spinner"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
type resultOutput struct {
|
|
|
|
HostIdentifier string `json:"host"`
|
|
|
|
Rows []map[string]string `json:"rows"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryCommand() cli.Command {
|
|
|
|
var (
|
2018-06-05 04:28:49 +00:00
|
|
|
flHosts, flLabels, flQuery string
|
2018-08-16 22:31:18 +00:00
|
|
|
flDebug, flQuiet, flExit bool
|
2019-01-15 19:06:22 +00:00
|
|
|
flTimeout time.Duration
|
2018-05-17 22:54:34 +00:00
|
|
|
)
|
|
|
|
return cli.Command{
|
|
|
|
Name: "query",
|
|
|
|
Usage: "Run a live query",
|
|
|
|
UsageText: `fleetctl query [options]`,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
configFlag(),
|
|
|
|
contextFlag(),
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "hosts",
|
|
|
|
EnvVar: "HOSTS",
|
|
|
|
Value: "",
|
|
|
|
Destination: &flHosts,
|
|
|
|
Usage: "Comma separated hostnames to target",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "labels",
|
|
|
|
EnvVar: "LABELS",
|
|
|
|
Value: "",
|
|
|
|
Destination: &flLabels,
|
|
|
|
Usage: "Comma separated label names to target",
|
|
|
|
},
|
2018-08-16 22:31:18 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "quiet",
|
|
|
|
EnvVar: "QUIET",
|
|
|
|
Destination: &flQuiet,
|
|
|
|
Usage: "Only print results (no status information)",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "exit",
|
|
|
|
EnvVar: "EXIT",
|
|
|
|
Destination: &flExit,
|
2019-01-15 19:06:22 +00:00
|
|
|
Usage: "Exit when 100% of online hosts have results returned",
|
2018-08-16 22:31:18 +00:00
|
|
|
},
|
2018-05-17 22:54:34 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "query",
|
|
|
|
EnvVar: "QUERY",
|
|
|
|
Value: "",
|
|
|
|
Destination: &flQuery,
|
|
|
|
Usage: "Query to run",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "debug",
|
|
|
|
EnvVar: "DEBUG",
|
|
|
|
Destination: &flDebug,
|
|
|
|
Usage: "Whether or not to enable debug logging",
|
|
|
|
},
|
2019-01-15 19:06:22 +00:00
|
|
|
cli.DurationFlag{
|
|
|
|
Name: "timeout",
|
|
|
|
EnvVar: "TIMEOUT",
|
|
|
|
Destination: &flTimeout,
|
|
|
|
Usage: "How long to run query before exiting (10s, 1h, etc.)",
|
|
|
|
},
|
2018-05-17 22:54:34 +00:00
|
|
|
},
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
fleet, err := clientFromCLI(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if flHosts == "" && flLabels == "" {
|
|
|
|
return errors.New("No hosts or labels targeted")
|
|
|
|
}
|
|
|
|
|
|
|
|
if flQuery == "" {
|
|
|
|
return errors.New("No query specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
hosts := strings.Split(flHosts, ",")
|
|
|
|
labels := strings.Split(flLabels, ",")
|
|
|
|
|
|
|
|
res, err := fleet.LiveQuery(flQuery, labels, hosts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tick := time.NewTicker(100 * time.Millisecond)
|
|
|
|
defer tick.Stop()
|
|
|
|
|
|
|
|
// See charsets at
|
|
|
|
// https://godoc.org/github.com/briandowns/spinner#pkg-variables
|
|
|
|
s := spinner.New(spinner.CharSets[24], 200*time.Millisecond)
|
|
|
|
s.Writer = os.Stderr
|
2018-08-16 22:31:18 +00:00
|
|
|
if !flQuiet {
|
|
|
|
s.Start()
|
|
|
|
}
|
2018-05-17 22:54:34 +00:00
|
|
|
|
2019-01-15 19:06:22 +00:00
|
|
|
var timeoutChan <-chan time.Time
|
|
|
|
if flTimeout > 0 {
|
|
|
|
timeoutChan = time.After(flTimeout)
|
|
|
|
} else {
|
2019-03-10 20:51:11 +00:00
|
|
|
// Channel that never fires (so that we can
|
|
|
|
// read from the channel in the below select
|
|
|
|
// statement without panicking)
|
2019-01-15 19:06:22 +00:00
|
|
|
timeoutChan = make(chan time.Time)
|
|
|
|
}
|
|
|
|
|
2018-05-17 22:54:34 +00:00
|
|
|
for {
|
|
|
|
select {
|
2019-01-15 19:06:22 +00:00
|
|
|
// Print a result
|
2018-05-17 22:54:34 +00:00
|
|
|
case hostResult := <-res.Results():
|
|
|
|
out := resultOutput{hostResult.Host.HostName, hostResult.Rows}
|
2018-09-07 22:37:10 +00:00
|
|
|
s.Stop()
|
2018-05-17 22:54:34 +00:00
|
|
|
if err := json.NewEncoder(os.Stdout).Encode(out); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Error writing output: %s\n", err)
|
|
|
|
}
|
2018-09-07 22:37:10 +00:00
|
|
|
s.Start()
|
2018-05-17 22:54:34 +00:00
|
|
|
|
2019-01-15 19:06:22 +00:00
|
|
|
// Print an error
|
2018-05-17 22:54:34 +00:00
|
|
|
case err := <-res.Errors():
|
|
|
|
fmt.Fprintf(os.Stderr, "Error talking to server: %s\n", err.Error())
|
|
|
|
|
2019-01-15 19:06:22 +00:00
|
|
|
// Update status message on interval
|
2018-05-17 22:54:34 +00:00
|
|
|
case <-tick.C:
|
|
|
|
status := res.Status()
|
|
|
|
totals := res.Totals()
|
|
|
|
var percentTotal, percentOnline float64
|
|
|
|
var responded, total, online uint
|
|
|
|
if status != nil && totals != nil {
|
|
|
|
total = totals.Total
|
|
|
|
online = totals.Online
|
|
|
|
responded = status.ActualResults
|
|
|
|
if total > 0 {
|
|
|
|
percentTotal = 100 * float64(responded) / float64(total)
|
|
|
|
}
|
|
|
|
if online > 0 {
|
|
|
|
percentOnline = 100 * float64(responded) / float64(online)
|
|
|
|
}
|
|
|
|
}
|
2018-08-16 22:31:18 +00:00
|
|
|
|
|
|
|
if responded >= online && flExit {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-07 22:37:10 +00:00
|
|
|
msg := fmt.Sprintf(" %.f%% responded (%.f%% online) | %d/%d targeted hosts (%d/%d online)", percentTotal, percentOnline, responded, total, responded, online)
|
2018-08-16 22:31:18 +00:00
|
|
|
if !flQuiet {
|
2018-09-07 22:37:10 +00:00
|
|
|
s.Suffix = msg
|
|
|
|
}
|
|
|
|
if total == responded {
|
|
|
|
s.Stop()
|
|
|
|
if !flQuiet {
|
2019-01-15 19:06:22 +00:00
|
|
|
fmt.Fprintln(os.Stderr, msg)
|
2018-09-07 22:37:10 +00:00
|
|
|
}
|
|
|
|
return nil
|
2018-08-16 22:31:18 +00:00
|
|
|
}
|
2019-01-15 19:06:22 +00:00
|
|
|
|
|
|
|
// Check for timeout expiring
|
|
|
|
case <-timeoutChan:
|
|
|
|
s.Stop()
|
|
|
|
if !flQuiet {
|
|
|
|
fmt.Fprintln(os.Stderr, s.Suffix+"\nStopped by timeout")
|
|
|
|
}
|
|
|
|
return nil
|
2018-05-17 22:54:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|