-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck-raid.py
524 lines (450 loc) · 17.2 KB
/
check-raid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#!/usr/bin/env python3
import json
import socket
import subprocess
import hashlib
import copy
import paho.mqtt.client as mqtt
import time
import psutil
import argparse
import os
import sys
import atexit
import shutil
parser = argparse.ArgumentParser(description="Check Raid Utility")
parser.add_argument("-v", "--verbose", help="verbose output", action="store_true", default=False)
parser.add_argument("-c", "--config", help="configuration file", default="config.json")
parser.add_argument("-i", "--interval", help="interval to update the raid status in seconds", type=int, default=900)
parser.add_argument("-p", "--print", help="print configuration and exit", action="store_true", default=False)
args = parser.parse_args()
VERBOSE = args.verbose
def verbose(msg):
if VERBOSE:
print(msg)
def error(msg, e=None):
print(msg, file=sys.stderr)
if e:
print('Exception: %s' % e, file=sys.stderr)
exit(1)
def has_mqtt_username(username):
return username!=None and username!=''
def get_mqtt_username(username):
if has_mqtt_username(username):
return username
return 'Anonymous'
def get_mqtt_password(password):
if isinstance(password, str):
return len(password) * '*'
return '<None>'
def get_mqtt_account(config):
if has_mqtt_username(config['username']):
return '%s:%s' % (config['username'], get_mqtt_password(config['password']))
return 'Anonymous'
device_name = None
state = {
'connected': False,
'current_delay': None,
'resend_online_status': False
}
client = None
config_file = os.path.abspath(args.config);
config = {
'sys': {},
'hass': {},
'mqtt': {},
'devices': None,
'info': {}
}
try:
with open(config_file) as file:
config = dict(config | json.loads(file.read()))
except Exception as e:
error('Failed to read configuration: %s' % config_file, e)
# get mdadm command line args
def mdadm_cmd(args):
if config['sys']['sudo']:
return [config['sys']['sudo'], config['sys']['mdadm_bin']] + args
else:
return [config['sys']['mdadm_bin']] + args
# validate configuration
if 'default_config' in config:
del config['default_config']
if 'info' in config:
del config['info']
# default config for devices
default_device_config = {
'display_unit': 'TB',
'display_decimal_places': 2
}
# default settings
config_defaults = {
'sys': {
'device_name': '$HOSTNAME',
'mdadm_bin': 'mdadm',
'sudo': False,
'interval': args.interval
},
'hass': {
'autoconf_topic': 'homeassistant',
'base_topic': 'home',
'availability_topic': '${base_topic}/${device_name}-check-raid/status'
},
'mqtt': {
'host': None,
'port': 1883,
'qos': 2,
'keepalive': 60,
'version': mqtt.MQTTv311,
'transport': 'tcp',
'username': None,
'password': None,
'status': [ 'OFF', 'ON' ],
'icon': 'mdi:harddisk'
},
'info': {
'mdadm_version': None,
'os_version': None
}
};
# merge default settings per key
for (key, val) in config_defaults.items():
if key not in config:
config[key] = val
else:
config[key] = dict(val | config[key]);
# resolve device name
if config['sys']['device_name']=='$HOSTNAME':
config['sys']['device_name'] = socket.gethostname()
device_name = config['sys']['device_name']
# check if any devices have been configured
if not isinstance(config['devices'], list) or len(config['devices'])==0:
error('No devices configured')
# apply config defaults per device and normalize values
idx = 0
for device in config['devices']:
tmp = dict(default_device_config | device)
if not 'raid_device' in tmp:
raise Exception('raid_device missing for device #%u' % idx)
if not 'mount_point' in tmp:
raise Exception('mount_point missing for device #%u' % idx)
try:
psutil.disk_usage(tmp['mount_point'])
except Exception as e:
error('Cannot find mount point: %s' % tmp['mount_point'], e)
unit = tmp['display_unit'].upper()
if unit=='B':
multiplier = 1
elif unit=='KB':
multiplier = 1024
elif unit=='MB':
multiplier = 1024 << 10
elif unit=='GB':
multiplier = 1024 << 20
elif unit=='TB':
multiplier = 1024 << 30
else:
raise Exception('Invalid unit %s for device %s' % (unit, tmp['raid_device']))
multiplier = 1 / multiplier
tmp['display_unit'] = unit
tmp['multiplier'] = multiplier
tmp['display_decimal_places'] = max(min(int(tmp['display_decimal_places']), 4), 0) # allow 0-4 decimal places
config['devices'][idx] = tmp
idx += 1
# normalize qos
qos = int(config['mqtt']['qos'])
if not qos in(0, 1, 2):
error('Invalid QoS value: %d' % qos)
config['mqtt']['qos'] = qos
# replace variables
s = config['hass']['availability_topic'];
s = s.replace('${base_topic}', config['hass']['base_topic'])
s = s.replace('${device_name}', config['sys']['device_name'])
config['hass']['availability_topic'] = s
# keepalive 5-3600 seconds
config['mqtt']['keepalive'] = min(3600, max(5, int(config['mqtt']['keepalive'])))
# normalize port
config['mqtt']['port'] = int(config['mqtt']['port'])
# update interval
config['sys']['interval'] = max(30, int(config['sys']['interval']))
# find binary if no absolute path was given
if not os.path.isabs(config['sys']['mdadm_bin']):
config['sys']['mdadm_bin'] = shutil.which(config['sys']['mdadm_bin'])
if config['sys']['sudo']:
config['sys']['sudo'] = shutil.which('sudo')
# mdadm version
res = subprocess.run(mdadm_cmd(['-V']), capture_output=True)
mdadm_version = res.stderr.decode().strip()
config['info']['mdadm_version'] = mdadm_version
# os version
res = subprocess.run(['uname', '-r', '-o', '-s', '-m'], capture_output=True)
os_version = res.stdout.decode().strip()
config['info']['os_version'] = os_version
# print config and exit
if args.print:
print('Configuration:')
try:
import pprint
pprint.pprint(config, indent=1, sort_dicts=False)
except:
print(config)
exit(0)
# config helpers
def get_online_status():
return config['mqtt']['status'][1]
def get_offline_status():
return config['mqtt']['status'][0]
def get_qos_value():
return config['mqtt']['qos']
# home assistant config
extra_info = {
"model": "mdadm",
"sw_version": mdadm_version,
"manufacturer": os_version
}
topics = {
"avty_t": None,
"pl_avail": get_online_status(),
"pl_not_avail": get_offline_status(),
"stat_t": None
}
device_info = {
"dev": {
"identifiers": None,
"name": None,
"model": "mdadm",
"sw_version": mdadm_version,
"manufacturer": os_version
}
}
def publish_online(client):
verbose('sending online status')
client.publish(config['hass']['availability_topic'], payload=get_online_status(), qos=2, retain=True)
def publish_offline(client):
verbose('sending offline status')
client.publish(config['hass']['availability_topic'], payload=get_offline_status(), qos=2, retain=True)
# returns True, False or None for connection terminated
def is_connected():
return state['connected']
def set_connected(connected):
state['connected'] = connected
def reset_main_loop_delay(delay = 2):
if delay<=0:
delay = None
if delay==state['current_delay']:
return
verbose('reset main delay')
state['current_delay'] = delay
def set_resend_online_status(resend):
state['resend_online_status'] = resend
# set connection online
def on_connect(client, userdata, flags, rc, *args):
if is_connected()==None:
verbose('client terminated')
return
if rc!=0:
verbose('bad connection rc=%s' % str(rc))
set_connected(False)
return
if is_connected()==False:
set_connected(True)
set_resend_online_status(False)
# subscribe to availability topic
client.subscribe(config['hass']['availability_topic'], qos=2)
publish_online(client)
reset_main_loop_delay()
else:
raise Exception('Invalid connection state')
# set connection offline
def on_disconnect(client, userdata, rc, *args):
if is_connected():
set_connected(False)
if rc==0:
try:
publish_offline(client)
except Exception as e:
verbose('Exception: %s' % e)
# update online status on connection failures
def on_message(client, userdata, message, *args):
if is_connected():
if message.topic==config['hass']['availability_topic']:
payload = message.payload.decode('utf-8')
if payload==get_online_status():
set_resend_online_status(False)
else:
set_resend_online_status(True)
reset_main_loop_delay()
# at exit, set device offline
def disconnect_mqtt():
flag = is_connected()
set_connected(None) # mark as terminated
if flag:
publish_offline(client)
time.sleep(1)
client.disconnect()
client.loop_stop(True)
# publish message
def publish(client, topic, payload):
verbose('topic: %s\nmessage: %s' % (topic, payload))
client.publish(topic, payload=payload, qos=get_qos_value(), retain=True)
# publish config
def publish_config(client, topic, payload, object_id):
if not isinstance(payload, dict):
raise Exception('Invalid config payload topic: %s' % topic)
topic = '%s/sensor/%s/%s/config' % (config['hass']['autoconf_topic'], object_id, topic)
publish(client, topic, json.dumps(payload))
# connect to MQTT server
try:
# configure client
version = int(config['mqtt']['version'])
client = mqtt.Client(client_id=None, clean_session=version <= mqtt.MQTTv311 and True or None, userdata=None, protocol=version, transport=config['mqtt']['transport'], reconnect_on_failure=True)
if has_mqtt_username(config['mqtt']['username']):
verbose('setting username=%s password=%s' % (config['mqtt']['username'], get_mqtt_password(config['mqtt']['password'])))
client.username_pw_set(config['mqtt']['username'], config['mqtt']['password'])
# callbacks and exit routines
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_message = on_message
atexit.register(disconnect_mqtt)
# last will in case the connection is aborted
client.will_set(config['hass']['availability_topic'], payload=get_offline_status(), qos=2, retain=True)
# connect client
verbose('connecting to %s@%s:%d' % (get_mqtt_account(config['mqtt']), config['mqtt']['host'], config['mqtt']['port']))
set_connected(False)
client.connect(config['mqtt']['host'], port=config['mqtt']['port'], keepalive=config['mqtt']['keepalive'], bind_address='')
except Exception as e:
error('Failed to connect to MQTT: %s@%s:%d' % (get_mqtt_account(config['mqtt']), config['mqtt']['host'], config['mqtt']['port']), e)
# start mqtt client loop
client.loop_start()
# generate autoconf
number = 42
for device in config['devices']:
verbose('------------------------------------')
# get unique id from the device block id
res = subprocess.run(['blkid', '-o', 'value', device['raid_device']], capture_output=True)
unique_id = hashlib.sha256(res.stdout).hexdigest()[:12]
raid_state = None
raid_level = None
raid_device = device['raid_device'].split('/').pop()
args = mdadm_cmd(['--misc', '--detail', device['raid_device']])
res = subprocess.run(args, capture_output=True)
if res.returncode:
error('Error executing %d: %s' % (res.returncode, ' '.join(args)), None)
for line in res.stdout.decode().split('\n'):
args = line.strip().split(':', maxsplit=2)
if len(args)==2:
name = args[0].strip()
value = args[1].strip()
key = name.lower().replace(' ', '_')
if key=='raid_level':
raid_level = value.lower()
elif key=='state':
raid_state = value.capitalize()
display_device_name = '%s %s %s' % (device_name.capitalize(), raid_level.capitalize(), device['raid_device'])
unique_id_dev = unique_id + ('%02x' % number)
topics['avty_t'] = config['hass']['availability_topic']
topics['stat_t'] = '%s/%s_%s_%s/state' % (config['hass']['base_topic'], device_name, raid_level, raid_device)
device_info['dev']['identifiers'] = [ '%s_%s_%s_%s' % (device_name, raid_level, raid_device, unique_id_dev) ]
device_info['dev']['name'] = '%s_%s_dev_%s' % (device_name, raid_level, raid_device)
object_id = '%s_%s_%s' % (device_name, raid_level, raid_device)
# state config / template for all
device_state = {
'name': display_device_name,
'platform': 'mqtt',
'uniq_id': unique_id_dev,
'obj_id': object_id + '_state',
'icon': config['mqtt']['icon']
}
device_state.update(topics)
device_state.update(extra_info)
device_state.update(device_info)
# total space
number += 23
total_space = copy.deepcopy(device_state)
total_space['name'] += ' Total Space'
total_space['obj_id'] = object_id + '_total'
total_space['uniq_id'] = unique_id + ('%02x' % number)
total_space['stat_t'] = '%s/%s_%s_%s/total' % (config['hass']['base_topic'], device_name, raid_level, raid_device)
total_space['unit_of_measurement'] = device['display_unit']
# free space
number += 23
free_space = copy.deepcopy(device_state)
free_space['name'] += ' Free Space'
free_space['obj_id'] = object_id + '_free'
free_space['uniq_id'] = unique_id + ('%02x' % number)
free_space['stat_t'] = '%s/%s_%s_%s/free' % (config['hass']['base_topic'], device_name, raid_level, raid_device)
free_space['unit_of_measurement'] = device['display_unit']
# used space percentage
number += 23
free_pct_space = copy.deepcopy(device_state)
free_pct_space['name'] += ' Free Space Pct'
free_pct_space['obj_id'] = object_id + '_free_pct'
free_pct_space['uniq_id'] = unique_id + ('%02x' % number)
free_pct_space['stat_t'] = '%s/%s_%s_%s/free_pct' % (config['hass']['base_topic'], device_name, raid_level, raid_device)
free_pct_space['unit_of_measurement'] = '%'
# used space
number += 23
used_space = copy.deepcopy(device_state)
used_space['name'] += ' Used Space'
used_space['obj_id'] = object_id + '_used'
used_space['uniq_id'] = unique_id + ('%02x' % number)
used_space['stat_t'] = '%s/%s_%s_%s/used' % (config['hass']['base_topic'], device_name, raid_level, raid_device)
used_space['unit_of_measurement'] = device['display_unit']
# append State to name
device_state['name'] = device_state['name'] + ' State'
# send homeassistant config
verbose('state: %s\ndevice: %s\nraid device: %s\nmount point: %s\ndisplay unit: %s' % (raid_state, device_name, device['raid_device'], device['mount_point'], device['display_unit']))
publish_config(client, 'state', device_state, object_id)
publish_config(client, 'total_space', total_space, object_id)
publish_config(client, 'free_space', free_space, object_id)
publish_config(client, 'free_pct_space', free_pct_space, object_id)
publish_config(client, 'used_space', used_space, object_id)
# update raid status
def main_loop(client):
for device in config['devices']:
raid_state = 'N/A'
args = mdadm_cmd(['--misc', '--detail', device['raid_device']])
res = subprocess.run(args, capture_output=True)
if res.returncode:
error('Error executing %d: %s' % (res.returncode, ' '.join(args)), None)
for line in res.stdout.decode().split('\n'):
args = line.strip().split(':', maxsplit=2)
if len(args)==2:
name = args[0].strip()
value = args[1].strip()
key = name.lower().replace(' ', '_')
if key=='state':
raid_state = value.capitalize()
break
result = psutil.disk_usage(device['mount_point'])
verbose('---')
publish(client, device_state['stat_t'], raid_state)
publish(client, total_space['stat_t'], ('%%.%df' % device['display_decimal_places']) % (result.total * device['multiplier']))
publish(client, used_space['stat_t'], ('%%.%df' % device['display_decimal_places']) % (result.used * device['multiplier']))
publish(client, free_space['stat_t'], ('%%.%df' % device['display_decimal_places']) % (result.free * device['multiplier']))
publish(client, free_pct_space['stat_t'], ('%.1f' % (100.0 - result.percent)))
# run indefinitely
while True:
try:
if is_connected():
# check if we have to resend the online status
if state['resend_online_status']:
set_resend_online_status(False)
publish_online(client)
# publish raid state
try:
main_loop(client)
except Exception as e:
error('Main loop aborted', e)
verbose('---\nwaiting %d seconds....' % config['sys']['interval'])
state['current_delay'] = config['sys']['interval']
else:
state['current_delay'] = 1
while state['current_delay']:
time.sleep(1)
try:
state['current_delay'] -= 1 # TypeError if None
except:
break
except KeyboardInterrupt:
error('Aborted')