From c3ff0514ee3d4f626cc89a3e34363f35335cb835 Mon Sep 17 00:00:00 2001 From: "martin.fencl" Date: Tue, 23 Dec 2025 21:08:39 +0100 Subject: [PATCH] fix raid --- check_raid.yml | 99 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 59 insertions(+), 40 deletions(-) diff --git a/check_raid.yml b/check_raid.yml index 9e204cd..dd4eb42 100644 --- a/check_raid.yml +++ b/check_raid.yml @@ -86,57 +86,76 @@ raid_md: "{{ RAID_DEVICE | regex_replace('^/dev/', '') }}" mdstat_text: "{{ mdstat_cmd.stdout | default('') }}" changed_when: false - - - name: Extract selected md block from mdstat # English comments + + - name: Extract selected md block from mdstat (no regex) # English comments ansible.builtin.set_fact: raid_block: >- - {{ - ( - mdstat_text - | regex_findall( - '(?ms)^' ~ raid_md ~ '\\s*:.*?(?=^md\\d+\\s*:|^unused devices:|\\Z)' - ) - | first - | default('', true) - ) - }} + {%- set ns = namespace(block='') -%} + {%- for b in (mdstat_text.split('\n\n')) -%} + {%- set bb = (b | trim) -%} + {%- if bb.startswith(raid_md ~ ' :') or bb.startswith(raid_md ~ ':') -%} + {%- set ns.block = bb -%} + {%- endif -%} + {%- endfor -%} + {{ ns.block }} changed_when: false - - name: Parse RAID status from mdstat (safe parsing) # English comments + - name: Parse RAID status from mdstat (no regex) # English comments ansible.builtin.set_fact: - raid_present: "{{ (raid_block | length) > 0 }}" + raid_present: "{{ (raid_block | trim | length) > 0 }}" + + # Extract [UU] / [U_] token by scanning bracket tokens and keeping only U/_ chars raid_status: >- - {{ - ( - raid_block - | regex_findall('\\[[0-9]+/[0-9]+\\]\\s*\\[([U_]+)\\]') - | first - | default('', true) - ) - }} + {%- set ns = namespace(st='') -%} + {%- for line in (raid_block.splitlines() if (raid_block | length) > 0 else []) -%} + {%- for tok in line.split() -%} + {%- if tok.startswith('[') and tok.endswith(']') -%} + {%- set inner = tok[1:-1] -%} + {%- if (inner | length) > 0 and ((inner | list | difference(['U','_'])) | length == 0) -%} + {%- set ns.st = inner -%} + {%- endif -%} + {%- endif -%} + {%- endfor -%} + {%- endfor -%} + {{ ns.st }} + raid_is_degraded: "{{ (raid_status | length > 0) and ('_' in raid_status) }}" - raid_is_rebuilding: "{{ raid_block is search('(?i)\\b(resync|recovery|reshape|repair)\\b') }}" - raid_is_checking: "{{ raid_block is search('(?i)\\bcheck\\b') }}" + + raid_block_lower: "{{ raid_block | lower }}" + raid_is_rebuilding: >- + {{ + ('resync' in raid_block_lower) or + ('recovery' in raid_block_lower) or + ('reshape' in raid_block_lower) or + ('repair' in raid_block_lower) + }} + raid_is_checking: "{{ 'check' in raid_block_lower }}" + + # First line that contains an action keyword raid_action_line: >- - {{ - ( - raid_block - | regex_findall('(?im)^(\\s*\\[[^\\]]+\\].*\\b(?:resync|recovery|reshape|repair|check)\\b.*)$') - | first - | default('', true) - ) - }} + {%- set ns = namespace(line='') -%} + {%- set keys = ['resync','recovery','reshape','repair','check'] -%} + {%- for line in (raid_block.splitlines() if (raid_block | length) > 0 else []) -%} + {%- set l = (line | lower) -%} + {%- for k in keys -%} + {%- if ns.line == '' and (k in l) -%} + {%- set ns.line = (line | trim) -%} + {%- endif -%} + {%- endfor -%} + {%- endfor -%} + {{ ns.line }} + + # Parse progress from ".... = 12.3%" if present raid_progress: >- - {{ - ( - raid_block - | regex_findall('(?i)\\b(?:resync|recovery|reshape|repair|check)\\b\\s*=\\s*([0-9.]+)%') - | first - | default('', true) - ) - }} + {%- set ns = namespace(p='') -%} + {%- if (raid_action_line | length) > 0 and ('=' in raid_action_line) and ('%' in raid_action_line) -%} + {%- set right = raid_action_line.split('=', 1)[1] -%} + {%- set ns.p = (right.split('%', 1)[0] | trim) -%} + {%- endif -%} + {{ ns.p }} changed_when: false + - name: Debug | Show mdstat and parsed values # English comments ansible.builtin.debug: msg: |