ソースを参照

check_shelly improved + library update

master
Dirk Alders 3ヶ月前
コミット
e0be677806
9個のファイルの変更118行の追加95行の削除
  1. 0
    92
      check_shelly
  2. 1
    0
      check_shelly_filesystem
  3. 1
    0
      check_shelly_memory
  4. 1
    0
      check_shelly_mqtt
  5. 1
    0
      check_shelly_temperature
  6. 111
    0
      check_shelly_wifi
  7. 1
    1
      nagios
  8. 1
    1
      z_server/devdi
  9. 1
    1
      z_server/report

+ 0
- 92
check_shelly ファイルの表示

1
-#!/bin/python3
2
-#
3
-import argparse
4
-import json
5
-import nagios
6
-import os
7
-import urllib.request
8
-
9
-CHECKS = ['wifi', 'mqtt', 'memory', 'filesystem', 'temperature']
10
-#
11
-WIFI_QUALITY_ERROR = -92
12
-WIFI_QUALITY_WARNING = -91
13
-#
14
-RAM_ERROR = .15
15
-RAM_WARNING = .30
16
-#
17
-FS_ERROR = .15
18
-FS_WARNING = .30
19
-#
20
-TMP_WARNING = 55
21
-TMP_ERROR = 60
22
-#
23
-
24
-if __name__ == "__main__":
25
-    parser = argparse.ArgumentParser(
26
-        prog=os.path.basename(__file__),
27
-        description='Check shelly for nagios monitorin',
28
-        # epilog='Text at the bottom of help'
29
-    )
30
-    parser.add_argument('-H', '--hostname', required=True)
31
-    parser.add_argument('-c', '--check', choices=CHECKS, required=True)
32
-    args = parser.parse_args()
33
-    #
34
-    n = nagios.Nagios()
35
-    status = n.UNKNOWN
36
-    #
37
-    with urllib.request.urlopen(f"http://{args.hostname}/status") as response:
38
-        data = json.load(response)
39
-    #
40
-    #
41
-    if args.check == 'wifi':
42
-        connected = data['wifi_sta']['connected']
43
-        quality = data['wifi_sta']['rssi']
44
-        if not connected or quality <= WIFI_QUALITY_ERROR:
45
-            status = n.ERROR
46
-        elif quality <= WIFI_QUALITY_WARNING:
47
-            status = n.WARNING
48
-        else:
49
-            status = n.OK
50
-        n.exit(status, f"connected: {connected} - quality: {quality} > {WIFI_QUALITY_WARNING} > {WIFI_QUALITY_ERROR}")
51
-    elif args.check == 'mqtt':
52
-        connected = data['mqtt']['connected']
53
-        if not connected:
54
-            status = n.ERROR
55
-        else:
56
-            status = n.OK
57
-        n.exit(status, f"connected: {connected}")
58
-    elif args.check == 'memory':
59
-        ram_total = data['ram_total']
60
-        ram_free = data['ram_free']
61
-        ram_left = ram_free / ram_total
62
-        if ram_left <= RAM_ERROR:
63
-            status = n.ERROR
64
-        elif ram_left <= RAM_WARNING:
65
-            status = n.WARNING
66
-        else:
67
-            status = n.OK
68
-        n.exit(status, f"ram_left: {ram_left} ({ram_total}) > {RAM_WARNING} > {RAM_ERROR}")
69
-    elif args.check == 'filesystem':
70
-        fs_size = data['fs_size']
71
-        fs_free = data['fs_free']
72
-        fs_left = fs_free / fs_size
73
-        if fs_left <= FS_ERROR:
74
-            status = n.ERROR
75
-        elif fs_left <= FS_WARNING:
76
-            status = n.WARNING
77
-        else:
78
-            status = n.OK
79
-        n.exit(status, f"fs_left: {fs_left} ({fs_size}) > {FS_WARNING} > {FS_ERROR}")
80
-    elif args.check == 'temperature':
81
-        temperature = data.get('tmp', {}).get('tC')
82
-        if temperature is None:
83
-            if args.hostname.startswith('shelly1-'):
84
-                n.exit(n.OK, "Shelly1 has no temperature information")
85
-            status = n.UNKNOWN
86
-        elif temperature >= TMP_ERROR:
87
-            status = n.ERROR
88
-        elif temperature >= TMP_WARNING:
89
-            status = n.WARNING
90
-        else:
91
-            status = n.OK
92
-        n.exit(status, f"temperature: {temperature} C < {TMP_WARNING} < {TMP_ERROR}")

+ 1
- 0
check_shelly_filesystem ファイルの表示

1
+check_shelly_wifi

+ 1
- 0
check_shelly_memory ファイルの表示

1
+check_shelly_wifi

+ 1
- 0
check_shelly_mqtt ファイルの表示

1
+check_shelly_wifi

+ 1
- 0
check_shelly_temperature ファイルの表示

1
+check_shelly_wifi

+ 111
- 0
check_shelly_wifi ファイルの表示

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)

+ 1
- 1
nagios

1
-Subproject commit 407641b1d5b157ada46694dd81183fb962dc4831
1
+Subproject commit 8726b19639832d3ce0653bb91bd74e2e2a64bc3f

+ 1
- 1
z_server/devdi

1
-Subproject commit 4bda94e4f7e969e51e368ccfae9b25512a717171
1
+Subproject commit 66a471979d41bb6477b676dddd3f5959ba0326cc

+ 1
- 1
z_server/report

1
-Subproject commit b53dd30eae1d679b7eec4999bec50aed55bc105b
1
+Subproject commit 7003c13ef8c7e7c3a55a545cbbad4039cc024a9f

読み込み中…
キャンセル
保存