mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 08:55:24 +00:00
33858d7301
# Checklist for submitter If some of the following don't apply, delete the relevant line. - [ ] Changes file added for user-visible changes in `changes/` or `orbit/changes/`. See [Changes files](https://fleetdm.com/docs/contributing/committing-changes#changes-files) for more information. - [ ] Documented any API changes (docs/Using-Fleet/REST-API.md or docs/Contributing/API-for-contributors.md) - [ ] Documented any permissions changes (docs/Using Fleet/manage-access.md) - [ ] Input data is properly validated, `SELECT *` is avoided, SQL injection is prevented (using placeholders for values in statements) - [ ] Added support on fleet's osquery simulator `cmd/osquery-perf` for new osquery data ingestion features. - [ ] Added/updated tests - [ ] Manual QA for all new/changed functionality - For Orbit and Fleet Desktop changes: - [ ] Manual QA must be performed in the three main OSs, macOS, Windows and Linux. - [ ] Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)). Signed-off-by: guoguangwu <guoguangwu@magic-shield.com>
221 lines
5.3 KiB
Go
221 lines
5.3 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/briandowns/spinner"
|
|
"github.com/urfave/cli/v2"
|
|
)
|
|
|
|
func queryCommand() *cli.Command {
|
|
var (
|
|
flHosts, flLabels, flQuery, flQueryName string
|
|
flQuiet, flExit, flPretty bool
|
|
flTimeout time.Duration
|
|
)
|
|
return &cli.Command{
|
|
Name: "query",
|
|
Usage: "Run a live query",
|
|
UsageText: `fleetctl query [options]`,
|
|
Flags: []cli.Flag{
|
|
&cli.StringFlag{
|
|
Name: "hosts",
|
|
EnvVars: []string{"HOSTS"},
|
|
Value: "",
|
|
Destination: &flHosts,
|
|
Usage: "Comma separated hostnames to target",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "labels",
|
|
EnvVars: []string{"LABELS"},
|
|
Value: "",
|
|
Destination: &flLabels,
|
|
Usage: "Comma separated label names to target",
|
|
},
|
|
&cli.BoolFlag{
|
|
Name: "quiet",
|
|
EnvVars: []string{"QUIET"},
|
|
Destination: &flQuiet,
|
|
Usage: "Only print results (no status information)",
|
|
},
|
|
&cli.BoolFlag{
|
|
Name: "exit",
|
|
EnvVars: []string{"EXIT"},
|
|
Destination: &flExit,
|
|
Usage: "Exit when 100% of online hosts have results returned",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "query",
|
|
EnvVars: []string{"QUERY"},
|
|
Value: "",
|
|
Destination: &flQuery,
|
|
Usage: "Query to run",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "query-name",
|
|
EnvVars: []string{"QUERYNAME"},
|
|
Value: "",
|
|
Destination: &flQueryName,
|
|
Usage: "Name of saved query to run",
|
|
},
|
|
&cli.BoolFlag{
|
|
Name: "pretty",
|
|
EnvVars: []string{"PRETTY"},
|
|
Destination: &flPretty,
|
|
Usage: "Enable pretty-printing",
|
|
},
|
|
&cli.DurationFlag{
|
|
Name: "timeout",
|
|
EnvVars: []string{"TIMEOUT"},
|
|
Destination: &flTimeout,
|
|
Usage: "How long to run query before exiting (10s, 1h, etc.)",
|
|
},
|
|
&cli.UintFlag{
|
|
Name: teamFlagName,
|
|
Usage: "ID of the team where the named query belongs to (0 means global)",
|
|
},
|
|
configFlag(),
|
|
contextFlag(),
|
|
debugFlag(),
|
|
},
|
|
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. Please provide either --hosts or --labels.")
|
|
}
|
|
|
|
if flQuery != "" && flQueryName != "" {
|
|
return errors.New("--query and --query-name must not be provided together")
|
|
}
|
|
|
|
if flQueryName != "" {
|
|
var teamID *uint
|
|
if tid := c.Uint(teamFlagName); tid != 0 {
|
|
teamID = &tid
|
|
}
|
|
q, err := fleet.GetQuerySpec(teamID, flQueryName)
|
|
if err != nil {
|
|
return fmt.Errorf("Query '%s' not found", flQueryName)
|
|
}
|
|
flQuery = q.Query
|
|
}
|
|
|
|
if flQuery == "" {
|
|
return errors.New("Query must be specified with --query or --query-name")
|
|
}
|
|
|
|
var output outputWriter
|
|
if flPretty {
|
|
output = newPrettyWriter()
|
|
} else {
|
|
output = newJsonWriter(c.App.Writer)
|
|
}
|
|
|
|
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
|
|
if flQuiet {
|
|
s.Writer = io.Discard
|
|
}
|
|
s.Start()
|
|
|
|
var timeoutChan <-chan time.Time
|
|
if flTimeout > 0 {
|
|
timeoutChan = time.After(flTimeout)
|
|
} else {
|
|
// Channel that never fires (so that we can
|
|
// read from the channel in the below select
|
|
// statement without panicking)
|
|
timeoutChan = make(chan time.Time)
|
|
}
|
|
|
|
for {
|
|
select {
|
|
// Print a result
|
|
case hostResult := <-res.Results():
|
|
s.Stop()
|
|
|
|
if err := output.WriteResult(hostResult); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error writing result: %s\n", err)
|
|
}
|
|
|
|
s.Start()
|
|
|
|
// Print an error
|
|
case err := <-res.Errors():
|
|
fmt.Fprintf(os.Stderr, "Error talking to server: %s\n", err.Error())
|
|
|
|
// Update status message on interval
|
|
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)
|
|
}
|
|
}
|
|
|
|
msg := fmt.Sprintf(" %.f%% responded (%.f%% online) | %d/%d targeted hosts (%d/%d online)", percentTotal, percentOnline, responded, total, responded, online)
|
|
|
|
s.Lock()
|
|
s.Suffix = msg
|
|
s.Unlock()
|
|
|
|
if total == responded && status != nil {
|
|
s.Stop()
|
|
if !flQuiet {
|
|
fmt.Fprintln(os.Stderr, msg)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
if status != nil && totals != nil && responded >= online && flExit {
|
|
s.Stop()
|
|
if !flQuiet {
|
|
fmt.Fprintln(os.Stderr, msg)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Check for timeout expiring
|
|
case <-timeoutChan:
|
|
s.Stop()
|
|
if !flQuiet {
|
|
fmt.Fprintln(os.Stderr, s.Suffix+"\nStopped by timeout")
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
},
|
|
}
|
|
}
|