From 3d191bf3550f717fd56954d83e6fb2007e7a19d9 Mon Sep 17 00:00:00 2001 From: Cyberes Date: Thu, 8 Aug 2024 19:38:53 -0600 Subject: [PATCH] add check_home_assistant --- check_home_assistant.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100755 check_home_assistant.py diff --git a/check_home_assistant.py b/check_home_assistant.py new file mode 100755 index 0000000..8ceeee1 --- /dev/null +++ b/check_home_assistant.py @@ -0,0 +1,37 @@ +#!/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') + traceback.print_exc() + sys.exit(nagios.STATE_UNKNOWN) + +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_UNKNOWN)