-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_haproxy_conns.py
More file actions
40 lines (31 loc) · 1.7 KB
/
check_haproxy_conns.py
File metadata and controls
40 lines (31 loc) · 1.7 KB
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
#!/usr/bin/env python3.6
import subprocess
import argparse
def check_haproxy_conns(url, user, password, warning, critical):
curl_command = f'curl -s -u {user}:{password} -H "Accept: application/json" {url}/stats | grep conns'
try:
result = subprocess.run(curl_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = result.stdout.decode('utf-8')
current_conns = int(output.split('=')[1].split(';')[0].strip())
perfdata = f"'current_conns'={current_conns};{warning};{critical}"
if current_conns >= critical:
print(f'CRITICAL - Current connections: {current_conns} | {perfdata}')
exit(2)
elif current_conns >= warning:
print(f'WARNING - Current connections: {current_conns} | {perfdata}')
exit(1)
else:
print(f'OK - Current connections: {current_conns} | {perfdata}')
exit(0)
except subprocess.CalledProcessError as e:
print(f'UNKNOWN - Error executing command: {e}')
exit(3)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Check HAProxy current connections.')
parser.add_argument('-U', '--url', help='HAProxy URL')
parser.add_argument('-u', '--user', help='HAProxy username')
parser.add_argument('-p', '--password', help='HAProxy password')
parser.add_argument('-w', '--warning', type=int, help='Warning threshold for current connections')
parser.add_argument('-c', '--critical', type=int, help='Critical threshold for current connections')
args = parser.parse_args()
check_haproxy_conns(args.url, args.user, args.password, args.warning, args.critical)