-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_haproxy.py
419 lines (319 loc) · 14.1 KB
/
check_haproxy.py
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
#!/usr/bin/env python3
"""
###############################################################################
# check_haproxy.py
# Icinga/Nagios plugin that checks the metrics of a HAProxy load balancer
#
# Author : Mauno Erhardt <[email protected]>
# Copyright : (c) 2022 Burkert Fluid Control Systems
# Source : https://github.com/m-erhardt/check-haproxy
# License : GPLv3 (http://www.gnu.org/licenses/gpl-3.0.txt)
#
###############################################################################
"""
import sys
import socket
import time
from argparse import ArgumentParser, Namespace as Arguments
class HaproxyFrontend:
""" Class for haproxy frontend object """
# pylint: disable=too-few-public-methods
def __init__(self):
self.name: str = None
self.state: str = None
self.sessions: int = None
self.sessionlimit: int = None
self.bytein: int = None
self.byteout: int = None
class HaproxyBackend:
""" Class for haproxy backend object """
# pylint: disable=too-few-public-methods
def __init__(self):
self.name: str = None
self.state: str = None
self.sessions: int = None
self.sessionlimit: int = None
self.bytein: int = None
self.byteout: int = None
class HaproxyServer:
""" Class for haproxy server object """
# pylint: disable=too-few-public-methods,too-many-instance-attributes
def __init__(self):
self.name: str = None
self.backend: str = None
self.state: str = None
self.sessions: int = None
self.sessionlimit: int = None
self.sessionstotal: int = None
self.queue: int = None
self.bytein: int = None
self.byteout: int = None
def get_args():
""" Parse Arguments """
parser = ArgumentParser(description="Icinga/Nagios plugin which checks a haproxy load balancer")
parser.add_argument("--socketfile", required=False,
help="Location of haproxy stats socket file",
type=str, dest='socketfile',
default="/var/lib/haproxy/stats")
parser.add_argument("--mode", required=False, type=str, dest="mode",
default="instance", choices=["instance", "frontend"],
help="Plugin mode")
thresholds = parser.add_argument_group('Thresholds')
thresholds.add_argument("--slimwarn", required=False,
help="Exit WARN if sessions reach <slimwarn>%% of session limit",
type=int, dest='slim_warn', default=80)
thresholds.add_argument("--slimcrit", required=False,
help="Exit CRIT if sessions reach <slimcrit>%% of session limit",
type=int, dest='slim_crit', default=90)
modeargs = parser.add_argument_group('Mode-specific arguments')
modeargs.add_argument("--frontend", required=False, default=None,
help="Name of frontend to check (only with \"--mode frontend\")",
type=str, dest="frontend")
args = parser.parse_args()
# Validate arguments
if args.frontend is not None and args.mode != "frontend":
exit_plugin(3, '--frontend only works with --mode frontend', '')
if (args.slim_warn is not None
and args.slim_crit is not None
and args.slim_warn > args.slim_crit):
exit_plugin(3, '--slimcrit must be higher than --slimwarn', '')
return args
def exit_plugin(returncode: int, output: str, perfdata: str):
""" Check status and exit accordingly """
if returncode == 3:
print("UNKNOWN - " + str(output))
sys.exit(3)
if returncode == 2:
print("CRITICAL - " + str(output) + str(perfdata))
sys.exit(2)
if returncode == 1:
print("WARNING - " + str(output) + str(perfdata))
sys.exit(1)
elif returncode == 0:
print("OK - " + str(output) + str(perfdata))
sys.exit(0)
def set_state(newstate: int, state: int):
""" Set return state of plugin """
if (newstate == 2) or (state == 2):
returnstate = 2
elif (newstate == 1) and (state not in [2]):
returnstate = 1
elif (newstate == 3) and (state not in [1, 2]):
returnstate = 3
else:
returnstate = 0
return returnstate
def haproxy_cmd(cmd: str, socketfile: str):
""" send cmd to haproxy socket and return reply as str """
try:
# Open connection to haproxy socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect(socketfile)
# Send command
sock.sendall(cmd.encode("ascii"))
time.sleep(0.1)
# Shut down sending
sock.shutdown(socket.SHUT_WR)
# Create buffer for receiving
buf = ""
while True:
# Write reply to buffer in chunks of 1024 bytes
data = sock.recv(1024)
if not data:
break
buf += data.decode()
# Close socket connection
sock.close()
except FileNotFoundError:
exit_plugin(3, f'Socket file { socketfile } not found!', "")
except PermissionError:
exit_plugin(3, f'Access to socket file { socketfile } denied!', "")
except TimeoutError:
exit_plugin(3, f'Connection to socket { socketfile } timed out!', "")
except ConnectionError as err:
exit_plugin(3, f'Error during socket connection: { err }', "")
return buf
def get_haproxy_stats(socketfile: str):
""" Execute haproxy "show stat" command and return reply as dict """
resp = haproxy_cmd("show stat \n ", socketfile)
# Initialize return object
stats = {}
# Extract column names from first line
stats["columns"] = resp.splitlines()[0].split(",")
# Initiate list for rows
stats["values"] = []
# Write values to dict
for line in resp.splitlines()[1:]:
if line != "":
stats["values"].append(line.split(","))
# Initiate lists for objects of type HaproxyFrontend, HaproxyBackend and HaproxyServer
frontends = []
backends = []
servers = []
# Get column numbers of values
col_nr = {}
for item in ['# pxname', 'svname', 'status', 'scur', 'slim', 'stot', 'qcur', 'bin', 'bout']:
col_nr[item] = stats["columns"].index(item)
# Loop through returned rows
for row in stats["values"]:
if row[col_nr['svname']] == "FRONTEND":
obj = HaproxyFrontend()
obj.name = row[col_nr['# pxname']]
obj.state = row[col_nr['status']]
obj.sessions = int(row[col_nr['scur']])
obj.bytein = int(row[col_nr['bin']])
obj.byteout = int(row[col_nr['bout']])
try:
obj.sessionlimit = int(row[col_nr['slim']])
except ValueError:
obj.sessionlimit = None
frontends.append(obj)
elif row[col_nr['svname']] == "BACKEND":
obj = HaproxyBackend()
obj.name = row[col_nr['# pxname']]
obj.state = row[col_nr['status']]
obj.sessions = int(row[col_nr['scur']])
obj.bytein = int(row[col_nr['bin']])
obj.byteout = int(row[col_nr['bout']])
try:
obj.sessionlimit = int(row[col_nr['slim']])
except ValueError:
obj.sessionlimit = None
backends.append(obj)
else:
obj = HaproxyServer()
obj.name = row[col_nr['svname']]
obj.state = row[col_nr['status']]
obj.sessions = int(row[col_nr['scur']])
obj.bytein = int(row[col_nr['bin']])
obj.byteout = int(row[col_nr['bout']])
try:
obj.sessionlimit = int(row[col_nr['slim']])
except ValueError:
obj.sessionlimit = None
obj.sessionstotal = int(row[col_nr['stot']])
obj.queue = int(row[col_nr['qcur']])
servers.append(obj)
return frontends, backends, servers
def check_instance(frontends: list, backends: list, servers: list, args: Arguments):
""" Check HAproxy instance """
# pylint: disable=too-many-locals,too-many-branches,too-many-statements
# Initialize state and output string
output = ""
state = 0
output += (f'haproxy running with { len(frontends) } frontends, { len(backends) } backends, '
f'{ len(servers) } servers ')
# Calculate total sessions
sessions = {'server_current': 0, 'server_total': 0, 'frontend_current': 0, 'backend_current': 0}
queues = {'server_current': 0}
serverstates = ""
# Loop through server objects
for server in servers:
sessions["server_current"] += server.sessions
sessions["server_total"] += server.sessionstotal
queues["server_current"] += server.queue
if server.state != "UP":
serverstates += f'Warn: server { server.name } is { server.state }, '
state = set_state(1, state)
if server.sessionlimit is not None:
# Calculate session thresholds for server
wthres = server.sessionlimit * (args.slim_warn / 100)
cthres = server.sessionlimit * (args.slim_crit / 100)
if (server.sessions >= wthres) and (server.sessions < cthres):
serverstates += (f'Warn: server { server.name } is using { server.sessions }/'
f'{ server.sessionlimit } sessions, ')
state = set_state(1, state)
if server.sessions >= cthres:
serverstates += (f'Crit: server { server.name } is using { server.sessions }/'
f'{ server.sessionlimit } sessions, ')
state = set_state(2, state)
output += f'and { sessions["server_current"] } sessions - { serverstates }'
# Loop through frontend objects
frontendstates = ""
for frontend in frontends:
sessions["frontend_current"] += frontend.sessions
if frontend.state != "OPEN":
frontendstates += f'Warn: frontend { frontend.name } is { frontend.state }, '
state = set_state(1, state)
if frontend.sessionlimit is not None:
wthres = frontend.sessionlimit * (args.slim_warn / 100)
cthres = frontend.sessionlimit * (args.slim_crit / 100)
if (frontend.sessions >= wthres) and (frontend.sessions < cthres):
frontendstates += (f'Warn: frontend { frontend.name } is using '
f'{ frontend.sessions }/{ frontend.sessionlimit } sessions, ')
state = set_state(1, state)
if frontend.sessions >= cthres:
frontendstates += (f'Crit: frontend { frontend.name } is using '
f'{ frontend.sessions }/{ frontend.sessionlimit } sessions, ')
state = set_state(2, state)
output += f'{ frontendstates }'
# Loop through backend objects
backendstates = ""
for backend in backends:
sessions["backend_current"] += backend.sessions
if backend.state != "UP":
backendstates += f'Warn: backend { backend.name } is { backend.state }, '
state = set_state(1, state)
if backend.sessionlimit is not None:
wthres = backend.sessionlimit * (args.slim_warn / 100)
cthres = backend.sessionlimit * (args.slim_crit / 100)
if (backend.sessions >= wthres) and (backend.sessions < cthres):
backendstates += (f'Warn: backend { backend.name } is using { backend.sessions }/'
f'{ backend.sessionlimit } sessions, ')
state = set_state(1, state)
if backend.sessions >= cthres:
backendstates += (f'Crit: backend { backend.name } is using { backend.sessions }/'
f'{ backend.sessionlimit } sessions, ')
state = set_state(2, state)
output += f'{ backendstates }'
perfdata = (f' | \'sessions\'={ sessions["server_current"] };;;; '
f'\'sessions_total\'={ sessions["server_total"] };;;; '
f'\'frontends\'={ len(frontends) };;;; '
f'\'backends\'={ len(backends) };;;; '
f'\'servers\'={ len(servers) };;;; ')
exit_plugin(state, output, perfdata)
def check_frontend(frontends: list, args: Arguments):
""" Check single HAproxy frontend """
frontend = None
# Extract only the frontend we want to check
for item in frontends:
if item.name == args.frontend:
frontend = item
break
if frontend is None:
exit_plugin(3, f'Unable to find frontend { args.frontend }', '')
# Calculate absolute WARN and CRIT thresholds for frontend
if frontend.sessionlimit is not None:
wthres = frontend.sessionlimit * (args.slim_warn / 100)
cthres = frontend.sessionlimit * (args.slim_crit / 100)
perfdata = (f' | \'sessions\'={ frontend.sessions }'
f';{ wthres or "" };{ cthres or "" };0;{ frontend.sessionlimit or "" } '
f'\'bytein\'={ frontend.bytein }B;;;; '
f'\'byteout\'={ frontend.byteout }B;;;;')
output = (f'HAProxy frontend { frontend.name } is { frontend.state }, '
f'Sessions: { frontend.sessions }/{ frontend.sessionlimit or "-" }')
if frontend.state != "OPEN":
# Frontend is not OPEN, exit critical
exit_plugin(2, output, perfdata)
elif frontend.sessions >= cthres:
# Frontend sesions above CRIT threshold
exit_plugin(2, output, perfdata)
elif frontend.sessions >= wthres:
# Frontend sesions above WARN threshold
exit_plugin(1, output, perfdata)
else:
# Everything OK
exit_plugin(0, output, perfdata)
def main():
""" Main program code """
# Get Arguments
args = get_args()
frontends, backends, servers = get_haproxy_stats(args.socketfile)
if args.mode == "frontend":
check_frontend(frontends, args)
elif args.mode == "instance":
check_instance(frontends, backends, servers, args)
else:
exit_plugin(3, 'Unknown plugin mode', '')
if __name__ == "__main__":
main()