-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_eve_ng.go
More file actions
192 lines (175 loc) · 6.54 KB
/
check_eve_ng.go
File metadata and controls
192 lines (175 loc) · 6.54 KB
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
package main
import (
"fmt"
"github.com/inexio/check_eve_ng/evengapi"
"github.com/inexio/go-monitoringplugin"
"github.com/jessevdk/go-flags"
"github.com/oleiade/reflections"
"github.com/pkg/errors"
"os"
"regexp"
"strings"
)
func main() {
opts, err := parseArgs()
if err != nil {
os.Exit(3) //parseArgs() prints errors to stdout
}
err = validateArgs(opts)
if err != nil {
fmt.Println("Invalid arguments: " + err.Error())
os.Exit(3)
}
response := monitoringplugin.NewResponse("checked")
defer response.OutputAndExit()
if opts.PerformanceDataJSONLabel {
response.SetPerformanceDataJsonLabel(true)
}
eveNgAPI, err := evengapi.NewEveNgAPI(opts.Hostname, opts.Username, opts.Password)
if err != nil {
response.UpdateStatus(monitoringplugin.UNKNOWN, "error during creating new eve ng api: "+err.Error())
return
}
if opts.ForceHTTP {
err = eveNgAPI.ForceHTTP(true)
if err != nil {
response.UpdateStatus(monitoringplugin.UNKNOWN, "eve ng api might not have been created properly: "+err.Error())
return
}
}
err = eveNgAPI.Login()
if err != nil {
response.UpdateStatus(monitoringplugin.UNKNOWN, "error during Login: "+err.Error())
return
}
defer func() {
err = eveNgAPI.Logout()
if err != nil {
response.UpdateStatus(monitoringplugin.UNKNOWN, "error during logout: "+err.Error())
}
}()
//System Status
statusMetrics := []string{"iol", "dynamips", "qemu", "docker", "vpcs"}
systemStatus, err := eveNgAPI.GetSystemStatus()
if err != nil {
response.UpdateStatus(monitoringplugin.UNKNOWN, "error during Get System Status: "+err.Error())
}
for _, metric := range statusMetrics {
valueInterface, err := reflections.GetField(systemStatus, strings.Title(metric))
if err != nil {
response.UpdateStatus(monitoringplugin.UNKNOWN, "error while getting field from system status struct: "+err.Error())
continue
}
value, ok := valueInterface.(*float64)
if !ok {
response.UpdateStatus(monitoringplugin.UNKNOWN, "error while converting interface to *int")
continue
}
err = response.AddPerformanceDataPoint(monitoringplugin.NewPerformanceDataPoint(metric, *value, ""))
if err != nil {
response.UpdateStatus(monitoringplugin.UNKNOWN, "error while adding new performance data point: "+err.Error())
}
}
labs, err := getLabs(&opts.Labs, eveNgAPI)
if err != nil {
response.UpdateStatus(monitoringplugin.UNKNOWN, "error during getLabs: "+err.Error())
return
}
//fmt.Println(labs)
for _, lab := range labs {
result, err :=
eveNgAPI.GetAllNodesForLab(lab)
if err != nil {
match, errRegex := regexp.MatchString(`Lab does not exist`, err.Error())
if errRegex != nil {
response.UpdateStatus(monitoringplugin.UNKNOWN, "regex error: "+errRegex.Error())
continue
}
if match && opts.LabsExist {
response.UpdateStatus(monitoringplugin.CRITICAL, "lab "+lab+" does not exist!")
continue
}
response.UpdateStatus(monitoringplugin.UNKNOWN, "error during request for all nodes for lab "+lab+": "+err.Error())
} else {
labHostsUp := 0
labHostsDown := 0
for _, nodeData := range result {
if nodeData.Status == 0 {
labHostsDown++
if opts.AllNodesUp && !inArray(nodeData.UUID, opts.ExcludeNode) {
response.UpdateStatus(monitoringplugin.CRITICAL, "node "+nodeData.Name+" ("+nodeData.Image+") in lab "+lab+" is down! (uuid: "+nodeData.UUID+")")
}
} else {
labHostsUp++
}
}
if opts.LabPerformanceData {
err := response.AddPerformanceDataPoint(monitoringplugin.NewPerformanceDataPoint("nodes_up", float64(labHostsUp), "").SetLabelTag(lab))
if err != nil {
response.UpdateStatus(monitoringplugin.UNKNOWN, "error during add performance data point: "+err.Error())
}
err = response.AddPerformanceDataPoint(monitoringplugin.NewPerformanceDataPoint("nodes_down", float64(labHostsDown), "").SetLabelTag(lab))
if err != nil {
response.UpdateStatus(monitoringplugin.UNKNOWN, "error during add performance data point: "+err.Error())
}
}
}
}
return
}
type cliOpts struct {
Hostname string `long:"hostname" description:"Hostname" required:"true"`
Username string `long:"username" description:"Username" required:"true"`
Password string `long:"password" description:"Password" required:"true"`
Labs []string `long:"lab" description:"Lab that will be included in monitoring" required:"false"`
AllNodesUp bool `long:"all-nodes-up" description:"Check if all nodes in the given labs are up" required:"false"`
PerformanceDataJSONLabel bool `long:"performance-data-json-label" description:"Output performance data label in json format" required:"false"`
ExcludeNode []string `long:"exclude-node" description:"Exclude a node by its uuid" required:"false"`
LabsExist bool `long:"labs-exist" description:"Check if all given labs exist (only checks for implicit named labs in the input parameters)" required:"false"`
LabPerformanceData bool `long:"lab-performance-data" description:"Print performance data for all included labs" required:"false"`
ForceHTTP bool `long:"force-http" description:"Force http instead of https" required:"false"`
}
func parseArgs() (*cliOpts, error) {
opts := &cliOpts{}
_, err := flags.Parse(opts)
return opts, err
}
func validateArgs(opts *cliOpts) error {
if (len(opts.Labs) == 0) && (opts.LabsExist || opts.LabPerformanceData) {
return errors.New("the options --labs-exist and --lab-performance-data cannot be used when there are no given labs")
}
if (len(opts.Labs) == 1) && opts.Labs[0] == "all" && (opts.LabsExist) {
return errors.New("the options --labs-exist cannot be used when there is no specific given lab to check for existence")
}
if len(opts.Labs) > 0 && !(opts.LabsExist || opts.LabPerformanceData || opts.AllNodesUp) {
return errors.New("there are labs defined but no monitoring modes for them (like --all-nodes-up, --labs-exist etc.)")
}
return nil
}
func getLabs(optLabs *[]string, eveNgAPI *evengapi.EveNgAPI) ([]string, error) {
useAllLabs := false
for i, lab := range *optLabs {
if lab == "all" {
useAllLabs = true
(*optLabs)[i] = (*optLabs)[len(*optLabs)-1]
*optLabs = (*optLabs)[:len(*optLabs)-1]
break
}
}
if useAllLabs {
labs, err := eveNgAPI.GetAllLabs()
if err != nil {
return nil, errors.Wrap(err, "error during GetAllLabs")
}
return evengapi.SliceMerge(labs, *optLabs), nil
}
return *optLabs, nil
}
func inArray(search string, arr []string) bool {
for _, i := range arr {
if i == search {
return true
}
}
return false
}