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
|
package internal
import (
"bytes"
"context"
"os/exec"
"strings"
"time"
)
type check struct {
Plugin string
Args []string
DependsOn []string `json:"DependsOn,omitempty"`
Retries int `json:"Retries,omitempty"`
RetryInterval int `json:"RetryInterval,omitempty"`
}
type namedCheck struct {
check
name string
}
type checkResult struct {
name string
output string
epoch int64
status nagiosCode
federated bool
}
func (c check) run(ctx context.Context, name string) checkResult {
cmd := exec.CommandContext(ctx, c.Plugin, c.Args...)
var bytes bytes.Buffer
cmd.Stdout = &bytes
cmd.Stderr = &bytes
if err := cmd.Run(); err != nil {
if ctx.Err() == context.DeadlineExceeded {
return checkResult{name, "Check command timed out", time.Now().Unix(), nagiosCritical, false}
}
}
// Remove Nagios perf data from output and trim whitespaces
parts := strings.Split(bytes.String(), "|")
output := strings.TrimSpace(parts[0])
ec := cmd.ProcessState.ExitCode()
if ec < int(nagiosOk) || ec > int(nagiosUnknown) {
// If the exit code is not in the range of known Nagios codes, treat it as unknown
ec = int(nagiosUnknown)
}
return checkResult{name, output, time.Now().Unix(), nagiosCode(ec), false}
}
func (c check) skip(name, output string) checkResult {
return checkResult{name, output, time.Now().Unix(), nagiosUnknown, false}
}
func (c namedCheck) run(ctx context.Context) checkResult {
return c.check.run(ctx, c.name)
}
func (c namedCheck) skip(output string) checkResult {
return c.check.skip(c.name, output)
}
|