forked from redpanda-data/redpanda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.go
110 lines (100 loc) 路 2.57 KB
/
check.go
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
// Copyright 2021 Vectorized, Inc.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.md
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0
//go:build linux
// +build linux
package redpanda
import (
"fmt"
"os"
"time"
"github.com/fatih/color"
"github.com/olekukonko/tablewriter"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/vectorizedio/redpanda/src/go/rpk/pkg/cli/ui"
"github.com/vectorizedio/redpanda/src/go/rpk/pkg/config"
"github.com/vectorizedio/redpanda/src/go/rpk/pkg/tuners"
)
func NewCheckCommand(fs afero.Fs, mgr config.Manager) *cobra.Command {
var (
configFile string
timeout time.Duration
)
command := &cobra.Command{
Use: "check",
Short: "Check if system meets redpanda requirements.",
RunE: func(ccmd *cobra.Command, args []string) error {
return executeCheck(fs, mgr, configFile, timeout)
},
}
command.Flags().StringVar(
&configFile,
"config",
"",
"Redpanda config file, if not set the file will be searched for"+
" in the default locations.",
)
command.Flags().DurationVar(
&timeout,
"timeout",
2000*time.Millisecond,
"The maximum amount of time to wait for the checks and tune processes to complete. "+
"The value passed is a sequence of decimal numbers, each with optional "+
"fraction and a unit suffix, such as '300ms', '1.5s' or '2h45m'. "+
"Valid time units are 'ns', 'us' (or '碌s'), 'ms', 's', 'm', 'h'",
)
return command
}
func appendToTable(t *tablewriter.Table, r tuners.CheckResult) {
t.Append([]string{
r.Desc,
r.Required,
r.Current,
fmt.Sprint(r.Severity),
fmt.Sprint(printResult(r.Severity, r.IsOk)),
})
}
func executeCheck(
fs afero.Fs, mgr config.Manager, configFile string, timeout time.Duration,
) error {
conf, err := mgr.FindOrGenerate(configFile)
if err != nil {
return err
}
results, err := tuners.Check(fs, conf, timeout)
if err != nil {
return err
}
table := ui.NewRpkTable(os.Stdout)
table.SetHeader([]string{
"Condition",
"Required",
"Current",
"Severity",
"Passed",
})
for _, res := range results {
appendToTable(table, res)
}
fmt.Printf("\nSystem check results\n")
table.Render()
return nil
}
func printResult(sev tuners.Severity, isOk bool) string {
if isOk {
return color.GreenString("%v", isOk)
}
switch sev {
case tuners.Fatal:
return color.RedString("%v", isOk)
case tuners.Warning:
return color.YellowString("%v", isOk)
}
return fmt.Sprint(isOk)
}