-
Notifications
You must be signed in to change notification settings - Fork 32
/
checker.go
149 lines (136 loc) · 3.16 KB
/
checker.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
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
package checks
import (
"fmt"
"io"
"os"
"runtime"
"strings"
"sync"
"github.com/mackerelio/checkers"
"github.com/mackerelio/mackerel-agent/config"
"github.com/urfave/cli"
yaml "gopkg.in/yaml.v2"
)
// Command is command definition of mkr checks
var Command = cli.Command{
Name: "checks",
Usage: "Utility for check plugins",
Subcommands: []cli.Command{
commandRun,
},
}
var commandRun = cli.Command{
Name: "run",
Usage: "run check commands in mackerel-agent.conf",
Description: `
Execute command of check plugins in mackerel-agent.conf all at once.
It is used for checking setting and operation of the check plugins.
The result is output to stdout in TAP format. If any check fails,
it exits non-zero.
`,
Action: doRunChecks,
}
func doRunChecks(c *cli.Context) error {
confFile := c.GlobalString("conf")
conf, err := config.LoadConfig(confFile)
if err != nil {
return err
}
checkers := make([]checker, len(conf.CheckPlugins))
i := 0
for name, p := range conf.CheckPlugins {
checkers[i] = &checkPluginChecker{
name: name,
cp: p,
}
i++
}
return runChecks(checkers, os.Stdout)
}
type result struct {
Name string `yaml:"-"`
Memo string `yaml:"memo,omitempty"`
Cmd []string `yaml:"command,flow"`
Status string `yaml:"status"`
Stdout string `yaml:"stdout,omitempty"`
Stderr string `yaml:"stderr,omitempty"`
ExitCode int `yaml:"exitCode,omitempty"`
ErrMsg string `yaml:"error,omitempty"`
}
func (re *result) ok() bool {
return re.ExitCode == 0 && re.ErrMsg == ""
}
func (re *result) tapFormat(num int) string {
okOrNot := "ok"
if !re.ok() {
okOrNot = "not ok"
}
b, _ := yaml.Marshal(re)
// indent
yamlStr := " " + strings.Replace(strings.TrimSpace(string(b)), "\n", "\n ", -1)
return fmt.Sprintf("%s %d - %s\n ---\n%s\n ...",
okOrNot, num, re.Name, yamlStr)
}
type checkPluginChecker struct {
name string
cp *config.CheckPlugin
}
func (cpc *checkPluginChecker) check() *result {
p := cpc.cp
stdout, stderr, exitCode, err := p.Command.Run()
cmd := p.Command.Args
if len(cmd) == 0 {
cmd = append(cmd, p.Command.Cmd)
}
errMsg := ""
if err != nil {
errMsg = err.Error()
}
return &result{
Name: cpc.name,
Memo: p.Memo,
Cmd: cmd,
Status: checkers.Status(exitCode).String(),
ExitCode: exitCode,
Stdout: strings.TrimSpace(stdout),
Stderr: strings.TrimSpace(stderr),
ErrMsg: errMsg,
}
}
type checker interface {
check() *result
}
func runChecks(checkers []checker, w io.Writer) error {
ch := make(chan *result)
total := len(checkers)
go func() {
sem := make(chan struct{}, runtime.NumCPU()*2)
wg := &sync.WaitGroup{}
wg.Add(total)
for _, c := range checkers {
go func(c checker) {
defer wg.Done()
sem <- struct{}{}
ch <- c.check()
<-sem
}(c)
}
wg.Wait()
close(ch)
}()
fmt.Fprintln(w, "TAP version 13")
fmt.Fprintf(w, "1..%d\n", total)
testNum, errNum := 1, 0
for re := range ch {
fmt.Fprintln(w, re.tapFormat(testNum))
testNum++
if !re.ok() {
errNum++
}
}
if errNum > 0 {
return fmt.Errorf("Failed %d/%d tests, %3.2f%% okay",
errNum, total, float64(100*(total-errNum))/float64(total))
}
return nil
}