-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
431 lines (405 loc) · 11.4 KB
/
main.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package main
import (
"fmt"
"strconv"
"strings"
"time"
human "github.com/dustin/go-humanize"
"github.com/sensu-community/sensu-plugin-sdk/sensu"
"github.com/sensu/sensu-go/types"
"github.com/shirou/gopsutil/disk"
)
// Config represents the check plugin config.
type Config struct {
sensu.PluginConfig
IncludeFSType []string
ExcludeFSType []string
IncludeFSPath []string
ExcludeFSPath []string
Warning float64
Critical float64
IncludePseudo bool
IncludeReadOnly bool
FailOnError bool
HumanReadable bool
MetricsMode bool
ExtraTags []string
}
type MetricGroup struct {
Comment string
Type string
Name string
Metrics []Metric
}
func (g *MetricGroup) AddMetric(tags map[string]string, value float64, timeNow int64) {
g.Metrics = append(g.Metrics, Metric{
Tags: tags,
Timestamp: timeNow,
Value: value,
})
}
func (g *MetricGroup) Output() {
var output string
metricName := strings.Replace(g.Name, ".", "_", -1)
fmt.Printf("# HELP %s [%s] %s\n", metricName, g.Type, g.Comment)
fmt.Printf("# TYPE %s %s\n", metricName, g.Type)
for _, m := range g.Metrics {
tagStr := ""
for tag, tvalue := range m.Tags {
if len(tagStr) > 0 {
tagStr = tagStr + ","
}
tagStr = tagStr + tag + "=\"" + tvalue + "\""
}
if len(tagStr) > 0 {
tagStr = "{" + tagStr + "}"
}
output = strings.Join(
[]string{metricName + tagStr, fmt.Sprintf("%v", m.Value), strconv.FormatInt(m.Timestamp, 10)}, " ")
fmt.Println(output)
}
fmt.Println("")
}
type Metric struct {
Tags map[string]string
Timestamp int64
Value float64
}
var (
tags = map[string]string{}
extraTags = map[string]string{}
plugin = Config{
PluginConfig: sensu.PluginConfig{
Name: "check-disk-usage",
Short: "Cross platform disk usage check for Sensu",
Keyspace: "sensu.io/plugins/check-disk-usage/config",
},
}
options = []*sensu.PluginConfigOption{
{
Path: "include-fs-type",
Env: "",
Argument: "include-fs-type",
Shorthand: "i",
Default: []string{},
Usage: "Comma separated list of file system types to check",
Value: &plugin.IncludeFSType,
},
{
Path: "exclude-fs-type",
Env: "",
Argument: "exclude-fs-type",
Shorthand: "e",
Default: []string{},
Usage: "Comma separated list of file system types to exclude from checking",
Value: &plugin.ExcludeFSType,
},
{
Path: "include-fs-path",
Env: "",
Argument: "include-fs-path",
Shorthand: "I",
Default: []string{},
Usage: "Comma separated list of file system paths to check",
Value: &plugin.IncludeFSPath,
},
{
Path: "exclude-fs-path",
Env: "",
Argument: "exclude-fs-path",
Shorthand: "E",
Default: []string{},
Usage: "Comma separated list of file system paths to exclude from checking",
Value: &plugin.ExcludeFSPath,
},
{
Path: "warning",
Env: "",
Argument: "warning",
Shorthand: "w",
Default: float64(85),
Usage: "Warning threshold for file system usage",
Value: &plugin.Warning,
},
{
Path: "critical",
Env: "",
Argument: "critical",
Shorthand: "c",
Default: float64(95),
Usage: "Critical threshold for file system usage",
Value: &plugin.Critical,
},
{
Path: "include-pseudo-fs",
Env: "",
Argument: "include-pseudo-fs",
Shorthand: "p",
Default: false,
Usage: "Include pseudo-filesystems (e.g. tmpfs) (default false)",
Value: &plugin.IncludePseudo,
},
{
Path: "fail-on-error",
Env: "",
Argument: "fail-on-error",
Shorthand: "f",
Default: false,
Usage: "Fail and exit on errors getting file system usage (e.g. permission denied) (default false)",
Value: &plugin.FailOnError,
},
{
Path: "include-read-only",
Env: "",
Argument: "include-read-only",
Shorthand: "r",
Default: false,
Usage: "Include read-only filesystems (default false)",
Value: &plugin.IncludeReadOnly,
},
{
Path: "human-readable",
Env: "",
Argument: "human-readable",
Shorthand: "H",
Default: false,
Usage: "print sizes in powers of 1024 (default false)",
Value: &plugin.HumanReadable,
},
{
Path: "metrics",
Env: "",
Argument: "metrics",
Default: false,
Usage: "Output metrics instead of human readable output",
Value: &plugin.MetricsMode,
},
{
Path: "tags",
Env: "",
Argument: "tags",
Default: []string{},
Usage: "Comma separated list of additional metrics tags using key=value format.",
Value: &plugin.ExtraTags,
},
}
)
func main() {
check := sensu.NewGoCheck(&plugin.PluginConfig, options, checkArgs, executeCheck, false)
check.Execute()
}
func checkArgs(event *types.Event) (int, error) {
if len(plugin.IncludeFSType) > 0 && len(plugin.ExcludeFSType) > 0 {
return sensu.CheckStateCritical, fmt.Errorf("--include-fs-type and --exclude-fs-type are mutually exclusive")
}
if len(plugin.IncludeFSPath) > 0 && len(plugin.ExcludeFSPath) > 0 {
return sensu.CheckStateCritical, fmt.Errorf("--include-fs-path and --exclude-fs-path are mutually exclusive")
}
if plugin.Warning >= plugin.Critical {
return sensu.CheckStateCritical, fmt.Errorf("--warning value can not be greater than or equal to --critical value")
}
for _, tagString := range plugin.ExtraTags {
fmt.Println(tagString)
parts := strings.Split(tagString, `=`)
if len(parts) == 2 {
extraTags[parts[0]] = parts[1]
} else {
return sensu.CheckStateCritical, fmt.Errorf("Failed to parse input tag: %s", tagString)
}
}
return sensu.CheckStateOK, nil
}
func executeCheck(event *types.Event) (int, error) {
var (
criticals int
warnings int
)
timeNow := time.Now().UnixNano() / 1000000
parts, err := disk.Partitions(plugin.IncludePseudo)
if err != nil {
return sensu.CheckStateCritical, fmt.Errorf("Failed to get partitions, error: %v", err)
}
metricGroups := map[string]*MetricGroup{
"disk.critical": &MetricGroup{
Name: "disk.critical",
Type: "GAUGE",
Comment: "non-zero value indicates mountpoint usage is above critical threshold",
Metrics: []Metric{},
},
"disk.warning": &MetricGroup{
Name: "disk.warning",
Type: "GAUGE",
Comment: "non-zero value indicates mountpoint usage is above warning threshold",
Metrics: []Metric{},
},
"disk.percent_used": &MetricGroup{
Name: "disk.percent_usage",
Type: "GAUGE",
Comment: "Percentage of mounted volume used",
Metrics: []Metric{},
},
"disk.total_bytes": &MetricGroup{
Name: "disk.total_bytes",
Type: "GAUGE",
Comment: "Total size in bytes of mounted volumed",
Metrics: []Metric{},
},
"disk.used_bytes": &MetricGroup{
Name: "disk.used_bytes",
Type: "GAUGE",
Comment: "Used size in bytes of mounted volumed",
Metrics: []Metric{},
},
"disk.free_bytes": &MetricGroup{
Name: "disk.free_bytes",
Type: "GAUGE",
Comment: "Free size in bytes of mounted volumed",
Metrics: []Metric{},
},
}
for _, p := range parts {
tags = map[string]string{}
for key, value := range extraTags {
tags[key] = value
}
// Ignore excluded (or non-included) file system types
if !isValidFSType(p.Fstype) {
continue
}
// Ignore excluded (or non-included) file systems
if !isValidFSPath(p.Mountpoint) {
continue
}
// Ignore read-only file systems?
if !plugin.IncludeReadOnly && isReadOnly(p.Opts) {
continue
}
tags["mountpoint"] = p.Mountpoint
device := p.Mountpoint
s, err := disk.Usage(device)
if err != nil {
if plugin.FailOnError {
return sensu.CheckStateCritical, fmt.Errorf("Failed to get disk usage for %s, error: %v", device, err)
}
if !plugin.MetricsMode {
fmt.Printf("%s UNKNOWN: %s - error: %v\n", plugin.PluginConfig.Name, device, err)
}
continue
}
// Ignore empty file systems
if s.Total == 0 {
continue
}
// implement magic factor for larger file systems?
crit := 0
warn := 0
if s.UsedPercent >= plugin.Critical {
criticals++
crit = 1
}
if s.UsedPercent >= plugin.Warning {
warnings++
warn = 1
}
metricGroups["disk.critical"].AddMetric(tags, float64(crit), timeNow)
metricGroups["disk.warning"].AddMetric(tags, float64(warn), timeNow)
if !plugin.MetricsMode {
fmt.Printf("%s ", plugin.PluginConfig.Name)
if crit > 0 {
fmt.Printf("CRITICAL: ")
} else if warn > 0 {
fmt.Printf(" WARNING: ")
} else {
fmt.Printf(" OK: ")
}
if plugin.HumanReadable {
fmt.Printf("%s %.2f%% - Total: %s, Used: %s, Free: %s\n",
p.Mountpoint, s.UsedPercent, human.IBytes(s.Total), human.IBytes(s.Used), human.IBytes(s.Free))
} else {
fmt.Printf("%s %.2f%% - Total: %s, Used: %s, Free: %s\n",
p.Mountpoint, s.UsedPercent, human.Bytes(s.Total), human.Bytes(s.Used), human.Bytes(s.Free))
}
}
metricGroups["disk.percent_used"].AddMetric(tags, float64(s.UsedPercent), timeNow)
metricGroups["disk.total_bytes"].AddMetric(tags, float64(s.Total), timeNow)
metricGroups["disk.used_bytes"].AddMetric(tags, float64(s.Used), timeNow)
metricGroups["disk.free_bytes"].AddMetric(tags, float64(s.Free), timeNow)
}
tags = map[string]string{}
for key, value := range extraTags {
tags[key] = value
}
tags["mountpoint"] = "any"
anyCritical := func() float64 {
if criticals > 0 {
return 1
} else {
return 0
}
}()
metricGroups["disk.critical"].AddMetric(tags, anyCritical, timeNow)
anyWarning := func() float64 {
if warnings > 0 {
return 1
} else {
return 0
}
}()
metricGroups["disk.warning"].AddMetric(tags, anyWarning, timeNow)
if plugin.MetricsMode {
// output metrics in a fixed order
metricGroups["disk.critical"].Output()
metricGroups["disk.warning"].Output()
metricGroups["disk.percent_used"].Output()
metricGroups["disk.total_bytes"].Output()
metricGroups["disk.used_bytes"].Output()
metricGroups["disk.free_bytes"].Output()
}
if criticals > 0 {
return sensu.CheckStateCritical, nil
} else if warnings > 0 {
return sensu.CheckStateWarning, nil
}
return sensu.CheckStateOK, nil
}
func isValidFSType(fsType string) bool {
// should i account for case insensitive searches for windows (ntfs vs. NTFS)?
if len(plugin.IncludeFSType) > 0 && contains(plugin.IncludeFSType, fsType) {
return true
} else if len(plugin.IncludeFSType) > 0 {
return false
} else if len(plugin.ExcludeFSType) > 0 && contains(plugin.ExcludeFSType, fsType) {
return false
}
// either not in exclude list or neither list is specified
return true
}
func isValidFSPath(fsPath string) bool {
// should i account for case insensitive searches for windows (c: vs. C:)?
if len(plugin.IncludeFSPath) > 0 && contains(plugin.IncludeFSPath, fsPath) {
return true
} else if len(plugin.IncludeFSPath) > 0 {
return false
} else if len(plugin.ExcludeFSPath) > 0 && contains(plugin.ExcludeFSPath, fsPath) {
return false
}
// either not in exclude list or neither list is specified
return true
}
func isReadOnly(mountOpts string) bool {
mOpts := strings.Split(mountOpts, ",")
// "ro" should cover Linux, macOS, and Windows, "read-only" is reportd by mount(8)
// on macOS so check for it, just in case
if contains(mOpts, "ro") || contains(mOpts, "read-only") {
return true
}
return false
}
func contains(a []string, s string) bool {
for _, v := range a {
if v == s {
return true
}
}
return false
}