2025-11-04 22:28:42 +01:00
|
|
|
"""
|
2025-11-05 18:30:31 +01:00
|
|
|
ProxMenux Health Monitor Module
|
|
|
|
|
Provides comprehensive, lightweight health checks for Proxmox systems.
|
|
|
|
|
Optimized for minimal system impact with intelligent thresholds and hysteresis.
|
|
|
|
|
|
|
|
|
|
Author: MacRimi
|
2025-11-09 16:30:29 +01:00
|
|
|
Version: 1.2 (Always returns all 10 categories)
|
2025-11-04 22:28:42 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import psutil
|
|
|
|
|
import subprocess
|
|
|
|
|
import json
|
2025-11-05 18:30:31 +01:00
|
|
|
import time
|
|
|
|
|
import os
|
2025-11-07 18:49:37 +01:00
|
|
|
from typing import Dict, List, Any, Tuple, Optional
|
2025-11-05 18:30:31 +01:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
from collections import defaultdict
|
2025-11-09 16:30:29 +01:00
|
|
|
import re
|
2025-11-04 22:28:42 +01:00
|
|
|
|
|
|
|
|
class HealthMonitor:
|
2025-11-05 18:30:31 +01:00
|
|
|
"""
|
|
|
|
|
Monitors system health across multiple components with minimal impact.
|
|
|
|
|
Implements hysteresis, intelligent caching, and progressive escalation.
|
2025-11-09 16:30:29 +01:00
|
|
|
Always returns all 10 health categories.
|
2025-11-05 18:30:31 +01:00
|
|
|
"""
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-05 18:30:31 +01:00
|
|
|
# CPU Thresholds
|
|
|
|
|
CPU_WARNING = 85
|
|
|
|
|
CPU_CRITICAL = 95
|
|
|
|
|
CPU_RECOVERY = 75
|
2025-11-07 18:49:37 +01:00
|
|
|
CPU_WARNING_DURATION = 60
|
|
|
|
|
CPU_CRITICAL_DURATION = 120
|
|
|
|
|
CPU_RECOVERY_DURATION = 120
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-05 18:30:31 +01:00
|
|
|
# Memory Thresholds
|
|
|
|
|
MEMORY_WARNING = 85
|
|
|
|
|
MEMORY_CRITICAL = 95
|
2025-11-07 18:49:37 +01:00
|
|
|
MEMORY_DURATION = 60
|
|
|
|
|
SWAP_WARNING_DURATION = 300
|
|
|
|
|
SWAP_CRITICAL_PERCENT = 5
|
|
|
|
|
SWAP_CRITICAL_DURATION = 120
|
2025-11-05 18:30:31 +01:00
|
|
|
|
|
|
|
|
# Storage Thresholds
|
|
|
|
|
STORAGE_WARNING = 85
|
|
|
|
|
STORAGE_CRITICAL = 95
|
|
|
|
|
|
|
|
|
|
# Temperature Thresholds
|
|
|
|
|
TEMP_WARNING = 80
|
|
|
|
|
TEMP_CRITICAL = 90
|
|
|
|
|
|
|
|
|
|
# Network Thresholds
|
2025-11-07 18:49:37 +01:00
|
|
|
NETWORK_LATENCY_WARNING = 100
|
|
|
|
|
NETWORK_LATENCY_CRITICAL = 300
|
|
|
|
|
NETWORK_TIMEOUT = 0.9
|
|
|
|
|
NETWORK_INACTIVE_DURATION = 600
|
2025-11-05 18:30:31 +01:00
|
|
|
|
|
|
|
|
# Log Thresholds
|
|
|
|
|
LOG_ERRORS_WARNING = 5
|
2025-11-07 18:49:37 +01:00
|
|
|
LOG_ERRORS_CRITICAL = 10
|
|
|
|
|
LOG_WARNINGS_WARNING = 15
|
2025-11-05 18:30:31 +01:00
|
|
|
LOG_WARNINGS_CRITICAL = 30
|
2025-11-07 18:49:37 +01:00
|
|
|
LOG_CHECK_INTERVAL = 300
|
|
|
|
|
|
|
|
|
|
# Updates Thresholds
|
|
|
|
|
UPDATES_WARNING = 10
|
|
|
|
|
UPDATES_CRITICAL = 30
|
2025-11-05 18:30:31 +01:00
|
|
|
|
|
|
|
|
# Critical keywords for immediate escalation
|
|
|
|
|
CRITICAL_LOG_KEYWORDS = [
|
|
|
|
|
'I/O error', 'EXT4-fs error', 'XFS', 'LVM activation failed',
|
|
|
|
|
'md/raid: device failed', 'Out of memory', 'kernel panic',
|
2025-11-07 18:49:37 +01:00
|
|
|
'filesystem read-only', 'cannot mount', 'failed to start',
|
|
|
|
|
'task hung', 'oom_kill'
|
2025-11-05 18:30:31 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
# PVE Critical Services
|
|
|
|
|
PVE_SERVICES = ['pveproxy', 'pvedaemon', 'pvestatd', 'pve-cluster']
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-05 18:30:31 +01:00
|
|
|
def __init__(self):
|
|
|
|
|
"""Initialize health monitor with state tracking"""
|
2025-11-07 18:49:37 +01:00
|
|
|
self.state_history = defaultdict(list)
|
|
|
|
|
self.last_check_times = {}
|
|
|
|
|
self.cached_results = {}
|
|
|
|
|
self.network_baseline = {}
|
|
|
|
|
self.io_error_history = defaultdict(list)
|
|
|
|
|
self.failed_vm_history = set() # Track VMs that failed to start
|
|
|
|
|
|
|
|
|
|
def get_system_info(self) -> Dict[str, Any]:
|
|
|
|
|
"""
|
|
|
|
|
Get lightweight system info for header display.
|
|
|
|
|
Returns: hostname, uptime, and cached health status.
|
|
|
|
|
This is extremely lightweight and uses cached health status.
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
# Get hostname
|
|
|
|
|
hostname = os.uname().nodename
|
|
|
|
|
|
|
|
|
|
# Get uptime (very cheap operation)
|
|
|
|
|
uptime_seconds = time.time() - psutil.boot_time()
|
|
|
|
|
|
|
|
|
|
# Get cached health status (no expensive checks)
|
|
|
|
|
health_status = self.get_cached_health_status()
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
'hostname': hostname,
|
|
|
|
|
'uptime_seconds': int(uptime_seconds),
|
|
|
|
|
'uptime_formatted': self._format_uptime(uptime_seconds),
|
|
|
|
|
'health': health_status,
|
|
|
|
|
'timestamp': datetime.now().isoformat()
|
|
|
|
|
}
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return {
|
|
|
|
|
'hostname': 'unknown',
|
|
|
|
|
'uptime_seconds': 0,
|
|
|
|
|
'uptime_formatted': 'Unknown',
|
|
|
|
|
'health': {'status': 'UNKNOWN', 'summary': f'Error: {str(e)}'},
|
|
|
|
|
'timestamp': datetime.now().isoformat()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def _format_uptime(self, seconds: float) -> str:
|
|
|
|
|
"""Format uptime in human-readable format"""
|
|
|
|
|
days = int(seconds // 86400)
|
|
|
|
|
hours = int((seconds % 86400) // 3600)
|
|
|
|
|
minutes = int((seconds % 3600) // 60)
|
2025-11-05 18:30:31 +01:00
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
if days > 0:
|
|
|
|
|
return f"{days}d {hours}h {minutes}m"
|
|
|
|
|
elif hours > 0:
|
|
|
|
|
return f"{hours}h {minutes}m"
|
|
|
|
|
else:
|
|
|
|
|
return f"{minutes}m"
|
|
|
|
|
|
|
|
|
|
def get_cached_health_status(self) -> Dict[str, str]:
|
|
|
|
|
"""
|
|
|
|
|
Get cached health status without running expensive checks.
|
|
|
|
|
Returns the last calculated status or triggers a check if too old.
|
|
|
|
|
"""
|
|
|
|
|
cache_key = 'overall_health'
|
|
|
|
|
current_time = time.time()
|
|
|
|
|
|
|
|
|
|
# If cache exists and is less than 60 seconds old, return it
|
|
|
|
|
if cache_key in self.last_check_times:
|
|
|
|
|
if current_time - self.last_check_times[cache_key] < 60:
|
|
|
|
|
return self.cached_results.get(cache_key, {'status': 'OK', 'summary': 'System operational'})
|
|
|
|
|
|
|
|
|
|
# Otherwise, calculate and cache
|
|
|
|
|
status = self.get_overall_status()
|
|
|
|
|
self.cached_results[cache_key] = {
|
|
|
|
|
'status': status['status'],
|
|
|
|
|
'summary': status['summary']
|
|
|
|
|
}
|
|
|
|
|
self.last_check_times[cache_key] = current_time
|
|
|
|
|
|
|
|
|
|
return self.cached_results[cache_key]
|
|
|
|
|
|
2025-11-04 22:28:42 +01:00
|
|
|
def get_overall_status(self) -> Dict[str, Any]:
|
2025-11-05 18:30:31 +01:00
|
|
|
"""Get overall health status summary with minimal overhead"""
|
|
|
|
|
details = self.get_detailed_status()
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-05 18:30:31 +01:00
|
|
|
overall_status = details.get('overall', 'OK')
|
|
|
|
|
summary = details.get('summary', '')
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-05 18:30:31 +01:00
|
|
|
# Count statuses
|
|
|
|
|
critical_count = 0
|
|
|
|
|
warning_count = 0
|
|
|
|
|
ok_count = 0
|
|
|
|
|
|
|
|
|
|
for category, data in details.get('details', {}).items():
|
|
|
|
|
if isinstance(data, dict):
|
|
|
|
|
status = data.get('status', 'OK')
|
|
|
|
|
if status == 'CRITICAL':
|
|
|
|
|
critical_count += 1
|
|
|
|
|
elif status == 'WARNING':
|
|
|
|
|
warning_count += 1
|
|
|
|
|
elif status == 'OK':
|
|
|
|
|
ok_count += 1
|
2025-11-04 22:28:42 +01:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
'status': overall_status,
|
2025-11-05 18:30:31 +01:00
|
|
|
'summary': summary,
|
2025-11-04 22:28:42 +01:00
|
|
|
'critical_count': critical_count,
|
|
|
|
|
'warning_count': warning_count,
|
2025-11-05 18:30:31 +01:00
|
|
|
'ok_count': ok_count,
|
|
|
|
|
'timestamp': datetime.now().isoformat()
|
2025-11-04 22:28:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def get_detailed_status(self) -> Dict[str, Any]:
|
2025-11-05 18:30:31 +01:00
|
|
|
"""
|
|
|
|
|
Get comprehensive health status with all checks.
|
2025-11-09 16:30:29 +01:00
|
|
|
Returns JSON structure with ALL 10 categories always present.
|
2025-11-05 18:30:31 +01:00
|
|
|
"""
|
2025-11-09 16:30:29 +01:00
|
|
|
details = {
|
|
|
|
|
'cpu': {'status': 'OK'},
|
|
|
|
|
'memory': {'status': 'OK'},
|
|
|
|
|
'storage': {'status': 'OK'},
|
|
|
|
|
'disks': {'status': 'OK'},
|
|
|
|
|
'network': {'status': 'OK'},
|
|
|
|
|
'vms': {'status': 'OK'},
|
|
|
|
|
'services': {'status': 'OK'},
|
|
|
|
|
'logs': {'status': 'OK'},
|
|
|
|
|
'updates': {'status': 'OK'},
|
|
|
|
|
'security': {'status': 'OK'}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-05 18:30:31 +01:00
|
|
|
critical_issues = []
|
|
|
|
|
warning_issues = []
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
# Priority 1: Services PVE
|
2025-11-05 18:30:31 +01:00
|
|
|
services_status = self._check_pve_services()
|
2025-11-09 16:30:29 +01:00
|
|
|
details['services'] = services_status
|
|
|
|
|
if services_status['status'] == 'CRITICAL':
|
|
|
|
|
critical_issues.append(services_status.get('reason', 'Service failure'))
|
|
|
|
|
elif services_status['status'] == 'WARNING':
|
|
|
|
|
warning_issues.append(services_status.get('reason', 'Service issue'))
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-09 16:30:29 +01:00
|
|
|
# Priority 2: Storage
|
2025-11-07 18:49:37 +01:00
|
|
|
storage_status = self._check_storage_optimized()
|
2025-11-09 16:30:29 +01:00
|
|
|
if storage_status:
|
2025-11-07 18:49:37 +01:00
|
|
|
details['storage'] = storage_status
|
|
|
|
|
if storage_status.get('status') == 'CRITICAL':
|
|
|
|
|
critical_issues.append(storage_status.get('reason', 'Storage failure'))
|
|
|
|
|
elif storage_status.get('status') == 'WARNING':
|
|
|
|
|
warning_issues.append(storage_status.get('reason', 'Storage issue'))
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-09 16:30:29 +01:00
|
|
|
# Priority 3: Disks
|
2025-11-07 18:49:37 +01:00
|
|
|
disks_status = self._check_disks_optimized()
|
2025-11-09 16:30:29 +01:00
|
|
|
if disks_status:
|
2025-11-07 18:49:37 +01:00
|
|
|
details['disks'] = disks_status
|
|
|
|
|
if disks_status.get('status') == 'CRITICAL':
|
|
|
|
|
critical_issues.append(disks_status.get('reason', 'Disk failure'))
|
|
|
|
|
elif disks_status.get('status') == 'WARNING':
|
|
|
|
|
warning_issues.append(disks_status.get('reason', 'Disk issue'))
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-09 16:30:29 +01:00
|
|
|
# Priority 4: VMs/CTs - now detects qmp errors from logs
|
2025-11-07 18:49:37 +01:00
|
|
|
vms_status = self._check_vms_cts_optimized()
|
2025-11-09 16:30:29 +01:00
|
|
|
if vms_status:
|
2025-11-07 18:49:37 +01:00
|
|
|
details['vms'] = vms_status
|
|
|
|
|
if vms_status.get('status') == 'CRITICAL':
|
|
|
|
|
critical_issues.append(vms_status.get('reason', 'VM/CT failure'))
|
|
|
|
|
elif vms_status.get('status') == 'WARNING':
|
|
|
|
|
warning_issues.append(vms_status.get('reason', 'VM/CT issue'))
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-09 16:30:29 +01:00
|
|
|
# Priority 5: Network
|
2025-11-07 18:49:37 +01:00
|
|
|
network_status = self._check_network_optimized()
|
2025-11-09 16:30:29 +01:00
|
|
|
if network_status:
|
2025-11-07 18:49:37 +01:00
|
|
|
details['network'] = network_status
|
|
|
|
|
if network_status.get('status') == 'CRITICAL':
|
|
|
|
|
critical_issues.append(network_status.get('reason', 'Network failure'))
|
|
|
|
|
elif network_status.get('status') == 'WARNING':
|
|
|
|
|
warning_issues.append(network_status.get('reason', 'Network issue'))
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-09 16:30:29 +01:00
|
|
|
# Priority 6: CPU
|
2025-11-05 18:30:31 +01:00
|
|
|
cpu_status = self._check_cpu_with_hysteresis()
|
2025-11-09 16:30:29 +01:00
|
|
|
details['cpu'] = cpu_status
|
|
|
|
|
if cpu_status.get('status') == 'WARNING':
|
|
|
|
|
warning_issues.append(cpu_status.get('reason', 'CPU high'))
|
|
|
|
|
elif cpu_status.get('status') == 'CRITICAL':
|
|
|
|
|
critical_issues.append(cpu_status.get('reason', 'CPU critical'))
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-09 16:30:29 +01:00
|
|
|
# Priority 7: Memory
|
2025-11-05 18:30:31 +01:00
|
|
|
memory_status = self._check_memory_comprehensive()
|
2025-11-09 16:30:29 +01:00
|
|
|
details['memory'] = memory_status
|
|
|
|
|
if memory_status.get('status') == 'CRITICAL':
|
|
|
|
|
critical_issues.append(memory_status.get('reason', 'Memory critical'))
|
|
|
|
|
elif memory_status.get('status') == 'WARNING':
|
|
|
|
|
warning_issues.append(memory_status.get('reason', 'Memory high'))
|
|
|
|
|
|
|
|
|
|
# Priority 8: Logs
|
2025-11-05 18:30:31 +01:00
|
|
|
logs_status = self._check_logs_lightweight()
|
2025-11-09 16:30:29 +01:00
|
|
|
details['logs'] = logs_status
|
|
|
|
|
if logs_status.get('status') == 'CRITICAL':
|
|
|
|
|
critical_issues.append(logs_status.get('reason', 'Critical log errors'))
|
|
|
|
|
elif logs_status.get('status') == 'WARNING':
|
|
|
|
|
warning_issues.append(logs_status.get('reason', 'Log warnings'))
|
2025-11-07 18:49:37 +01:00
|
|
|
|
2025-11-09 16:30:29 +01:00
|
|
|
# Priority 9: Updates
|
2025-11-07 18:49:37 +01:00
|
|
|
updates_status = self._check_updates()
|
2025-11-09 16:30:29 +01:00
|
|
|
if updates_status:
|
2025-11-07 18:49:37 +01:00
|
|
|
details['updates'] = updates_status
|
|
|
|
|
if updates_status.get('status') == 'WARNING':
|
|
|
|
|
warning_issues.append(updates_status.get('reason', 'Updates pending'))
|
2025-11-05 18:30:31 +01:00
|
|
|
|
2025-11-09 16:30:29 +01:00
|
|
|
# Priority 10: Security
|
2025-11-05 18:30:31 +01:00
|
|
|
security_status = self._check_security()
|
2025-11-09 16:30:29 +01:00
|
|
|
details['security'] = security_status
|
|
|
|
|
if security_status.get('status') == 'WARNING':
|
|
|
|
|
warning_issues.append(security_status.get('reason', 'Security issue'))
|
2025-11-05 18:30:31 +01:00
|
|
|
|
|
|
|
|
# Determine overall status
|
|
|
|
|
if critical_issues:
|
|
|
|
|
overall = 'CRITICAL'
|
2025-11-07 18:49:37 +01:00
|
|
|
summary = '; '.join(critical_issues[:3])
|
2025-11-05 18:30:31 +01:00
|
|
|
elif warning_issues:
|
|
|
|
|
overall = 'WARNING'
|
2025-11-07 18:49:37 +01:00
|
|
|
summary = '; '.join(warning_issues[:3])
|
2025-11-04 22:28:42 +01:00
|
|
|
else:
|
2025-11-05 18:30:31 +01:00
|
|
|
overall = 'OK'
|
|
|
|
|
summary = 'All systems operational'
|
2025-11-04 22:28:42 +01:00
|
|
|
|
|
|
|
|
return {
|
2025-11-05 18:30:31 +01:00
|
|
|
'overall': overall,
|
|
|
|
|
'summary': summary,
|
|
|
|
|
'details': details,
|
|
|
|
|
'timestamp': datetime.now().isoformat()
|
2025-11-04 22:28:42 +01:00
|
|
|
}
|
|
|
|
|
|
2025-11-05 18:30:31 +01:00
|
|
|
def _check_cpu_with_hysteresis(self) -> Dict[str, Any]:
|
2025-11-07 18:49:37 +01:00
|
|
|
"""Check CPU with hysteresis to avoid flapping alerts"""
|
2025-11-05 18:30:31 +01:00
|
|
|
try:
|
|
|
|
|
cpu_percent = psutil.cpu_percent(interval=1)
|
|
|
|
|
current_time = time.time()
|
|
|
|
|
|
|
|
|
|
state_key = 'cpu_usage'
|
|
|
|
|
self.state_history[state_key].append({
|
|
|
|
|
'value': cpu_percent,
|
|
|
|
|
'time': current_time
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
self.state_history[state_key] = [
|
|
|
|
|
entry for entry in self.state_history[state_key]
|
|
|
|
|
if current_time - entry['time'] < 300
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
critical_duration = sum(
|
|
|
|
|
1 for entry in self.state_history[state_key]
|
|
|
|
|
if entry['value'] >= self.CPU_CRITICAL and
|
|
|
|
|
current_time - entry['time'] <= self.CPU_CRITICAL_DURATION
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
warning_duration = sum(
|
|
|
|
|
1 for entry in self.state_history[state_key]
|
|
|
|
|
if entry['value'] >= self.CPU_WARNING and
|
|
|
|
|
current_time - entry['time'] <= self.CPU_WARNING_DURATION
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
recovery_duration = sum(
|
|
|
|
|
1 for entry in self.state_history[state_key]
|
|
|
|
|
if entry['value'] < self.CPU_RECOVERY and
|
|
|
|
|
current_time - entry['time'] <= self.CPU_RECOVERY_DURATION
|
|
|
|
|
)
|
|
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
if critical_duration >= 2:
|
2025-11-05 18:30:31 +01:00
|
|
|
status = 'CRITICAL'
|
|
|
|
|
reason = f'CPU >{self.CPU_CRITICAL}% for {self.CPU_CRITICAL_DURATION}s'
|
|
|
|
|
elif warning_duration >= 2 and recovery_duration < 2:
|
|
|
|
|
status = 'WARNING'
|
|
|
|
|
reason = f'CPU >{self.CPU_WARNING}% for {self.CPU_WARNING_DURATION}s'
|
|
|
|
|
else:
|
|
|
|
|
status = 'OK'
|
|
|
|
|
reason = None
|
|
|
|
|
|
|
|
|
|
temp_status = self._check_cpu_temperature()
|
|
|
|
|
|
|
|
|
|
result = {
|
|
|
|
|
'status': status,
|
|
|
|
|
'usage': round(cpu_percent, 1),
|
|
|
|
|
'cores': psutil.cpu_count()
|
2025-11-04 22:28:42 +01:00
|
|
|
}
|
2025-11-05 18:30:31 +01:00
|
|
|
|
|
|
|
|
if reason:
|
|
|
|
|
result['reason'] = reason
|
|
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
if temp_status and temp_status.get('status') != 'UNKNOWN':
|
2025-11-05 18:30:31 +01:00
|
|
|
result['temperature'] = temp_status
|
|
|
|
|
if temp_status.get('status') == 'CRITICAL':
|
|
|
|
|
result['status'] = 'CRITICAL'
|
|
|
|
|
result['reason'] = temp_status.get('reason')
|
|
|
|
|
elif temp_status.get('status') == 'WARNING' and status == 'OK':
|
|
|
|
|
result['status'] = 'WARNING'
|
|
|
|
|
result['reason'] = temp_status.get('reason')
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return {'status': 'UNKNOWN', 'reason': f'CPU check failed: {str(e)}'}
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
def _check_cpu_temperature(self) -> Optional[Dict[str, Any]]:
|
2025-11-05 18:30:31 +01:00
|
|
|
"""Check CPU temperature (cached, max 1 check per minute)"""
|
|
|
|
|
cache_key = 'cpu_temp'
|
|
|
|
|
current_time = time.time()
|
|
|
|
|
|
|
|
|
|
if cache_key in self.last_check_times:
|
|
|
|
|
if current_time - self.last_check_times[cache_key] < 60:
|
2025-11-07 18:49:37 +01:00
|
|
|
return self.cached_results.get(cache_key)
|
2025-11-04 22:28:42 +01:00
|
|
|
|
|
|
|
|
try:
|
2025-11-05 18:30:31 +01:00
|
|
|
result = subprocess.run(
|
|
|
|
|
['sensors', '-A', '-u'],
|
|
|
|
|
capture_output=True,
|
|
|
|
|
text=True,
|
|
|
|
|
timeout=2
|
|
|
|
|
)
|
|
|
|
|
|
2025-11-04 22:28:42 +01:00
|
|
|
if result.returncode == 0:
|
2025-11-05 18:30:31 +01:00
|
|
|
temps = []
|
|
|
|
|
for line in result.stdout.split('\n'):
|
|
|
|
|
if 'temp' in line.lower() and '_input' in line:
|
|
|
|
|
try:
|
|
|
|
|
temp = float(line.split(':')[1].strip())
|
|
|
|
|
temps.append(temp)
|
|
|
|
|
except:
|
|
|
|
|
continue
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-05 18:30:31 +01:00
|
|
|
if temps:
|
|
|
|
|
max_temp = max(temps)
|
|
|
|
|
|
|
|
|
|
if max_temp >= self.TEMP_CRITICAL:
|
|
|
|
|
status = 'CRITICAL'
|
|
|
|
|
reason = f'CPU temperature {max_temp}°C ≥{self.TEMP_CRITICAL}°C'
|
|
|
|
|
elif max_temp >= self.TEMP_WARNING:
|
|
|
|
|
status = 'WARNING'
|
|
|
|
|
reason = f'CPU temperature {max_temp}°C ≥{self.TEMP_WARNING}°C'
|
2025-11-04 22:28:42 +01:00
|
|
|
else:
|
2025-11-05 18:30:31 +01:00
|
|
|
status = 'OK'
|
|
|
|
|
reason = None
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-05 18:30:31 +01:00
|
|
|
temp_result = {
|
2025-11-04 22:28:42 +01:00
|
|
|
'status': status,
|
2025-11-05 18:30:31 +01:00
|
|
|
'value': round(max_temp, 1),
|
|
|
|
|
'unit': '°C'
|
|
|
|
|
}
|
|
|
|
|
if reason:
|
|
|
|
|
temp_result['reason'] = reason
|
|
|
|
|
|
|
|
|
|
self.cached_results[cache_key] = temp_result
|
|
|
|
|
self.last_check_times[cache_key] = current_time
|
|
|
|
|
return temp_result
|
|
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
return None
|
2025-11-05 18:30:31 +01:00
|
|
|
|
|
|
|
|
except Exception:
|
2025-11-07 18:49:37 +01:00
|
|
|
return None
|
2025-11-05 18:30:31 +01:00
|
|
|
|
|
|
|
|
def _check_memory_comprehensive(self) -> Dict[str, Any]:
|
|
|
|
|
"""Check memory including RAM and swap with sustained thresholds"""
|
|
|
|
|
try:
|
|
|
|
|
memory = psutil.virtual_memory()
|
|
|
|
|
swap = psutil.swap_memory()
|
|
|
|
|
current_time = time.time()
|
|
|
|
|
|
|
|
|
|
mem_percent = memory.percent
|
|
|
|
|
swap_percent = swap.percent if swap.total > 0 else 0
|
|
|
|
|
swap_vs_ram = (swap.used / memory.total * 100) if memory.total > 0 else 0
|
|
|
|
|
|
|
|
|
|
state_key = 'memory_usage'
|
|
|
|
|
self.state_history[state_key].append({
|
|
|
|
|
'mem_percent': mem_percent,
|
|
|
|
|
'swap_percent': swap_percent,
|
|
|
|
|
'swap_vs_ram': swap_vs_ram,
|
|
|
|
|
'time': current_time
|
2025-11-04 22:28:42 +01:00
|
|
|
})
|
2025-11-05 18:30:31 +01:00
|
|
|
|
|
|
|
|
self.state_history[state_key] = [
|
|
|
|
|
entry for entry in self.state_history[state_key]
|
|
|
|
|
if current_time - entry['time'] < 600
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
mem_critical = sum(
|
|
|
|
|
1 for entry in self.state_history[state_key]
|
|
|
|
|
if entry['mem_percent'] >= self.MEMORY_CRITICAL and
|
|
|
|
|
current_time - entry['time'] <= self.MEMORY_DURATION
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
mem_warning = sum(
|
|
|
|
|
1 for entry in self.state_history[state_key]
|
|
|
|
|
if entry['mem_percent'] >= self.MEMORY_WARNING and
|
|
|
|
|
current_time - entry['time'] <= self.MEMORY_DURATION
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
swap_critical = sum(
|
|
|
|
|
1 for entry in self.state_history[state_key]
|
|
|
|
|
if entry['swap_vs_ram'] > self.SWAP_CRITICAL_PERCENT and
|
|
|
|
|
current_time - entry['time'] <= self.SWAP_CRITICAL_DURATION
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
swap_warning = sum(
|
|
|
|
|
1 for entry in self.state_history[state_key]
|
|
|
|
|
if entry['swap_percent'] > 0 and
|
|
|
|
|
current_time - entry['time'] <= self.SWAP_WARNING_DURATION
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if mem_critical >= 2:
|
|
|
|
|
status = 'CRITICAL'
|
|
|
|
|
reason = f'RAM >{self.MEMORY_CRITICAL}% for {self.MEMORY_DURATION}s'
|
|
|
|
|
elif swap_critical >= 2:
|
|
|
|
|
status = 'CRITICAL'
|
|
|
|
|
reason = f'Swap >{self.SWAP_CRITICAL_PERCENT}% of RAM for {self.SWAP_CRITICAL_DURATION}s'
|
|
|
|
|
elif mem_warning >= 2:
|
|
|
|
|
status = 'WARNING'
|
|
|
|
|
reason = f'RAM >{self.MEMORY_WARNING}% for {self.MEMORY_DURATION}s'
|
|
|
|
|
elif swap_warning >= 2:
|
|
|
|
|
status = 'WARNING'
|
|
|
|
|
reason = f'Swap active for >{self.SWAP_WARNING_DURATION}s'
|
|
|
|
|
else:
|
|
|
|
|
status = 'OK'
|
|
|
|
|
reason = None
|
|
|
|
|
|
|
|
|
|
result = {
|
|
|
|
|
'status': status,
|
|
|
|
|
'ram_percent': round(mem_percent, 1),
|
|
|
|
|
'ram_available_gb': round(memory.available / (1024**3), 2),
|
|
|
|
|
'swap_percent': round(swap_percent, 1),
|
|
|
|
|
'swap_used_gb': round(swap.used / (1024**3), 2)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if reason:
|
|
|
|
|
result['reason'] = reason
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return {'status': 'UNKNOWN', 'reason': f'Memory check failed: {str(e)}'}
|
|
|
|
|
|
2025-11-09 16:30:29 +01:00
|
|
|
def _check_storage_optimized(self) -> Dict[str, Any]:
|
2025-11-05 18:30:31 +01:00
|
|
|
"""
|
2025-11-09 16:30:29 +01:00
|
|
|
Optimized storage check - always returns status.
|
2025-11-07 18:49:37 +01:00
|
|
|
Checks critical mounts, LVM, and Proxmox storages.
|
2025-11-05 18:30:31 +01:00
|
|
|
"""
|
2025-11-07 18:49:37 +01:00
|
|
|
issues = []
|
|
|
|
|
storage_details = {}
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-05 18:30:31 +01:00
|
|
|
# Check critical filesystems
|
2025-11-07 18:49:37 +01:00
|
|
|
critical_mounts = ['/', '/var/lib/vz']
|
2025-11-05 18:30:31 +01:00
|
|
|
|
|
|
|
|
for mount_point in critical_mounts:
|
2025-11-09 16:30:29 +01:00
|
|
|
is_mounted = False
|
|
|
|
|
try:
|
|
|
|
|
result = subprocess.run(
|
|
|
|
|
['mountpoint', '-q', mount_point],
|
|
|
|
|
capture_output=True,
|
|
|
|
|
timeout=2
|
|
|
|
|
)
|
|
|
|
|
is_mounted = (result.returncode == 0)
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
if not is_mounted:
|
|
|
|
|
# Only report as error if it's supposed to exist
|
|
|
|
|
if mount_point == '/':
|
|
|
|
|
issues.append(f'{mount_point}: Not mounted')
|
|
|
|
|
storage_details[mount_point] = {
|
|
|
|
|
'status': 'CRITICAL',
|
|
|
|
|
'reason': 'Not mounted'
|
|
|
|
|
}
|
|
|
|
|
# For /var/lib/vz, it might not be a separate mount, check if dir exists
|
|
|
|
|
elif mount_point == '/var/lib/vz':
|
|
|
|
|
if os.path.exists(mount_point):
|
|
|
|
|
# It exists as directory, check usage
|
|
|
|
|
fs_status = self._check_filesystem(mount_point)
|
|
|
|
|
if fs_status['status'] != 'OK':
|
|
|
|
|
issues.append(f"{mount_point}: {fs_status['reason']}")
|
|
|
|
|
storage_details[mount_point] = fs_status
|
|
|
|
|
# If doesn't exist, skip silently (might use different storage)
|
2025-11-07 18:49:37 +01:00
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
fs_status = self._check_filesystem(mount_point)
|
|
|
|
|
if fs_status['status'] != 'OK':
|
|
|
|
|
issues.append(f"{mount_point}: {fs_status['reason']}")
|
|
|
|
|
storage_details[mount_point] = fs_status
|
2025-11-05 18:30:31 +01:00
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
# Check LVM
|
2025-11-05 18:30:31 +01:00
|
|
|
lvm_status = self._check_lvm()
|
2025-11-07 18:49:37 +01:00
|
|
|
if lvm_status and lvm_status.get('status') != 'OK':
|
|
|
|
|
issues.append(lvm_status.get('reason', 'LVM issue'))
|
|
|
|
|
storage_details['lvm'] = lvm_status
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
# Check Proxmox storages (PBS, NFS, etc)
|
2025-11-05 18:30:31 +01:00
|
|
|
pve_storages = self._check_proxmox_storages()
|
2025-11-07 18:49:37 +01:00
|
|
|
for storage_name, storage_data in pve_storages.items():
|
|
|
|
|
if storage_data.get('status') != 'OK':
|
|
|
|
|
issues.append(f"{storage_name}: {storage_data.get('reason', 'Storage issue')}")
|
|
|
|
|
storage_details[storage_name] = storage_data
|
2025-11-05 18:30:31 +01:00
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
if not issues:
|
|
|
|
|
return {'status': 'OK'}
|
|
|
|
|
|
|
|
|
|
# Determine overall status
|
|
|
|
|
has_critical = any(d.get('status') == 'CRITICAL' for d in storage_details.values())
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
'status': 'CRITICAL' if has_critical else 'WARNING',
|
|
|
|
|
'reason': '; '.join(issues[:3]),
|
|
|
|
|
'details': storage_details
|
|
|
|
|
}
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-05 18:30:31 +01:00
|
|
|
def _check_filesystem(self, mount_point: str) -> Dict[str, Any]:
|
|
|
|
|
"""Check individual filesystem for space and mount status"""
|
|
|
|
|
try:
|
|
|
|
|
result = subprocess.run(
|
|
|
|
|
['mountpoint', '-q', mount_point],
|
|
|
|
|
capture_output=True,
|
|
|
|
|
timeout=2
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if result.returncode != 0:
|
|
|
|
|
return {
|
|
|
|
|
'status': 'CRITICAL',
|
2025-11-07 18:49:37 +01:00
|
|
|
'reason': 'Not mounted'
|
2025-11-05 18:30:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Check if read-only
|
|
|
|
|
with open('/proc/mounts', 'r') as f:
|
|
|
|
|
for line in f:
|
|
|
|
|
parts = line.split()
|
|
|
|
|
if len(parts) >= 4 and parts[1] == mount_point:
|
|
|
|
|
options = parts[3].split(',')
|
|
|
|
|
if 'ro' in options:
|
|
|
|
|
return {
|
|
|
|
|
'status': 'CRITICAL',
|
|
|
|
|
'reason': 'Mounted read-only'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
usage = psutil.disk_usage(mount_point)
|
|
|
|
|
percent = usage.percent
|
|
|
|
|
|
|
|
|
|
if percent >= self.STORAGE_CRITICAL:
|
|
|
|
|
status = 'CRITICAL'
|
|
|
|
|
reason = f'{percent:.1f}% full (≥{self.STORAGE_CRITICAL}%)'
|
|
|
|
|
elif percent >= self.STORAGE_WARNING:
|
|
|
|
|
status = 'WARNING'
|
|
|
|
|
reason = f'{percent:.1f}% full (≥{self.STORAGE_WARNING}%)'
|
|
|
|
|
else:
|
|
|
|
|
status = 'OK'
|
|
|
|
|
reason = None
|
|
|
|
|
|
|
|
|
|
result = {
|
|
|
|
|
'status': status,
|
2025-11-07 18:49:37 +01:00
|
|
|
'usage_percent': round(percent, 1)
|
2025-11-05 18:30:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if reason:
|
|
|
|
|
result['reason'] = reason
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return {
|
|
|
|
|
'status': 'WARNING',
|
|
|
|
|
'reason': f'Check failed: {str(e)}'
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-09 16:30:29 +01:00
|
|
|
def _check_lvm(self) -> Dict[str, Any]:
|
|
|
|
|
"""Check LVM volumes - improved detection"""
|
2025-11-05 18:30:31 +01:00
|
|
|
try:
|
|
|
|
|
result = subprocess.run(
|
|
|
|
|
['lvs', '--noheadings', '--options', 'lv_name,vg_name,lv_attr'],
|
|
|
|
|
capture_output=True,
|
|
|
|
|
text=True,
|
|
|
|
|
timeout=3
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if result.returncode != 0:
|
2025-11-09 16:30:29 +01:00
|
|
|
return {'status': 'OK'}
|
2025-11-05 18:30:31 +01:00
|
|
|
|
|
|
|
|
volumes = []
|
|
|
|
|
|
|
|
|
|
for line in result.stdout.strip().split('\n'):
|
|
|
|
|
if line.strip():
|
|
|
|
|
parts = line.split()
|
|
|
|
|
if len(parts) >= 2:
|
|
|
|
|
lv_name = parts[0].strip()
|
|
|
|
|
vg_name = parts[1].strip()
|
|
|
|
|
volumes.append(f'{vg_name}/{lv_name}')
|
|
|
|
|
|
2025-11-09 16:30:29 +01:00
|
|
|
return {'status': 'OK', 'volumes': len(volumes)}
|
2025-11-05 18:30:31 +01:00
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
except Exception:
|
2025-11-09 16:30:29 +01:00
|
|
|
return {'status': 'OK'}
|
2025-11-05 18:30:31 +01:00
|
|
|
|
|
|
|
|
def _check_proxmox_storages(self) -> Dict[str, Any]:
|
2025-11-07 18:49:37 +01:00
|
|
|
"""Check Proxmox-specific storages (only report problems)"""
|
2025-11-05 18:30:31 +01:00
|
|
|
storages = {}
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
if os.path.exists('/etc/pve/storage.cfg'):
|
|
|
|
|
with open('/etc/pve/storage.cfg', 'r') as f:
|
|
|
|
|
current_storage = None
|
|
|
|
|
storage_type = None
|
|
|
|
|
|
|
|
|
|
for line in f:
|
|
|
|
|
line = line.strip()
|
|
|
|
|
|
|
|
|
|
if line.startswith('dir:') or line.startswith('nfs:') or \
|
|
|
|
|
line.startswith('cifs:') or line.startswith('pbs:'):
|
|
|
|
|
parts = line.split(':', 1)
|
|
|
|
|
storage_type = parts[0]
|
|
|
|
|
current_storage = parts[1].strip()
|
|
|
|
|
elif line.startswith('path ') and current_storage:
|
|
|
|
|
path = line.split(None, 1)[1]
|
|
|
|
|
|
|
|
|
|
if storage_type == 'dir':
|
2025-11-07 18:49:37 +01:00
|
|
|
if not os.path.exists(path):
|
2025-11-05 18:30:31 +01:00
|
|
|
storages[f'storage_{current_storage}'] = {
|
|
|
|
|
'status': 'CRITICAL',
|
|
|
|
|
'reason': 'Directory does not exist',
|
|
|
|
|
'type': 'dir',
|
|
|
|
|
'path': path
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
current_storage = None
|
|
|
|
|
storage_type = None
|
2025-11-07 18:49:37 +01:00
|
|
|
except Exception:
|
|
|
|
|
pass
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-05 18:30:31 +01:00
|
|
|
return storages
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-09 16:30:29 +01:00
|
|
|
def _check_disks_optimized(self) -> Dict[str, Any]:
|
2025-11-07 18:49:37 +01:00
|
|
|
"""
|
2025-11-09 16:30:29 +01:00
|
|
|
Optimized disk check - always returns status.
|
2025-11-07 18:49:37 +01:00
|
|
|
"""
|
2025-11-05 18:30:31 +01:00
|
|
|
current_time = time.time()
|
2025-11-07 18:49:37 +01:00
|
|
|
disk_issues = {}
|
2025-11-04 22:28:42 +01:00
|
|
|
|
|
|
|
|
try:
|
2025-11-07 18:49:37 +01:00
|
|
|
# Check dmesg for I/O errors
|
2025-11-05 18:30:31 +01:00
|
|
|
result = subprocess.run(
|
|
|
|
|
['dmesg', '-T', '--level=err,warn', '--since', '5 minutes ago'],
|
|
|
|
|
capture_output=True,
|
|
|
|
|
text=True,
|
|
|
|
|
timeout=2
|
|
|
|
|
)
|
|
|
|
|
|
2025-11-04 22:28:42 +01:00
|
|
|
if result.returncode == 0:
|
2025-11-05 18:30:31 +01:00
|
|
|
for line in result.stdout.split('\n'):
|
|
|
|
|
line_lower = line.lower()
|
|
|
|
|
if any(keyword in line_lower for keyword in ['i/o error', 'ata error', 'scsi error']):
|
|
|
|
|
for part in line.split():
|
|
|
|
|
if part.startswith('sd') or part.startswith('nvme') or part.startswith('hd'):
|
|
|
|
|
disk_name = part.rstrip(':,')
|
|
|
|
|
self.io_error_history[disk_name].append(current_time)
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
# Clean old history
|
2025-11-05 18:30:31 +01:00
|
|
|
for disk in list(self.io_error_history.keys()):
|
|
|
|
|
self.io_error_history[disk] = [
|
|
|
|
|
t for t in self.io_error_history[disk]
|
|
|
|
|
if current_time - t < 300
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
error_count = len(self.io_error_history[disk])
|
|
|
|
|
|
|
|
|
|
if error_count >= 3:
|
2025-11-07 18:49:37 +01:00
|
|
|
disk_issues[f'/dev/{disk}'] = {
|
2025-11-05 18:30:31 +01:00
|
|
|
'status': 'CRITICAL',
|
|
|
|
|
'reason': f'{error_count} I/O errors in 5 minutes'
|
|
|
|
|
}
|
|
|
|
|
elif error_count >= 1:
|
2025-11-07 18:49:37 +01:00
|
|
|
disk_issues[f'/dev/{disk}'] = {
|
2025-11-05 18:30:31 +01:00
|
|
|
'status': 'WARNING',
|
|
|
|
|
'reason': f'{error_count} I/O error(s) in 5 minutes'
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
if not disk_issues:
|
|
|
|
|
return {'status': 'OK'}
|
2025-11-05 18:30:31 +01:00
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
has_critical = any(d.get('status') == 'CRITICAL' for d in disk_issues.values())
|
2025-11-05 18:30:31 +01:00
|
|
|
|
|
|
|
|
return {
|
2025-11-07 18:49:37 +01:00
|
|
|
'status': 'CRITICAL' if has_critical else 'WARNING',
|
|
|
|
|
'reason': f"{len(disk_issues)} disk(s) with errors",
|
|
|
|
|
'details': disk_issues
|
2025-11-05 18:30:31 +01:00
|
|
|
}
|
2025-11-07 18:49:37 +01:00
|
|
|
|
|
|
|
|
except Exception:
|
2025-11-09 16:30:29 +01:00
|
|
|
return {'status': 'OK'}
|
2025-11-05 18:30:31 +01:00
|
|
|
|
2025-11-09 16:30:29 +01:00
|
|
|
def _check_network_optimized(self) -> Dict[str, Any]:
|
2025-11-07 18:49:37 +01:00
|
|
|
"""
|
2025-11-09 16:30:29 +01:00
|
|
|
Optimized network check - always returns status.
|
2025-11-07 18:49:37 +01:00
|
|
|
"""
|
2025-11-05 18:30:31 +01:00
|
|
|
try:
|
|
|
|
|
issues = []
|
|
|
|
|
interface_details = {}
|
|
|
|
|
|
|
|
|
|
net_if_stats = psutil.net_if_stats()
|
|
|
|
|
|
|
|
|
|
for interface, stats in net_if_stats.items():
|
|
|
|
|
if interface == 'lo':
|
|
|
|
|
continue
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
# Check if important interface is down
|
2025-11-05 18:30:31 +01:00
|
|
|
if not stats.isup:
|
|
|
|
|
if interface.startswith('vmbr') or interface.startswith('eth') or interface.startswith('ens'):
|
|
|
|
|
issues.append(f'{interface} is DOWN')
|
|
|
|
|
interface_details[interface] = {
|
|
|
|
|
'status': 'CRITICAL',
|
|
|
|
|
'reason': 'Interface DOWN'
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
# Check connectivity
|
2025-11-05 18:30:31 +01:00
|
|
|
latency_status = self._check_network_latency()
|
2025-11-07 18:49:37 +01:00
|
|
|
if latency_status and latency_status.get('status') not in ['OK', 'UNKNOWN']:
|
2025-11-05 18:30:31 +01:00
|
|
|
issues.append(latency_status.get('reason', 'Network latency issue'))
|
|
|
|
|
interface_details['connectivity'] = latency_status
|
|
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
if not issues:
|
|
|
|
|
return {'status': 'OK'}
|
2025-11-05 18:30:31 +01:00
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
has_critical = any(d.get('status') == 'CRITICAL' for d in interface_details.values())
|
2025-11-05 18:30:31 +01:00
|
|
|
|
|
|
|
|
return {
|
2025-11-07 18:49:37 +01:00
|
|
|
'status': 'CRITICAL' if has_critical else 'WARNING',
|
|
|
|
|
'reason': '; '.join(issues[:2]),
|
|
|
|
|
'details': interface_details
|
2025-11-05 18:30:31 +01:00
|
|
|
}
|
2025-11-07 18:49:37 +01:00
|
|
|
|
|
|
|
|
except Exception:
|
2025-11-09 16:30:29 +01:00
|
|
|
return {'status': 'OK'}
|
2025-11-05 18:30:31 +01:00
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
def _check_network_latency(self) -> Optional[Dict[str, Any]]:
|
|
|
|
|
"""Check network latency to 1.1.1.1 (cached)"""
|
2025-11-05 18:30:31 +01:00
|
|
|
cache_key = 'network_latency'
|
|
|
|
|
current_time = time.time()
|
|
|
|
|
|
|
|
|
|
if cache_key in self.last_check_times:
|
|
|
|
|
if current_time - self.last_check_times[cache_key] < 60:
|
2025-11-07 18:49:37 +01:00
|
|
|
return self.cached_results.get(cache_key)
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-05 18:30:31 +01:00
|
|
|
try:
|
|
|
|
|
result = subprocess.run(
|
|
|
|
|
['ping', '-c', '1', '-W', '1', '1.1.1.1'],
|
|
|
|
|
capture_output=True,
|
|
|
|
|
text=True,
|
|
|
|
|
timeout=self.NETWORK_TIMEOUT
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if result.returncode == 0:
|
|
|
|
|
for line in result.stdout.split('\n'):
|
|
|
|
|
if 'time=' in line:
|
|
|
|
|
try:
|
|
|
|
|
latency_str = line.split('time=')[1].split()[0]
|
|
|
|
|
latency = float(latency_str)
|
|
|
|
|
|
|
|
|
|
if latency > self.NETWORK_LATENCY_CRITICAL:
|
|
|
|
|
status = 'CRITICAL'
|
|
|
|
|
reason = f'Latency {latency:.1f}ms >{self.NETWORK_LATENCY_CRITICAL}ms'
|
|
|
|
|
elif latency > self.NETWORK_LATENCY_WARNING:
|
|
|
|
|
status = 'WARNING'
|
|
|
|
|
reason = f'Latency {latency:.1f}ms >{self.NETWORK_LATENCY_WARNING}ms'
|
|
|
|
|
else:
|
|
|
|
|
status = 'OK'
|
|
|
|
|
reason = None
|
|
|
|
|
|
|
|
|
|
latency_result = {
|
|
|
|
|
'status': status,
|
|
|
|
|
'latency_ms': round(latency, 1)
|
|
|
|
|
}
|
|
|
|
|
if reason:
|
|
|
|
|
latency_result['reason'] = reason
|
|
|
|
|
|
|
|
|
|
self.cached_results[cache_key] = latency_result
|
|
|
|
|
self.last_check_times[cache_key] = current_time
|
|
|
|
|
return latency_result
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
packet_loss_result = {
|
|
|
|
|
'status': 'CRITICAL',
|
|
|
|
|
'reason': 'Packet loss or timeout'
|
|
|
|
|
}
|
|
|
|
|
self.cached_results[cache_key] = packet_loss_result
|
|
|
|
|
self.last_check_times[cache_key] = current_time
|
|
|
|
|
return packet_loss_result
|
|
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
except Exception:
|
|
|
|
|
return None
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-09 16:30:29 +01:00
|
|
|
def _check_vms_cts_optimized(self) -> Dict[str, Any]:
|
2025-11-07 18:49:37 +01:00
|
|
|
"""
|
2025-11-09 16:30:29 +01:00
|
|
|
Optimized VM/CT check - detects qmp failures and other VM errors.
|
|
|
|
|
Now parses logs for VM/CT specific errors like qmp command failures.
|
2025-11-07 18:49:37 +01:00
|
|
|
"""
|
2025-11-04 22:28:42 +01:00
|
|
|
try:
|
2025-11-05 18:30:31 +01:00
|
|
|
issues = []
|
|
|
|
|
vm_details = {}
|
|
|
|
|
|
2025-11-09 16:30:29 +01:00
|
|
|
# Check logs for VM/CT errors
|
2025-11-07 18:49:37 +01:00
|
|
|
result = subprocess.run(
|
2025-11-09 16:30:29 +01:00
|
|
|
['journalctl', '--since', '10 minutes ago', '--no-pager', '-u', 'pve*', '-p', 'warning'],
|
2025-11-07 18:49:37 +01:00
|
|
|
capture_output=True,
|
|
|
|
|
text=True,
|
|
|
|
|
timeout=3
|
|
|
|
|
)
|
2025-11-05 18:30:31 +01:00
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
if result.returncode == 0:
|
|
|
|
|
for line in result.stdout.split('\n'):
|
|
|
|
|
line_lower = line.lower()
|
|
|
|
|
|
2025-11-09 16:30:29 +01:00
|
|
|
# Pattern 1: "VM 106 qmp command failed"
|
|
|
|
|
vm_qmp_match = re.search(r'vm\s+(\d+)\s+qmp\s+command', line_lower)
|
|
|
|
|
if vm_qmp_match:
|
|
|
|
|
vmid = vm_qmp_match.group(1)
|
|
|
|
|
key = f'vm_{vmid}'
|
|
|
|
|
if key not in vm_details:
|
|
|
|
|
issues.append(f'VM {vmid}: QMP command error')
|
|
|
|
|
vm_details[key] = {
|
|
|
|
|
'status': 'WARNING',
|
|
|
|
|
'reason': 'QMP command failed',
|
|
|
|
|
'id': vmid,
|
|
|
|
|
'type': 'VM'
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
# Pattern 2: "CT 103 error" or "Container 103"
|
|
|
|
|
ct_match = re.search(r'(?:ct|container)\s+(\d+)', line_lower)
|
|
|
|
|
if ct_match and ('error' in line_lower or 'fail' in line_lower):
|
|
|
|
|
ctid = ct_match.group(1)
|
|
|
|
|
key = f'ct_{ctid}'
|
|
|
|
|
if key not in vm_details:
|
|
|
|
|
issues.append(f'CT {ctid}: Error detected')
|
|
|
|
|
vm_details[key] = {
|
|
|
|
|
'status': 'WARNING',
|
|
|
|
|
'reason': 'Container error',
|
|
|
|
|
'id': ctid,
|
|
|
|
|
'type': 'CT'
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
# Pattern 3: Generic VM/CT start failures
|
2025-11-07 18:49:37 +01:00
|
|
|
if 'failed to start' in line_lower or 'error starting' in line_lower or \
|
|
|
|
|
'start error' in line_lower or 'cannot start' in line_lower:
|
|
|
|
|
# Extract VM/CT ID
|
2025-11-09 16:30:29 +01:00
|
|
|
id_match = re.search(r'\b(\d{3,4})\b', line)
|
|
|
|
|
if id_match:
|
|
|
|
|
vmid = id_match.group(1)
|
|
|
|
|
key = f'vmct_{vmid}'
|
|
|
|
|
if key not in vm_details:
|
|
|
|
|
issues.append(f'VM/CT {vmid}: Failed to start')
|
|
|
|
|
vm_details[key] = {
|
|
|
|
|
'status': 'CRITICAL',
|
|
|
|
|
'reason': 'Failed to start',
|
|
|
|
|
'id': vmid,
|
|
|
|
|
'type': 'VM/CT'
|
|
|
|
|
}
|
2025-11-05 18:30:31 +01:00
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
if not issues:
|
|
|
|
|
return {'status': 'OK'}
|
2025-11-05 18:30:31 +01:00
|
|
|
|
2025-11-09 16:30:29 +01:00
|
|
|
has_critical = any(d.get('status') == 'CRITICAL' for d in vm_details.values())
|
|
|
|
|
|
2025-11-05 18:30:31 +01:00
|
|
|
return {
|
2025-11-09 16:30:29 +01:00
|
|
|
'status': 'CRITICAL' if has_critical else 'WARNING',
|
2025-11-07 18:49:37 +01:00
|
|
|
'reason': '; '.join(issues[:3]),
|
|
|
|
|
'details': vm_details
|
2025-11-05 18:30:31 +01:00
|
|
|
}
|
2025-11-07 18:49:37 +01:00
|
|
|
|
|
|
|
|
except Exception:
|
2025-11-09 16:30:29 +01:00
|
|
|
return {'status': 'OK'}
|
2025-11-05 18:30:31 +01:00
|
|
|
|
|
|
|
|
def _check_pve_services(self) -> Dict[str, Any]:
|
|
|
|
|
"""Check critical Proxmox services"""
|
|
|
|
|
try:
|
|
|
|
|
failed_services = []
|
|
|
|
|
|
|
|
|
|
for service in self.PVE_SERVICES:
|
|
|
|
|
try:
|
|
|
|
|
result = subprocess.run(
|
|
|
|
|
['systemctl', 'is-active', service],
|
|
|
|
|
capture_output=True,
|
|
|
|
|
text=True,
|
|
|
|
|
timeout=2
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if result.returncode != 0 or result.stdout.strip() != 'active':
|
|
|
|
|
failed_services.append(service)
|
|
|
|
|
except Exception:
|
|
|
|
|
failed_services.append(service)
|
|
|
|
|
|
|
|
|
|
if failed_services:
|
|
|
|
|
return {
|
|
|
|
|
'status': 'CRITICAL',
|
|
|
|
|
'reason': f'Services inactive: {", ".join(failed_services)}',
|
|
|
|
|
'failed': failed_services
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {'status': 'OK'}
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return {
|
|
|
|
|
'status': 'WARNING',
|
|
|
|
|
'reason': f'Service check failed: {str(e)}'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def _check_logs_lightweight(self) -> Dict[str, Any]:
|
|
|
|
|
"""Lightweight log analysis (cached, checked every 5 minutes)"""
|
|
|
|
|
cache_key = 'logs_analysis'
|
|
|
|
|
current_time = time.time()
|
|
|
|
|
|
|
|
|
|
if cache_key in self.last_check_times:
|
|
|
|
|
if current_time - self.last_check_times[cache_key] < self.LOG_CHECK_INTERVAL:
|
|
|
|
|
return self.cached_results.get(cache_key, {'status': 'OK'})
|
|
|
|
|
|
|
|
|
|
try:
|
2025-11-04 22:28:42 +01:00
|
|
|
result = subprocess.run(
|
2025-11-05 18:30:31 +01:00
|
|
|
['journalctl', '--since', '5 minutes ago', '--no-pager', '-p', 'warning'],
|
2025-11-04 22:28:42 +01:00
|
|
|
capture_output=True,
|
|
|
|
|
text=True,
|
2025-11-05 18:30:31 +01:00
|
|
|
timeout=3
|
2025-11-04 22:28:42 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if result.returncode == 0:
|
2025-11-05 18:30:31 +01:00
|
|
|
lines = result.stdout.strip().split('\n')
|
|
|
|
|
|
|
|
|
|
errors_5m = 0
|
|
|
|
|
warnings_5m = 0
|
|
|
|
|
critical_keywords_found = []
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-05 18:30:31 +01:00
|
|
|
for line in lines:
|
|
|
|
|
line_lower = line.lower()
|
|
|
|
|
|
|
|
|
|
for keyword in self.CRITICAL_LOG_KEYWORDS:
|
|
|
|
|
if keyword.lower() in line_lower:
|
|
|
|
|
critical_keywords_found.append(keyword)
|
|
|
|
|
errors_5m += 1
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
if 'error' in line_lower or 'critical' in line_lower or 'fatal' in line_lower:
|
|
|
|
|
errors_5m += 1
|
|
|
|
|
elif 'warning' in line_lower or 'warn' in line_lower:
|
|
|
|
|
warnings_5m += 1
|
|
|
|
|
|
|
|
|
|
if critical_keywords_found:
|
|
|
|
|
status = 'CRITICAL'
|
|
|
|
|
reason = f'Critical errors: {", ".join(set(critical_keywords_found[:3]))}'
|
|
|
|
|
elif errors_5m >= self.LOG_ERRORS_CRITICAL:
|
|
|
|
|
status = 'CRITICAL'
|
2025-11-07 18:49:37 +01:00
|
|
|
reason = f'{errors_5m} errors in 5 minutes'
|
2025-11-05 18:30:31 +01:00
|
|
|
elif warnings_5m >= self.LOG_WARNINGS_CRITICAL:
|
2025-11-07 18:49:37 +01:00
|
|
|
status = 'WARNING'
|
|
|
|
|
reason = f'{warnings_5m} warnings in 5 minutes'
|
2025-11-05 18:30:31 +01:00
|
|
|
elif errors_5m >= self.LOG_ERRORS_WARNING:
|
|
|
|
|
status = 'WARNING'
|
|
|
|
|
reason = f'{errors_5m} errors in 5 minutes'
|
|
|
|
|
elif warnings_5m >= self.LOG_WARNINGS_WARNING:
|
|
|
|
|
status = 'WARNING'
|
|
|
|
|
reason = f'{warnings_5m} warnings in 5 minutes'
|
2025-11-04 22:28:42 +01:00
|
|
|
else:
|
2025-11-05 18:30:31 +01:00
|
|
|
status = 'OK'
|
|
|
|
|
reason = None
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
log_result = {'status': status}
|
2025-11-05 18:30:31 +01:00
|
|
|
if reason:
|
|
|
|
|
log_result['reason'] = reason
|
|
|
|
|
|
|
|
|
|
self.cached_results[cache_key] = log_result
|
|
|
|
|
self.last_check_times[cache_key] = current_time
|
|
|
|
|
return log_result
|
|
|
|
|
|
|
|
|
|
ok_result = {'status': 'OK'}
|
|
|
|
|
self.cached_results[cache_key] = ok_result
|
|
|
|
|
self.last_check_times[cache_key] = current_time
|
|
|
|
|
return ok_result
|
|
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
except Exception:
|
|
|
|
|
return {'status': 'OK'}
|
2025-11-05 18:30:31 +01:00
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
def _check_updates(self) -> Optional[Dict[str, Any]]:
|
|
|
|
|
"""Check for pending system updates (cached, checked every 10 minutes)"""
|
|
|
|
|
cache_key = 'updates_check'
|
|
|
|
|
current_time = time.time()
|
|
|
|
|
|
|
|
|
|
if cache_key in self.last_check_times:
|
|
|
|
|
if current_time - self.last_check_times[cache_key] < 600:
|
|
|
|
|
return self.cached_results.get(cache_key)
|
|
|
|
|
|
2025-11-05 18:30:31 +01:00
|
|
|
try:
|
2025-11-07 18:49:37 +01:00
|
|
|
# Check apt updates
|
|
|
|
|
result = subprocess.run(
|
|
|
|
|
['apt', 'list', '--upgradable'],
|
|
|
|
|
capture_output=True,
|
|
|
|
|
text=True,
|
|
|
|
|
timeout=5
|
|
|
|
|
)
|
2025-11-05 18:30:31 +01:00
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
if result.returncode == 0:
|
|
|
|
|
lines = result.stdout.strip().split('\n')
|
|
|
|
|
# First line is header
|
|
|
|
|
update_count = len([l for l in lines if l and not l.startswith('Listing')])
|
2025-11-05 18:30:31 +01:00
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
if update_count >= self.UPDATES_CRITICAL:
|
|
|
|
|
status = 'WARNING'
|
|
|
|
|
reason = f'{update_count} updates pending (≥{self.UPDATES_CRITICAL})'
|
|
|
|
|
elif update_count >= self.UPDATES_WARNING:
|
|
|
|
|
status = 'WARNING'
|
|
|
|
|
reason = f'{update_count} updates pending'
|
|
|
|
|
else:
|
|
|
|
|
status = 'OK'
|
|
|
|
|
reason = None
|
|
|
|
|
|
|
|
|
|
update_result = {
|
|
|
|
|
'status': status,
|
|
|
|
|
'count': update_count
|
|
|
|
|
}
|
|
|
|
|
if reason:
|
|
|
|
|
update_result['reason'] = reason
|
|
|
|
|
|
|
|
|
|
self.cached_results[cache_key] = update_result
|
|
|
|
|
self.last_check_times[cache_key] = current_time
|
|
|
|
|
return update_result
|
|
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def _check_security(self) -> Dict[str, Any]:
|
|
|
|
|
"""Check security-related items (certificates, uptime)"""
|
|
|
|
|
try:
|
|
|
|
|
issues = []
|
2025-11-05 18:30:31 +01:00
|
|
|
|
|
|
|
|
# Check uptime (warning if >180 days)
|
|
|
|
|
try:
|
|
|
|
|
uptime_seconds = time.time() - psutil.boot_time()
|
|
|
|
|
uptime_days = uptime_seconds / 86400
|
|
|
|
|
|
|
|
|
|
if uptime_days > 180:
|
|
|
|
|
issues.append(f'Uptime {int(uptime_days)} days (>180)')
|
|
|
|
|
except Exception:
|
|
|
|
|
pass
|
|
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
# Check SSL certificates
|
2025-11-05 18:30:31 +01:00
|
|
|
cert_status = self._check_certificates()
|
2025-11-07 18:49:37 +01:00
|
|
|
if cert_status and cert_status.get('status') != 'OK':
|
2025-11-05 18:30:31 +01:00
|
|
|
issues.append(cert_status.get('reason', 'Certificate issue'))
|
|
|
|
|
|
|
|
|
|
if issues:
|
|
|
|
|
return {
|
|
|
|
|
'status': 'WARNING',
|
|
|
|
|
'reason': '; '.join(issues[:2])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {'status': 'OK'}
|
|
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
except Exception:
|
|
|
|
|
return {'status': 'OK'}
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
def _check_certificates(self) -> Optional[Dict[str, Any]]:
|
2025-11-05 18:30:31 +01:00
|
|
|
"""Check SSL certificate expiration (cached, checked once per day)"""
|
|
|
|
|
cache_key = 'certificates'
|
|
|
|
|
current_time = time.time()
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-05 18:30:31 +01:00
|
|
|
if cache_key in self.last_check_times:
|
|
|
|
|
if current_time - self.last_check_times[cache_key] < 86400:
|
2025-11-07 18:49:37 +01:00
|
|
|
return self.cached_results.get(cache_key)
|
2025-11-04 22:28:42 +01:00
|
|
|
|
2025-11-05 18:30:31 +01:00
|
|
|
try:
|
|
|
|
|
cert_path = '/etc/pve/local/pve-ssl.pem'
|
|
|
|
|
|
|
|
|
|
if os.path.exists(cert_path):
|
|
|
|
|
result = subprocess.run(
|
|
|
|
|
['openssl', 'x509', '-enddate', '-noout', '-in', cert_path],
|
|
|
|
|
capture_output=True,
|
|
|
|
|
text=True,
|
|
|
|
|
timeout=2
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if result.returncode == 0:
|
|
|
|
|
date_str = result.stdout.strip().replace('notAfter=', '')
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
exp_date = datetime.strptime(date_str, '%b %d %H:%M:%S %Y %Z')
|
|
|
|
|
days_until_expiry = (exp_date - datetime.now()).days
|
|
|
|
|
|
|
|
|
|
if days_until_expiry < 0:
|
|
|
|
|
status = 'CRITICAL'
|
|
|
|
|
reason = 'Certificate expired'
|
|
|
|
|
elif days_until_expiry < 15:
|
|
|
|
|
status = 'WARNING'
|
|
|
|
|
reason = f'Certificate expires in {days_until_expiry} days'
|
|
|
|
|
else:
|
|
|
|
|
status = 'OK'
|
|
|
|
|
reason = None
|
|
|
|
|
|
|
|
|
|
cert_result = {'status': status}
|
|
|
|
|
if reason:
|
|
|
|
|
cert_result['reason'] = reason
|
|
|
|
|
|
|
|
|
|
self.cached_results[cache_key] = cert_result
|
|
|
|
|
self.last_check_times[cache_key] = current_time
|
|
|
|
|
return cert_result
|
|
|
|
|
except Exception:
|
|
|
|
|
pass
|
|
|
|
|
|
2025-11-07 18:49:37 +01:00
|
|
|
return None
|
2025-11-05 18:30:31 +01:00
|
|
|
|
|
|
|
|
except Exception:
|
2025-11-07 18:49:37 +01:00
|
|
|
return None
|
2025-11-05 18:30:31 +01:00
|
|
|
|
2025-11-04 22:28:42 +01:00
|
|
|
|
|
|
|
|
# Global instance
|
|
|
|
|
health_monitor = HealthMonitor()
|