2019-01-30 19:49:25 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
2019-02-18 15:16:48 +00:00
|
|
|
"time"
|
2019-01-30 19:49:25 +00:00
|
|
|
|
|
|
|
"github.com/fatih/color"
|
|
|
|
"github.com/golang/protobuf/ptypes"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/memory"
|
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/rpc"
|
2019-02-11 11:17:32 +00:00
|
|
|
"storj.io/storj/pkg/process"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/version"
|
2019-01-30 19:49:25 +00:00
|
|
|
)
|
|
|
|
|
2019-10-03 19:31:39 +01:00
|
|
|
const contactWindow = time.Hour * 2
|
2019-03-22 13:27:59 +00:00
|
|
|
|
2019-03-07 18:19:37 +00:00
|
|
|
type dashboardClient struct {
|
2019-09-19 05:46:39 +01:00
|
|
|
conn *rpc.Conn
|
2019-03-07 18:19:37 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 21:00:51 +01:00
|
|
|
func dialDashboardClient(ctx context.Context, address string) (*dashboardClient, error) {
|
2019-09-19 05:46:39 +01:00
|
|
|
conn, err := rpc.NewDefaultDialer(nil).DialAddressUnencrypted(ctx, address)
|
2019-03-07 18:19:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return &dashboardClient{}, err
|
|
|
|
}
|
2019-09-19 05:46:39 +01:00
|
|
|
return &dashboardClient{conn: conn}, nil
|
2019-03-07 18:19:37 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 21:00:51 +01:00
|
|
|
func (dash *dashboardClient) dashboard(ctx context.Context) (*pb.DashboardResponse, error) {
|
2019-12-22 15:07:50 +00:00
|
|
|
return pb.NewDRPCPieceStoreInspectorClient(dash.conn.Raw()).Dashboard(ctx, &pb.DashboardRequest{})
|
2019-06-25 21:00:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (dash *dashboardClient) close() error {
|
|
|
|
return dash.conn.Close()
|
|
|
|
}
|
|
|
|
|
2019-03-07 18:19:37 +00:00
|
|
|
func cmdDashboard(cmd *cobra.Command, args []string) (err error) {
|
2019-09-19 17:37:40 +01:00
|
|
|
ctx, _ := process.Ctx(cmd)
|
2019-01-30 19:49:25 +00:00
|
|
|
|
|
|
|
ident, err := runCfg.Identity.Load()
|
|
|
|
if err != nil {
|
|
|
|
zap.S().Fatal(err)
|
|
|
|
} else {
|
|
|
|
zap.S().Info("Node ID: ", ident.ID)
|
|
|
|
}
|
|
|
|
|
2019-06-25 21:00:51 +01:00
|
|
|
client, err := dialDashboardClient(ctx, dashboardCfg.Address)
|
2019-01-30 19:49:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-25 21:00:51 +01:00
|
|
|
defer func() {
|
|
|
|
if err := client.close(); err != nil {
|
|
|
|
zap.S().Debug("closing dashboard client failed", err)
|
|
|
|
}
|
|
|
|
}()
|
2019-01-30 19:49:25 +00:00
|
|
|
|
|
|
|
for {
|
2019-03-07 18:19:37 +00:00
|
|
|
data, err := client.dashboard(ctx)
|
2019-01-30 19:49:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-22 13:27:59 +00:00
|
|
|
if err := printDashboard(data); err != nil {
|
2019-03-07 18:19:37 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-01-30 19:49:25 +00:00
|
|
|
|
2019-03-07 18:19:37 +00:00
|
|
|
// Refresh the dashboard every 3 seconds
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
2019-01-30 19:49:25 +00:00
|
|
|
|
2019-03-22 13:27:59 +00:00
|
|
|
func printDashboard(data *pb.DashboardResponse) error {
|
2019-03-07 18:19:37 +00:00
|
|
|
clearScreen()
|
2019-05-24 14:53:29 +01:00
|
|
|
var warnFlag bool
|
2019-03-07 18:19:37 +00:00
|
|
|
color.NoColor = !useColor
|
2019-01-30 19:49:25 +00:00
|
|
|
|
2019-03-07 18:19:37 +00:00
|
|
|
heading := color.New(color.FgGreen, color.Bold)
|
2019-04-03 20:13:39 +01:00
|
|
|
_, _ = heading.Printf("\nStorage Node Dashboard ( Node Version: %s )\n", version.Build.Version.String())
|
2019-03-07 18:19:37 +00:00
|
|
|
_, _ = heading.Printf("\n======================\n\n")
|
2019-01-30 19:49:25 +00:00
|
|
|
|
2019-03-07 18:19:37 +00:00
|
|
|
w := tabwriter.NewWriter(color.Output, 0, 0, 1, ' ', 0)
|
2019-03-18 10:55:06 +00:00
|
|
|
fmt.Fprintf(w, "ID\t%s\n", color.YellowString(data.NodeId.String()))
|
2019-01-30 19:49:25 +00:00
|
|
|
|
2019-10-03 19:31:39 +01:00
|
|
|
if data.LastPinged.IsZero() || time.Since(data.LastPinged) >= contactWindow {
|
2019-04-08 16:30:29 +01:00
|
|
|
fmt.Fprintf(w, "Last Contact\t%s\n", color.RedString("OFFLINE"))
|
2019-10-03 19:31:39 +01:00
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, "Last Contact\t%s\n", color.GreenString("ONLINE"))
|
2019-03-07 18:19:37 +00:00
|
|
|
}
|
2019-01-30 19:49:25 +00:00
|
|
|
|
2019-07-25 16:01:44 +01:00
|
|
|
// TODO: use stdtime in protobuf
|
2019-03-07 18:19:37 +00:00
|
|
|
uptime, err := ptypes.Duration(data.GetUptime())
|
2019-07-25 16:01:44 +01:00
|
|
|
if err == nil {
|
2019-03-07 18:19:37 +00:00
|
|
|
fmt.Fprintf(w, "Uptime\t%s\n", color.YellowString(uptime.Truncate(time.Second).String()))
|
|
|
|
}
|
2019-01-30 19:49:25 +00:00
|
|
|
|
2019-03-07 18:19:37 +00:00
|
|
|
if err = w.Flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
stats := data.GetStats()
|
|
|
|
if stats != nil {
|
|
|
|
usedBandwidth := color.WhiteString(memory.Size(stats.GetUsedBandwidth()).Base10String())
|
|
|
|
availableSpace := color.WhiteString(memory.Size(stats.GetAvailableSpace()).Base10String())
|
|
|
|
usedSpace := color.WhiteString(memory.Size(stats.GetUsedSpace()).Base10String())
|
2019-03-27 19:44:18 +00:00
|
|
|
usedEgress := color.WhiteString(memory.Size(stats.GetUsedEgress()).Base10String())
|
|
|
|
usedIngress := color.WhiteString(memory.Size(stats.GetUsedIngress()).Base10String())
|
2019-03-07 18:19:37 +00:00
|
|
|
|
|
|
|
w = tabwriter.NewWriter(color.Output, 0, 0, 5, ' ', tabwriter.AlignRight)
|
2019-03-27 19:44:18 +00:00
|
|
|
fmt.Fprintf(w, "\n\t%s\t%s\t%s\t%s\t\n", color.GreenString("Available"), color.GreenString("Used"), color.GreenString("Egress"), color.GreenString("Ingress"))
|
2020-03-09 19:40:23 +00:00
|
|
|
fmt.Fprintf(w, "Bandwidth\t%s\t%s\t%s\t%s\t (since %s 1)\n", "N/A", usedBandwidth, usedEgress, usedIngress, time.Now().Format("Jan"))
|
2019-03-07 18:19:37 +00:00
|
|
|
fmt.Fprintf(w, "Disk\t%s\t%s\t\n", availableSpace, usedSpace)
|
2019-01-30 19:49:25 +00:00
|
|
|
if err = w.Flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-05 20:48:37 +00:00
|
|
|
|
2019-03-07 18:19:37 +00:00
|
|
|
} else {
|
|
|
|
color.Yellow("Loading...\n")
|
2019-03-05 20:48:37 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 18:19:37 +00:00
|
|
|
w = tabwriter.NewWriter(color.Output, 0, 0, 1, ' ', 0)
|
|
|
|
// TODO: Get addresses from server data
|
|
|
|
fmt.Fprintf(w, "Internal\t%s\n", color.WhiteString(dashboardCfg.Address))
|
|
|
|
fmt.Fprintf(w, "External\t%s\n", color.WhiteString(data.GetExternalAddress()))
|
2019-07-16 22:03:03 +01:00
|
|
|
// Disabling the Link to the Dashboard as its not working yet
|
|
|
|
// fmt.Fprintf(w, "Dashboard\t%s\n", color.WhiteString(data.GetDashboardAddress()))
|
2019-03-07 18:19:37 +00:00
|
|
|
if err = w.Flush(); err != nil {
|
|
|
|
return err
|
2019-01-30 19:49:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-24 14:53:29 +01:00
|
|
|
if warnFlag {
|
|
|
|
fmt.Fprintf(w, "\nWARNING!!!!! %s\n", color.WhiteString("Increase your bandwidth"))
|
|
|
|
}
|
|
|
|
|
2019-03-07 18:19:37 +00:00
|
|
|
return nil
|
2019-01-30 19:49:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// clearScreen clears the screen so it can be redrawn
|
|
|
|
func clearScreen() {
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "linux", "darwin":
|
|
|
|
cmd := exec.Command("clear")
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
_ = cmd.Run()
|
|
|
|
case "windows":
|
|
|
|
cmd := exec.Command("cmd", "/c", "cls")
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
_ = cmd.Run()
|
|
|
|
default:
|
|
|
|
fmt.Print(strings.Repeat("\n", 100))
|
|
|
|
}
|
|
|
|
}
|