forked from resourced/resourced
/
nagios_plugins.go
61 lines (49 loc) · 1.41 KB
/
nagios_plugins.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
package readers
import (
"encoding/json"
"strings"
"github.com/resourced/resourced/libprocess"
"github.com/resourced/resourced/libstring"
)
func init() {
Register("NagiosPlugin", NewNagiosPlugin)
}
func NewNagiosPlugin() IReader {
s := &NagiosPlugin{}
s.Data = make(map[string]interface{})
return s
}
type NagiosPlugin struct {
Shell
}
// Run NagiosPlugins out external program and store the output on c.Data.
func (s *NagiosPlugin) Run() error {
if s.Command != "" {
s.Command = libstring.ExpandTildeAndEnv(s.Command)
nagiosPluginOutputBytes, err := libprocess.NewCmd(s.Command).CombinedOutput()
if err != nil {
s.Data["ExitStatus"] = 1
} else {
s.Data["ExitStatus"] = 0
}
nagiosPluginOutput := strings.TrimSpace(string(nagiosPluginOutputBytes))
if strings.Contains(nagiosPluginOutput, "OK") {
s.Data["ExitStatus"] = 0
s.Data["Message"] = nagiosPluginOutput
} else if strings.Contains(nagiosPluginOutput, "WARNING") {
s.Data["ExitStatus"] = 1
s.Data["Message"] = nagiosPluginOutput
} else if strings.Contains(nagiosPluginOutput, "CRITICAL") {
s.Data["ExitStatus"] = 2
s.Data["Message"] = nagiosPluginOutput
} else if strings.Contains(nagiosPluginOutput, "UNKNOWN") {
s.Data["ExitStatus"] = 3
s.Data["Message"] = nagiosPluginOutput
}
}
return nil
}
// ToJson serialize Data field to JSON.
func (s *NagiosPlugin) ToJson() ([]byte, error) {
return json.Marshal(s.Data)
}