-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhost_resource_checker.py
91 lines (75 loc) · 3.93 KB
/
host_resource_checker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import logging
import json
class HostResourceChecker:
"""
Class to check and monitor host resource usage via SSH.
"""
def __init__(self, ssh_client):
"""
Initialize the HostResourceChecker with an SSH client.
:param ssh_client: Instance of SSH client for executing remote commands.
"""
self.ssh_client = ssh_client
self.logger = logging.getLogger("host_resource_checker")
def check_host_resources(self, max_host_cpu_percent, max_host_ram_percent):
"""
Check host CPU and RAM usage against specified thresholds.
:param max_host_cpu_percent: Maximum allowable CPU usage percentage.
:param max_host_ram_percent: Maximum allowable RAM usage percentage.
:return: True if resources are within limits, False otherwise.
"""
try:
# Command to retrieve host resource status
command = "pvesh get /nodes/$(hostname)/status --output-format json"
output, error, exit_status = self.ssh_client.execute_command(command) # Properly unpack the tuple
# Debug logging
self.logger.debug(f"Raw command output: {output}")
self.logger.debug(f"Error output: {error}")
self.logger.debug(f"Exit status: {exit_status}")
# Check for error output
if error:
raise Exception(f"Command execution error: {error}")
# Make sure output is a string
if not isinstance(output, str):
output = output.decode() if isinstance(output, bytes) else str(output)
# Parse JSON response
data = json.loads(output.strip()) # Add strip() to remove any whitespace
# Rest of your code remains the same
if 'cpu' not in data or 'memory' not in data:
raise KeyError("Missing 'cpu' or 'memory' in the command output.")
# Extract and calculate CPU usage
host_cpu_usage = data['cpu'] * 100 # Convert to percentage
# Extract memory details
memory_data = data['memory']
total_mem = memory_data.get('total', 1) # Avoid division by zero
used_mem = memory_data.get('used', 0)
cached_mem = memory_data.get('cached', 0)
free_mem = memory_data.get('free', 0)
# Calculate RAM usage as a percentage
available_mem = free_mem + cached_mem
host_ram_usage = ((total_mem - available_mem) / total_mem) * 100
# Log resource usage
self.logger.info(f"Host CPU Usage: {host_cpu_usage:.2f}%, "
f"Host RAM Usage: {host_ram_usage:.2f}%")
# Check CPU usage threshold
if host_cpu_usage > max_host_cpu_percent:
self.logger.warning(f"Host CPU usage exceeds maximum allowed limit: "
f"{host_cpu_usage:.2f}% > {max_host_cpu_percent}%")
return False
# Check RAM usage threshold
if host_ram_usage > max_host_ram_percent:
self.logger.warning(f"Host RAM usage exceeds maximum allowed limit: "
f"{host_ram_usage:.2f}% > {max_host_ram_percent}%")
return False
# Resources are within limits
return True
except json.JSONDecodeError as json_err:
self.logger.error(f"Failed to parse JSON output: {str(json_err)}")
self.logger.error(f"Raw output causing JSON error: {output}")
raise
except KeyError as key_err:
self.logger.error(f"Missing data in the response: {str(key_err)}")
raise
except Exception as e:
self.logger.error(f"Failed to check host resources: {str(e)}")
raise