Skip to content

Instantly share code, notes, and snippets.

@banks
Created December 5, 2018 21:30
Show Gist options
  • Save banks/7ab62dc82a77bd20f819c9e6dd638ee5 to your computer and use it in GitHub Desktop.
Save banks/7ab62dc82a77bd20f819c9e6dd638ee5 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io"
"os"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/go-msgpack/codec"
)
// snapshotHeader is the first entry in our snapshot
type snapshotHeader struct {
// LastIndex is the last index that affects the data.
// This is used when we do the restore for watchers.
LastIndex uint64
}
func main() {
// msgpackHandle is a shared handle for encoding/decoding msgpack payloads
var msgpackHandle = &codec.MsgpackHandle{
RawToString: true,
}
old := os.Stdin
dec := codec.NewDecoder(old, msgpackHandle)
// Read in the header
var header snapshotHeader
if err := dec.Decode(&header); err != nil {
panic(err)
}
// Populate the new state
msgType := make([]byte, 1)
for {
// Read the message type
_, err := old.Read(msgType)
if err == io.EOF {
break
} else if err != nil {
panic(err)
}
// Decode
msg := structs.MessageType(msgType[0])
fmt.Printf("Message Type %d\n", msg)
var val interface{}
err = dec.Decode(&val)
if err != nil {
panic(err)
}
fmt.Println(val)
}
}
$ # Note message type 15 is connect CA config and is usually near the end.
$ cat /tmp/consul-corrupt/raft/snapshots/3-7136-1544044771131/state.bin | go run dump-snap.go
Message Type 0
map[Check:<nil> Checks:<nil> ID:d101a112-0598-5581-33db-4382a01017c5 Token: SkipNodeUpdate:false TaggedAddresses:map[lan:127.0.0.1 wan:127.0.0.1] Address:127.0.0.1 Datacenter:dc1 Node:Evariste.local NodeMeta:map[consul-network-segment:] Service:<nil>]
Message Type 0
map[Datacenter:dc1 TaggedAddresses:map[lan:127.0.0.1 wan:127.0.0.1] Checks:<nil> Check:<nil> ID:d101a112-0598-5581-33db-4382a01017c5 Node:Evariste.local NodeMeta:map[consul-network-segment:] Service:map[Proxy:map[Config:<nil> DestinationServiceID: DestinationServiceName: LocalServiceAddress: LocalServicePort:0 Upstreams:<nil>] Kind: LocallyRegisteredAsSidecar:false ModifyIndex:2564 ProxyDestination: Tags:<nil> Address: Connect:map[Native:false Proxy:<nil> SidecarService:<nil>] EnableTagOverride:false ID:consul Service:consul Weights:map[Warning:1 Passing:1] CreateIndex:2564 Meta:<nil> Port:8300] SkipNodeUpdate:false Token: Address:127.0.0.1]
...
Message Type 15
map[Provider: ClusterID: Config:<nil> CreateIndex:0 ModifyIndex:0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment