gitee.com/liu-zhao234568/cntest@v1.0.0/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  	"gitee.com/liu-zhao234568/cntest/core"
    27  	"gitee.com/liu-zhao234568/cntest/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  		services: make(map[string]map[string]string),
    86  	}
    87  	if client == nil {
    88  		conn, err := dial(server, pubkey)
    89  		if err != nil {
    90  			logger.Error("Failed to establish remote connection", "err", err)
    91  			stat.failure = err.Error()
    92  			return stat
    93  		}
    94  		client = conn
    95  	}
    96  	stat.address = client.address
    97  
    98  	// Client connected one way or another, run health-checks
    99  	logger.Debug("Checking for nginx availability")
   100  	if infos, err := checkNginx(client, w.network); err != nil {
   101  		if err != ErrServiceUnknown {
   102  			stat.services["nginx"] = map[string]string{"offline": err.Error()}
   103  		}
   104  	} else {
   105  		stat.services["nginx"] = infos.Report()
   106  	}
   107  	logger.Debug("Checking for ethstats availability")
   108  	if infos, err := checkEthstats(client, w.network); err != nil {
   109  		if err != ErrServiceUnknown {
   110  			stat.services["ethstats"] = map[string]string{"offline": err.Error()}
   111  		}
   112  	} else {
   113  		stat.services["ethstats"] = infos.Report()
   114  		ethstats = infos.config
   115  	}
   116  	logger.Debug("Checking for bootnode availability")
   117  	if infos, err := checkNode(client, w.network, true); err != nil {
   118  		if err != ErrServiceUnknown {
   119  			stat.services["bootnode"] = map[string]string{"offline": err.Error()}
   120  		}
   121  	} else {
   122  		stat.services["bootnode"] = infos.Report()
   123  
   124  		genesis = string(infos.genesis)
   125  		bootnodes = append(bootnodes, infos.enode)
   126  	}
   127  	logger.Debug("Checking for sealnode availability")
   128  	if infos, err := checkNode(client, w.network, false); err != nil {
   129  		if err != ErrServiceUnknown {
   130  			stat.services["sealnode"] = map[string]string{"offline": err.Error()}
   131  		}
   132  	} else {
   133  		stat.services["sealnode"] = infos.Report()
   134  		genesis = string(infos.genesis)
   135  	}
   136  	logger.Debug("Checking for explorer availability")
   137  	if infos, err := checkExplorer(client, w.network); err != nil {
   138  		if err != ErrServiceUnknown {
   139  			stat.services["explorer"] = map[string]string{"offline": err.Error()}
   140  		}
   141  	} else {
   142  		stat.services["explorer"] = infos.Report()
   143  	}
   144  	logger.Debug("Checking for faucet availability")
   145  	if infos, err := checkFaucet(client, w.network); err != nil {
   146  		if err != ErrServiceUnknown {
   147  			stat.services["faucet"] = map[string]string{"offline": err.Error()}
   148  		}
   149  	} else {
   150  		stat.services["faucet"] = infos.Report()
   151  	}
   152  	logger.Debug("Checking for dashboard availability")
   153  	if infos, err := checkDashboard(client, w.network); err != nil {
   154  		if err != ErrServiceUnknown {
   155  			stat.services["dashboard"] = map[string]string{"offline": err.Error()}
   156  		}
   157  	} else {
   158  		stat.services["dashboard"] = infos.Report()
   159  	}
   160  	// Feed and newly discovered information into the wizard
   161  	w.lock.Lock()
   162  	defer w.lock.Unlock()
   163  
   164  	if genesis != "" && w.conf.Genesis == nil {
   165  		g := new(core.Genesis)
   166  		if err := json.Unmarshal([]byte(genesis), g); err != nil {
   167  			log.Error("Failed to parse remote genesis", "err", err)
   168  		} else {
   169  			w.conf.Genesis = g
   170  		}
   171  	}
   172  	if ethstats != "" {
   173  		w.conf.ethstats = ethstats
   174  	}
   175  	w.conf.bootnodes = append(w.conf.bootnodes, bootnodes...)
   176  
   177  	return stat
   178  }
   179  
   180  // serverStat is a collection of service configuration parameters and health
   181  // check reports to print to the user.
   182  type serverStat struct {
   183  	address  string
   184  	failure  string
   185  	services map[string]map[string]string
   186  }
   187  
   188  // serverStats is a collection of server stats for multiple hosts.
   189  type serverStats map[string]*serverStat
   190  
   191  // render converts the gathered statistics into a user friendly tabular report
   192  // and prints it to the standard output.
   193  func (stats serverStats) render() {
   194  	// Start gathering service statistics and config parameters
   195  	table := tablewriter.NewWriter(os.Stdout)
   196  
   197  	table.SetHeader([]string{"Server", "Address", "Service", "Config", "Value"})
   198  	table.SetAlignment(tablewriter.ALIGN_LEFT)
   199  	table.SetColWidth(40)
   200  
   201  	// Find the longest lines for all columns for the hacked separator
   202  	separator := make([]string, 5)
   203  	for server, stat := range stats {
   204  		if len(server) > len(separator[0]) {
   205  			separator[0] = strings.Repeat("-", len(server))
   206  		}
   207  		if len(stat.address) > len(separator[1]) {
   208  			separator[1] = strings.Repeat("-", len(stat.address))
   209  		}
   210  		if len(stat.failure) > len(separator[1]) {
   211  			separator[1] = strings.Repeat("-", len(stat.failure))
   212  		}
   213  		for service, configs := range stat.services {
   214  			if len(service) > len(separator[2]) {
   215  				separator[2] = strings.Repeat("-", len(service))
   216  			}
   217  			for config, value := range configs {
   218  				if len(config) > len(separator[3]) {
   219  					separator[3] = strings.Repeat("-", len(config))
   220  				}
   221  				for _, val := range strings.Split(value, "\n") {
   222  					if len(val) > len(separator[4]) {
   223  						separator[4] = strings.Repeat("-", len(val))
   224  					}
   225  				}
   226  			}
   227  		}
   228  	}
   229  	// Fill up the server report in alphabetical order
   230  	servers := make([]string, 0, len(stats))
   231  	for server := range stats {
   232  		servers = append(servers, server)
   233  	}
   234  	sort.Strings(servers)
   235  
   236  	for i, server := range servers {
   237  		// Add a separator between all servers
   238  		if i > 0 {
   239  			table.Append(separator)
   240  		}
   241  		// Fill up the service report in alphabetical order
   242  		services := make([]string, 0, len(stats[server].services))
   243  		for service := range stats[server].services {
   244  			services = append(services, service)
   245  		}
   246  		sort.Strings(services)
   247  
   248  		if len(services) == 0 {
   249  			if stats[server].failure != "" {
   250  				table.Append([]string{server, stats[server].failure, "", "", ""})
   251  			} else {
   252  				table.Append([]string{server, stats[server].address, "", "", ""})
   253  			}
   254  		}
   255  		for j, service := range services {
   256  			// Add an empty line between all services
   257  			if j > 0 {
   258  				table.Append([]string{"", "", "", separator[3], separator[4]})
   259  			}
   260  			// Fill up the config report in alphabetical order
   261  			configs := make([]string, 0, len(stats[server].services[service]))
   262  			for service := range stats[server].services[service] {
   263  				configs = append(configs, service)
   264  			}
   265  			sort.Strings(configs)
   266  
   267  			for k, config := range configs {
   268  				for l, value := range strings.Split(stats[server].services[service][config], "\n") {
   269  					switch {
   270  					case j == 0 && k == 0 && l == 0:
   271  						table.Append([]string{server, stats[server].address, service, config, value})
   272  					case k == 0 && l == 0:
   273  						table.Append([]string{"", "", service, config, value})
   274  					case l == 0:
   275  						table.Append([]string{"", "", "", config, value})
   276  					default:
   277  						table.Append([]string{"", "", "", "", value})
   278  					}
   279  				}
   280  			}
   281  		}
   282  	}
   283  	table.Render()
   284  }