forked from r0bj/check_influxdb_query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_influxdb_query.go
133 lines (115 loc) · 4.08 KB
/
check_influxdb_query.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
package main
import (
"fmt"
"encoding/json"
"github.com/influxdata/influxdb/client/v2"
"github.com/olorin/nagiosplugin"
"github.com/alecthomas/kingpin/v2"
)
const (
ver string = "0.10"
)
var (
host = kingpin.Flag("host", "influxdb host").Default("localhost").Short('H').String()
port = kingpin.Flag("port", "influxdb port").Default("8086").Short('P').String()
username = kingpin.Flag("username", "influxdb username").Default("admin").Short('u').String()
password = kingpin.Flag("password", "influxdb password").Default("admin").Short('p').String()
db = kingpin.Flag("db", "influxdb database name").Default("telegraf").Short('d').String()
warningThreshold = kingpin.Flag("warning-threshold", "warning threshold for returned value").Short('w').Required().Int()
criticalThreshold = kingpin.Flag("critical-threshold", "critical threshold for returned value").Short('c').Required().Int()
compareOperator = kingpin.Flag("compare-operator", "operator to compare returned value with thresholds, 'lt' or 'gt'").Short('o').Default("lt").String()
query = kingpin.Arg("query", "influxdb query which returns one value to be able compare against integer thresholds").Required().String()
)
func queryDB(c client.Client, db string, cmd string) (res []client.Result, err error) {
q := client.Query{
Command: cmd,
Database: db,
}
if response, err := c.Query(q); err == nil {
if response.Error() != nil {
return res, response.Error()
}
res = response.Results
} else {
return res, err
}
return res, nil
}
func main() {
kingpin.Version(ver)
kingpin.Parse()
check := nagiosplugin.NewCheck()
defer check.Finish()
if *compareOperator == "gt" {
if *warningThreshold < *criticalThreshold {
check.AddResult(nagiosplugin.UNKNOWN, "warning threshold lower than critical")
return
}
} else if *compareOperator == "lt" {
if *warningThreshold > *criticalThreshold {
check.AddResult(nagiosplugin.UNKNOWN, "warning threshold higher than critical")
return
}
}
if *warningThreshold == *criticalThreshold {
check.AddResult(nagiosplugin.UNKNOWN, "warning and critical thresholds are equal")
return
}
if *compareOperator != "lt" && *compareOperator != "gt" {
check.AddResult(nagiosplugin.UNKNOWN, "compare-operator parameter should be 'lt' or 'gt'")
return
}
url := fmt.Sprintf("http://%s:%s", *host, *port)
c, err := client.NewHTTPClient(client.HTTPConfig{
Addr: url,
Username: *username,
Password: *password,
})
if err != nil {
check.AddResult(nagiosplugin.UNKNOWN, "influxdb connect error")
return
}
res, err := queryDB(c, *db, *query)
if err != nil {
check.AddResult(nagiosplugin.UNKNOWN, "influxdb query failed")
return
}
if len(res[0].Series) < 1 {
check.AddResult(nagiosplugin.UNKNOWN, "influxdb query returns no value")
return
}
if len(res[0].Series) > 1 {
check.AddResult(nagiosplugin.UNKNOWN, "influxdb query returns more that one series")
return
}
if len(res[0].Series[0].Values) > 1 {
check.AddResult(nagiosplugin.UNKNOWN, "influxdb query returns more that one value")
return
}
count := res[0].Series[0].Values[0][1]
var i float64
err = json.Unmarshal([]byte(count.(json.Number)), &i)
if err != nil {
check.AddResult(nagiosplugin.UNKNOWN, "unmarshal error")
return
}
var warn float64 = float64(*warningThreshold)
var crit float64 = float64(*criticalThreshold)
if *compareOperator == "gt" {
if i > warn {
check.AddResult(nagiosplugin.OK, fmt.Sprintf("value %.2f is above thresholds", i))
} else if i < warn && i > crit {
check.AddResult(nagiosplugin.WARNING, fmt.Sprintf("value %.2f is below warning threshold", i))
} else if i < crit {
check.AddResult(nagiosplugin.CRITICAL, fmt.Sprintf("value %.2f is below critical threshold", i))
}
} else if *compareOperator == "lt" {
if i < warn {
check.AddResult(nagiosplugin.OK, fmt.Sprintf("value %.2f is below thresholds", i))
} else if i > warn && i < crit {
check.AddResult(nagiosplugin.WARNING, fmt.Sprintf("value %.2f is above warning threshold", i))
} else if i > crit {
check.AddResult(nagiosplugin.CRITICAL, fmt.Sprintf("value %.2f is above critical threshold", i))
}
}
}