nagios-plugins/check_shelly

41 lines
1.1 KiB
Python
Executable File

#!/bin/python3
#
import argparse
import json
import nagios
import os
import urllib.request
CHECKS = ['wifi']
#
WIFI_QUALITY_ERROR = -30
WIFI_QUALITY_WARNING = -50
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog=os.path.basename(__file__),
description='Check shelly for nagios monitorin',
# epilog='Text at the bottom of help'
)
parser.add_argument('-H', '--hostname', required=True)
parser.add_argument('-c', '--check', choices=CHECKS, required=True)
args = parser.parse_args()
#
n = nagios.Nagios()
status = n.UNKNOWN
#
with urllib.request.urlopen(f"http://{args.hostname}/status") as response:
data = json.load(response)
#
#
if args.check == 'wifi':
connected = data['wifi_sta']['connected']
quality = data['wifi_sta']['rssi']
if not connected or quality > WIFI_QUALITY_ERROR:
status = n.ERROR
elif quality > WIFI_QUALITY_WARNING:
status = n.WARNING
else:
status = n.OK
n.exit(status, f"connected: {connected} - quality: {quality}")