-
Notifications
You must be signed in to change notification settings - Fork 3
/
tools.go
270 lines (219 loc) · 6.11 KB
/
tools.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright 2021 Adam Chalkley
//
// https://github.com/atc0005/check-vmware
//
// Licensed under the MIT License. See LICENSE file in the project root for
// full license information.
package vsphere
import (
"fmt"
"os"
"sort"
"strings"
"time"
"github.com/atc0005/go-nagios"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
)
// GetVMToolsStatusSummary accepts a collection of VirtualMachines and checks
// the ToolsStatus for each one providing an overall Nagios state label and
// exit code for the collection.
func GetVMToolsStatusSummary(vms []mo.VirtualMachine) (string, int) {
funcTimeStart := time.Now()
defer func() {
fmt.Fprintf(
os.Stderr,
"It took %v to execute GetVMToolsStatusSummary func.\n",
time.Since(funcTimeStart),
)
}()
var nagiosExitStateLabel string
var nagiosExitStateCode int
Loop:
for _, vm := range vms {
// check specific tools issue to determine final Nagios state
switch vm.Guest.ToolsStatus {
case types.VirtualMachineToolsStatusToolsOk:
continue
case types.VirtualMachineToolsStatusToolsOld:
// Not severe enough to immediately break as other more severe
// issues may be present. Set state and allow the state to "carry"
// at function exit.
nagiosExitStateLabel = nagios.StateWARNINGLabel
nagiosExitStateCode = nagios.StateWARNINGExitCode
case types.VirtualMachineToolsStatusToolsNotRunning:
nagiosExitStateLabel = nagios.StateCRITICALLabel
nagiosExitStateCode = nagios.StateCRITICALExitCode
break Loop
case types.VirtualMachineToolsStatusToolsNotInstalled:
nagiosExitStateLabel = nagios.StateCRITICALLabel
nagiosExitStateCode = nagios.StateCRITICALExitCode
break Loop
// This should not be reached
default:
nagiosExitStateLabel = nagios.StateUNKNOWNLabel
nagiosExitStateCode = nagios.StateUNKNOWNExitCode
break Loop
}
}
return nagiosExitStateLabel, nagiosExitStateCode
}
// FilterVMsWithToolsIssues filters the provided collection of VirtualMachines
// to just those with non-OK status.
func FilterVMsWithToolsIssues(vms []mo.VirtualMachine) []mo.VirtualMachine {
// setup early so we can reference it from deferred stats output
var vmsWithIssues []mo.VirtualMachine
funcTimeStart := time.Now()
defer func(vms []mo.VirtualMachine, filteredVMs *[]mo.VirtualMachine) {
fmt.Fprintf(
os.Stderr,
"It took %v to execute FilterVMsWithToolsIssues func (for %d VMs, yielding %d VMs).\n",
time.Since(funcTimeStart),
len(vms),
len(*filteredVMs),
)
}(vms, &vmsWithIssues)
for _, vm := range vms {
if vm.Guest.ToolsStatus != types.VirtualMachineToolsStatusToolsOk {
vmsWithIssues = append(vmsWithIssues, vm)
}
}
return vmsWithIssues
}
// VMToolsOneLineCheckSummary is used to generate a one-line Nagios service
// check results summary. This is the line most prominent in notifications.
func VMToolsOneLineCheckSummary(stateLabel string, vmsWithIssues []mo.VirtualMachine, evaluatedVMs []mo.VirtualMachine, rps []mo.ResourcePool) string {
funcTimeStart := time.Now()
defer func() {
fmt.Fprintf(
os.Stderr,
"It took %v to execute VMToolsOneLineCheckSummary func.\n",
time.Since(funcTimeStart),
)
}()
switch {
case len(vmsWithIssues) > 0:
return fmt.Sprintf(
"%s: %d VMs with VMware Tools issues detected (evaluated %d VMs, %d Resource Pools)",
stateLabel,
len(vmsWithIssues),
len(evaluatedVMs),
len(rps),
)
default:
return fmt.Sprintf(
"%s: No VMware Tools issues detected (evaluated %d VMs, %d Resource Pools)",
stateLabel,
len(evaluatedVMs),
len(rps),
)
}
}
// VMToolsReport generates a comprehensive summary including any active issues
// along with various verbose details intended to aid in troubleshooting check
// results at a glance. This information is provided for use with the Long
// Service Output field commonly displayed on the detailed service check
// results display in the web UI or in the body of many notifications.
func VMToolsReport(
c *vim25.Client,
allVMs []mo.VirtualMachine,
evaluatedVMs []mo.VirtualMachine,
vmsWithIssues []mo.VirtualMachine,
vmsToExclude []string,
evalPoweredOffVMs bool,
includeRPs []string,
excludeRPs []string,
rps []mo.ResourcePool,
) string {
funcTimeStart := time.Now()
defer func() {
fmt.Fprintf(
os.Stderr,
"It took %v to execute VMToolsReport func.\n",
time.Since(funcTimeStart),
)
}()
rpNames := make([]string, len(rps))
for i := range rps {
rpNames[i] = rps[i].Name
}
var vmsReport strings.Builder
switch {
case len(vmsWithIssues) > 1:
sort.Slice(vmsWithIssues, func(i, j int) bool {
return strings.ToLower(vmsWithIssues[i].Name) < strings.ToLower(vmsWithIssues[j].Name)
})
for idx, vm := range vmsWithIssues {
fmt.Fprintf(
&vmsReport,
"* %02d) %s (%s)%s",
idx+1,
vm.Name,
string(vm.Guest.ToolsStatus),
nagios.CheckOutputEOL,
)
}
default:
fmt.Fprintf(
&vmsReport,
"* No VMware Tools issues detected.%s",
nagios.CheckOutputEOL,
)
}
fmt.Fprintf(
&vmsReport,
"%s---%s%s",
nagios.CheckOutputEOL,
nagios.CheckOutputEOL,
nagios.CheckOutputEOL,
)
fmt.Fprintf(
&vmsReport,
"* vSphere environment: %s%s",
c.URL().String(),
nagios.CheckOutputEOL,
)
fmt.Fprintf(
&vmsReport,
"* VMs (evaluated: %d, total: %d)%s",
len(evaluatedVMs),
len(allVMs),
nagios.CheckOutputEOL,
)
fmt.Fprintf(
&vmsReport,
"* Powered off VMs evaluated: %t%s",
evalPoweredOffVMs,
nagios.CheckOutputEOL,
)
fmt.Fprintf(
&vmsReport,
"* Specified VMs to exclude (%d): [%v]%s",
len(vmsToExclude),
strings.Join(vmsToExclude, ", "),
nagios.CheckOutputEOL,
)
fmt.Fprintf(
&vmsReport,
"* Specified Resource Pools to explicitly include (%d): [%v]%s",
len(includeRPs),
strings.Join(includeRPs, ", "),
nagios.CheckOutputEOL,
)
fmt.Fprintf(
&vmsReport,
"* Specified Resource Pools to explicitly exclude (%d): [%v]%s",
len(excludeRPs),
strings.Join(excludeRPs, ", "),
nagios.CheckOutputEOL,
)
fmt.Fprintf(
&vmsReport,
"* Resource Pools evaluated (%d): [%v]%s",
len(rpNames),
strings.Join(rpNames, ", "),
nagios.CheckOutputEOL,
)
return vmsReport.String()
}