github.com/gochain-io/gochain@v2.2.26+incompatible/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/gochain-io/gochain/core" 27 "github.com/gochain-io/gochain/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.netstats = "" 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 netstats 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 netstats availability") 107 if infos, err := checkNetstats(client, w.network); err != nil { 108 if err != ErrServiceUnknown { 109 stat.services["netstats"] = map[string]string{"offline": err.Error()} 110 } 111 } else { 112 stat.services["netstats"] = infos.Report() 113 netstats = 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 136 logger.Debug("Checking for wallet availability") 137 if infos, err := checkWallet(client, w.network); err != nil { 138 if err != ErrServiceUnknown { 139 stat.services["wallet"] = map[string]string{"offline": err.Error()} 140 } 141 } else { 142 stat.services["wallet"] = 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 153 // Feed and newly discovered information into the wizard 154 w.lock.Lock() 155 defer w.lock.Unlock() 156 157 if genesis != "" && w.conf.Genesis == nil { 158 g := new(core.Genesis) 159 if err := json.Unmarshal([]byte(genesis), g); err != nil { 160 log.Error("Failed to parse remote genesis", "err", err) 161 } else { 162 w.conf.Genesis = g 163 } 164 } 165 if netstats != "" { 166 w.conf.netstats = netstats 167 } 168 w.conf.bootnodes = append(w.conf.bootnodes, bootnodes...) 169 170 return stat 171 } 172 173 // serverStat is a collection of service configuration parameters and health 174 // check reports to print to the user. 175 type serverStat struct { 176 address string 177 failure string 178 services map[string]map[string]string 179 } 180 181 // serverStats is a collection of server stats for multiple hosts. 182 type serverStats map[string]*serverStat 183 184 // render converts the gathered statistics into a user friendly tabular report 185 // and prints it to the standard output. 186 func (stats serverStats) render() { 187 // Start gathering service statistics and config parameters 188 table := tablewriter.NewWriter(os.Stdout) 189 190 table.SetHeader([]string{"Server", "Address", "Service", "Config", "Value"}) 191 table.SetAlignment(tablewriter.ALIGN_LEFT) 192 table.SetColWidth(100) 193 194 // Find the longest lines for all columns for the hacked separator 195 separator := make([]string, 5) 196 for server, stat := range stats { 197 if len(server) > len(separator[0]) { 198 separator[0] = strings.Repeat("-", len(server)) 199 } 200 if len(stat.address) > len(separator[1]) { 201 separator[1] = strings.Repeat("-", len(stat.address)) 202 } 203 for service, configs := range stat.services { 204 if len(service) > len(separator[2]) { 205 separator[2] = strings.Repeat("-", len(service)) 206 } 207 for config, value := range configs { 208 if len(config) > len(separator[3]) { 209 separator[3] = strings.Repeat("-", len(config)) 210 } 211 if len(value) > len(separator[4]) { 212 separator[4] = strings.Repeat("-", len(value)) 213 } 214 } 215 } 216 } 217 // Fill up the server report in alphabetical order 218 servers := make([]string, 0, len(stats)) 219 for server := range stats { 220 servers = append(servers, server) 221 } 222 sort.Strings(servers) 223 224 for i, server := range servers { 225 // Add a separator between all servers 226 if i > 0 { 227 table.Append(separator) 228 } 229 // Fill up the service report in alphabetical order 230 services := make([]string, 0, len(stats[server].services)) 231 for service := range stats[server].services { 232 services = append(services, service) 233 } 234 sort.Strings(services) 235 236 if len(services) == 0 { 237 table.Append([]string{server, stats[server].address, "", "", ""}) 238 } 239 for j, service := range services { 240 // Add an empty line between all services 241 if j > 0 { 242 table.Append([]string{"", "", "", separator[3], separator[4]}) 243 } 244 // Fill up the config report in alphabetical order 245 configs := make([]string, 0, len(stats[server].services[service])) 246 for service := range stats[server].services[service] { 247 configs = append(configs, service) 248 } 249 sort.Strings(configs) 250 251 for k, config := range configs { 252 switch { 253 case j == 0 && k == 0: 254 table.Append([]string{server, stats[server].address, service, config, stats[server].services[service][config]}) 255 case k == 0: 256 table.Append([]string{"", "", service, config, stats[server].services[service][config]}) 257 default: 258 table.Append([]string{"", "", "", config, stats[server].services[service][config]}) 259 } 260 } 261 } 262 } 263 table.Render() 264 }