forked from coreos/torus
/
list-peers.go
99 lines (91 loc) · 2.14 KB
/
list-peers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"fmt"
"os"
"time"
"github.com/dustin/go-humanize"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
var outputAsCSV bool
var listPeersCommand = &cobra.Command{
Use: "list-peers",
Short: "show the active storage peers in the cluster",
Run: listPeersAction,
}
func init() {
listPeersCommand.Flags().BoolVarP(&outputAsCSV, "csv", "", false, "output as csv instead")
}
func listPeersAction(cmd *cobra.Command, args []string) {
var totalStorage uint64
var usedStorage uint64
mds := mustConnectToMDS()
gmd, err := mds.GlobalMetadata()
if err != nil {
die("couldn't get global metadata: %v", err)
}
peers, err := mds.GetPeers()
if err != nil {
die("couldn't get peers: %v", err)
}
ring, err := mds.GetRing()
if err != nil {
die("couldn't get ring: %v", err)
}
members := ring.Members()
table := tablewriter.NewWriter(os.Stdout)
if outputAsCSV {
table.SetBorder(false)
table.SetColumnSeparator(",")
} else {
table.SetHeader([]string{"Address", "UUID", "Size", "Used", "Member", "Updated", "Reb/Rep Data"})
}
rebalancing := false
for _, x := range peers {
ringStatus := "Avail"
if x.Address == "" {
continue
}
if members.Has(x.UUID) {
ringStatus = "OK"
}
table.Append([]string{
x.Address,
x.UUID,
humanize.IBytes(x.TotalBlocks * gmd.BlockSize),
humanize.IBytes(x.UsedBlocks * gmd.BlockSize),
ringStatus,
humanize.Time(time.Unix(0, x.LastSeen)),
humanize.IBytes(x.RebalanceInfo.LastRebalanceBlocks*gmd.BlockSize*uint64(time.Second)/uint64(x.LastSeen+1-x.RebalanceInfo.LastRebalanceFinish)) + "/sec",
})
if x.RebalanceInfo.Rebalancing {
rebalancing = true
}
totalStorage += x.TotalBlocks * gmd.BlockSize
usedStorage += x.UsedBlocks * gmd.BlockSize
}
for _, x := range members {
ringStatus := "DOWN"
ok := false
for _, p := range peers {
if p.UUID == x {
ok = true
break
}
}
if ok {
continue
}
table.Append([]string{
"",
x,
"???",
"???",
ringStatus,
"Missing",
"",
})
}
table.Render()
fmt.Printf("Balanced: %v Usage: %5.2f%%\n", !rebalancing, (float64(usedStorage) / float64(totalStorage) * 100.0))
}