-
Notifications
You must be signed in to change notification settings - Fork 19
feat: Add system health monitoring module (#128) #292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hyaku0121
wants to merge
8
commits into
cortexlinux:main
Choose a base branch
from
hyaku0121:feature/health-score-128
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
aa17f57
feat: Implement comprehensive system health checks for #128
hyaku0121 bdfcccf
feat: Add health monitor core logic, CLI integration, and unit tests
hyaku0121 95215f7
fix: Add timeouts to subprocess calls to improve reliability
hyaku0121 dff2092
refactor: Address code review feedback (docstrings, timeouts, complex…
hyaku0121 f7a5653
refactor: Improve security check complexity and SSH parsing logic
hyaku0121 dc4143e
fix: Resolve SonarCloud code smells and reduce complexity
hyaku0121 6ba1715
docs: Add missing docstrings to HealthMonitor public APIs
hyaku0121 618e075
fix: Address SonarCloud and CodeRabbit feedback (redundant exceptions…
hyaku0121 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import shutil | ||
| from ..monitor import HealthCheck, CheckResult | ||
|
|
||
| class DiskCheck(HealthCheck): | ||
| """Check root filesystem disk usage.""" | ||
|
|
||
| def run(self) -> CheckResult: | ||
| """ | ||
| Calculate disk usage percentage. | ||
|
|
||
| Returns: | ||
| CheckResult based on usage thresholds. | ||
| """ | ||
| try: | ||
| # Use _ for unused variable (free space) | ||
| total, used, _ = shutil.disk_usage("/") | ||
| usage_percent = (used / total) * 100 | ||
| except Exception as e: | ||
| return CheckResult( | ||
| name="Disk Usage", | ||
|
Check failure on line 20 in cortex/health/checks/disk.py
|
||
| category="disk", | ||
| score=0, | ||
| status="CRITICAL", | ||
| details=f"Check failed: {e}", | ||
| recommendation="Check disk mounts and permissions", | ||
| weight=0.20 | ||
| ) | ||
|
|
||
| # Explicit early returns to avoid static analysis confusion | ||
| if usage_percent > 90: | ||
| return CheckResult( | ||
| name="Disk Usage", | ||
| category="disk", | ||
| score=0, | ||
| status="CRITICAL", | ||
| details=f"{usage_percent:.1f}% used", | ||
| recommendation="Clean up disk space immediately", | ||
| weight=0.20 | ||
| ) | ||
|
|
||
| if usage_percent > 80: | ||
| return CheckResult( | ||
| name="Disk Usage", | ||
| category="disk", | ||
| score=50, | ||
| status="WARNING", | ||
| details=f"{usage_percent:.1f}% used", | ||
| recommendation="Consider cleaning up disk space", | ||
| weight=0.20 | ||
| ) | ||
|
|
||
| return CheckResult( | ||
| name="Disk Usage", | ||
| category="disk", | ||
| score=100, | ||
| status="OK", | ||
| details=f"{usage_percent:.1f}% used", | ||
| recommendation=None, | ||
| weight=0.20 | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import os | ||
| import multiprocessing | ||
| from ..monitor import HealthCheck, CheckResult | ||
|
|
||
| class PerformanceCheck(HealthCheck): | ||
| def run(self) -> CheckResult: | ||
| score = 100 | ||
| issues = [] | ||
| rec = None | ||
|
|
||
| # 1. Load Average (1min) | ||
| try: | ||
| load1, _, _ = os.getloadavg() | ||
| cores = multiprocessing.cpu_count() | ||
| # Load ratio against core count | ||
| load_ratio = load1 / cores | ||
|
|
||
| if load_ratio > 1.0: | ||
| score -= 50 | ||
| issues.append(f"High Load ({load1:.2f})") | ||
| rec = "Check top processes" | ||
| except Exception: | ||
| pass # Skip on Windows etc. | ||
|
|
||
| # 2. Memory Usage (Linux /proc/meminfo) | ||
| try: | ||
| with open('/proc/meminfo', 'r') as f: | ||
| meminfo = {} | ||
| for line in f: | ||
| parts = line.split(':') | ||
| if len(parts) == 2: | ||
| meminfo[parts[0].strip()] = int(parts[1].strip().split()[0]) | ||
|
|
||
| if 'MemTotal' in meminfo and 'MemAvailable' in meminfo: | ||
| total = meminfo['MemTotal'] | ||
| avail = meminfo['MemAvailable'] | ||
| used_percent = ((total - avail) / total) * 100 | ||
|
|
||
| if used_percent > 80: | ||
| penalty = int(used_percent - 80) | ||
| score -= penalty | ||
| issues.append(f"High Memory ({used_percent:.0f}%)") | ||
| except FileNotFoundError: | ||
| pass # Non-Linux systems | ||
|
|
||
| # Summary of results | ||
| status = "OK" | ||
| if score < 50: | ||
| status = "CRITICAL" | ||
| elif score < 90: | ||
| status = "WARNING" | ||
|
|
||
| details = ", ".join(issues) if issues else "Optimal" | ||
|
|
||
| return CheckResult( | ||
| name="System Load", | ||
| category="performance", | ||
| score=max(0, score), | ||
| status=status, | ||
| details=details, | ||
| recommendation=rec, | ||
| weight=0.20 # 20% | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import subprocess | ||
| import os | ||
| from ..monitor import HealthCheck, CheckResult | ||
|
|
||
| class SecurityCheck(HealthCheck): | ||
| def run(self) -> CheckResult: | ||
|
Check failure on line 6 in cortex/health/checks/security.py
|
||
| score = 100 | ||
| issues = [] | ||
| recommendations = [] | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # 1. Firewall (UFW) Check | ||
| ufw_active = False | ||
| try: | ||
| # Add timeout to prevent hanging (Fixes Reliability Issue) | ||
| res = subprocess.run( | ||
| ["systemctl", "is-active", "ufw"], | ||
| capture_output=True, | ||
| text=True, | ||
| timeout=5 | ||
| ) | ||
| # Fix: Use exact match to avoid matching "inactive" which contains "active" | ||
| if res.returncode == 0 and res.stdout.strip() == "active": | ||
| ufw_active = True | ||
| except subprocess.TimeoutExpired: | ||
| pass # Command timed out, treat as inactive or unavailable | ||
| except FileNotFoundError: | ||
| pass # Environment without systemctl (e.g., Docker or non-systemd) | ||
| except Exception: | ||
| pass # Generic error protection | ||
|
|
||
| if not ufw_active: | ||
| score = 0 # Spec: 0 points if Firewall is inactive | ||
| issues.append("Firewall Inactive") | ||
| recommendations.append("Enable UFW Firewall") | ||
|
|
||
| # 2. SSH Root Login Check | ||
| try: | ||
| ssh_config = "/etc/ssh/sshd_config" | ||
| if os.path.exists(ssh_config): | ||
| with open(ssh_config, 'r') as f: | ||
| for line in f: | ||
| line = line.strip() | ||
| # Check for uncommented PermitRootLogin yes | ||
| if line.startswith("PermitRootLogin") and "yes" in line.split(): | ||
| score -= 50 | ||
| issues.append("Root SSH Allowed") | ||
| recommendations.append("Disable SSH Root Login in sshd_config") | ||
| break | ||
| except PermissionError: | ||
| pass # Cannot read config, skip check | ||
| except Exception: | ||
| pass # Generic error protection | ||
|
|
||
| status = "OK" | ||
| if score < 50: status = "CRITICAL" | ||
| elif score < 100: status = "WARNING" | ||
|
|
||
| return CheckResult( | ||
| name="Security Posture", | ||
| category="security", | ||
| score=max(0, score), | ||
| status=status, | ||
| details=", ".join(issues) if issues else "Secure", | ||
| recommendation=", ".join(recommendations) if recommendations else None, | ||
| weight=0.35 | ||
| ) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
Add docstrings for
PerformanceCheckandrunto meet public-API guidelinePerformanceCheckis part of the public health-check surface but currently lacks docstrings. Adding brief class and method docstrings will align with the “docstrings required for all public APIs” guideline and clarify what this check measures.🤖 Prompt for AI Agents