|
@@ -0,0 +1,111 @@
|
|
1
|
+#!/bin/python3
|
|
2
|
+#
|
|
3
|
+import argparse
|
|
4
|
+import json
|
|
5
|
+import nagios
|
|
6
|
+import os
|
|
7
|
+import urllib.request
|
|
8
|
+
|
|
9
|
+def get_shelly_data(hostname):
|
|
10
|
+ with urllib.request.urlopen(f"http://{hostname}/status") as response:
|
|
11
|
+ return json.load(response)
|
|
12
|
+
|
|
13
|
+def check_wifi(hostname, warn_lvl, crit_lvl):
|
|
14
|
+ data = get_shelly_data(hostname)
|
|
15
|
+ #
|
|
16
|
+ connected = data['wifi_sta']['connected']
|
|
17
|
+ quality = data['wifi_sta']['rssi']
|
|
18
|
+ if not connected or quality <= crit_lvl:
|
|
19
|
+ status = nagios.Nagios.ERROR
|
|
20
|
+ elif quality <= warn_lvl:
|
|
21
|
+ status = nagios.Nagios.WARNING
|
|
22
|
+ else:
|
|
23
|
+ status = nagios.Nagios.OK
|
|
24
|
+ return (status, f"shelly: connected={connected}; quality={quality} shall be > warn={warn_lvl} / crit={crit_lvl}")
|
|
25
|
+
|
|
26
|
+def check_mqtt(hostname, warn_lvl, crit_lvl):
|
|
27
|
+ data = get_shelly_data(hostname)
|
|
28
|
+ #
|
|
29
|
+ connected = data['mqtt']['connected']
|
|
30
|
+ if not connected:
|
|
31
|
+ status = nagios.Nagios.ERROR
|
|
32
|
+ else:
|
|
33
|
+ status = nagios.Nagios.OK
|
|
34
|
+ return (status, f"shelly: connected={connected}")
|
|
35
|
+
|
|
36
|
+def check_memory(hostname, warn_lvl, crit_lvl):
|
|
37
|
+ data = get_shelly_data(hostname)
|
|
38
|
+ #
|
|
39
|
+ ram_total = data['ram_total']
|
|
40
|
+ ram_free = data['ram_free']
|
|
41
|
+ ram_left = ram_free / ram_total * 100
|
|
42
|
+ if ram_left <= crit_lvl:
|
|
43
|
+ status = nagios.Nagios.ERROR
|
|
44
|
+ elif ram_left <= warn_lvl:
|
|
45
|
+ status = nagios.Nagios.WARNING
|
|
46
|
+ else:
|
|
47
|
+ status = nagios.Nagios.OK
|
|
48
|
+ return (status, f"shelly: ram_left={ram_left:.1f}percent shall be > warn={warn_lvl} / crit={crit_lvl}")
|
|
49
|
+
|
|
50
|
+def check_filesystem(hostname, warn_lvl, crit_lvl):
|
|
51
|
+ data = get_shelly_data(hostname)
|
|
52
|
+ #
|
|
53
|
+ fs_size = data['fs_size']
|
|
54
|
+ fs_free = data['fs_free']
|
|
55
|
+ fs_left = fs_free / fs_size * 100
|
|
56
|
+ if fs_left <= crit_lvl:
|
|
57
|
+ status = nagios.Nagios.ERROR
|
|
58
|
+ elif fs_left <= warn_lvl:
|
|
59
|
+ status = nagios.Nagios.WARNING
|
|
60
|
+ else:
|
|
61
|
+ status = nagios.Nagios.OK
|
|
62
|
+ return (status, f"shelly: fs_left={fs_left:.1f}percent shall be > warn={warn_lvl} / crit={crit_lvl}")
|
|
63
|
+
|
|
64
|
+def check_temperature(hostname, warn_lvl, crit_lvl):
|
|
65
|
+ data = get_shelly_data(hostname)
|
|
66
|
+ #
|
|
67
|
+ temperature = data.get('tmp', {}).get('tC')
|
|
68
|
+ if temperature is None:
|
|
69
|
+ if args.hostname.startswith('shelly1-'):
|
|
70
|
+ return (nagios.Nagios.OK, "shelly: Shelly1 has no temperature information")
|
|
71
|
+ status = nagios.Nagios.UNKNOWN
|
|
72
|
+ elif temperature >= crit_lvl:
|
|
73
|
+ status = nagios.Nagios.ERROR
|
|
74
|
+ elif temperature >= warn_lvl:
|
|
75
|
+ status = nagios.Nagios.WARNING
|
|
76
|
+ else:
|
|
77
|
+ status = nagios.Nagios.OK
|
|
78
|
+ return (status, f"shelly: temperature:={temperature} C shall be < warn={warn_lvl} / crit={crit_lvl}")
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+if __name__ == "__main__":
|
|
82
|
+ # Parse arguments
|
|
83
|
+ parser = argparse.ArgumentParser(
|
|
84
|
+ prog=os.path.basename(__file__),
|
|
85
|
+ description='Check shelly for nagios monitorin',
|
|
86
|
+ )
|
|
87
|
+ parser.add_argument('-H', '--hostname', required=True)
|
|
88
|
+ parser.add_argument('-w', '--warn_lvl', type=float, required=True)
|
|
89
|
+ parser.add_argument('-c', '--critical_lvl', type=float, required=True)
|
|
90
|
+ args = parser.parse_args()
|
|
91
|
+
|
|
92
|
+ # Init nagios instance
|
|
93
|
+ n = nagios.Nagios()
|
|
94
|
+
|
|
95
|
+ # Identify check
|
|
96
|
+ this_check = parser.prog[len("check_shelly_"):]
|
|
97
|
+ check_function = {
|
|
98
|
+ 'wifi': check_wifi,
|
|
99
|
+ 'mqtt': check_mqtt,
|
|
100
|
+ 'memory': check_memory,
|
|
101
|
+ 'filesystem': check_filesystem,
|
|
102
|
+ 'temperature': check_temperature
|
|
103
|
+ }.get(this_check)
|
|
104
|
+
|
|
105
|
+ # process check and return feedback
|
|
106
|
+ if check_function is None:
|
|
107
|
+ status = n.UNKNOWN
|
|
108
|
+ message = f"Unknown check - {this_check}"
|
|
109
|
+ else:
|
|
110
|
+ status, message = check_function(args.hostname, args.warn_lvl, args.critical_lvl)
|
|
111
|
+ n.exit(status, message)
|