-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnagios.go
63 lines (55 loc) · 2.24 KB
/
nagios.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
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
package main
import (
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/olorin/nagiosplugin"
)
// Abstraction of the check for warning or critical
func warningCritical(statistic *string, value *float64, check *nagiosplugin.Check, warningRange, criticalRange *nagiosplugin.Range) {
check.AddResult(nagiosplugin.OK, "Values are normal")
check.AddPerfDatum(*statistic, "", *value)
if warningRange.Check(*value) {
check.AddResultf(nagiosplugin.WARNING, "The %s is %.2f.", *statistic, *value)
}
if criticalRange.Check(*value) {
check.AddResultf(nagiosplugin.CRITICAL, "The %s is %.2f.", *statistic, *value)
}
}
func nagiosCheck(warning, critical, statistic *string, datapoints []*cloudwatch.Datapoint, awsErr error) {
// Initialize the check - this will return an UNKNOWN result
// until more results are added.
check := nagiosplugin.NewCheck()
// If we exit early or panic() we'll still output a result.
defer check.Finish()
// If the amazon error is not nil then fail with Critical
if awsErr != nil {
check.Criticalf("Cloudwatch check failed - %s", awsErr.Error())
}
// Validate ranges
warnRange, err := nagiosplugin.ParseRange(*warning)
if err != nil {
check.AddResult(nagiosplugin.UNKNOWN, "Error parsing warning range")
}
critRange, err := nagiosplugin.ParseRange(*critical)
if err != nil {
check.AddResult(nagiosplugin.UNKNOWN, "Error parsing critical range")
}
// Check which metric we need to validate
switch *statistic {
case "Average":
warningCritical(statistic, datapoints[0].Average, check, warnRange, critRange)
case "Sum":
warningCritical(statistic, datapoints[0].Sum, check, warnRange, critRange)
case "SampleCount":
warningCritical(statistic, datapoints[0].SampleCount, check, warnRange, critRange)
case "Maximum":
warningCritical(statistic, datapoints[0].Maximum, check, warnRange, critRange)
case "Minimum":
warningCritical(statistic, datapoints[0].Minimum, check, warnRange, critRange)
default:
check.Criticalf("Unknown statistic: %s", *statistic)
}
}