Replace fragile column-index LXC storage parsing with regex
Use regex pattern matching instead of split()[N] indexing for parsing pct df output. This is more robust against variations in column formatting and whitespace. Resolves #11 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -3454,32 +3454,38 @@ class SystemHealthMonitor:
|
|||||||
if not fs_line.strip() or 'MP' in fs_line:
|
if not fs_line.strip() or 'MP' in fs_line:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Fix: Use fs_line instead of line, and columns consistently
|
# Parse df output using regex for reliable column extraction
|
||||||
columns = fs_line.split()
|
match = re.match(
|
||||||
|
r'(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\d+\.?\d*)%?\s+(.*)',
|
||||||
if len(columns) >= 6:
|
fs_line.strip()
|
||||||
try:
|
)
|
||||||
# Skip excluded mounts by checking the first column
|
if not match:
|
||||||
if columns[0].startswith('appPool:') or '/mnt/pve/mediaf' in columns[1]:
|
logger.debug(f"Could not parse filesystem line: {fs_line}")
|
||||||
|
continue
|
||||||
|
|
||||||
|
pool, device_col, total_str, used_str, avail_str, percent_str, mountpoint = match.groups()
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Skip excluded mounts
|
||||||
|
if pool.startswith('appPool:') or '/mnt/pve/mediaf' in device_col:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Get the mountpoint (last column)
|
mountpoint = mountpoint.strip()
|
||||||
mountpoint = columns[-1]
|
|
||||||
|
|
||||||
# Skip excluded mountpoints
|
# Skip excluded mountpoints
|
||||||
if self._is_excluded_mount(mountpoint):
|
if self._is_excluded_mount(mountpoint):
|
||||||
logger.debug(f"Skipping excluded mount: {mountpoint}")
|
logger.debug(f"Skipping excluded mount: {mountpoint}")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Parse size values safely - use correct column indices
|
# Parse size values from named regex groups
|
||||||
total_space = self._parse_size(columns[2]) # 3rd column
|
total_space = self._parse_size(total_str)
|
||||||
used_space = self._parse_size(columns[3]) # 4th column
|
used_space = self._parse_size(used_str)
|
||||||
available_space = self._parse_size(columns[4]) # 5th column
|
available_space = self._parse_size(avail_str)
|
||||||
|
|
||||||
# Parse percentage safely
|
# Parse percentage from regex group
|
||||||
try:
|
try:
|
||||||
usage_percent = float(columns[5].rstrip('%')) # 6th column
|
usage_percent = float(percent_str)
|
||||||
except (ValueError, IndexError):
|
except ValueError:
|
||||||
# Calculate percentage if parsing fails
|
# Calculate percentage if parsing fails
|
||||||
usage_percent = (used_space / total_space * 100) if total_space > 0 else 0
|
usage_percent = (used_space / total_space * 100) if total_space > 0 else 0
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user