diff --git a/hwmonDaemon.py b/hwmonDaemon.py index c925f5b..aaa113a 100644 --- a/hwmonDaemon.py +++ b/hwmonDaemon.py @@ -775,6 +775,43 @@ class SystemHealthMonitor: if re.match(pattern, mountpoint): return True return False + + def _parse_size(self, size_str: str) -> float: + """ + Parse size string with units to bytes. + Examples: '1.5G', '500M', '2.3T', etc. + """ + if not size_str or not isinstance(size_str, str): + return 0.0 + + # Remove any spaces and convert to uppercase for consistency + size_str = size_str.strip().upper() + + # Handle special case for MB notation + if 'MB' in size_str: + size_str = size_str.replace('MB', 'M') + + # Define unit multipliers + units = { + 'B': 1, + 'K': 1024, + 'M': 1024**2, + 'G': 1024**3, + 'T': 1024**4, + 'P': 1024**5 + } + + try: + # Extract numeric value and unit + if size_str[-1] in units: + number = float(size_str[:-1]) + unit = size_str[-1] + return number * units[unit] + # Handle case where the input is just a number + return float(size_str) + except (ValueError, IndexError): + logger.debug(f"Could not parse size: {size_str}") + return 0.0 def _is_physical_disk(self, device_path): """ @@ -1370,58 +1407,46 @@ class SystemHealthMonitor: continue logger.debug(f"Processing filesystem line: {fs_line}") - try: - parts = fs_line.split() - if len(parts) >= 6: - usage_percent = 0 + parts = fs_line.split() + if len(parts) >= 6: + try: + # Skip appPool entries and mediafs mounts + if parts[0].startswith('appPool:') or '/mnt/pve/mediaf' in parts[0]: + continue + filesystem = { - 'mountpoint': parts[5], # Last column is the mountpoint - 'total': parts[2], # Size column - 'used': parts[3], # Used column - 'available': parts[4], # Avail column - 'usage_percent': float(parts[4].rstrip('%')) # Use% column + 'mountpoint': parts[5], + 'total_space': parts[1], # Size + 'used_space': parts[2], # Used + 'available': parts[3], # Avail + 'usage_percent': float(parts[4].rstrip('%')) # Use% } - - if parts[0].startswith('appPool:'): - logger.debug(f"Skipping appPool entry: {parts[0]}") - continue - - if '/mnt/pve/mediaf' in parts[0]: - logger.debug(f"Skipping mediafs mount: {parts[0]}") - continue - - mountpoint = parts[5] - logger.debug(f"Processing mountpoint: {mountpoint}") - if parts[1][-1] in 'BKMGT' and parts[2][-1] in 'BKMGT': - total_size = self._convert_size_to_bytes(parts[1]) - used_size = self._convert_size_to_bytes(parts[2]) - usage_percent = float(parts[4].rstrip('%')) - - logger.debug(f"Storage metrics parsed for LXC {vmid}:") - logger.debug(f" Filesystem: {parts[0]}") - logger.debug(f" Total: {filesystem['total']}") - logger.debug(f" Used: {filesystem['used']}") - logger.debug(f" Available: {filesystem['available']}") - logger.debug(f" Usage: {usage_percent}%") - logger.debug(f" Mountpoint: {filesystem['mountpoint']}") - - if usage_percent >= self.CONFIG['THRESHOLDS']['DISK_CRITICAL']: - logger.debug(f"CRITICAL: Storage usage {usage_percent}% exceeds critical threshold") + # Convert sizes to bytes for comparison + total_bytes = self._parse_size(filesystem['total_space']) + used_bytes = self._parse_size(filesystem['used_space']) + avail_bytes = self._parse_size(filesystem['available']) + + # Store both human readable and byte values + filesystem['total_bytes'] = total_bytes + filesystem['used_bytes'] = used_bytes + filesystem['avail_bytes'] = avail_bytes + + if filesystem['usage_percent'] >= self.CONFIG['THRESHOLDS']['DISK_CRITICAL']: lxc_health['status'] = 'CRITICAL' - issue = f"LXC {vmid} critical storage usage: {usage_percent}% on {mountpoint}" + issue = f"LXC {vmid} critical storage usage: {filesystem['usage_percent']}% on {filesystem['mountpoint']}" lxc_health['issues'].append(issue) - elif usage_percent >= self.CONFIG['THRESHOLDS']['DISK_WARNING']: - logger.debug(f"WARNING: Storage usage {usage_percent}% exceeds warning threshold") + elif filesystem['usage_percent'] >= self.CONFIG['THRESHOLDS']['DISK_WARNING']: if lxc_health['status'] != 'CRITICAL': lxc_health['status'] = 'WARNING' - issue = f"LXC {vmid} high storage usage: {usage_percent}% on {mountpoint}" + issue = f"LXC {vmid} high storage usage: {filesystem['usage_percent']}% on {filesystem['mountpoint']}" lxc_health['issues'].append(issue) container_info['filesystems'].append(filesystem) - except Exception as e: - logger.debug(f"Error processing filesystem line for container {vmid}: {str(e)}") - continue + + except Exception as e: + logger.debug(f"Error processing filesystem line for container {vmid}: {str(e)}") + continue lxc_health['containers'].append(container_info) logger.debug(f"Added container info for VMID {vmid}")