github.com/niluplatform/go-nilu@v1.7.4-0.20200912082737-a0cb0776d52c/cmd/puppeth/wizard_netstats.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of go-ethereum.
     3  //
     4  // go-ethereum is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // go-ethereum is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"encoding/json"
    21  	"os"
    22  	"sort"
    23  	"strings"
    24  	"sync"
    25  
    26  	"github.com/NiluPlatform/go-nilu/core"
    27  	"github.com/NiluPlatform/go-nilu/log"
    28  	"github.com/olekukonko/tablewriter"
    29  )
    30  
    31  // networkStats verifies the status of network components and generates a protip
    32  // configuration set to give users hints on how to do various tasks.
    33  func (w *wizard) networkStats() {
    34  	if len(w.servers) == 0 {
    35  		log.Info("No remote machines to gather stats from")
    36  		return
    37  	}
    38  	// Clear out some previous configs to refill from current scan
    39  	w.conf.ethstats = ""
    40  	w.conf.bootnodes = w.conf.bootnodes[:0]
    41  
    42  	// Iterate over all the specified hosts and check their status
    43  	var pend sync.WaitGroup
    44  
    45  	stats := make(serverStats)
    46  	for server, pubkey := range w.conf.Servers {
    47  		pend.Add(1)
    48  
    49  		// Gather the service stats for each server concurrently
    50  		go func(server string, pubkey []byte) {
    51  			defer pend.Done()
    52  
    53  			stat := w.gatherStats(server, pubkey, w.servers[server])
    54  
    55  			// All status checks complete, report and check next server
    56  			w.lock.Lock()
    57  			defer w.lock.Unlock()
    58  
    59  			delete(w.services, server)
    60  			for service := range stat.services {
    61  				w.services[server] = append(w.services[server], service)
    62  			}
    63  			stats[server] = stat
    64  		}(server, pubkey)
    65  	}
    66  	pend.Wait()
    67  
    68  	// Print any collected stats and return
    69  	stats.render()
    70  }
    71  
    72  // gatherStats gathers service statistics for a particular remote server.
    73  func (w *wizard) gatherStats(server string, pubkey []byte, client *sshClient) *serverStat {
    74  	// Gather some global stats to feed into the wizard
    75  	var (
    76  		genesis   string
    77  		ethstats  string
    78  		bootnodes []string
    79  	)
    80  	// Ensure a valid SSH connection to the remote server
    81  	logger := log.New("server", server)
    82  	logger.Info("Starting remote server health-check")
    83  
    84  	stat := &serverStat{
    85  		address:  client.address,
    86  		services: make(map[string]map[string]string),
    87  	}
    88  	if client == nil {
    89  		conn, err := dial(server, pubkey)
    90  		if err != nil {
    91  			logger.Error("Failed to establish remote connection", "err", err)
    92  			stat.failure = err.Error()
    93  			return stat
    94  		}
    95  		client = conn
    96  	}
    97  	// Client connected one way or another, run health-checks
    98  	logger.Debug("Checking for nginx availability")
    99  	if infos, err := checkNginx(client, w.network); err != nil {
   100  		if err != ErrServiceUnknown {
   101  			stat.services["nginx"] = map[string]string{"offline": err.Error()}
   102  		}
   103  	} else {
   104  		stat.services["nginx"] = infos.Report()
   105  	}
   106  	logger.Debug("Checking for ethstats availability")
   107  	if infos, err := checkEthstats(client, w.network); err != nil {
   108  		if err != ErrServiceUnknown {
   109  			stat.services["ethstats"] = map[string]string{"offline": err.Error()}
   110  		}
   111  	} else {
   112  		stat.services["ethstats"] = infos.Report()
   113  		ethstats = infos.config
   114  	}
   115  	logger.Debug("Checking for bootnode availability")
   116  	if infos, err := checkNode(client, w.network, true); err != nil {
   117  		if err != ErrServiceUnknown {
   118  			stat.services["bootnode"] = map[string]string{"offline": err.Error()}
   119  		}
   120  	} else {
   121  		stat.services["bootnode"] = infos.Report()
   122  
   123  		genesis = string(infos.genesis)
   124  		bootnodes = append(bootnodes, infos.enode)
   125  	}
   126  	logger.Debug("Checking for sealnode availability")
   127  	if infos, err := checkNode(client, w.network, false); err != nil {
   128  		if err != ErrServiceUnknown {
   129  			stat.services["sealnode"] = map[string]string{"offline": err.Error()}
   130  		}
   131  	} else {
   132  		stat.services["sealnode"] = infos.Report()
   133  		genesis = string(infos.genesis)
   134  	}
   135  	logger.Debug("Checking for explorer availability")
   136  	if infos, err := checkExplorer(client, w.network); err != nil {
   137  		if err != ErrServiceUnknown {
   138  			stat.services["explorer"] = map[string]string{"offline": err.Error()}
   139  		}
   140  	} else {
   141  		stat.services["explorer"] = infos.Report()
   142  	}
   143  	logger.Debug("Checking for wallet availability")
   144  	if infos, err := checkWallet(client, w.network); err != nil {
   145  		if err != ErrServiceUnknown {
   146  			stat.services["wallet"] = map[string]string{"offline": err.Error()}
   147  		}
   148  	} else {
   149  		stat.services["wallet"] = infos.Report()
   150  	}
   151  	logger.Debug("Checking for faucet availability")
   152  	if infos, err := checkFaucet(client, w.network); err != nil {
   153  		if err != ErrServiceUnknown {
   154  			stat.services["faucet"] = map[string]string{"offline": err.Error()}
   155  		}
   156  	} else {
   157  		stat.services["faucet"] = infos.Report()
   158  	}
   159  	logger.Debug("Checking for dashboard availability")
   160  	if infos, err := checkDashboard(client, w.network); err != nil {
   161  		if err != ErrServiceUnknown {
   162  			stat.services["dashboard"] = map[string]string{"offline": err.Error()}
   163  		}
   164  	} else {
   165  		stat.services["dashboard"] = infos.Report()
   166  	}
   167  	// Feed and newly discovered information into the wizard
   168  	w.lock.Lock()
   169  	defer w.lock.Unlock()
   170  
   171  	if genesis != "" && w.conf.Genesis == nil {
   172  		g := new(core.Genesis)
   173  		if err := json.Unmarshal([]byte(genesis), g); err != nil {
   174  			log.Error("Failed to parse remote genesis", "err", err)
   175  		} else {
   176  			w.conf.Genesis = g
   177  		}
   178  	}
   179  	if ethstats != "" {
   180  		w.conf.ethstats = ethstats
   181  	}
   182  	w.conf.bootnodes = append(w.conf.bootnodes, bootnodes...)
   183  
   184  	return stat
   185  }
   186  
   187  // serverStat is a collection of service configuration parameters and health
   188  // check reports to print to the user.
   189  type serverStat struct {
   190  	address  string
   191  	failure  string
   192  	services map[string]map[string]string
   193  }
   194  
   195  // serverStats is a collection of server stats for multiple hosts.
   196  type serverStats map[string]*serverStat
   197  
   198  // render converts the gathered statistics into a user friendly tabular report
   199  // and prints it to the standard output.
   200  func (stats serverStats) render() {
   201  	// Start gathering service statistics and config parameters
   202  	table := tablewriter.NewWriter(os.Stdout)
   203  
   204  	table.SetHeader([]string{"Server", "Address", "Service", "Config", "Value"})
   205  	table.SetAlignment(tablewriter.ALIGN_LEFT)
   206  	table.SetColWidth(100)
   207  
   208  	// Find the longest lines for all columns for the hacked separator
   209  	separator := make([]string, 5)
   210  	for server, stat := range stats {
   211  		if len(server) > len(separator[0]) {
   212  			separator[0] = strings.Repeat("-", len(server))
   213  		}
   214  		if len(stat.address) > len(separator[1]) {
   215  			separator[1] = strings.Repeat("-", len(stat.address))
   216  		}
   217  		for service, configs := range stat.services {
   218  			if len(service) > len(separator[2]) {
   219  				separator[2] = strings.Repeat("-", len(service))
   220  			}
   221  			for config, value := range configs {
   222  				if len(config) > len(separator[3]) {
   223  					separator[3] = strings.Repeat("-", len(config))
   224  				}
   225  				if len(value) > len(separator[4]) {
   226  					separator[4] = strings.Repeat("-", len(value))
   227  				}
   228  			}
   229  		}
   230  	}
   231  	// Fill up the server report in alphabetical order
   232  	servers := make([]string, 0, len(stats))
   233  	for server := range stats {
   234  		servers = append(servers, server)
   235  	}
   236  	sort.Strings(servers)
   237  
   238  	for i, server := range servers {
   239  		// Add a separator between all servers
   240  		if i > 0 {
   241  			table.Append(separator)
   242  		}
   243  		// Fill up the service report in alphabetical order
   244  		services := make([]string, 0, len(stats[server].services))
   245  		for service := range stats[server].services {
   246  			services = append(services, service)
   247  		}
   248  		sort.Strings(services)
   249  
   250  		if len(services) == 0 {
   251  			table.Append([]string{server, stats[server].address, "", "", ""})
   252  		}
   253  		for j, service := range services {
   254  			// Add an empty line between all services
   255  			if j > 0 {
   256  				table.Append([]string{"", "", "", separator[3], separator[4]})
   257  			}
   258  			// Fill up the config report in alphabetical order
   259  			configs := make([]string, 0, len(stats[server].services[service]))
   260  			for service := range stats[server].services[service] {
   261  				configs = append(configs, service)
   262  			}
   263  			sort.Strings(configs)
   264  
   265  			for k, config := range configs {
   266  				switch {
   267  				case j == 0 && k == 0:
   268  					table.Append([]string{server, stats[server].address, service, config, stats[server].services[service][config]})
   269  				case k == 0:
   270  					table.Append([]string{"", "", service, config, stats[server].services[service][config]})
   271  				default:
   272  					table.Append([]string{"", "", "", config, stats[server].services[service][config]})
   273  				}
   274  			}
   275  		}
   276  	}
   277  	table.Render()
   278  }
   279  
   280  // protips contains a collection of network infos to report pro-tips
   281  // based on.
   282  type protips struct {
   283  	genesis   string
   284  	network   int64
   285  	bootFull  []string
   286  	bootLight []string
   287  	ethstats  string
   288  }