2017-10-17 11:06:40 +00:00
|
|
|
package inspect
|
2016-10-31 23:37:27 +00:00
|
|
|
|
|
|
|
import (
|
2021-04-23 15:17:08 +00:00
|
|
|
"encoding/json"
|
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"
|
2021-04-23 15:17:08 +00:00
|
|
|
"path"
|
2020-10-09 19:57:29 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2016-10-31 23:37:27 +00:00
|
|
|
|
2022-02-14 17:45:45 +00:00
|
|
|
"github.com/hashicorp/consul-net-rpc/go-msgpack/codec"
|
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/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
|
2020-11-10 16:44:09 +00:00
|
|
|
kvDetails bool
|
|
|
|
kvDepth int
|
|
|
|
kvFilter string
|
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-10 16:44:09 +00:00
|
|
|
c.flags.BoolVar(&c.kvDetails, "kvdetails", false,
|
2020-11-10 16:35:24 +00:00
|
|
|
"Provides a detailed KV space usage breakdown for any KV data that's been stored.")
|
2020-11-11 16:46:51 +00:00
|
|
|
c.flags.IntVar(&c.kvDepth, "kvdepth", 2,
|
2020-11-10 16:44:09 +00:00
|
|
|
"Can only be used with -kvdetails. The key prefix depth used to breakdown KV store data. Defaults to 2.")
|
|
|
|
c.flags.StringVar(&c.kvFilter, "kvfilter", "",
|
|
|
|
"Can only be used with -kvdetails. Limits KV key breakdown using this prefix filter.")
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-05 20:26:47 +00:00
|
|
|
// SnapshotInfo is used for passing snapshot stat
|
|
|
|
// information between functions
|
|
|
|
type SnapshotInfo struct {
|
|
|
|
Meta MetadataInfo
|
|
|
|
Stats map[structs.MessageType]typeStats
|
|
|
|
StatsKV map[string]typeStats
|
|
|
|
TotalSize int
|
|
|
|
TotalSizeKV int
|
|
|
|
}
|
|
|
|
|
2020-10-29 16:31:14 +00:00
|
|
|
// OutputFormat is used for passing information
|
|
|
|
// through the formatter
|
|
|
|
type OutputFormat struct {
|
2020-11-05 16:32:23 +00:00
|
|
|
Meta *MetadataInfo
|
|
|
|
Stats []typeStats
|
|
|
|
StatsKV []typeStats
|
|
|
|
TotalSize int
|
|
|
|
TotalSizeKV int
|
2020-10-29 16:31:14 +00:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
2021-04-23 15:17:08 +00:00
|
|
|
var readFile *os.File
|
|
|
|
var meta *raft.SnapshotMeta
|
|
|
|
|
|
|
|
if strings.ToLower(path.Base(file)) == "state.bin" {
|
|
|
|
// This is an internal raw raft snapshot not a gzipped archive one
|
|
|
|
// downloaded from the API, we can read it directly
|
|
|
|
readFile = f
|
|
|
|
|
|
|
|
// Assume the meta is colocated and error if not.
|
2022-11-10 16:26:01 +00:00
|
|
|
metaRaw, err := os.ReadFile(path.Join(path.Dir(file), "meta.json"))
|
2021-04-23 15:17:08 +00:00
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error reading meta.json from internal snapshot dir: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
var metaDecoded raft.SnapshotMeta
|
|
|
|
err = json.Unmarshal(metaRaw, &metaDecoded)
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error parsing meta.json from internal snapshot dir: %s", err))
|
|
|
|
return 1
|
2020-10-09 19:57:29 +00:00
|
|
|
}
|
2021-04-23 15:17:08 +00:00
|
|
|
meta = &metaDecoded
|
|
|
|
} else {
|
|
|
|
readFile, meta, err = snapshot.Read(hclog.New(nil), f)
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error reading snapshot: %s", err))
|
2021-04-23 19:48:10 +00:00
|
|
|
return 1
|
2020-10-09 19:57:29 +00:00
|
|
|
}
|
2021-04-23 15:17:08 +00:00
|
|
|
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-10-09 19:57:29 +00:00
|
|
|
|
2020-11-05 20:26:47 +00:00
|
|
|
info, err := c.enhance(readFile)
|
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-10 16:09:03 +00:00
|
|
|
formattedStats := generateStats(info)
|
|
|
|
formattedStatsKV := generateKVStats(info)
|
2020-10-09 19:57:29 +00:00
|
|
|
|
2020-10-29 16:31:14 +00:00
|
|
|
in := &OutputFormat{
|
2020-11-05 16:32:23 +00:00
|
|
|
Meta: metaformat,
|
|
|
|
Stats: formattedStats,
|
|
|
|
StatsKV: formattedStatsKV,
|
2020-11-05 20:26:47 +00:00
|
|
|
TotalSize: info.TotalSize,
|
|
|
|
TotalSizeKV: info.TotalSizeKV,
|
2020-11-04 00:00:44 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 20:26:47 +00:00
|
|
|
out, err := formatter.Format(in)
|
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-11 17:33:38 +00:00
|
|
|
// generateStats formats the stats for the output struct
|
|
|
|
// that's used to produce the printed output the user sees.
|
2020-11-10 16:09:03 +00:00
|
|
|
func generateStats(info SnapshotInfo) []typeStats {
|
2020-11-05 20:26:47 +00:00
|
|
|
ss := make([]typeStats, 0, len(info.Stats))
|
2020-10-29 16:31:14 +00:00
|
|
|
|
2020-11-05 20:26:47 +00:00
|
|
|
for _, s := range info.Stats {
|
2020-10-29 16:31:14 +00:00
|
|
|
ss = append(ss, s)
|
|
|
|
}
|
|
|
|
|
2020-11-05 22:25:21 +00:00
|
|
|
ss = sortTypeStats(ss)
|
2020-10-29 16:31:14 +00:00
|
|
|
|
2020-11-10 16:09:03 +00:00
|
|
|
return ss
|
|
|
|
}
|
|
|
|
|
2020-11-11 17:33:38 +00:00
|
|
|
// generateKVStats reformats the KV stats to work with
|
|
|
|
// the output struct that's used to produce the printed
|
|
|
|
// output the user sees.
|
2020-11-10 16:09:03 +00:00
|
|
|
func generateKVStats(info SnapshotInfo) []typeStats {
|
2020-11-11 16:44:55 +00:00
|
|
|
kvLen := len(info.StatsKV)
|
|
|
|
if kvLen > 0 {
|
|
|
|
ks := make([]typeStats, 0, kvLen)
|
2020-11-04 00:00:44 +00:00
|
|
|
|
2020-11-05 20:26:47 +00:00
|
|
|
for _, s := range info.StatsKV {
|
2020-11-04 00:00:44 +00:00
|
|
|
ks = append(ks, s)
|
|
|
|
}
|
|
|
|
|
2020-11-05 22:25:21 +00:00
|
|
|
ks = sortTypeStats(ks)
|
2020-11-04 00:00:44 +00:00
|
|
|
|
2020-11-10 16:09:03 +00:00
|
|
|
return ks
|
2020-11-04 00:00:44 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 16:09:03 +00:00
|
|
|
return nil
|
2020-10-29 16:31:14 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 17:33:38 +00:00
|
|
|
// sortTypeStats sorts the stat slice by size and then
|
|
|
|
// alphabetically in the case the size is identical
|
2020-11-05 22:25:21 +00:00
|
|
|
func sortTypeStats(stats []typeStats) []typeStats {
|
|
|
|
sort.Slice(stats, func(i, j int) bool {
|
|
|
|
// sort alphabetically if size is equal
|
|
|
|
if stats[i].Sum == stats[j].Sum {
|
|
|
|
return stats[i].Name < stats[j].Name
|
|
|
|
}
|
|
|
|
|
|
|
|
return stats[i].Sum > stats[j].Sum
|
|
|
|
})
|
|
|
|
|
|
|
|
return stats
|
|
|
|
}
|
|
|
|
|
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-05 20:26:47 +00:00
|
|
|
func (c *cmd) enhance(file io.Reader) (SnapshotInfo, error) {
|
|
|
|
info := SnapshotInfo{
|
|
|
|
Stats: make(map[structs.MessageType]typeStats),
|
|
|
|
StatsKV: make(map[string]typeStats),
|
|
|
|
TotalSize: 0,
|
|
|
|
TotalSizeKV: 0,
|
|
|
|
}
|
2020-10-09 19:57:29 +00:00
|
|
|
cr := &countingReader{wrappedReader: file}
|
|
|
|
handler := func(header *fsm.SnapshotHeader, msg structs.MessageType, dec *codec.Decoder) error {
|
|
|
|
name := structs.MessageType.String(msg)
|
2020-11-05 20:26:47 +00:00
|
|
|
s := info.Stats[msg]
|
2020-10-09 19:57:29 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-11-05 20:26:47 +00:00
|
|
|
size := cr.read - info.TotalSize
|
2020-10-09 19:57:29 +00:00
|
|
|
s.Sum += size
|
|
|
|
s.Count++
|
2020-11-05 20:26:47 +00:00
|
|
|
info.TotalSize = cr.read
|
|
|
|
info.Stats[msg] = s
|
2020-11-04 00:00:44 +00:00
|
|
|
|
2020-11-10 16:29:49 +00:00
|
|
|
c.kvEnhance(s.Name, val, size, &info)
|
2020-11-04 00:00:44 +00:00
|
|
|
|
2020-10-09 19:57:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := fsm.ReadSnapshot(cr, handler); err != nil {
|
2020-11-05 20:26:47 +00:00
|
|
|
return info, err
|
2020-10-09 19:57:29 +00:00
|
|
|
}
|
2020-11-05 20:26:47 +00:00
|
|
|
return info, nil
|
2020-10-09 19:57:29 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-10 22:40:12 +00:00
|
|
|
// kvEnhance populates the struct with all of the snapshot's
|
|
|
|
// size information for KV data stored in it
|
2020-11-10 16:29:49 +00:00
|
|
|
func (c *cmd) kvEnhance(keyType string, val interface{}, size int, info *SnapshotInfo) {
|
2020-11-10 16:44:09 +00:00
|
|
|
if c.kvDetails {
|
2020-11-10 16:29:49 +00:00
|
|
|
if keyType != "KVS" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// have to coerce this into a usable type here or this won't work
|
|
|
|
keyVal := val.(map[string]interface{})
|
|
|
|
for k, v := range keyVal {
|
|
|
|
// we only care about the entry on the key specifically
|
|
|
|
// related to the key name, so skip all others
|
|
|
|
if k != "Key" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for whether a filter is specified. if it is, skip
|
|
|
|
// any keys that don't match.
|
2020-11-10 16:44:09 +00:00
|
|
|
if len(c.kvFilter) > 0 && !strings.HasPrefix(v.(string), c.kvFilter) {
|
2020-11-10 16:29:49 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
split := strings.Split(v.(string), "/")
|
|
|
|
|
|
|
|
// handle the situation where the key is shorter than
|
|
|
|
// the specified depth.
|
2020-11-10 16:44:09 +00:00
|
|
|
actualDepth := c.kvDepth
|
|
|
|
if c.kvDepth > len(split) {
|
2020-11-10 16:29:49 +00:00
|
|
|
actualDepth = len(split)
|
|
|
|
}
|
|
|
|
prefix := strings.Join(split[0:actualDepth], "/")
|
|
|
|
kvs := info.StatsKV[prefix]
|
|
|
|
if kvs.Name == "" {
|
|
|
|
kvs.Name = prefix
|
|
|
|
}
|
|
|
|
|
|
|
|
kvs.Sum += size
|
|
|
|
kvs.Count++
|
|
|
|
info.TotalSizeKV += size
|
|
|
|
info.StatsKV[prefix] = kvs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
`
|