54 lines
1.3 KiB
Python
54 lines
1.3 KiB
Python
|
|
#!/usr/bin/env python
|
||
|
|
import getopt
|
||
|
|
import requests
|
||
|
|
import json
|
||
|
|
import sys
|
||
|
|
|
||
|
|
goodstate = 'UP'
|
||
|
|
icinga_ok = 0
|
||
|
|
icinga_crit = 2
|
||
|
|
icinga_unknown = 3
|
||
|
|
|
||
|
|
try:
|
||
|
|
options, args = getopt.getopt(sys.argv[1:], 'u:t:', ['url=', 'timeout=',])
|
||
|
|
except getopt.GetoptError as e:
|
||
|
|
print e
|
||
|
|
sys.exit(icinga_crit)
|
||
|
|
|
||
|
|
for opt, arg in options:
|
||
|
|
if opt in ('-u', '--url'):
|
||
|
|
url = arg
|
||
|
|
elif opt in ('-t', '--timeout'):
|
||
|
|
timeout_s = int(arg)
|
||
|
|
else:
|
||
|
|
sys.exit(1)
|
||
|
|
|
||
|
|
try:
|
||
|
|
resp = requests.get(url, timeout=timeout_s)
|
||
|
|
except (requests.exceptions.ConnectionError, requests.exceptions.ReadTimeout) as e:
|
||
|
|
print e
|
||
|
|
sys.exit(icinga_crit)
|
||
|
|
|
||
|
|
try:
|
||
|
|
json_resp = json.loads(resp.text)
|
||
|
|
except ValueError as e:
|
||
|
|
print e
|
||
|
|
sys.exit(icinga_crit)
|
||
|
|
|
||
|
|
http_status = resp.status_code
|
||
|
|
state_global = json_resp['status']
|
||
|
|
|
||
|
|
if (state_global == goodstate and
|
||
|
|
http_status == requests.codes.ok):
|
||
|
|
# 'OK' case
|
||
|
|
print 'All is fine (status: ' + state_global + ', HTTP: ' + str(http_status) + ')'
|
||
|
|
sys.exit(icinga_ok)
|
||
|
|
elif (state_global != goodstate or
|
||
|
|
http_status != requests.codes.ok):
|
||
|
|
# 'NOK' case
|
||
|
|
print 'Global state is bad (status: ' + state_global + ', HTTP: ' + str(http_status) + '). Check ' + url
|
||
|
|
sys.exit(icinga_crit)
|
||
|
|
else:
|
||
|
|
# 'unknown' case
|
||
|
|
print 'Undefined state. Check other monitoring tools for evidence'
|
||
|
|
sys.exit(icinga_unknown)
|