From aefa9c2944368e6c84b8780381871b0c84dc46c0 Mon Sep 17 00:00:00 2001 From: Jared Vititoe Date: Sun, 9 Mar 2025 16:19:17 -0400 Subject: [PATCH] LXC storage utilization additon --- hwmonDaemon.py | 82 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/hwmonDaemon.py b/hwmonDaemon.py index b5e3434..179446b 100644 --- a/hwmonDaemon.py +++ b/hwmonDaemon.py @@ -287,7 +287,8 @@ class SystemHealthMonitor: 'drives_health': self._check_drives_health(), 'memory_health': self._check_memory_usage(), 'cpu_health': self._check_cpu_usage(), - 'network_health': self._check_network_status() + 'network_health': self._check_network_status(), + 'lxc_health': self._check_lxc_storage() } if self.dry_run: @@ -1253,6 +1254,85 @@ class SystemHealthMonitor: 'status': 'ERROR', 'error': str(e) } + + def _check_lxc_storage(self) -> Dict[str, Any]: + """ + Check storage utilization for all running LXC containers + """ + lxc_health = { + 'status': 'OK', + 'containers': [], + 'issues': [] + } + + try: + # Get list of running LXC containers + result = subprocess.run( + ['pct', 'list'], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True + ) + + # Skip header line and process each container + for line in result.stdout.split('\n')[1:]: + if not line.strip(): + continue + + vmid, status, *_ = line.split() + + if status.lower() == 'running': + # Get container disk usage + disk_info = subprocess.run( + ['pct', 'df', vmid], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True + ) + + container_info = { + 'vmid': vmid, + 'filesystems': [] + } + + # Skip header and process each filesystem + for fs_line in disk_info.stdout.split('\n')[1:]: + if not fs_line.strip(): + continue + + fs = fs_line.split() + if len(fs) >= 6: + usage_percent = int(fs[4].rstrip('%')) + filesystem = { + 'mountpoint': fs[5], + 'total': fs[1], + 'used': fs[2], + 'available': fs[3], + 'usage_percent': usage_percent + } + + # Check thresholds + if usage_percent >= self.CONFIG['THRESHOLDS']['DISK_CRITICAL']: + lxc_health['status'] = 'CRITICAL' + lxc_health['issues'].append( + f"LXC {vmid} critical storage usage: {usage_percent}% on {fs[5]}" + ) + elif usage_percent >= self.CONFIG['THRESHOLDS']['DISK_WARNING']: + if lxc_health['status'] != 'CRITICAL': + lxc_health['status'] = 'WARNING' + lxc_health['issues'].append( + f"LXC {vmid} high storage usage: {usage_percent}% on {fs[5]}" + ) + + container_info['filesystems'].append(filesystem) + + lxc_health['containers'].append(container_info) + + except Exception as e: + lxc_health['status'] = 'ERROR' + lxc_health['issues'].append(f"Error checking LXC storage: {str(e)}") + + return lxc_health def main(): parser = argparse.ArgumentParser(description="System Health Monitor")