Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/check and resync status sensors added #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 50 additions & 7 deletions check-raid.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,13 @@ def publish_config(client, topic, payload, object_id):
client.loop_start()

# generate autoconf
device_states = {}
resync_progresses = {}
check_progresses = {}
total_spaces = {}
free_spaces = {}
free_pct_spaces = {}
used_spaces = {}
number = 42
for device in config['devices']:
verbose('------------------------------------')
Expand Down Expand Up @@ -417,6 +424,7 @@ def publish_config(client, topic, payload, object_id):
device_state.update(topics)
device_state.update(extra_info)
device_state.update(device_info)
device_states[device['raid_device']] = device_state

# total space
number += 23
Expand All @@ -426,6 +434,7 @@ def publish_config(client, topic, payload, object_id):
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']
total_spaces[device['raid_device']] = total_space

# free space
number += 23
Expand All @@ -435,6 +444,7 @@ def publish_config(client, topic, payload, object_id):
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']
free_spaces[device['raid_device']] = free_space

# used space percentage
number += 23
Expand All @@ -444,6 +454,7 @@ def publish_config(client, topic, payload, object_id):
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'] = '%'
free_pct_spaces[device['raid_device']] = free_pct_space

# used space
number += 23
Expand All @@ -453,6 +464,27 @@ def publish_config(client, topic, payload, object_id):
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']
used_spaces[device['raid_device']] = used_space

# resync progress
number += 24
resync_progress = copy.deepcopy(device_state)
resync_progress['name'] += ' Resync Progress'
resync_progress['obj_id'] = object_id + '_resync_progress'
resync_progress['uniq_id'] = unique_id + ('%02x' % number)
resync_progress['stat_t'] = '%s/%s_%s_%s/resync_progress' % (config['hass']['base_topic'], device_name, raid_level, raid_device)
resync_progress['unit_of_measurement'] = '%'
resync_progresses[device['raid_device']] = resync_progress

# check progress
number += 25
check_progress = copy.deepcopy(device_state)
check_progress['name'] += ' Check Progress'
check_progress['obj_id'] = object_id + '_check_progress'
check_progress['uniq_id'] = unique_id + ('%02x' % number)
check_progress['stat_t'] = '%s/%s_%s_%s/check_progress' % (config['hass']['base_topic'], device_name, raid_level, raid_device)
check_progress['unit_of_measurement'] = '%'
check_progresses[device['raid_device']] = check_progress

# append State to name
device_state['name'] = device_state['name'] + ' State'
Expand All @@ -465,11 +497,15 @@ def publish_config(client, topic, payload, 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)
publish_config(client, 'resync_progress', resync_progress, object_id)
publish_config(client, 'check_progress', check_progress, object_id)

# update raid status
def main_loop(client):
for device in config['devices']:
raid_state = 'N/A'
resync_progress_value = '100'
check_progress_value = '100'
args = mdadm_cmd(['--misc', '--detail', device['raid_device']])
res = subprocess.run(args, capture_output=True)
if res.returncode:
Expand All @@ -482,17 +518,24 @@ def main_loop(client):
key = name.lower().replace(' ', '_')
if key=='state':
raid_state = value.capitalize()
break
continue
elif key=='resync_status':
resync_progress_value = value.strip().split('%', maxsplit=2)[0]
continue
elif key == 'check_status':
check_progress_value = value.strip().split('%', maxsplit=2)[0]
continue

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)))

publish(client, device_states[device['raid_device']]['stat_t'], raid_state)
publish(client, total_spaces[device['raid_device']]['stat_t'], ('%%.%df' % device['display_decimal_places']) % (result.total * device['multiplier']))
publish(client, used_spaces[device['raid_device']]['stat_t'], ('%%.%df' % device['display_decimal_places']) % (result.used * device['multiplier']))
publish(client, free_spaces[device['raid_device']]['stat_t'], ('%%.%df' % device['display_decimal_places']) % (result.free * device['multiplier']))
publish(client, free_pct_spaces[device['raid_device']]['stat_t'], ('%.1f' % (100.0 - result.percent)))
publish(client, resync_progresses[device['raid_device']]['stat_t'], resync_progress_value)
publish(client, check_progresses[device['raid_device']]['stat_t'], check_progress_value)

# run indefinitely
while True:
Expand Down