-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathsystem_health_monitor.py
More file actions
1101 lines (935 loc) · 44.8 KB
/
Copy pathsystem_health_monitor.py
File metadata and controls
1101 lines (935 loc) · 44.8 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
System Health Monitor for SIM-ONE Framework
This component provides comprehensive system health monitoring capabilities,
focusing on hardware resources, system performance, and operational metrics
while adhering to the Five Laws of Cognitive Governance.
Key Features:
- Real-time hardware monitoring (CPU, memory, disk, network)
- Process and thread monitoring
- System resource utilization analysis
- Performance bottleneck detection
- Energy-efficient monitoring with adaptive sampling
- Historical trend analysis
- Predictive health assessment
- Integration with Five Laws compliance monitoring
"""
import time
import logging
import psutil
import threading
import statistics
from dataclasses import dataclass, field
from typing import Dict, List, Optional, NamedTuple, Callable
from collections import deque, defaultdict
from enum import Enum
import json
import os
import platform
from datetime import datetime, timedelta
logger = logging.getLogger(__name__)
class HealthStatus(Enum):
"""System health status levels"""
EXCELLENT = "excellent" # All metrics optimal
GOOD = "good" # Minor issues, no action needed
WARNING = "warning" # Issues present, monitoring required
CRITICAL = "critical" # Immediate attention required
UNKNOWN = "unknown" # Unable to determine status
class ResourceType(Enum):
"""Types of system resources to monitor"""
CPU = "cpu"
MEMORY = "memory"
DISK = "disk"
NETWORK = "network"
PROCESSES = "processes"
SYSTEM = "system"
@dataclass
class CPUMetrics:
"""CPU performance metrics"""
usage_percent: float = 0.0
user_time: float = 0.0
system_time: float = 0.0
idle_time: float = 0.0
iowait: float = 0.0
core_count: int = 0
frequency_mhz: float = 0.0
load_average: List[float] = field(default_factory=list)
temperature_celsius: Optional[float] = None
timestamp: float = field(default_factory=time.time)
@dataclass
class MemoryMetrics:
"""Memory usage metrics"""
total_bytes: int = 0
available_bytes: int = 0
used_bytes: int = 0
usage_percent: float = 0.0
swap_total_bytes: int = 0
swap_used_bytes: int = 0
swap_percent: float = 0.0
cached_bytes: int = 0
buffers_bytes: int = 0
timestamp: float = field(default_factory=time.time)
@dataclass
class DiskMetrics:
"""Disk usage and I/O metrics"""
total_bytes: int = 0
used_bytes: int = 0
free_bytes: int = 0
usage_percent: float = 0.0
read_count: int = 0
write_count: int = 0
read_bytes: int = 0
write_bytes: int = 0
read_time_ms: int = 0
write_time_ms: int = 0
io_time_ms: int = 0
timestamp: float = field(default_factory=time.time)
@dataclass
class NetworkMetrics:
"""Network interface metrics"""
bytes_sent: int = 0
bytes_recv: int = 0
packets_sent: int = 0
packets_recv: int = 0
errors_in: int = 0
errors_out: int = 0
drops_in: int = 0
drops_out: int = 0
connections_established: int = 0
connections_listening: int = 0
timestamp: float = field(default_factory=time.time)
@dataclass
class ProcessMetrics:
"""Process monitoring metrics"""
total_processes: int = 0
running_processes: int = 0
sleeping_processes: int = 0
zombie_processes: int = 0
open_files: int = 0
threads: int = 0
memory_rss_bytes: int = 0
memory_vms_bytes: int = 0
cpu_percent: float = 0.0
timestamp: float = field(default_factory=time.time)
@dataclass
class SystemMetrics:
"""Overall system metrics"""
uptime_seconds: float = 0.0
boot_time: float = 0.0
users_count: int = 0
context_switches: int = 0
interrupts: int = 0
soft_interrupts: int = 0
system_calls: int = 0
platform_info: str = ""
timestamp: float = field(default_factory=time.time)
@dataclass
class HealthAssessment:
"""Comprehensive health assessment"""
overall_status: HealthStatus = HealthStatus.UNKNOWN
cpu_status: HealthStatus = HealthStatus.UNKNOWN
memory_status: HealthStatus = HealthStatus.UNKNOWN
disk_status: HealthStatus = HealthStatus.UNKNOWN
network_status: HealthStatus = HealthStatus.UNKNOWN
process_status: HealthStatus = HealthStatus.UNKNOWN
system_status: HealthStatus = HealthStatus.UNKNOWN
score: float = 0.0 # 0-100 overall health score
issues: List[str] = field(default_factory=list)
recommendations: List[str] = field(default_factory=list)
timestamp: float = field(default_factory=time.time)
class SystemHealthMonitor:
"""
Comprehensive system health monitoring component
Provides detailed monitoring of system resources and performance metrics
while maintaining energy efficiency and compliance with SIM-ONE principles.
Features:
- Multi-threaded resource monitoring
- Adaptive sampling rates based on system load
- Historical data analysis and trending
- Predictive health assessment
- Configurable thresholds and alerting
- Energy-efficient data collection
- Integration with compliance monitoring
"""
def __init__(self,
collection_interval: float = 1.0,
enable_detailed_monitoring: bool = True,
max_history_size: int = 3600,
health_callback: Optional[Callable] = None):
"""
Initialize System Health Monitor
Args:
collection_interval: Base interval for metric collection (seconds)
enable_detailed_monitoring: Enable detailed process/network monitoring
max_history_size: Maximum number of historical samples to retain
health_callback: Optional callback for health status changes
"""
self.collection_interval = collection_interval
self.enable_detailed_monitoring = enable_detailed_monitoring
self.max_history_size = max_history_size
self.health_callback = health_callback
# Current metrics
self.current_cpu = CPUMetrics()
self.current_memory = MemoryMetrics()
self.current_disk = DiskMetrics()
self.current_network = NetworkMetrics()
self.current_process = ProcessMetrics()
self.current_system = SystemMetrics()
self.current_assessment = HealthAssessment()
# Historical data
self.cpu_history = deque(maxlen=max_history_size)
self.memory_history = deque(maxlen=max_history_size)
self.disk_history = deque(maxlen=max_history_size)
self.network_history = deque(maxlen=max_history_size)
self.process_history = deque(maxlen=max_history_size)
self.system_history = deque(maxlen=max_history_size)
self.assessment_history = deque(maxlen=max_history_size)
# Health thresholds (configurable)
self.thresholds = {
'cpu_warning': 75.0,
'cpu_critical': 90.0,
'memory_warning': 80.0,
'memory_critical': 95.0,
'disk_warning': 85.0,
'disk_critical': 95.0,
'swap_warning': 50.0,
'swap_critical': 80.0,
'load_warning': 2.0, # Per CPU core
'load_critical': 4.0,
'zombie_warning': 5,
'zombie_critical': 20,
'open_files_warning': 1000,
'open_files_critical': 5000
}
# Monitoring state
self.is_monitoring = False
self.monitoring_thread = None
self.shutdown_event = threading.Event()
self.lock = threading.RLock()
# Performance optimization
self.last_collection_time = 0.0
self.adaptive_interval = collection_interval
self.collection_errors = 0
# System information cache
self._cache_system_info()
logger.info("SystemHealthMonitor initialized")
def _cache_system_info(self):
"""Cache static system information"""
try:
self.system_info = {
'platform': platform.platform(),
'processor': platform.processor(),
'architecture': platform.architecture()[0],
'hostname': platform.node(),
'cpu_count_logical': psutil.cpu_count(logical=True),
'cpu_count_physical': psutil.cpu_count(logical=False),
'total_memory': psutil.virtual_memory().total,
'boot_time': psutil.boot_time()
}
# Get disk information
self.disk_info = {}
for partition in psutil.disk_partitions():
try:
usage = psutil.disk_usage(partition.mountpoint)
self.disk_info[partition.device] = {
'mountpoint': partition.mountpoint,
'fstype': partition.fstype,
'total': usage.total
}
except (PermissionError, FileNotFoundError):
continue
except Exception as e:
logger.error(f"Error caching system information: {e}")
self.system_info = {}
self.disk_info = {}
def start_monitoring(self):
"""Start system health monitoring"""
if self.is_monitoring:
logger.warning("System health monitoring is already running")
return
self.is_monitoring = True
self.shutdown_event.clear()
# Start monitoring thread
self.monitoring_thread = threading.Thread(
target=self._monitoring_loop,
name="SystemHealthMonitor",
daemon=True
)
self.monitoring_thread.start()
logger.info("System health monitoring started")
def stop_monitoring(self):
"""Stop system health monitoring"""
if not self.is_monitoring:
logger.warning("System health monitoring is not running")
return
self.is_monitoring = False
self.shutdown_event.set()
if self.monitoring_thread:
self.monitoring_thread.join(timeout=5.0)
logger.info("System health monitoring stopped")
def _monitoring_loop(self):
"""Main monitoring loop"""
logger.info("System health monitoring loop started")
while not self.shutdown_event.is_set():
try:
start_time = time.time()
# Collect all metrics
self._collect_all_metrics()
# Perform health assessment
self._assess_system_health()
# Update historical data
self._update_history()
# Calculate adaptive interval based on system load
self._adjust_collection_interval()
# Calculate sleep time
collection_time = time.time() - start_time
sleep_time = max(0.1, self.adaptive_interval - collection_time)
self.last_collection_time = collection_time
self.collection_errors = max(0, self.collection_errors - 1) # Decay error count
self.shutdown_event.wait(sleep_time)
except Exception as e:
logger.error(f"Error in system health monitoring loop: {e}")
self.collection_errors += 1
# Increase interval on repeated errors to reduce system load
if self.collection_errors > 5:
self.adaptive_interval = min(self.adaptive_interval * 1.5, 30.0)
self.shutdown_event.wait(1.0)
def _collect_all_metrics(self):
"""Collect all system health metrics"""
try:
# Collect CPU metrics
self.current_cpu = self._collect_cpu_metrics()
# Collect memory metrics
self.current_memory = self._collect_memory_metrics()
# Collect disk metrics
self.current_disk = self._collect_disk_metrics()
# Collect network metrics (if detailed monitoring enabled)
if self.enable_detailed_monitoring:
self.current_network = self._collect_network_metrics()
self.current_process = self._collect_process_metrics()
# Collect system metrics
self.current_system = self._collect_system_metrics()
except Exception as e:
logger.error(f"Error collecting system metrics: {e}")
def _collect_cpu_metrics(self) -> CPUMetrics:
"""Collect CPU performance metrics"""
try:
# CPU usage
cpu_percent = psutil.cpu_percent(interval=None)
# CPU times
cpu_times = psutil.cpu_times()
# CPU frequency
cpu_freq = psutil.cpu_freq()
frequency = cpu_freq.current if cpu_freq else 0.0
# Load average (Unix-like systems)
load_avg = list(os.getloadavg()) if hasattr(os, 'getloadavg') else [0.0, 0.0, 0.0]
# CPU temperature (if available)
temperature = None
try:
temps = psutil.sensors_temperatures()
if temps:
for name, entries in temps.items():
if entries and 'cpu' in name.lower() or 'core' in name.lower():
temperature = entries[0].current
break
except (AttributeError, OSError):
pass
return CPUMetrics(
usage_percent=cpu_percent,
user_time=cpu_times.user,
system_time=cpu_times.system,
idle_time=cpu_times.idle,
iowait=getattr(cpu_times, 'iowait', 0.0),
core_count=psutil.cpu_count(logical=True),
frequency_mhz=frequency,
load_average=load_avg,
temperature_celsius=temperature,
timestamp=time.time()
)
except Exception as e:
logger.error(f"Error collecting CPU metrics: {e}")
return CPUMetrics()
def _collect_memory_metrics(self) -> MemoryMetrics:
"""Collect memory usage metrics"""
try:
# Virtual memory
vmem = psutil.virtual_memory()
# Swap memory
swap = psutil.swap_memory()
return MemoryMetrics(
total_bytes=vmem.total,
available_bytes=vmem.available,
used_bytes=vmem.used,
usage_percent=vmem.percent,
swap_total_bytes=swap.total,
swap_used_bytes=swap.used,
swap_percent=swap.percent,
cached_bytes=getattr(vmem, 'cached', 0),
buffers_bytes=getattr(vmem, 'buffers', 0),
timestamp=time.time()
)
except Exception as e:
logger.error(f"Error collecting memory metrics: {e}")
return MemoryMetrics()
def _collect_disk_metrics(self) -> DiskMetrics:
"""Collect disk usage and I/O metrics"""
try:
# Disk usage (root partition)
disk_usage = psutil.disk_usage('/')
# Disk I/O
disk_io = psutil.disk_io_counters()
return DiskMetrics(
total_bytes=disk_usage.total,
used_bytes=disk_usage.used,
free_bytes=disk_usage.free,
usage_percent=(disk_usage.used / disk_usage.total) * 100 if disk_usage.total > 0 else 0.0,
read_count=disk_io.read_count if disk_io else 0,
write_count=disk_io.write_count if disk_io else 0,
read_bytes=disk_io.read_bytes if disk_io else 0,
write_bytes=disk_io.write_bytes if disk_io else 0,
read_time_ms=disk_io.read_time if disk_io else 0,
write_time_ms=disk_io.write_time if disk_io else 0,
timestamp=time.time()
)
except Exception as e:
logger.error(f"Error collecting disk metrics: {e}")
return DiskMetrics()
def _collect_network_metrics(self) -> NetworkMetrics:
"""Collect network interface metrics"""
try:
# Network I/O
net_io = psutil.net_io_counters()
# Network connections
connections = psutil.net_connections()
established = len([c for c in connections if c.status == 'ESTABLISHED'])
listening = len([c for c in connections if c.status == 'LISTEN'])
return NetworkMetrics(
bytes_sent=net_io.bytes_sent if net_io else 0,
bytes_recv=net_io.bytes_recv if net_io else 0,
packets_sent=net_io.packets_sent if net_io else 0,
packets_recv=net_io.packets_recv if net_io else 0,
errors_in=net_io.errin if net_io else 0,
errors_out=net_io.errout if net_io else 0,
drops_in=net_io.dropin if net_io else 0,
drops_out=net_io.dropout if net_io else 0,
connections_established=established,
connections_listening=listening,
timestamp=time.time()
)
except Exception as e:
logger.error(f"Error collecting network metrics: {e}")
return NetworkMetrics()
def _collect_process_metrics(self) -> ProcessMetrics:
"""Collect process monitoring metrics"""
try:
# Process counts by status
process_counts = defaultdict(int)
total_processes = 0
total_threads = 0
total_open_files = 0
total_memory_rss = 0
total_memory_vms = 0
current_process_cpu = 0.0
current_process = psutil.Process()
for proc in psutil.process_iter(['status', 'num_threads']):
try:
process_counts[proc.info['status']] += 1
total_processes += 1
total_threads += proc.info['num_threads']
# Get additional info for current process
if proc.pid == current_process.pid:
try:
total_open_files = len(current_process.open_files())
mem_info = current_process.memory_info()
total_memory_rss = mem_info.rss
total_memory_vms = mem_info.vms
current_process_cpu = current_process.cpu_percent()
except (psutil.NoSuchProcess, psutil.AccessDenied):
pass
except (psutil.NoSuchProcess, psutil.AccessDenied):
continue
return ProcessMetrics(
total_processes=total_processes,
running_processes=process_counts.get('running', 0),
sleeping_processes=process_counts.get('sleeping', 0),
zombie_processes=process_counts.get('zombie', 0),
open_files=total_open_files,
threads=total_threads,
memory_rss_bytes=total_memory_rss,
memory_vms_bytes=total_memory_vms,
cpu_percent=current_process_cpu,
timestamp=time.time()
)
except Exception as e:
logger.error(f"Error collecting process metrics: {e}")
return ProcessMetrics()
def _collect_system_metrics(self) -> SystemMetrics:
"""Collect overall system metrics"""
try:
# System uptime
boot_time = psutil.boot_time()
uptime = time.time() - boot_time
# Users
users = len(psutil.users())
# System stats (if available)
context_switches = 0
interrupts = 0
soft_interrupts = 0
system_calls = 0
try:
cpu_stats = psutil.cpu_stats()
context_switches = cpu_stats.ctx_switches
interrupts = cpu_stats.interrupts
soft_interrupts = cpu_stats.soft_interrupts
system_calls = getattr(cpu_stats, 'syscalls', 0)
except AttributeError:
pass
return SystemMetrics(
uptime_seconds=uptime,
boot_time=boot_time,
users_count=users,
context_switches=context_switches,
interrupts=interrupts,
soft_interrupts=soft_interrupts,
system_calls=system_calls,
platform_info=platform.platform(),
timestamp=time.time()
)
except Exception as e:
logger.error(f"Error collecting system metrics: {e}")
return SystemMetrics()
def _assess_system_health(self):
"""Perform comprehensive system health assessment"""
try:
issues = []
recommendations = []
# Assess CPU health
cpu_status = self._assess_cpu_health(issues, recommendations)
# Assess memory health
memory_status = self._assess_memory_health(issues, recommendations)
# Assess disk health
disk_status = self._assess_disk_health(issues, recommendations)
# Assess network health
network_status = HealthStatus.GOOD
if self.enable_detailed_monitoring:
network_status = self._assess_network_health(issues, recommendations)
# Assess process health
process_status = HealthStatus.GOOD
if self.enable_detailed_monitoring:
process_status = self._assess_process_health(issues, recommendations)
# Assess overall system health
system_status = self._assess_overall_system_health(issues, recommendations)
# Calculate overall status and score
status_scores = {
HealthStatus.EXCELLENT: 100,
HealthStatus.GOOD: 80,
HealthStatus.WARNING: 60,
HealthStatus.CRITICAL: 20,
HealthStatus.UNKNOWN: 0
}
statuses = [cpu_status, memory_status, disk_status, network_status, process_status, system_status]
scores = [status_scores[status] for status in statuses]
overall_score = statistics.mean(scores) if scores else 0.0
# Determine overall status
if overall_score >= 90:
overall_status = HealthStatus.EXCELLENT
elif overall_score >= 70:
overall_status = HealthStatus.GOOD
elif overall_score >= 50:
overall_status = HealthStatus.WARNING
elif overall_score >= 20:
overall_status = HealthStatus.CRITICAL
else:
overall_status = HealthStatus.UNKNOWN
# Update current assessment
previous_status = self.current_assessment.overall_status
self.current_assessment = HealthAssessment(
overall_status=overall_status,
cpu_status=cpu_status,
memory_status=memory_status,
disk_status=disk_status,
network_status=network_status,
process_status=process_status,
system_status=system_status,
score=overall_score,
issues=issues,
recommendations=recommendations,
timestamp=time.time()
)
# Trigger callback if status changed
if self.health_callback and previous_status != overall_status:
try:
self.health_callback(self.current_assessment)
except Exception as e:
logger.error(f"Error in health callback: {e}")
except Exception as e:
logger.error(f"Error assessing system health: {e}")
def _assess_cpu_health(self, issues: List[str], recommendations: List[str]) -> HealthStatus:
"""Assess CPU health status"""
try:
cpu = self.current_cpu
# Check CPU usage
if cpu.usage_percent >= self.thresholds['cpu_critical']:
issues.append(f"Critical CPU usage: {cpu.usage_percent:.1f}%")
recommendations.append("Identify high-CPU processes and optimize or scale resources")
return HealthStatus.CRITICAL
elif cpu.usage_percent >= self.thresholds['cpu_warning']:
issues.append(f"High CPU usage: {cpu.usage_percent:.1f}%")
recommendations.append("Monitor CPU trends and consider optimization")
return HealthStatus.WARNING
# Check load average
if len(cpu.load_average) > 0:
load_per_core = cpu.load_average[0] / max(cpu.core_count, 1)
if load_per_core >= self.thresholds['load_critical']:
issues.append(f"Critical system load: {load_per_core:.2f} per core")
recommendations.append("System is heavily loaded, investigate processes")
return HealthStatus.CRITICAL
elif load_per_core >= self.thresholds['load_warning']:
issues.append(f"High system load: {load_per_core:.2f} per core")
recommendations.append("System load is elevated, monitor closely")
return HealthStatus.WARNING
# Check temperature if available
if cpu.temperature_celsius and cpu.temperature_celsius > 85:
issues.append(f"High CPU temperature: {cpu.temperature_celsius:.1f}°C")
recommendations.append("Check CPU cooling and system ventilation")
return HealthStatus.WARNING
return HealthStatus.EXCELLENT if cpu.usage_percent < 50 else HealthStatus.GOOD
except Exception as e:
logger.error(f"Error assessing CPU health: {e}")
return HealthStatus.UNKNOWN
def _assess_memory_health(self, issues: List[str], recommendations: List[str]) -> HealthStatus:
"""Assess memory health status"""
try:
memory = self.current_memory
# Check memory usage
if memory.usage_percent >= self.thresholds['memory_critical']:
issues.append(f"Critical memory usage: {memory.usage_percent:.1f}%")
recommendations.append("Free memory immediately or risk system instability")
return HealthStatus.CRITICAL
elif memory.usage_percent >= self.thresholds['memory_warning']:
issues.append(f"High memory usage: {memory.usage_percent:.1f}%")
recommendations.append("Monitor memory usage and consider freeing unused memory")
return HealthStatus.WARNING
# Check swap usage
if memory.swap_percent >= self.thresholds['swap_critical']:
issues.append(f"Critical swap usage: {memory.swap_percent:.1f}%")
recommendations.append("System is heavily swapping, add more RAM or reduce memory usage")
return HealthStatus.CRITICAL
elif memory.swap_percent >= self.thresholds['swap_warning']:
issues.append(f"High swap usage: {memory.swap_percent:.1f}%")
recommendations.append("System is swapping, monitor memory allocation")
return HealthStatus.WARNING
return HealthStatus.EXCELLENT if memory.usage_percent < 60 else HealthStatus.GOOD
except Exception as e:
logger.error(f"Error assessing memory health: {e}")
return HealthStatus.UNKNOWN
def _assess_disk_health(self, issues: List[str], recommendations: List[str]) -> HealthStatus:
"""Assess disk health status"""
try:
disk = self.current_disk
# Check disk usage
if disk.usage_percent >= self.thresholds['disk_critical']:
issues.append(f"Critical disk usage: {disk.usage_percent:.1f}%")
recommendations.append("Free disk space immediately to prevent system issues")
return HealthStatus.CRITICAL
elif disk.usage_percent >= self.thresholds['disk_warning']:
issues.append(f"High disk usage: {disk.usage_percent:.1f}%")
recommendations.append("Clean up disk space or expand storage")
return HealthStatus.WARNING
# Check for excessive I/O (if we have historical data)
if len(self.disk_history) > 10:
recent_disks = list(self.disk_history)[-10:]
if len(recent_disks) > 1:
io_times = [d.io_time_ms for d in recent_disks if d.io_time_ms > 0]
if io_times:
avg_io_time = statistics.mean(io_times)
if avg_io_time > 1000: # More than 1 second average I/O time
issues.append(f"High disk I/O time: {avg_io_time:.0f}ms")
recommendations.append("Disk I/O is slow, check for disk issues or reduce I/O load")
return HealthStatus.WARNING
return HealthStatus.EXCELLENT if disk.usage_percent < 70 else HealthStatus.GOOD
except Exception as e:
logger.error(f"Error assessing disk health: {e}")
return HealthStatus.UNKNOWN
def _assess_network_health(self, issues: List[str], recommendations: List[str]) -> HealthStatus:
"""Assess network health status"""
try:
network = self.current_network
# Check for network errors
total_packets = network.packets_sent + network.packets_recv
total_errors = network.errors_in + network.errors_out
total_drops = network.drops_in + network.drops_out
if total_packets > 0:
error_rate = (total_errors / total_packets) * 100
drop_rate = (total_drops / total_packets) * 100
if error_rate > 5.0: # More than 5% error rate
issues.append(f"High network error rate: {error_rate:.2f}%")
recommendations.append("Investigate network connectivity issues")
return HealthStatus.WARNING
if drop_rate > 1.0: # More than 1% drop rate
issues.append(f"High network drop rate: {drop_rate:.2f}%")
recommendations.append("Network congestion detected, check bandwidth")
return HealthStatus.WARNING
# Check connection counts
if network.connections_established > 1000:
issues.append(f"High number of established connections: {network.connections_established}")
recommendations.append("Monitor network connection usage")
return HealthStatus.WARNING
return HealthStatus.GOOD
except Exception as e:
logger.error(f"Error assessing network health: {e}")
return HealthStatus.UNKNOWN
def _assess_process_health(self, issues: List[str], recommendations: List[str]) -> HealthStatus:
"""Assess process health status"""
try:
process = self.current_process
# Check for zombie processes
if process.zombie_processes >= self.thresholds['zombie_critical']:
issues.append(f"Critical number of zombie processes: {process.zombie_processes}")
recommendations.append("Kill zombie processes immediately")
return HealthStatus.CRITICAL
elif process.zombie_processes >= self.thresholds['zombie_warning']:
issues.append(f"High number of zombie processes: {process.zombie_processes}")
recommendations.append("Clean up zombie processes")
return HealthStatus.WARNING
# Check open files
if process.open_files >= self.thresholds['open_files_critical']:
issues.append(f"Critical number of open files: {process.open_files}")
recommendations.append("Close unused file handles to prevent resource exhaustion")
return HealthStatus.CRITICAL
elif process.open_files >= self.thresholds['open_files_warning']:
issues.append(f"High number of open files: {process.open_files}")
recommendations.append("Monitor file handle usage")
return HealthStatus.WARNING
return HealthStatus.GOOD
except Exception as e:
logger.error(f"Error assessing process health: {e}")
return HealthStatus.UNKNOWN
def _assess_overall_system_health(self, issues: List[str], recommendations: List[str]) -> HealthStatus:
"""Assess overall system health"""
try:
system = self.current_system
# Check system uptime (very long uptimes might indicate no reboots for updates)
if system.uptime_seconds > 90 * 24 * 3600: # More than 90 days
issues.append(f"Very long uptime: {system.uptime_seconds / (24 * 3600):.1f} days")
recommendations.append("Consider rebooting to apply system updates")
return HealthStatus.WARNING
# System is generally healthy if no other major issues
return HealthStatus.GOOD
except Exception as e:
logger.error(f"Error assessing system health: {e}")
return HealthStatus.UNKNOWN
def _update_history(self):
"""Update historical data"""
try:
with self.lock:
self.cpu_history.append(self.current_cpu)
self.memory_history.append(self.current_memory)
self.disk_history.append(self.current_disk)
if self.enable_detailed_monitoring:
self.network_history.append(self.current_network)
self.process_history.append(self.current_process)
self.system_history.append(self.current_system)
self.assessment_history.append(self.current_assessment)
except Exception as e:
logger.error(f"Error updating history: {e}")
def _adjust_collection_interval(self):
"""Adjust collection interval based on system load (energy stewardship)"""
try:
# Adaptive interval based on CPU usage and system load
cpu_usage = self.current_cpu.usage_percent
if cpu_usage > 80:
# Reduce monitoring frequency when system is heavily loaded
self.adaptive_interval = min(self.collection_interval * 2.0, 10.0)
elif cpu_usage < 20:
# Increase monitoring frequency when system is lightly loaded
self.adaptive_interval = max(self.collection_interval * 0.5, 0.5)
else:
# Return to normal interval
self.adaptive_interval = self.collection_interval
except Exception as e:
logger.error(f"Error adjusting collection interval: {e}")
def get_current_health(self) -> Dict[str, Any]:
"""Get current system health status"""
try:
with self.lock:
return {
'assessment': {
'overall_status': self.current_assessment.overall_status.value,
'score': self.current_assessment.score,
'cpu_status': self.current_assessment.cpu_status.value,
'memory_status': self.current_assessment.memory_status.value,
'disk_status': self.current_assessment.disk_status.value,
'network_status': self.current_assessment.network_status.value,
'process_status': self.current_assessment.process_status.value,
'system_status': self.current_assessment.system_status.value,
'issues': self.current_assessment.issues,
'recommendations': self.current_assessment.recommendations,
'timestamp': self.current_assessment.timestamp
},
'cpu': {
'usage_percent': self.current_cpu.usage_percent,
'load_average': self.current_cpu.load_average,
'core_count': self.current_cpu.core_count,
'frequency_mhz': self.current_cpu.frequency_mhz,
'temperature_celsius': self.current_cpu.temperature_celsius
},
'memory': {
'usage_percent': self.current_memory.usage_percent,
'total_gb': self.current_memory.total_bytes / (1024**3),
'available_gb': self.current_memory.available_bytes / (1024**3),
'swap_percent': self.current_memory.swap_percent
},
'disk': {
'usage_percent': self.current_disk.usage_percent,
'total_gb': self.current_disk.total_bytes / (1024**3),
'free_gb': self.current_disk.free_bytes / (1024**3)
},
'system': {
'uptime_hours': self.current_system.uptime_seconds / 3600,
'platform': self.current_system.platform_info,
'users': self.current_system.users_count
}
}
except Exception as e:
logger.error(f"Error getting current health: {e}")
return {'error': str(e)}
def get_health_history(self, hours: float = 1.0) -> Dict[str, List]:
"""Get health history for specified time period"""
try:
cutoff_time = time.time() - (hours * 3600)
with self.lock:
cpu_data = [
{
'timestamp': cpu.timestamp,
'usage_percent': cpu.usage_percent,
'load_average': cpu.load_average[0] if cpu.load_average else 0.0
}
for cpu in self.cpu_history
if cpu.timestamp >= cutoff_time
]
memory_data = [
{
'timestamp': mem.timestamp,
'usage_percent': mem.usage_percent,
'swap_percent': mem.swap_percent
}
for mem in self.memory_history
if mem.timestamp >= cutoff_time
]
disk_data = [
{
'timestamp': disk.timestamp,
'usage_percent': disk.usage_percent
}
for disk in self.disk_history
if disk.timestamp >= cutoff_time
]
assessment_data = [
{
'timestamp': assess.timestamp,
'overall_status': assess.overall_status.value,
'score': assess.score,
'issues_count': len(assess.issues)
}
for assess in self.assessment_history
if assess.timestamp >= cutoff_time
]
return {
'cpu': cpu_data,
'memory': memory_data,
'disk': disk_data,
'assessments': assessment_data
}
except Exception as e:
logger.error(f"Error getting health history: {e}")
return {}
def get_system_info(self) -> Dict[str, Any]:
"""Get cached system information"""
return {
'system_info': self.system_info,
'disk_info': self.disk_info,