123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- #!/bin/python3
- #
- import argparse
- import json
- import nagios
- import os
- import urllib.request
-
- CHECKS = ['wifi', 'mqtt', 'memory', 'filesystem']
- #
- WIFI_QUALITY_ERROR = -30
- WIFI_QUALITY_WARNING = -50
- #
- RAM_ERROR = .15
- RAM_WARNING = .30
- #
- FS_ERROR = .15
- FS_WARNING = .30
-
- 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}")
|