-
Notifications
You must be signed in to change notification settings - Fork 394
/
cmd_diag.go
103 lines (85 loc) · 2.38 KB
/
cmd_diag.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
100
101
102
103
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"fmt"
"os"
"path/filepath"
"sort"
"text/tabwriter"
"time"
"github.com/spf13/cobra"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/common/cfgstruct"
"storj.io/common/memory"
"storj.io/common/process"
"storj.io/common/storj"
"storj.io/storj/storagenode"
"storj.io/storj/storagenode/storagenodedb"
)
type diagCfg struct {
storagenode.Config
DiagDir string `internal:"true"`
}
func newDiagCmd(f *Factory) *cobra.Command {
var diagCfg diagCfg
cmd := &cobra.Command{
Use: "diag",
Short: "Diagnostic Tool support",
RunE: func(cmd *cobra.Command, args []string) error {
diagDir, err := filepath.Abs(f.ConfDir)
if err != nil {
return err
}
diagCfg.DiagDir = diagDir
return cmdDiag(cmd, &diagCfg)
},
Annotations: map[string]string{"type": "helper"},
}
process.Bind(cmd, &diagCfg, f.Defaults, cfgstruct.ConfDir(f.ConfDir), cfgstruct.IdentityDir(f.IdentityDir))
return cmd
}
func cmdDiag(cmd *cobra.Command, cfg *diagCfg) (err error) {
ctx, _ := process.Ctx(cmd)
// check if the directory exists
_, err = os.Stat(cfg.DiagDir)
if err != nil {
fmt.Println("storage node directory doesn't exist", cfg.DiagDir)
return err
}
db, err := storagenodedb.OpenExisting(ctx, zap.L().Named("db"), cfg.DatabaseConfig())
if err != nil {
return errs.New("Error starting master database on storage node: %v", err)
}
defer func() {
err = errs.Combine(err, db.Close())
}()
summaries, err := db.Bandwidth().SummaryBySatellite(ctx, time.Time{}, time.Now())
if err != nil {
fmt.Printf("unable to get bandwidth summary: %v\n", err)
return err
}
satellites := storj.NodeIDList{}
for id := range summaries {
satellites = append(satellites, id)
}
sort.Sort(satellites)
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', tabwriter.AlignRight|tabwriter.Debug)
defer func() { err = errs.Combine(err, w.Flush()) }()
fmt.Fprint(w, "Satellite\tTotal\tPut\tGet\tDelete\tAudit Get\tRepair Get\tRepair Put\n")
for _, id := range satellites {
summary := summaries[id]
fmt.Fprintf(w, "%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\n",
id,
memory.Size(summary.Total()),
memory.Size(summary.Put),
memory.Size(summary.Get),
memory.Size(summary.Delete),
memory.Size(summary.GetAudit),
memory.Size(summary.GetRepair),
memory.Size(summary.PutRepair),
)
}
return nil
}