-
Notifications
You must be signed in to change notification settings - Fork 2
/
self_healing.py
95 lines (82 loc) · 3.25 KB
/
self_healing.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
92
93
94
95
# self_healing.py
import logging
import time
import traceback
import psutil
logging.basicConfig(level=logging.INFO, format='%(asctime)s:%(levelname)s:%(message)s')
class SelfHealingSystem:
"""
Self-Healing System class that monitors and heals itself.
"""
def __init__(self, check_interval=10):
"""
Initializes the self-healing system.
:param check_interval: Time interval (in seconds) between health checks
"""
self.check_interval = check_interval
def is_system_healthy(self) -> bool:
"""
Checks if the system is healthy.
:returns: True if the system is healthy, False otherwise.
"""
try:
logging.info("Checking system health...")
cpu_usage = psutil.cpu_percent(interval=1)
memory_info = psutil.virtual_memory()
disk_usage = psutil.disk_usage('/')
health_status = cpu_usage < 80 and memory_info.percent < 80 and disk_usage.percent < 80
logging.info(f"CPU Usage: {cpu_usage}%, Memory Usage: {memory_info.percent}%, Disk Usage: {disk_usage.percent}%")
return health_status
except Exception as e:
logging.error(f"Error checking system health: {e}")
return False
def heal_system(self):
"""
Tries to heal the system if an issue is detected.
"""
try:
logging.info("Attempting to heal the system...")
# Example heal method: Restarting a service (placeholder)
# self.restart_service("example_service")
except Exception as e:
logging.error(f"Error during healing process: {e}")
logging.debug(traceback.format_exc())
def monitor(self):
"""
Continuously monitors the system and applies healing mechanisms if needed.
"""
while True:
if self.is_system_healthy():
logging.info("System is healthy.")
else:
logging.error("System is unhealthy! Initiating heal process.")
self.heal_system()
time.sleep(self.check_interval)
def check_database_connection(self) -> bool:
"""
Check if the database connection is alive.
:returns: True if connection is alive, otherwise False.
"""
try:
# Placeholder for actual database connection check.
# db_connection = self.get_database_connection()
# return db_connection.is_alive()
return True # Simulating a live database connection.
except Exception as e:
logging.error(f"Database connection check failed: {e}")
return False
def restart_service(self, service_name: str):
"""
Restart the given service.
:param service_name: Name of the service to restart.
"""
try:
logging.info(f"Restarting service: {service_name}")
# Placeholder for actual service restart logic.
# service_manager.restart(service_name)
except Exception as e:
logging.error(f"Failed to restart service {service_name}: {e}")
# Example usage
if __name__ == "__main__":
healer = SelfHealingSystem(check_interval=5)
healer.monitor()