[nagios] port haproxy connections script to py3

This commit is contained in:
Mark O'Brien 2020-09-25 12:08:43 +01:00 committed by mobrien
parent 0bf1f7ed0d
commit 95eb7c75d3

View file

@ -1,4 +1,4 @@
#!/usr/bin/python2 #!/usr/bin/python3
""" Nagios check for haproxy over-subscription. """ Nagios check for haproxy over-subscription.
fedmsg-gateway is the primary concern as it can eat up a ton of simultaneous fedmsg-gateway is the primary concern as it can eat up a ton of simultaneous
@ -22,28 +22,28 @@ def _numeric(value):
return value return value
def query(sockname="/var/run/haproxy-stat"): def query(sockname):
""" Read stats from the haproxy socket and return a dict """ """ Read stats from the haproxy socket and return a dict """
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect("/var/run/haproxy-stat") sock.connect(sockname)
s.send('show info\n') send_string = 'show info\n'
sock.send(send_string.encode())
try: try:
response = s.recv(2048).strip() response = sock.recv(2048).strip().decode()
lines = response.split('\n') lines = response.split('\n')
data = dict([map(str.strip, line.split(':')) for line in lines]) data = dict(line.split(':') for line in lines)
data = dict([(k, _numeric(v)) for k, v in data.items()]) data = {k:_numeric(v) for (k, v) in data.items()}
return data return data
except Exception, e: except Exception as err:
print str(e) print(str(err))
finally: finally:
s.close() sock.close()
return None return None
def nagios_check(data): def nagios_check(data):
""" Print warnings and return nagios exit codes. """ """ Print warnings and return nagios exit codes. """
current = data['CurrConns'] current = data['CurrConns']
maxconn = data['Maxconn'] maxconn = data['Maxconn']
percent = 100 * float(current) / float(maxconn) percent = 100 * float(current) / float(maxconn)
@ -52,25 +52,25 @@ def nagios_check(data):
) )
if percent < 50: if percent < 50:
print "HAPROXY SUBS OK: " + details print("HAPROXY SUBS OK: " + details)
return 0 return 0
if percent < 75: if percent < 75:
print "HAPROXY SUBS WARN: " + details print("HAPROXY SUBS WARN: " + details)
return 1 return 1
if percent <= 100: if percent <= 100:
print "HAPROXY SUBS CRIT: " + details print("HAPROXY SUBS CRIT: " + details)
return 2 return 2
print "HAPROXY SUBS UNKNOWN: " + details print("HAPROXY SUBS UNKNOWN: " + details)
return 3 return 3
if __name__ == '__main__': if __name__ == '__main__':
try: try:
data = query(sockname="/var/run/haproxy-stat") data = query(sockname="/var/run/haproxy-stat")
except Exception as e: except Exception as err:
print "HAPROXY SUBS UNKNOWN: " + str(e) print("HAPROXY SUBS UNKNOWN: " + str(err))
sys.exit(3) sys.exit(3)
sys.exit(nagios_check(data)) sys.exit(nagios_check(data))