Skip to content

Instantly share code, notes, and snippets.

@kchristensen
Last active August 31, 2021 23:07
Show Gist options
  • Save kchristensen/1a27741af6c742908141 to your computer and use it in GitHub Desktop.
Save kchristensen/1a27741af6c742908141 to your computer and use it in GitHub Desktop.
Nagios Haproxy Health Check
#!/usr/bin/env python
import sys
import urllib2
if len(sys.argv) != 2:
print('USAGE: {0} <hostname>').format(sys.argv[0])
sys.exit(2)
try:
error = ''
haproxy_url = 'http://' + str(sys.argv[1]) + '/;csv;noreload'
haproxy_status = urllib2.urlopen(
haproxy_url, timeout=10).read().rstrip().split("\n")
if not len(haproxy_status):
error = 'Ecelerity HAProxy status page contains unexpected output'
else:
for row in haproxy_status:
status = row.split(',')
# Rows must have the correct number of fields
if len(status) != 56:
error = 'Incorrect number of fields returned from HAProxy ' + \
'status page'
# A status of OPEN/UP is fine, so is a column header of
# 'status'
elif status[17] not in ['OPEN', 'UP', 'status']:
error = 'Status other than UP ({0}) found for {1}'.format(
status[17], status[1])
except urllib2.HTTPError, e:
error = str(e.reason)
except urllib2.URLError, e:
error = str(e.reason)
if len(error):
print('CRITICAL - ' + error)
sys.exit(2)
else:
print('OK - Ecelerity HAProxy is healthy')
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment