1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- #!/bin/python3
- #
- import argparse
- import json
- import nagios
- import os
- import urllib.request
-
- CHECKS = ['wifi', 'mqtt', 'memory', 'filesystem', 'temperature']
- #
- WIFI_QUALITY_ERROR = -92
- WIFI_QUALITY_WARNING = -91
- #
- RAM_ERROR = .15
- RAM_WARNING = .30
- #
- FS_ERROR = .15
- FS_WARNING = .30
- #
- TMP_WARNING = 55
- TMP_ERROR = 60
- #
-
- 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} > {WIFI_QUALITY_WARNING} > {WIFI_QUALITY_ERROR}")
- elif args.check == 'mqtt':
- connected = data['mqtt']['connected']
- if not connected:
- status = n.ERROR
- else:
- status = n.OK
- n.exit(status, f"connected: {connected}")
- elif args.check == 'memory':
- ram_total = data['ram_total']
- ram_free = data['ram_free']
- ram_left = ram_free / ram_total
- if ram_left <= RAM_ERROR:
- status = n.ERROR
- elif ram_left <= RAM_WARNING:
- status = n.WARNING
- else:
- status = n.OK
- n.exit(status, f"ram_left: {ram_left} ({ram_total}) > {RAM_WARNING} > {RAM_ERROR}")
- elif args.check == 'filesystem':
- fs_size = data['fs_size']
- fs_free = data['fs_free']
- fs_left = fs_free / fs_size
- if fs_left <= FS_ERROR:
- status = n.ERROR
- elif fs_left <= FS_WARNING:
- status = n.WARNING
- else:
- status = n.OK
- n.exit(status, f"fs_left: {fs_left} ({fs_size}) > {FS_WARNING} > {FS_ERROR}")
- elif args.check == 'temperature':
- temperature = data.get('tmp', {}).get('tC')
- if temperature is None:
- if args.hostname.startswith('shelly1-'):
- n.exit(n.OK, "Shelly1 has no temperature information")
- status = n.UNKNOWN
- elif temperature >= TMP_ERROR:
- status = n.ERROR
- elif temperature >= TMP_WARNING:
- status = n.WARNING
- else:
- status = n.OK
- n.exit(status, f"temperature: {temperature}°C < {TMP_WARNING} < {TMP_ERROR}")
|