icinga2-checks/check_home_assistant.py

38 lines
1.3 KiB
Python
Executable File

#!/usr/bin/python3
import argparse
import sys
import traceback
import requests
from checker import nagios
# https://github.com/LukasK13/check_home-assistant/blob/master/check_home-assistant.py
parser = argparse.ArgumentParser(prog='check_home-assistant.py', description='Check Home-Assistant API for state')
parser.add_argument('-u', '--url', dest='url', help='URL to the Home-Assistant API.')
parser.add_argument('-t', '--token', dest='token', metavar='Access-Token', help='Sccess token for Home-Assistant.', required=True)
parser.add_argument('-i', '--insecure', dest='verify', default=True, action='store_false', help="Insecure connection. Don't verify the SSL certificate.")
args = parser.parse_args()
headers = {'Authorization': 'Bearer ' + args.token, 'content-type': 'application/json'}
try:
res = requests.get(args.url.strip('/') + '/api/', headers=headers, verify=args.verify)
state_mes = res.json()
state = state_mes['message']
except:
print('Unknown: failed to fetch data')
print(traceback.format_exc())
sys.exit(nagios.STATE_CRIT)
if res.status_code == 200:
if state == 'API running.':
print('OK: API running')
else:
print(f'Critical: API is {state}')
sys.exit(nagios.STATE_CRIT)
else:
print(f'Unknown: Request returned status code {str(res.status_code)}')
sys.exit(nagios.STATE_CRIT)