2017-10-17 11:06:40 +00:00
|
|
|
package inspect
|
2016-10-31 23:37:27 +00:00
|
|
|
|
|
|
|
import (
|
2017-10-17 07:03:41 +00:00
|
|
|
"flag"
|
2016-10-31 23:37:27 +00:00
|
|
|
"fmt"
|
2020-10-09 19:57:29 +00:00
|
|
|
"io"
|
2016-10-31 23:37:27 +00:00
|
|
|
"os"
|
2020-10-09 19:57:29 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2016-10-31 23:37:27 +00:00
|
|
|
|
2020-10-09 19:57:29 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/fsm"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2017-10-17 07:03:41 +00:00
|
|
|
"github.com/hashicorp/consul/command/flags"
|
2016-11-04 04:36:25 +00:00
|
|
|
"github.com/hashicorp/consul/snapshot"
|
2020-10-09 19:57:29 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/go-msgpack/codec"
|
|
|
|
"github.com/hashicorp/raft"
|
2017-10-17 07:03:41 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2016-10-31 23:37:27 +00:00
|
|
|
)
|
|
|
|
|
2017-10-17 07:03:41 +00:00
|
|
|
func New(ui cli.Ui) *cmd {
|
|
|
|
c := &cmd{UI: ui}
|
|
|
|
c.init()
|
|
|
|
return c
|
2016-10-31 23:37:27 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 07:03:41 +00:00
|
|
|
type cmd struct {
|
2020-10-29 16:31:14 +00:00
|
|
|
UI cli.Ui
|
|
|
|
flags *flag.FlagSet
|
|
|
|
help string
|
|
|
|
format string
|
2020-11-04 00:00:44 +00:00
|
|
|
|
|
|
|
// flags
|
|
|
|
detailed bool
|
|
|
|
kvDepth int
|
2017-10-17 07:03:41 +00:00
|
|
|
}
|
2016-10-31 23:37:27 +00:00
|
|
|
|
2017-10-17 07:03:41 +00:00
|
|
|
func (c *cmd) init() {
|
|
|
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
2020-11-04 00:00:44 +00:00
|
|
|
c.flags.BoolVar(&c.detailed, "detailed", false,
|
|
|
|
"Provides detailed information about KV store data.")
|
|
|
|
c.flags.IntVar(&c.kvDepth, "kv-depth", 2,
|
|
|
|
"The key prefix depth used to breakdown KV store data. Defaults to 2.")
|
2020-10-29 16:31:14 +00:00
|
|
|
c.flags.StringVar(
|
|
|
|
&c.format,
|
|
|
|
"format",
|
|
|
|
PrettyFormat,
|
|
|
|
fmt.Sprintf("Output format {%s}", strings.Join(GetSupportedFormats(), "|")))
|
|
|
|
|
2017-10-17 22:00:01 +00:00
|
|
|
c.help = flags.Usage(help, c.flags)
|
2016-10-31 23:37:27 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 16:31:14 +00:00
|
|
|
// MetadataInfo is used for passing information
|
|
|
|
// through the formatter
|
|
|
|
type MetadataInfo struct {
|
|
|
|
ID string
|
|
|
|
Size int64
|
|
|
|
Index uint64
|
|
|
|
Term uint64
|
|
|
|
Version raft.SnapshotVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
// OutputFormat is used for passing information
|
|
|
|
// through the formatter
|
|
|
|
type OutputFormat struct {
|
|
|
|
Meta *MetadataInfo
|
|
|
|
Stats []typeStats
|
2020-11-04 00:00:44 +00:00
|
|
|
KStats []typeStats
|
2020-10-29 16:31:14 +00:00
|
|
|
TotalSize int
|
|
|
|
}
|
|
|
|
|
2017-10-17 07:03:41 +00:00
|
|
|
func (c *cmd) Run(args []string) int {
|
|
|
|
if err := c.flags.Parse(args); err != nil {
|
2020-10-09 19:57:29 +00:00
|
|
|
c.UI.Error(err.Error())
|
2016-10-31 23:37:27 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
var file string
|
2017-10-17 07:03:41 +00:00
|
|
|
args = c.flags.Args()
|
2020-10-09 19:57:29 +00:00
|
|
|
|
2016-10-31 23:37:27 +00:00
|
|
|
switch len(args) {
|
|
|
|
case 0:
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error("Missing FILE argument")
|
2016-10-31 23:37:27 +00:00
|
|
|
return 1
|
|
|
|
case 1:
|
|
|
|
file = args[0]
|
|
|
|
default:
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Too many arguments (expected 1, got %d)", len(args)))
|
2016-10-31 23:37:27 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the file.
|
|
|
|
f, err := os.Open(file)
|
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error opening snapshot file: %s", err))
|
2016-10-31 23:37:27 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2020-10-09 19:57:29 +00:00
|
|
|
readFile, meta, err := snapshot.Read(hclog.New(nil), f)
|
2016-10-31 23:37:27 +00:00
|
|
|
if err != nil {
|
2020-10-09 19:57:29 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error reading snapshot: %s", err))
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := readFile.Close(); err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Failed to close temp snapshot: %v", err))
|
|
|
|
}
|
|
|
|
if err := os.Remove(readFile.Name()); err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Failed to clean up temp snapshot: %v", err))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-11-04 00:00:44 +00:00
|
|
|
stats, kstats, totalSize, err := enhance(readFile, c.detailed, c.kvDepth)
|
2020-10-09 19:57:29 +00:00
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error extracting snapshot data: %s", err))
|
2017-10-30 21:51:08 +00:00
|
|
|
return 1
|
2016-10-31 23:37:27 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 16:31:14 +00:00
|
|
|
formatter, err := NewFormatter(c.format)
|
2020-10-09 19:57:29 +00:00
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error outputting enhanced snapshot data: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
2020-10-29 16:31:14 +00:00
|
|
|
//Generate structs for the formatter with information we read in
|
|
|
|
metaformat := &MetadataInfo{
|
|
|
|
ID: meta.ID,
|
|
|
|
Size: meta.Size,
|
|
|
|
Index: meta.Index,
|
|
|
|
Term: meta.Term,
|
|
|
|
Version: meta.Version,
|
|
|
|
}
|
2020-10-09 19:57:29 +00:00
|
|
|
|
2020-10-29 16:31:14 +00:00
|
|
|
//Restructures stats given above to be human readable
|
2020-11-04 00:00:44 +00:00
|
|
|
formattedStats, formattedKStats := generatetypeStats(stats, kstats, c.detailed)
|
2020-10-09 19:57:29 +00:00
|
|
|
|
2020-10-29 16:31:14 +00:00
|
|
|
in := &OutputFormat{
|
|
|
|
Meta: metaformat,
|
|
|
|
Stats: formattedStats,
|
|
|
|
TotalSize: totalSize,
|
2020-10-09 19:57:29 +00:00
|
|
|
}
|
2020-11-04 00:00:44 +00:00
|
|
|
inKV := &OutputFormat{
|
|
|
|
Meta: metaformat,
|
|
|
|
Stats: formattedKStats,
|
|
|
|
TotalSize: totalSize,
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := formatter.Format(in, inKV, c.detailed)
|
2020-10-29 16:31:14 +00:00
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
c.UI.Output(out)
|
|
|
|
return 0
|
2020-10-09 19:57:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type typeStats struct {
|
|
|
|
Name string
|
|
|
|
Sum int
|
|
|
|
Count int
|
|
|
|
}
|
|
|
|
|
2020-11-04 00:00:44 +00:00
|
|
|
func generatetypeStats(info map[structs.MessageType]typeStats, kvInfo map[string]typeStats, detailed bool) ([]typeStats, []typeStats) {
|
2020-10-29 16:31:14 +00:00
|
|
|
ss := make([]typeStats, 0, len(info))
|
|
|
|
|
|
|
|
for _, s := range info {
|
|
|
|
ss = append(ss, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the stat slice
|
|
|
|
sort.Slice(ss, func(i, j int) bool { return ss[i].Sum > ss[j].Sum })
|
|
|
|
|
2020-11-04 00:00:44 +00:00
|
|
|
if detailed {
|
|
|
|
ks := make([]typeStats, 0, len(kvInfo))
|
|
|
|
|
|
|
|
for _, s := range kvInfo {
|
|
|
|
ks = append(ks, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the kv stat slice
|
|
|
|
sort.Slice(ks, func(i, j int) bool { return ks[i].Sum > ks[j].Sum })
|
|
|
|
|
|
|
|
return ss, ks
|
|
|
|
}
|
|
|
|
|
|
|
|
return ss, nil
|
2020-10-29 16:31:14 +00:00
|
|
|
}
|
|
|
|
|
2020-10-09 19:57:29 +00:00
|
|
|
// countingReader helps keep track of the bytes we have read
|
|
|
|
// when reading snapshots
|
|
|
|
type countingReader struct {
|
|
|
|
wrappedReader io.Reader
|
|
|
|
read int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *countingReader) Read(p []byte) (n int, err error) {
|
|
|
|
n, err = r.wrappedReader.Read(p)
|
|
|
|
if err == nil {
|
|
|
|
r.read += n
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// enhance utilizes ReadSnapshot to populate the struct with
|
|
|
|
// all of the snapshot's itemized data
|
2020-11-04 00:00:44 +00:00
|
|
|
func enhance(file io.Reader, detailed bool, kvDepth int) (map[structs.MessageType]typeStats, map[string]typeStats, int, error) {
|
2020-10-09 19:57:29 +00:00
|
|
|
stats := make(map[structs.MessageType]typeStats)
|
2020-11-04 00:00:44 +00:00
|
|
|
kstats := make(map[string]typeStats)
|
2020-10-09 19:57:29 +00:00
|
|
|
cr := &countingReader{wrappedReader: file}
|
|
|
|
totalSize := 0
|
|
|
|
handler := func(header *fsm.SnapshotHeader, msg structs.MessageType, dec *codec.Decoder) error {
|
|
|
|
name := structs.MessageType.String(msg)
|
|
|
|
s := stats[msg]
|
|
|
|
if s.Name == "" {
|
|
|
|
s.Name = name
|
|
|
|
}
|
2020-11-04 00:00:44 +00:00
|
|
|
|
2020-10-09 19:57:29 +00:00
|
|
|
var val interface{}
|
|
|
|
err := dec.Decode(&val)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to decode msg type %v, error %v", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
size := cr.read - totalSize
|
|
|
|
s.Sum += size
|
|
|
|
s.Count++
|
|
|
|
totalSize = cr.read
|
|
|
|
stats[msg] = s
|
2020-11-04 00:00:44 +00:00
|
|
|
|
|
|
|
if detailed {
|
|
|
|
if s.Name == "KVS" {
|
|
|
|
switch val := val.(type) {
|
|
|
|
case map[string]interface{}:
|
|
|
|
fmt.Println("map-match")
|
|
|
|
for k, v := range val {
|
|
|
|
depth := kvDepth
|
|
|
|
if k == "Key" {
|
|
|
|
split := strings.Split(v.(string), "/")
|
|
|
|
if depth > len(split) {
|
|
|
|
depth = len(split)
|
|
|
|
}
|
|
|
|
prefix := strings.Join(split[0:depth], "/")
|
|
|
|
kvs := kstats[prefix]
|
|
|
|
if kvs.Name == "" {
|
|
|
|
kvs.Name = prefix
|
|
|
|
}
|
|
|
|
kvs.Sum += size
|
|
|
|
kvs.Count++
|
|
|
|
kstats[prefix] = kvs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:57:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := fsm.ReadSnapshot(cr, handler); err != nil {
|
2020-11-04 00:00:44 +00:00
|
|
|
return nil, nil, 0, err
|
2020-10-09 19:57:29 +00:00
|
|
|
}
|
2020-11-04 00:00:44 +00:00
|
|
|
return stats, kstats, totalSize, nil
|
2020-10-09 19:57:29 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-17 13:44:20 +00:00
|
|
|
func (c *cmd) Synopsis() string {
|
|
|
|
return synopsis
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Help() string {
|
|
|
|
return c.help
|
|
|
|
}
|
|
|
|
|
|
|
|
const synopsis = "Displays information about a Consul snapshot file"
|
|
|
|
const help = `
|
|
|
|
Usage: consul snapshot inspect [options] FILE
|
2017-10-17 07:03:41 +00:00
|
|
|
|
|
|
|
Displays information about a snapshot file on disk.
|
|
|
|
|
|
|
|
To inspect the file "backup.snap":
|
|
|
|
|
|
|
|
$ consul snapshot inspect backup.snap
|
2020-10-09 19:57:29 +00:00
|
|
|
|
2017-10-17 13:44:20 +00:00
|
|
|
For a full list of options and examples, please see the Consul documentation.
|
|
|
|
`
|