aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/puppeth/wizard_netstats.go
blob: c069721982c30ce5d22a73a19e9a2c31bd540339 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright 2017 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.

package main

import (
    "encoding/json"
    "fmt"
    "os"
    "strings"

    "github.com/ethereum/go-ethereum/core"
    "github.com/ethereum/go-ethereum/log"
    "github.com/olekukonko/tablewriter"
)

// networkStats verifies the status of network components and generates a protip
// configuration set to give users hints on how to do various tasks.
func (w *wizard) networkStats(tips bool) {
    if len(w.servers) == 0 {
        log.Error("No remote machines to gather stats from")
        return
    }
    protips := new(protips)

    // Iterate over all the specified hosts and check their status
    stats := tablewriter.NewWriter(os.Stdout)
    stats.SetHeader([]string{"Server", "IP", "Status", "Service", "Details"})
    stats.SetColWidth(100)

    for server, pubkey := range w.conf.Servers {
        client := w.servers[server]
        logger := log.New("server", server)
        logger.Info("Starting remote server health-check")

        // If the server is not connected, try to connect again
        if client == nil {
            conn, err := dial(server, pubkey)
            if err != nil {
                logger.Error("Failed to establish remote connection", "err", err)
                stats.Append([]string{server, "", err.Error(), "", ""})
                continue
            }
            client = conn
        }
        // Client connected one way or another, run health-checks
        services := make(map[string]string)
        logger.Debug("Checking for nginx availability")
        if infos, err := checkNginx(client, w.network); err != nil {
            if err != ErrServiceUnknown {
                services["nginx"] = err.Error()
            }
        } else {
            services["nginx"] = infos.String()
        }
        logger.Debug("Checking for ethstats availability")
        if infos, err := checkEthstats(client, w.network); err != nil {
            if err != ErrServiceUnknown {
                services["ethstats"] = err.Error()
            }
        } else {
            services["ethstats"] = infos.String()
            protips.ethstats = infos.config
        }
        logger.Debug("Checking for bootnode availability")
        if infos, err := checkNode(client, w.network, true); err != nil {
            if err != ErrServiceUnknown {
                services["bootnode"] = err.Error()
            }
        } else {
            services["bootnode"] = infos.String()

            protips.genesis = string(infos.genesis)
            protips.bootFull = append(protips.bootFull, infos.enodeFull)
            if infos.enodeLight != "" {
                protips.bootLight = append(protips.bootLight, infos.enodeLight)
            }
        }
        logger.Debug("Checking for sealnode availability")
        if infos, err := checkNode(client, w.network, false); err != nil {
            if err != ErrServiceUnknown {
                services["sealnode"] = err.Error()
            }
        } else {
            services["sealnode"] = infos.String()
            protips.genesis = string(infos.genesis)
        }
        logger.Debug("Checking for faucet availability")
        if infos, err := checkFaucet(client, w.network); err != nil {
            if err != ErrServiceUnknown {
                services["faucet"] = err.Error()
            }
        } else {
            services["faucet"] = infos.String()
        }
        logger.Debug("Checking for dashboard availability")
        if infos, err := checkDashboard(client, w.network); err != nil {
            if err != ErrServiceUnknown {
                services["dashboard"] = err.Error()
            }
        } else {
            services["dashboard"] = infos.String()
        }
        // All status checks complete, report and check next server
        delete(w.services, server)
        for service := range services {
            w.services[server] = append(w.services[server], service)
        }
        server, address := client.server, client.address
        for service, status := range services {
            stats.Append([]string{server, address, "online", service, status})
            server, address = "", ""
        }
        if len(services) == 0 {
            stats.Append([]string{server, address, "online", "", ""})
        }
    }
    // If a genesis block was found, load it into our configs
    if protips.genesis != "" && w.conf.genesis == nil {
        genesis := new(core.Genesis)
        if err := json.Unmarshal([]byte(protips.genesis), genesis); err != nil {
            log.Error("Failed to parse remote genesis", "err", err)
        } else {
            w.conf.genesis = genesis
            protips.network = genesis.Config.ChainId.Int64()
        }
    }
    if protips.ethstats != "" {
        w.conf.ethstats = protips.ethstats
    }
    w.conf.bootFull = protips.bootFull
    w.conf.bootLight = protips.bootLight

    // Print any collected stats and return
    if !tips {
        stats.Render()
    } else {
        protips.print(w.network)
    }
}

// protips contains a collection of network infos to report pro-tips
// based on.
type protips struct {
    genesis   string
    network   int64
    bootFull  []string
    bootLight []string
    ethstats  string
}

// print analyzes the network information available and prints a collection of
// pro tips for the user's consideration.
func (p *protips) print(network string) {
    // If a known genesis block is available, display it and prepend an init command
    fullinit, lightinit := "", ""
    if p.genesis != "" {
        fullinit = fmt.Sprintf("geth --datadir=$HOME/.%s init %s.json && ", network, network)
        lightinit = fmt.Sprintf("geth --datadir=$HOME/.%s --light init %s.json && ", network, network)
    }
    // If an ethstats server is available, add the ethstats flag
    statsflag := ""
    if p.ethstats != "" {
        if strings.Contains(p.ethstats, " ") {
            statsflag = fmt.Sprintf(` --ethstats="yournode:%s"`, p.ethstats)
        } else {
            statsflag = fmt.Sprintf(` --ethstats=yournode:%s`, p.ethstats)
        }
    }
    // If bootnodes have been specified, add the bootnode flag
    bootflagFull := ""
    if len(p.bootFull) > 0 {
        bootflagFull = fmt.Sprintf(` --bootnodes %s`, strings.Join(p.bootFull, ","))
    }
    bootflagLight := ""
    if len(p.bootLight) > 0 {
        bootflagLight = fmt.Sprintf(` --bootnodes %s`, strings.Join(p.bootLight, ","))
    }
    // Assemble all the known pro-tips
    var tasks, tips []string

    tasks = append(tasks, "Run an archive node with historical data")
    tips = append(tips, fmt.Sprintf("%sgeth --networkid=%d --datadir=$HOME/.%s --cache=1024%s%s", fullinit, p.network, network, statsflag, bootflagFull))

    tasks = append(tasks, "Run a full node with recent data only")
    tips = append(tips, fmt.Sprintf("%sgeth --networkid=%d --datadir=$HOME/.%s --cache=512 --fast%s%s", fullinit, p.network, network, statsflag, bootflagFull))

    tasks = append(tasks, "Run a light node with on demand retrievals")
    tips = append(tips, fmt.Sprintf("%sgeth --networkid=%d --datadir=$HOME/.%s --light%s%s", lightinit, p.network, network, statsflag, bootflagLight))

    tasks = append(tasks, "Run an embedded node with constrained memory")
    tips = append(tips, fmt.Sprintf("%sgeth --networkid=%d --datadir=$HOME/.%s --cache=32 --light%s%s", lightinit, p.network, network, statsflag, bootflagLight))

    // If the tips are short, display in a table
    short := true
    for _, tip := range tips {
        if len(tip) > 100 {
            short = false
            break
        }
    }
    fmt.Println()
    if short {
        howto := tablewriter.NewWriter(os.Stdout)
        howto.SetHeader([]string{"Fun tasks for you", "Tips on how to"})
        howto.SetColWidth(100)

        for i := 0; i < len(tasks); i++ {
            howto.Append([]string{tasks[i], tips[i]})
        }
        howto.Render()
        return
    }
    // Meh, tips got ugly, split into many lines
    for i := 0; i < len(tasks); i++ {
        fmt.Println(tasks[i])
        fmt.Println(strings.Repeat("-", len(tasks[i])))
        fmt.Println(tips[i])
        fmt.Println()
        fmt.Println()
    }
}