2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-21 17:31:27 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-11-21 22:01:09 +00:00
|
|
|
"bufio"
|
2018-11-21 17:31:27 +00:00
|
|
|
"context"
|
2018-11-21 22:01:09 +00:00
|
|
|
"encoding/csv"
|
2019-03-15 20:21:52 +00:00
|
|
|
"encoding/json"
|
2018-11-21 17:31:27 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2018-11-21 22:01:09 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
2019-03-15 20:21:52 +00:00
|
|
|
"strings"
|
2018-11-21 17:31:27 +00:00
|
|
|
|
2018-11-30 17:36:05 +00:00
|
|
|
"github.com/gogo/protobuf/jsonpb"
|
2019-01-16 20:40:30 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2019-03-15 20:21:52 +00:00
|
|
|
prompt "github.com/segmentio/go-prompt"
|
2018-11-21 17:31:27 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2019-01-18 15:02:35 +00:00
|
|
|
"storj.io/storj/pkg/identity"
|
2018-11-21 17:31:27 +00:00
|
|
|
"storj.io/storj/pkg/pb"
|
|
|
|
"storj.io/storj/pkg/process"
|
2018-11-30 13:40:13 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-11-21 17:31:27 +00:00
|
|
|
"storj.io/storj/pkg/transport"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-01-17 09:35:22 +00:00
|
|
|
// Addr is the address of peer from command flags
|
2019-03-07 18:19:37 +00:00
|
|
|
Addr = flag.String("address", "127.0.0.1:7778", "address of peer to inspect")
|
2018-11-21 17:31:27 +00:00
|
|
|
|
2019-01-18 15:02:35 +00:00
|
|
|
// IdentityPath is the path to the identity the inspector should use for network communication
|
|
|
|
IdentityPath = flag.String("identity-path", "", "path to the identity certificate for use on the network")
|
|
|
|
|
2018-11-21 17:31:27 +00:00
|
|
|
// ErrInspectorDial throws when there are errors dialing the inspector server
|
|
|
|
ErrInspectorDial = errs.Class("error dialing inspector server:")
|
|
|
|
|
|
|
|
// ErrRequest is for gRPC request errors after dialing
|
|
|
|
ErrRequest = errs.Class("error processing request:")
|
|
|
|
|
|
|
|
// ErrIdentity is for errors during identity creation for this CLI
|
|
|
|
ErrIdentity = errs.Class("error creating identity:")
|
|
|
|
|
2018-11-21 22:01:09 +00:00
|
|
|
// ErrArgs throws when there are errors with CLI args
|
|
|
|
ErrArgs = errs.Class("error with CLI args:")
|
|
|
|
|
2019-03-15 20:21:52 +00:00
|
|
|
irreparableLimit int32
|
|
|
|
|
2018-11-21 17:31:27 +00:00
|
|
|
// Commander CLI
|
|
|
|
rootCmd = &cobra.Command{
|
|
|
|
Use: "inspector",
|
|
|
|
Short: "CLI for interacting with Storj Kademlia network",
|
|
|
|
}
|
2018-11-21 22:01:09 +00:00
|
|
|
kadCmd = &cobra.Command{
|
|
|
|
Use: "kad",
|
|
|
|
Short: "commands for kademlia/overlay cache",
|
|
|
|
}
|
|
|
|
statsCmd = &cobra.Command{
|
|
|
|
Use: "statdb",
|
|
|
|
Short: "commands for statdb",
|
|
|
|
}
|
2019-04-02 15:55:58 +01:00
|
|
|
healthCmd = &cobra.Command{
|
|
|
|
Use: "health",
|
|
|
|
Short: "commands for querying health of a stored data",
|
|
|
|
}
|
2019-03-15 20:21:52 +00:00
|
|
|
irreparableCmd = &cobra.Command{
|
|
|
|
Use: "irreparable",
|
|
|
|
Short: "list segments in irreparable database",
|
|
|
|
RunE: getSegments,
|
|
|
|
}
|
2018-11-21 17:31:27 +00:00
|
|
|
countNodeCmd = &cobra.Command{
|
|
|
|
Use: "count",
|
|
|
|
Short: "count nodes in kademlia and overlay",
|
|
|
|
RunE: CountNodes,
|
|
|
|
}
|
2018-11-27 22:50:52 +00:00
|
|
|
pingNodeCmd = &cobra.Command{
|
2018-11-30 17:36:05 +00:00
|
|
|
Use: "ping <node_id> <ip:port>",
|
2018-11-27 22:50:52 +00:00
|
|
|
Short: "ping node at provided ID",
|
2018-11-30 17:36:05 +00:00
|
|
|
Args: cobra.MinimumNArgs(2),
|
2018-11-27 22:50:52 +00:00
|
|
|
RunE: PingNode,
|
|
|
|
}
|
2018-12-04 21:39:28 +00:00
|
|
|
lookupNodeCmd = &cobra.Command{
|
|
|
|
Use: "lookup <node_id>",
|
|
|
|
Short: "lookup a node by ID only",
|
|
|
|
Args: cobra.MinimumNArgs(1),
|
|
|
|
RunE: LookupNode,
|
|
|
|
}
|
2019-02-25 18:41:51 +00:00
|
|
|
nodeInfoCmd = &cobra.Command{
|
|
|
|
Use: "node-info <node_id>",
|
|
|
|
Short: "get node info directly from node",
|
|
|
|
Args: cobra.MinimumNArgs(1),
|
|
|
|
RunE: NodeInfo,
|
|
|
|
}
|
2018-12-07 18:26:33 +00:00
|
|
|
dumpNodesCmd = &cobra.Command{
|
|
|
|
Use: "dump-nodes",
|
|
|
|
Short: "dump all nodes in the routing table",
|
|
|
|
RunE: DumpNodes,
|
|
|
|
}
|
2018-11-21 22:01:09 +00:00
|
|
|
getStatsCmd = &cobra.Command{
|
2018-11-30 17:36:05 +00:00
|
|
|
Use: "getstats <node_id>",
|
2018-11-21 22:01:09 +00:00
|
|
|
Short: "Get node stats",
|
|
|
|
Args: cobra.MinimumNArgs(1),
|
|
|
|
RunE: GetStats,
|
|
|
|
}
|
|
|
|
getCSVStatsCmd = &cobra.Command{
|
2018-11-30 17:36:05 +00:00
|
|
|
Use: "getcsvstats <path to node ID csv file>",
|
2018-11-21 22:01:09 +00:00
|
|
|
Short: "Get node stats from csv",
|
|
|
|
Args: cobra.MinimumNArgs(1),
|
|
|
|
RunE: GetCSVStats,
|
|
|
|
}
|
|
|
|
createStatsCmd = &cobra.Command{
|
2018-11-30 17:36:05 +00:00
|
|
|
// TODO: add args to usage
|
2018-11-21 22:01:09 +00:00
|
|
|
Use: "createstats",
|
|
|
|
Short: "Create node with stats",
|
|
|
|
Args: cobra.MinimumNArgs(5), // id, auditct, auditsuccessct, uptimect, uptimesuccessct
|
|
|
|
RunE: CreateStats,
|
|
|
|
}
|
|
|
|
createCSVStatsCmd = &cobra.Command{
|
2018-11-30 17:36:05 +00:00
|
|
|
// TODO: add args to usage
|
2018-11-21 22:01:09 +00:00
|
|
|
Use: "createcsvstats",
|
|
|
|
Short: "Create node stats from csv",
|
|
|
|
Args: cobra.MinimumNArgs(1),
|
|
|
|
RunE: CreateCSVStats,
|
|
|
|
}
|
2019-04-02 15:55:58 +01:00
|
|
|
objectHealthCmd = &cobra.Command{
|
|
|
|
Use: "object <project-id> <bucket> <encrypted-path>",
|
|
|
|
Short: "Get stats about an object's health",
|
|
|
|
Args: cobra.MinimumNArgs(3),
|
|
|
|
RunE: ObjectHealth,
|
|
|
|
}
|
|
|
|
segmentHealthCmd = &cobra.Command{
|
|
|
|
Use: "segment <project-id> <segment-index> <bucket> <encrypted-path>",
|
|
|
|
Short: "Get stats about a segment's health",
|
|
|
|
Args: cobra.MinimumNArgs(4),
|
|
|
|
RunE: SegmentHealth,
|
|
|
|
}
|
2018-11-21 17:31:27 +00:00
|
|
|
)
|
|
|
|
|
2019-03-25 22:25:09 +00:00
|
|
|
// Inspector gives access to kademlia, overlay cache
|
2018-11-21 17:31:27 +00:00
|
|
|
type Inspector struct {
|
2019-01-30 20:47:21 +00:00
|
|
|
identity *identity.FullIdentity
|
2019-01-14 18:47:22 +00:00
|
|
|
kadclient pb.KadInspectorClient
|
|
|
|
overlayclient pb.OverlayInspectorClient
|
2019-03-15 20:21:52 +00:00
|
|
|
irrdbclient pb.IrreparableInspectorClient
|
2019-04-02 15:55:58 +01:00
|
|
|
healthclient pb.HealthInspectorClient
|
2018-11-21 17:31:27 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 18:19:37 +00:00
|
|
|
// NewInspector creates a new gRPC inspector client for access to kad,
|
2019-03-25 22:25:09 +00:00
|
|
|
// overlay cache
|
2019-01-18 15:02:35 +00:00
|
|
|
func NewInspector(address, path string) (*Inspector, error) {
|
2018-11-21 17:31:27 +00:00
|
|
|
ctx := context.Background()
|
2019-01-28 22:53:37 +00:00
|
|
|
|
2019-01-25 17:46:09 +00:00
|
|
|
id, err := identity.Config{
|
2019-01-28 22:53:37 +00:00
|
|
|
CertPath: fmt.Sprintf("%s/identity.cert", path),
|
|
|
|
KeyPath: fmt.Sprintf("%s/identity.key", path),
|
2019-01-18 15:02:35 +00:00
|
|
|
}.Load()
|
2019-02-11 11:17:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, ErrIdentity.Wrap(err)
|
|
|
|
}
|
2019-01-18 15:02:35 +00:00
|
|
|
|
2019-03-07 18:19:37 +00:00
|
|
|
conn, err := transport.DialAddressInsecure(ctx, address)
|
2018-11-21 17:31:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return &Inspector{}, ErrInspectorDial.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Inspector{
|
2019-01-25 17:46:09 +00:00
|
|
|
identity: id,
|
2019-01-14 18:47:22 +00:00
|
|
|
kadclient: pb.NewKadInspectorClient(conn),
|
|
|
|
overlayclient: pb.NewOverlayInspectorClient(conn),
|
2019-03-15 20:21:52 +00:00
|
|
|
irrdbclient: pb.NewIrreparableInspectorClient(conn),
|
2019-04-02 15:55:58 +01:00
|
|
|
healthclient: pb.NewHealthInspectorClient(conn),
|
2018-11-21 17:31:27 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-01-16 19:44:26 +00:00
|
|
|
// CountNodes returns the number of nodes in kademlia
|
2018-11-21 17:31:27 +00:00
|
|
|
func CountNodes(cmd *cobra.Command, args []string) (err error) {
|
2019-01-18 15:02:35 +00:00
|
|
|
i, err := NewInspector(*Addr, *IdentityPath)
|
2018-11-21 17:31:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return ErrInspectorDial.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-01-14 18:47:22 +00:00
|
|
|
kadcount, err := i.kadclient.CountNodes(context.Background(), &pb.CountNodesRequest{})
|
|
|
|
if err != nil {
|
|
|
|
return ErrRequest.Wrap(err)
|
|
|
|
}
|
2018-11-21 17:31:27 +00:00
|
|
|
|
2019-01-16 19:44:26 +00:00
|
|
|
fmt.Printf("Kademlia node count: %+v\n", kadcount.Count)
|
2018-11-21 17:31:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-04 21:39:28 +00:00
|
|
|
// LookupNode starts a Kademlia lookup for the provided Node ID
|
|
|
|
func LookupNode(cmd *cobra.Command, args []string) (err error) {
|
2019-01-18 15:02:35 +00:00
|
|
|
i, err := NewInspector(*Addr, *IdentityPath)
|
2018-12-04 21:39:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return ErrInspectorDial.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-01-14 18:47:22 +00:00
|
|
|
n, err := i.kadclient.LookupNode(context.Background(), &pb.LookupNodeRequest{
|
2018-12-04 21:39:28 +00:00
|
|
|
Id: args[0],
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ErrRequest.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-01-16 20:40:30 +00:00
|
|
|
fmt.Println(prettyPrint(n))
|
2018-12-04 21:39:28 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-25 18:41:51 +00:00
|
|
|
// NodeInfo get node info directly from the node with provided Node ID
|
|
|
|
func NodeInfo(cmd *cobra.Command, args []string) (err error) {
|
|
|
|
i, err := NewInspector(*Addr, *IdentityPath)
|
|
|
|
if err != nil {
|
|
|
|
return ErrInspectorDial.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// first lookup the node to get its address
|
|
|
|
n, err := i.kadclient.LookupNode(context.Background(), &pb.LookupNodeRequest{
|
|
|
|
Id: args[0],
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return ErrRequest.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// now ask the node directly for its node info
|
|
|
|
info, err := i.kadclient.NodeInfo(context.Background(), &pb.NodeInfoRequest{
|
2019-03-02 07:34:08 +00:00
|
|
|
Id: n.GetNode().Id,
|
2019-02-25 18:41:51 +00:00
|
|
|
Address: n.GetNode().GetAddress(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return ErrRequest.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(prettyPrint(info))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-07 18:26:33 +00:00
|
|
|
// DumpNodes outputs a json list of every node in every bucket in the satellite
|
|
|
|
func DumpNodes(cmd *cobra.Command, args []string) (err error) {
|
2019-01-18 15:02:35 +00:00
|
|
|
i, err := NewInspector(*Addr, *IdentityPath)
|
2018-12-07 18:26:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return ErrInspectorDial.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-01-28 22:53:37 +00:00
|
|
|
nodes, err := i.kadclient.FindNear(context.Background(), &pb.FindNearRequest{
|
|
|
|
Start: storj.NodeID{},
|
|
|
|
Limit: 100000,
|
|
|
|
})
|
2018-12-07 18:26:33 +00:00
|
|
|
if err != nil {
|
2019-01-28 22:53:37 +00:00
|
|
|
return err
|
2018-12-07 18:26:33 +00:00
|
|
|
}
|
|
|
|
|
2019-01-28 22:53:37 +00:00
|
|
|
fmt.Println(prettyPrint(nodes))
|
2018-12-07 18:26:33 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-16 20:40:30 +00:00
|
|
|
func prettyPrint(unformatted proto.Message) string {
|
|
|
|
m := jsonpb.Marshaler{Indent: " ", EmitDefaults: true}
|
|
|
|
formatted, err := m.MarshalToString(unformatted)
|
2018-11-30 17:36:05 +00:00
|
|
|
if err != nil {
|
2019-01-16 20:40:30 +00:00
|
|
|
fmt.Println("Error", err)
|
|
|
|
os.Exit(1)
|
2018-11-30 17:36:05 +00:00
|
|
|
}
|
2019-01-16 20:40:30 +00:00
|
|
|
return formatted
|
2018-11-30 17:36:05 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 22:50:52 +00:00
|
|
|
// PingNode sends a PING RPC across the Kad network to check node availability
|
|
|
|
func PingNode(cmd *cobra.Command, args []string) (err error) {
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeID, err := storj.NodeIDFromString(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-27 22:50:52 +00:00
|
|
|
|
2019-01-18 15:02:35 +00:00
|
|
|
i, err := NewInspector(*Addr, *IdentityPath)
|
2018-11-27 22:50:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return ErrInspectorDial.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Pinging node %s at %s", args[0], args[1])
|
|
|
|
|
2019-01-14 18:47:22 +00:00
|
|
|
p, err := i.kadclient.PingNode(context.Background(), &pb.PingNodeRequest{
|
2018-11-29 18:39:27 +00:00
|
|
|
Id: nodeID,
|
2018-11-27 22:50:52 +00:00
|
|
|
Address: args[1],
|
|
|
|
})
|
|
|
|
|
2018-11-30 17:36:05 +00:00
|
|
|
var okayString string
|
2019-01-17 21:14:34 +00:00
|
|
|
if p != nil && p.Ok {
|
2018-11-30 17:36:05 +00:00
|
|
|
okayString = "OK"
|
|
|
|
} else {
|
|
|
|
okayString = "Error"
|
|
|
|
}
|
|
|
|
fmt.Printf("\n -- Ping response: %s\n", okayString)
|
|
|
|
if err != nil {
|
2019-01-17 21:14:34 +00:00
|
|
|
fmt.Printf(" -- Error: %v\n", err)
|
2018-11-30 17:36:05 +00:00
|
|
|
}
|
2018-11-27 22:50:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-25 22:25:09 +00:00
|
|
|
// GetStats gets a node's stats from overlay
|
2018-11-21 22:01:09 +00:00
|
|
|
func GetStats(cmd *cobra.Command, args []string) (err error) {
|
2019-01-18 15:02:35 +00:00
|
|
|
i, err := NewInspector(*Addr, *IdentityPath)
|
2018-11-21 22:01:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return ErrInspectorDial.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeID, err := storj.NodeIDFromString(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-21 22:01:09 +00:00
|
|
|
|
2019-03-25 22:25:09 +00:00
|
|
|
res, err := i.overlayclient.GetStats(context.Background(), &pb.GetStatsRequest{
|
2018-11-29 18:39:27 +00:00
|
|
|
NodeId: nodeID,
|
2018-11-21 22:01:09 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return ErrRequest.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
fmt.Printf("Stats for ID %s:\n", nodeID)
|
2018-12-04 18:47:58 +00:00
|
|
|
fmt.Printf("AuditSuccessRatio: %f, AuditCount: %d, UptimeRatio: %f, UptimeCount: %d,\n",
|
|
|
|
res.AuditRatio, res.AuditCount, res.UptimeRatio, res.UptimeCount)
|
2018-11-21 22:01:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-25 22:25:09 +00:00
|
|
|
// GetCSVStats gets node stats from overlay based on a csv
|
2018-11-21 22:01:09 +00:00
|
|
|
func GetCSVStats(cmd *cobra.Command, args []string) (err error) {
|
2019-01-18 15:02:35 +00:00
|
|
|
i, err := NewInspector(*Addr, *IdentityPath)
|
2018-11-21 22:01:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return ErrInspectorDial.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get csv
|
|
|
|
csvPath := args[0]
|
|
|
|
csvFile, _ := os.Open(csvPath)
|
|
|
|
reader := csv.NewReader(bufio.NewReader(csvFile))
|
|
|
|
for {
|
|
|
|
line, err := reader.Read()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return ErrArgs.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeID, err := storj.NodeIDFromString(line[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-25 22:25:09 +00:00
|
|
|
res, err := i.overlayclient.GetStats(context.Background(), &pb.GetStatsRequest{
|
2018-11-29 18:39:27 +00:00
|
|
|
NodeId: nodeID,
|
2018-11-21 22:01:09 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return ErrRequest.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
fmt.Printf("Stats for ID %s:\n", nodeID)
|
2018-12-04 18:47:58 +00:00
|
|
|
fmt.Printf("AuditSuccessRatio: %f, AuditCount: %d, UptimeRatio: %f, UptimeCount: %d,\n",
|
|
|
|
res.AuditRatio, res.AuditCount, res.UptimeRatio, res.UptimeCount)
|
2018-11-21 22:01:09 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-25 22:25:09 +00:00
|
|
|
// CreateStats creates a node with stats in overlay
|
2018-11-21 22:01:09 +00:00
|
|
|
func CreateStats(cmd *cobra.Command, args []string) (err error) {
|
2019-01-18 15:02:35 +00:00
|
|
|
i, err := NewInspector(*Addr, *IdentityPath)
|
2018-11-21 22:01:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return ErrInspectorDial.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeID, err := storj.NodeIDFromString(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-21 22:01:09 +00:00
|
|
|
auditCount, err := strconv.ParseInt(args[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return ErrArgs.New("audit count must be an int")
|
|
|
|
}
|
|
|
|
auditSuccessCount, err := strconv.ParseInt(args[2], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return ErrArgs.New("audit success count must be an int")
|
|
|
|
}
|
|
|
|
uptimeCount, err := strconv.ParseInt(args[3], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return ErrArgs.New("uptime count must be an int")
|
|
|
|
}
|
|
|
|
uptimeSuccessCount, err := strconv.ParseInt(args[4], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return ErrArgs.New("uptime success count must be an int")
|
|
|
|
}
|
|
|
|
|
2019-03-25 22:25:09 +00:00
|
|
|
_, err = i.overlayclient.CreateStats(context.Background(), &pb.CreateStatsRequest{
|
2018-11-29 18:39:27 +00:00
|
|
|
NodeId: nodeID,
|
2018-11-21 22:01:09 +00:00
|
|
|
AuditCount: auditCount,
|
|
|
|
AuditSuccessCount: auditSuccessCount,
|
|
|
|
UptimeCount: uptimeCount,
|
|
|
|
UptimeSuccessCount: uptimeSuccessCount,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return ErrRequest.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-03-25 22:25:09 +00:00
|
|
|
fmt.Printf("Created stats entry for ID %s\n", nodeID)
|
2018-11-21 22:01:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-25 22:25:09 +00:00
|
|
|
// CreateCSVStats creates node with stats in overlay based on a CSV
|
2018-11-21 22:01:09 +00:00
|
|
|
func CreateCSVStats(cmd *cobra.Command, args []string) (err error) {
|
2019-01-18 15:02:35 +00:00
|
|
|
i, err := NewInspector(*Addr, *IdentityPath)
|
2018-11-21 22:01:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return ErrInspectorDial.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get csv
|
|
|
|
csvPath := args[0]
|
|
|
|
csvFile, _ := os.Open(csvPath)
|
|
|
|
reader := csv.NewReader(bufio.NewReader(csvFile))
|
|
|
|
for {
|
|
|
|
line, err := reader.Read()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return ErrArgs.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeID, err := storj.NodeIDFromString(line[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-21 22:01:09 +00:00
|
|
|
auditCount, err := strconv.ParseInt(line[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return ErrArgs.New("audit count must be an int")
|
|
|
|
}
|
|
|
|
auditSuccessCount, err := strconv.ParseInt(line[2], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return ErrArgs.New("audit success count must be an int")
|
|
|
|
}
|
|
|
|
uptimeCount, err := strconv.ParseInt(line[3], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return ErrArgs.New("uptime count must be an int")
|
|
|
|
}
|
|
|
|
uptimeSuccessCount, err := strconv.ParseInt(line[4], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return ErrArgs.New("uptime success count must be an int")
|
|
|
|
}
|
|
|
|
|
2019-03-25 22:25:09 +00:00
|
|
|
_, err = i.overlayclient.CreateStats(context.Background(), &pb.CreateStatsRequest{
|
2018-11-29 18:39:27 +00:00
|
|
|
NodeId: nodeID,
|
2018-11-21 22:01:09 +00:00
|
|
|
AuditCount: auditCount,
|
|
|
|
AuditSuccessCount: auditSuccessCount,
|
|
|
|
UptimeCount: uptimeCount,
|
|
|
|
UptimeSuccessCount: uptimeSuccessCount,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return ErrRequest.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-03-25 22:25:09 +00:00
|
|
|
fmt.Printf("Created stats entry for ID %s\n", nodeID)
|
2018-11-21 22:01:09 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-02 15:55:58 +01:00
|
|
|
// ObjectHealth gets information about the health of an object on the network
|
|
|
|
func ObjectHealth(cmd *cobra.Command, args []string) (err error) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
i, err := NewInspector(*Addr, *IdentityPath)
|
|
|
|
if err != nil {
|
|
|
|
return ErrArgs.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &pb.ObjectHealthRequest{
|
|
|
|
ProjectId: []byte(args[0]),
|
|
|
|
Bucket: []byte(args[1]),
|
|
|
|
EncryptedPath: []byte(args[2]),
|
|
|
|
StartAfterSegment: 0, // start from first segment
|
|
|
|
EndBeforeSegment: 0, // No end, so we stop when we've hit limit or arrived at the last segment
|
|
|
|
Limit: 0, // No limit, so we stop when we've arrived at the last segment
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = i.healthclient.ObjectHealth(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return ErrRequest.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SegmentHealth gets information about the health of a segment on the network
|
|
|
|
func SegmentHealth(cmd *cobra.Command, args []string) (err error) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
i, err := NewInspector(*Addr, *IdentityPath)
|
|
|
|
if err != nil {
|
|
|
|
return ErrArgs.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
segmentIndex, err := strconv.ParseInt(args[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return ErrRequest.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &pb.SegmentHealthRequest{
|
|
|
|
ProjectId: []byte(args[0]),
|
|
|
|
SegmentIndex: segmentIndex,
|
|
|
|
Bucket: []byte(args[2]),
|
|
|
|
EncryptedPath: []byte(args[3]),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = i.healthclient.SegmentHealth(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return ErrRequest.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-15 20:21:52 +00:00
|
|
|
func getSegments(cmd *cobra.Command, args []string) error {
|
|
|
|
if irreparableLimit <= int32(0) {
|
|
|
|
return ErrArgs.New("limit must be greater than 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
i, err := NewInspector(*Addr, *IdentityPath)
|
|
|
|
if err != nil {
|
|
|
|
return ErrInspectorDial.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
length := irreparableLimit
|
|
|
|
var offset int32
|
|
|
|
|
|
|
|
// query DB and paginate results
|
|
|
|
for length >= irreparableLimit {
|
2019-03-18 10:55:06 +00:00
|
|
|
res, err := i.irrdbclient.ListIrreparableSegments(context.Background(), &pb.ListIrreparableSegmentsRequest{Limit: irreparableLimit, Offset: offset})
|
2019-03-15 20:21:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return ErrRequest.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
objects := sortSegments(res.Segments)
|
|
|
|
if len(objects) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// format and print segments
|
|
|
|
enc := json.NewEncoder(os.Stdout)
|
|
|
|
enc.SetIndent("", " ")
|
|
|
|
err = enc.Encode(objects)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
length = int32(len(res.Segments))
|
|
|
|
offset += length
|
|
|
|
|
|
|
|
if length >= irreparableLimit {
|
|
|
|
if !prompt.Confirm("\nNext page? (y/n)") {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// sortSegments by the object they belong to
|
|
|
|
func sortSegments(segments []*pb.IrreparableSegment) map[string][]*pb.IrreparableSegment {
|
|
|
|
objects := make(map[string][]*pb.IrreparableSegment)
|
|
|
|
for _, seg := range segments {
|
|
|
|
pathElements := storj.SplitPath(string(seg.Path))
|
|
|
|
|
|
|
|
// by removing the segment index, we can easily sort segments into a map of objects
|
|
|
|
pathElements = append(pathElements[:1], pathElements[2:]...)
|
|
|
|
objPath := strings.Join(pathElements, "/")
|
|
|
|
objects[objPath] = append(objects[objPath], seg)
|
|
|
|
}
|
|
|
|
return objects
|
|
|
|
}
|
|
|
|
|
2018-11-21 17:31:27 +00:00
|
|
|
func init() {
|
2018-11-21 22:01:09 +00:00
|
|
|
rootCmd.AddCommand(kadCmd)
|
|
|
|
rootCmd.AddCommand(statsCmd)
|
2019-03-15 20:21:52 +00:00
|
|
|
rootCmd.AddCommand(irreparableCmd)
|
2019-04-02 15:55:58 +01:00
|
|
|
rootCmd.AddCommand(healthCmd)
|
2018-11-21 22:01:09 +00:00
|
|
|
|
|
|
|
kadCmd.AddCommand(countNodeCmd)
|
2018-11-27 22:50:52 +00:00
|
|
|
kadCmd.AddCommand(pingNodeCmd)
|
2018-12-04 21:39:28 +00:00
|
|
|
kadCmd.AddCommand(lookupNodeCmd)
|
2019-02-25 18:41:51 +00:00
|
|
|
kadCmd.AddCommand(nodeInfoCmd)
|
2018-12-07 18:26:33 +00:00
|
|
|
kadCmd.AddCommand(dumpNodesCmd)
|
2018-11-21 22:01:09 +00:00
|
|
|
|
|
|
|
statsCmd.AddCommand(getStatsCmd)
|
|
|
|
statsCmd.AddCommand(getCSVStatsCmd)
|
|
|
|
statsCmd.AddCommand(createStatsCmd)
|
|
|
|
statsCmd.AddCommand(createCSVStatsCmd)
|
|
|
|
|
2019-04-02 15:55:58 +01:00
|
|
|
healthCmd.AddCommand(objectHealthCmd)
|
|
|
|
healthCmd.AddCommand(segmentHealthCmd)
|
|
|
|
|
2019-03-15 20:21:52 +00:00
|
|
|
irreparableCmd.Flags().Int32Var(&irreparableLimit, "limit", 50, "max number of results per page")
|
|
|
|
|
2018-11-21 17:31:27 +00:00
|
|
|
flag.Parse()
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
process.Exec(rootCmd)
|
|
|
|
}
|