-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
316 lines (311 loc) · 8.69 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
package main
import (
"github.com/griesbacher/check_influxdb/mode"
"github.com/griesbacher/check_x"
"github.com/urfave/cli"
"os"
"time"
)
var (
path string
address string
username string
password string
timeout int
warning string
critical string
filterRegex string
timerange int
livestatus string
database string
query string
alias string
unknown2ok bool
)
func startTimeout() {
if timeout != 0 {
check_x.StartTimeout(time.Duration(timeout) * time.Second)
}
}
func main() {
app := cli.NewApp()
app.Name = "check_influxdb"
app.Usage = "Checks different influxdb stats\n Copyright (c) 2016 Philip Griesbacher\n https://github.com/Griesbacher/check_influxdb"
app.Version = "0.0.5"
flagAddress := cli.StringFlag{
Name: "address",
Usage: "InfluxDB address: Protocol + IP + Port",
Destination: &address,
Value: "http://localhost:8086",
}
flagUsername := cli.StringFlag{
Name: "username",
Destination: &username,
Value: "root",
}
flagPassword := cli.StringFlag{
Name: "password",
Destination: &password,
Value: "root",
}
flagFilter := cli.StringFlag{
Name: "filter",
Usage: "regex to filter databases",
Destination: &filterRegex,
}
app.Commands = []cli.Command{
{
Name: "mode",
Aliases: []string{"m"},
Usage: "check mode",
Subcommands: []cli.Command{
{
Name: "ping",
Usage: "Tests if the influxdb is alive",
Action: func(c *cli.Context) error {
startTimeout()
return mode.Ping(address, username, password, warning, critical)
},
Flags: []cli.Flag{
flagAddress,
flagUsername,
flagPassword,
cli.StringFlag{
Name: "w",
Usage: "warning: request duration in ms",
Destination: &warning,
Value: "200",
},
cli.StringFlag{
Name: "c",
Usage: "critical: request duration in ms",
Destination: &critical,
Value: "400",
},
},
}, {
Name: "disk",
Usage: "Checks the disk size per databases",
Action: func(c *cli.Context) error {
startTimeout()
return mode.Disk(path, warning, critical, filterRegex)
},
Flags: []cli.Flag{
flagFilter,
cli.StringFlag{
Name: "w",
Usage: "warning: size in bytes",
Destination: &warning,
},
cli.StringFlag{
Name: "c",
Usage: "critical: size in bytes",
Destination: &critical,
},
cli.StringFlag{
Name: "path",
Usage: "path to the influxdb folder",
Destination: &path,
Value: "/var/lib/influxdb",
},
},
}, {
Name: "num_series",
Usage: "The numbers of series/measurements",
Action: func(c *cli.Context) error {
startTimeout()
return mode.NumSeries(address, username, password, warning, critical, filterRegex)
},
Flags: []cli.Flag{
flagAddress,
flagUsername,
flagPassword,
flagFilter,
cli.StringFlag{
Name: "w",
Usage: "warning: measurements,series (only measurements: 10, only series: ,10)",
Destination: &warning,
},
cli.StringFlag{
Name: "c",
Usage: "critical: measurements,series (only measurements: 10, only series: ,10)",
Destination: &critical,
},
},
}, {
Name: "div_series",
Usage: "The diverence of series/measurements between now and x minutes. If a livestatus address is given, the overall state will switch to Warning if a core restart happened and due to that the metric got into Critical.",
Action: func(c *cli.Context) error {
startTimeout()
return mode.DivSeries(address, username, password, warning, critical, filterRegex, livestatus, timerange)
},
Flags: []cli.Flag{
flagAddress,
flagUsername,
flagPassword,
flagFilter,
cli.StringFlag{
Name: "w",
Usage: "warning: measurements,series (only measurements: 10, only series: ,10)",
Destination: &warning,
},
cli.StringFlag{
Name: "c",
Usage: "critical: measurements,series (only measurements: 10, only series: ,10)",
Destination: &critical,
},
cli.IntFlag{
Name: "m",
Usage: "amount of minutes to look back",
Destination: &timerange,
Value: 60,
},
cli.StringFlag{
Name: "l",
Usage: `livestatus address ("unix:/var/lib/nagios/rw/" or "tcp:localhost:6557")`,
Destination: &livestatus,
},
},
}, {
Name: "read_write",
Usage: "Checks the bytes/operations read and written the last x minutes",
Action: func(c *cli.Context) error {
startTimeout()
return mode.ReadWrite(address, username, password, warning, critical, timerange)
},
Flags: []cli.Flag{
flagAddress,
flagUsername,
flagPassword,
cli.StringFlag{
Name: "w",
Usage: "warning: read(Bps),read(Ops),write(Bps),read(Ops) (only read: 10,10 only write: ,,10,10)",
Destination: &warning,
},
cli.StringFlag{
Name: "c",
Usage: "critical: read(Bps),read(Ops),write(Bps),read(Ops) (only read: 10,10 only write: ,,10,10)",
Destination: &critical,
},
cli.IntFlag{
Name: "m",
Usage: "amount of minutes to look back",
Destination: &timerange,
Value: 10,
},
},
}, {
Name: "memory",
Usage: "RSS in Byte",
Action: func(c *cli.Context) error {
startTimeout()
return mode.Memory(address, username, password, warning, critical)
},
Flags: []cli.Flag{
flagAddress,
flagUsername,
flagPassword,
cli.StringFlag{
Name: "w",
Usage: "warning in B",
Destination: &warning,
},
cli.StringFlag{
Name: "c",
Usage: "critical in B",
Destination: &critical,
},
},
}, {
Name: "old_series",
Usage: "Returns a list of series older then x hours. This check makes only sense when the databases hast the tags: hostname and service - it's build for nagflux",
Action: func(c *cli.Context) error {
startTimeout()
return mode.OldSeries(address, database, username, password, warning, critical, timerange)
},
Flags: []cli.Flag{
flagAddress,
flagUsername,
flagPassword,
cli.StringFlag{
Name: "database",
Usage: "Database to use",
Destination: &database,
Value: "nagflux",
},
cli.IntFlag{
Name: "d",
Usage: "amount of hours",
Destination: &timerange,
Value: 24,
},
cli.StringFlag{
Name: "w",
Usage: "warning in hours",
Destination: &warning,
},
cli.StringFlag{
Name: "c",
Usage: "critical in hours",
Destination: &critical,
},
},
}, {
Name: "query",
Usage: "You could check a certain value from the database, but your query has to return only ONE value. Like 'select last(value) from metrics'",
Action: func(c *cli.Context) error {
startTimeout()
return mode.Query(address, database, username, password, warning, critical, query, alias, unknown2ok)
},
Flags: []cli.Flag{
flagAddress,
flagUsername,
flagPassword,
cli.StringFlag{
Name: "database",
Usage: "Database to use",
Destination: &database,
Value: "nagflux",
},
cli.StringFlag{
Name: "q",
Usage: "query to be executed",
Destination: &query,
},
cli.StringFlag{
Name: "w",
Usage: "warning value",
Destination: &warning,
},
cli.StringFlag{
Name: "c",
Usage: "critical value",
Destination: &critical,
},
cli.StringFlag{
Name: "a",
Usage: "alias, will replace the query within the output, if set",
Destination: &alias,
},
cli.BoolFlag{
Name: "unknown2ok",
Usage: "If this flag is set, a query which would return unknown will now return ok (Default: false)",
Destination: &unknown2ok,
},
},
},
},
},
}
app.Flags = []cli.Flag{
cli.IntFlag{
Name: "t",
Usage: "Seconds till check returns unknown, 0 to disable",
Value: 10,
Destination: &timeout,
},
}
if err := app.Run(os.Args); err != nil {
check_x.ErrorExit(err)
}
}