check_shelly improved + library update
This commit is contained in:
parent
af8a1ae836
commit
e0be677806
92
check_shelly
92
check_shelly
@ -1,92 +0,0 @@
|
||||
#!/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}")
|
1
check_shelly_filesystem
Symbolic link
1
check_shelly_filesystem
Symbolic link
@ -0,0 +1 @@
|
||||
check_shelly_wifi
|
1
check_shelly_memory
Symbolic link
1
check_shelly_memory
Symbolic link
@ -0,0 +1 @@
|
||||
check_shelly_wifi
|
1
check_shelly_mqtt
Symbolic link
1
check_shelly_mqtt
Symbolic link
@ -0,0 +1 @@
|
||||
check_shelly_wifi
|
1
check_shelly_temperature
Symbolic link
1
check_shelly_temperature
Symbolic link
@ -0,0 +1 @@
|
||||
check_shelly_wifi
|
111
check_shelly_wifi
Executable file
111
check_shelly_wifi
Executable file
@ -0,0 +1,111 @@
|
||||
#!/bin/python3
|
||||
#
|
||||
import argparse
|
||||
import json
|
||||
import nagios
|
||||
import os
|
||||
import urllib.request
|
||||
|
||||
def get_shelly_data(hostname):
|
||||
with urllib.request.urlopen(f"http://{hostname}/status") as response:
|
||||
return json.load(response)
|
||||
|
||||
def check_wifi(hostname, warn_lvl, crit_lvl):
|
||||
data = get_shelly_data(hostname)
|
||||
#
|
||||
connected = data['wifi_sta']['connected']
|
||||
quality = data['wifi_sta']['rssi']
|
||||
if not connected or quality <= crit_lvl:
|
||||
status = nagios.Nagios.ERROR
|
||||
elif quality <= warn_lvl:
|
||||
status = nagios.Nagios.WARNING
|
||||
else:
|
||||
status = nagios.Nagios.OK
|
||||
return (status, f"shelly: connected={connected}; quality={quality} shall be > warn={warn_lvl} / crit={crit_lvl}")
|
||||
|
||||
def check_mqtt(hostname, warn_lvl, crit_lvl):
|
||||
data = get_shelly_data(hostname)
|
||||
#
|
||||
connected = data['mqtt']['connected']
|
||||
if not connected:
|
||||
status = nagios.Nagios.ERROR
|
||||
else:
|
||||
status = nagios.Nagios.OK
|
||||
return (status, f"shelly: connected={connected}")
|
||||
|
||||
def check_memory(hostname, warn_lvl, crit_lvl):
|
||||
data = get_shelly_data(hostname)
|
||||
#
|
||||
ram_total = data['ram_total']
|
||||
ram_free = data['ram_free']
|
||||
ram_left = ram_free / ram_total * 100
|
||||
if ram_left <= crit_lvl:
|
||||
status = nagios.Nagios.ERROR
|
||||
elif ram_left <= warn_lvl:
|
||||
status = nagios.Nagios.WARNING
|
||||
else:
|
||||
status = nagios.Nagios.OK
|
||||
return (status, f"shelly: ram_left={ram_left:.1f}percent shall be > warn={warn_lvl} / crit={crit_lvl}")
|
||||
|
||||
def check_filesystem(hostname, warn_lvl, crit_lvl):
|
||||
data = get_shelly_data(hostname)
|
||||
#
|
||||
fs_size = data['fs_size']
|
||||
fs_free = data['fs_free']
|
||||
fs_left = fs_free / fs_size * 100
|
||||
if fs_left <= crit_lvl:
|
||||
status = nagios.Nagios.ERROR
|
||||
elif fs_left <= warn_lvl:
|
||||
status = nagios.Nagios.WARNING
|
||||
else:
|
||||
status = nagios.Nagios.OK
|
||||
return (status, f"shelly: fs_left={fs_left:.1f}percent shall be > warn={warn_lvl} / crit={crit_lvl}")
|
||||
|
||||
def check_temperature(hostname, warn_lvl, crit_lvl):
|
||||
data = get_shelly_data(hostname)
|
||||
#
|
||||
temperature = data.get('tmp', {}).get('tC')
|
||||
if temperature is None:
|
||||
if args.hostname.startswith('shelly1-'):
|
||||
return (nagios.Nagios.OK, "shelly: Shelly1 has no temperature information")
|
||||
status = nagios.Nagios.UNKNOWN
|
||||
elif temperature >= crit_lvl:
|
||||
status = nagios.Nagios.ERROR
|
||||
elif temperature >= warn_lvl:
|
||||
status = nagios.Nagios.WARNING
|
||||
else:
|
||||
status = nagios.Nagios.OK
|
||||
return (status, f"shelly: temperature:={temperature} C shall be < warn={warn_lvl} / crit={crit_lvl}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Parse arguments
|
||||
parser = argparse.ArgumentParser(
|
||||
prog=os.path.basename(__file__),
|
||||
description='Check shelly for nagios monitorin',
|
||||
)
|
||||
parser.add_argument('-H', '--hostname', required=True)
|
||||
parser.add_argument('-w', '--warn_lvl', type=float, required=True)
|
||||
parser.add_argument('-c', '--critical_lvl', type=float, required=True)
|
||||
args = parser.parse_args()
|
||||
|
||||
# Init nagios instance
|
||||
n = nagios.Nagios()
|
||||
|
||||
# Identify check
|
||||
this_check = parser.prog[len("check_shelly_"):]
|
||||
check_function = {
|
||||
'wifi': check_wifi,
|
||||
'mqtt': check_mqtt,
|
||||
'memory': check_memory,
|
||||
'filesystem': check_filesystem,
|
||||
'temperature': check_temperature
|
||||
}.get(this_check)
|
||||
|
||||
# process check and return feedback
|
||||
if check_function is None:
|
||||
status = n.UNKNOWN
|
||||
message = f"Unknown check - {this_check}"
|
||||
else:
|
||||
status, message = check_function(args.hostname, args.warn_lvl, args.critical_lvl)
|
||||
n.exit(status, message)
|
2
nagios
2
nagios
@ -1 +1 @@
|
||||
Subproject commit 407641b1d5b157ada46694dd81183fb962dc4831
|
||||
Subproject commit 8726b19639832d3ce0653bb91bd74e2e2a64bc3f
|
@ -1 +1 @@
|
||||
Subproject commit 4bda94e4f7e969e51e368ccfae9b25512a717171
|
||||
Subproject commit 66a471979d41bb6477b676dddd3f5959ba0326cc
|
@ -1 +1 @@
|
||||
Subproject commit b53dd30eae1d679b7eec4999bec50aed55bc105b
|
||||
Subproject commit 7003c13ef8c7e7c3a55a545cbbad4039cc024a9f
|
Loading…
x
Reference in New Issue
Block a user