sysmaint 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sysmaint/__init__.py +17 -0
- sysmaint/__main__.py +6 -0
- sysmaint/cli.py +637 -0
- sysmaint/config.py +229 -0
- sysmaint/gui/__init__.py +5 -0
- sysmaint/gui/app.py +193 -0
- sysmaint/gui/components/__init__.py +13 -0
- sysmaint/gui/components/cards.py +239 -0
- sysmaint/gui/components/console.py +61 -0
- sysmaint/gui/components/dialogs.py +79 -0
- sysmaint/gui/theme.py +52 -0
- sysmaint/gui/views/__init__.py +25 -0
- sysmaint/gui/views/backup.py +167 -0
- sysmaint/gui/views/cleaner.py +227 -0
- sysmaint/gui/views/dashboard.py +252 -0
- sysmaint/gui/views/diagnostics.py +243 -0
- sysmaint/gui/views/integrity.py +158 -0
- sysmaint/gui/views/malware.py +293 -0
- sysmaint/gui/views/network.py +171 -0
- sysmaint/gui/views/security.py +118 -0
- sysmaint/gui/views/settings.py +211 -0
- sysmaint/gui/views/startup.py +133 -0
- sysmaint/logging_setup.py +62 -0
- sysmaint/modules/__init__.py +41 -0
- sysmaint/modules/backup.py +106 -0
- sysmaint/modules/cleaner.py +249 -0
- sysmaint/modules/disk.py +160 -0
- sysmaint/modules/drivers.py +89 -0
- sysmaint/modules/integrity.py +148 -0
- sysmaint/modules/malware.py +412 -0
- sysmaint/modules/memory.py +105 -0
- sysmaint/modules/network.py +167 -0
- sysmaint/modules/optimizer.py +186 -0
- sysmaint/modules/recovery.py +353 -0
- sysmaint/modules/report.py +232 -0
- sysmaint/modules/restore.py +123 -0
- sysmaint/modules/scheduler.py +137 -0
- sysmaint/modules/security.py +88 -0
- sysmaint/modules/services.py +171 -0
- sysmaint/modules/startup.py +95 -0
- sysmaint/modules/sysinfo.py +112 -0
- sysmaint/service/__init__.py +5 -0
- sysmaint/service/server.py +210 -0
- sysmaint/tui/__init__.py +5 -0
- sysmaint/tui/app.py +830 -0
- sysmaint/tui/modals.py +201 -0
- sysmaint/tui/themes.py +97 -0
- sysmaint/utils/__init__.py +39 -0
- sysmaint/utils/common.py +255 -0
- sysmaint/utils/escalate.py +93 -0
- sysmaint/utils/linux.py +240 -0
- sysmaint/utils/parsers.py +171 -0
- sysmaint/utils/windows.py +308 -0
- sysmaint-0.1.0.dist-info/METADATA +307 -0
- sysmaint-0.1.0.dist-info/RECORD +57 -0
- sysmaint-0.1.0.dist-info/WHEEL +4 -0
- sysmaint-0.1.0.dist-info/entry_points.txt +5 -0
sysmaint/utils/linux.py
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
# sysmaint/utils/linux.py
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
import shutil
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
from .common import run_command
|
|
11
|
+
|
|
12
|
+
logger = logging.getLogger("sysmaint.linux")
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def detect_package_manager() -> str | None:
|
|
16
|
+
"""Detect the system package manager and return its command name."""
|
|
17
|
+
for pm in ("apt-get", "apt", "dnf", "yum", "zypper", "pacman", "apk"):
|
|
18
|
+
if shutil.which(pm):
|
|
19
|
+
return pm
|
|
20
|
+
return None
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def clean_package_cache(dry_run: bool = False) -> dict[str, Any]:
|
|
24
|
+
"""
|
|
25
|
+
Clean package manager download caches (e.g. apt clean, dnf clean all).
|
|
26
|
+
"""
|
|
27
|
+
pm = detect_package_manager()
|
|
28
|
+
if not pm:
|
|
29
|
+
return {"status": "skipped", "message": "No supported package manager detected"}
|
|
30
|
+
|
|
31
|
+
cmd_map = {
|
|
32
|
+
"apt": ["apt-get", "clean"],
|
|
33
|
+
"apt-get": ["apt-get", "clean"],
|
|
34
|
+
"dnf": ["dnf", "clean", "all"],
|
|
35
|
+
"yum": ["yum", "clean", "all"],
|
|
36
|
+
"zypper": ["zypper", "clean", "--all"],
|
|
37
|
+
"pacman": ["pacman", "-Sc", "--noconfirm"],
|
|
38
|
+
"apk": ["apk", "cache", "clean"],
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
cmd = cmd_map.get(pm)
|
|
42
|
+
if not cmd:
|
|
43
|
+
return {"status": "unsupported", "package_manager": pm}
|
|
44
|
+
|
|
45
|
+
if dry_run:
|
|
46
|
+
return {"status": "dry_run", "command": " ".join(cmd)}
|
|
47
|
+
|
|
48
|
+
result = run_command(cmd, check=False)
|
|
49
|
+
return {
|
|
50
|
+
"status": "success" if result.returncode == 0 else "error",
|
|
51
|
+
"package_manager": pm,
|
|
52
|
+
"returncode": result.returncode,
|
|
53
|
+
"output": result.stdout,
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def get_failed_systemd_units() -> list[dict[str, str]]:
|
|
58
|
+
"""
|
|
59
|
+
Return list of failed systemd units and services.
|
|
60
|
+
"""
|
|
61
|
+
if not shutil.which("systemctl"):
|
|
62
|
+
return []
|
|
63
|
+
|
|
64
|
+
result = run_command(
|
|
65
|
+
["systemctl", "--failed", "--no-legend", "--no-pager"],
|
|
66
|
+
check=False,
|
|
67
|
+
)
|
|
68
|
+
failed = []
|
|
69
|
+
for line in result.stdout.splitlines():
|
|
70
|
+
parts = line.strip().split(None, 4)
|
|
71
|
+
if len(parts) >= 4:
|
|
72
|
+
failed.append({
|
|
73
|
+
"unit": parts[0],
|
|
74
|
+
"load": parts[1],
|
|
75
|
+
"active": parts[2],
|
|
76
|
+
"sub": parts[3],
|
|
77
|
+
"description": parts[4] if len(parts) > 4 else "",
|
|
78
|
+
})
|
|
79
|
+
return failed
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def get_system_journal_errors(max_lines: int = 50) -> list[str]:
|
|
83
|
+
"""
|
|
84
|
+
Retrieve recent priority 3 (Error) and above systemd journal entries.
|
|
85
|
+
"""
|
|
86
|
+
if not shutil.which("journalctl"):
|
|
87
|
+
return []
|
|
88
|
+
|
|
89
|
+
result = run_command(
|
|
90
|
+
["journalctl", "-p", "3", "-xb", "--no-pager", "-n", str(max_lines)],
|
|
91
|
+
check=False,
|
|
92
|
+
)
|
|
93
|
+
return [line for line in result.stdout.splitlines() if line.strip()]
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def vacuum_systemd_journal(retention: str = "7d") -> bool:
|
|
97
|
+
"""
|
|
98
|
+
Vacuum old journal logs to free disk space.
|
|
99
|
+
"""
|
|
100
|
+
if not shutil.which("journalctl"):
|
|
101
|
+
return False
|
|
102
|
+
result = run_command(["journalctl", f"--vacuum-time={retention}"], check=False)
|
|
103
|
+
return result.returncode == 0
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def flush_linux_dns() -> bool:
|
|
107
|
+
"""
|
|
108
|
+
Flush Linux DNS resolver cache using resolvectl, systemd-resolve, or nscd.
|
|
109
|
+
"""
|
|
110
|
+
if shutil.which("resolvectl"):
|
|
111
|
+
res = run_command(["resolvectl", "flush-caches"], check=False)
|
|
112
|
+
return res.returncode == 0
|
|
113
|
+
elif shutil.which("systemd-resolve"):
|
|
114
|
+
res = run_command(["systemd-resolve", "--flush-caches"], check=False)
|
|
115
|
+
return res.returncode == 0
|
|
116
|
+
elif shutil.which("nscd"):
|
|
117
|
+
res = run_command(["nscd", "-i", "hosts"], check=False)
|
|
118
|
+
return res.returncode == 0
|
|
119
|
+
return False
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def get_linux_security_status() -> dict[str, Any]:
|
|
123
|
+
"""
|
|
124
|
+
Check Linux firewall, SELinux/AppArmor, and basic security configuration.
|
|
125
|
+
"""
|
|
126
|
+
status: dict[str, Any] = {
|
|
127
|
+
"firewall": "Unknown",
|
|
128
|
+
"firewall_active": False,
|
|
129
|
+
"mac_system": "None",
|
|
130
|
+
"mac_enforcing": False,
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
# Firewall check
|
|
134
|
+
if shutil.which("ufw"):
|
|
135
|
+
res = run_command(["ufw", "status"], check=False)
|
|
136
|
+
status["firewall"] = "UFW"
|
|
137
|
+
status["firewall_active"] = "Status: active" in res.stdout
|
|
138
|
+
elif shutil.which("firewall-cmd"):
|
|
139
|
+
res = run_command(["firewall-cmd", "--state"], check=False)
|
|
140
|
+
status["firewall"] = "firewalld"
|
|
141
|
+
status["firewall_active"] = "running" in res.stdout
|
|
142
|
+
elif shutil.which("iptables"):
|
|
143
|
+
res = run_command(["iptables", "-L", "-n"], check=False)
|
|
144
|
+
status["firewall"] = "iptables"
|
|
145
|
+
status["firewall_active"] = res.returncode == 0 and len(res.stdout.splitlines()) > 5
|
|
146
|
+
|
|
147
|
+
# Mandatory Access Control check (AppArmor / SELinux)
|
|
148
|
+
if shutil.which("aa-status"):
|
|
149
|
+
res = run_command(["aa-status", "--enabled"], check=False)
|
|
150
|
+
status["mac_system"] = "AppArmor"
|
|
151
|
+
status["mac_enforcing"] = res.returncode == 0
|
|
152
|
+
elif shutil.which("getenforce"):
|
|
153
|
+
res = run_command(["getenforce"], check=False)
|
|
154
|
+
status["mac_system"] = "SELinux"
|
|
155
|
+
status["mac_enforcing"] = res.stdout.strip().lower() == "enforcing"
|
|
156
|
+
|
|
157
|
+
return status
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def get_linux_startup_items() -> list[dict[str, Any]]:
|
|
161
|
+
"""
|
|
162
|
+
Scan Linux XDG autostart directories and user systemd units for startup apps.
|
|
163
|
+
"""
|
|
164
|
+
items = []
|
|
165
|
+
autostart_dirs = [
|
|
166
|
+
Path.home() / ".config" / "autostart",
|
|
167
|
+
Path("/etc/xdg/autostart"),
|
|
168
|
+
]
|
|
169
|
+
|
|
170
|
+
for d in autostart_dirs:
|
|
171
|
+
if d.is_dir():
|
|
172
|
+
for f in d.glob("*.desktop"):
|
|
173
|
+
try:
|
|
174
|
+
name = f.stem
|
|
175
|
+
exec_cmd = ""
|
|
176
|
+
hidden = False
|
|
177
|
+
for line in f.read_text(encoding="utf-8", errors="ignore").splitlines():
|
|
178
|
+
if line.startswith("Name="):
|
|
179
|
+
name = line.split("=", 1)[1].strip()
|
|
180
|
+
elif line.startswith("Exec="):
|
|
181
|
+
exec_cmd = line.split("=", 1)[1].strip()
|
|
182
|
+
elif line.startswith("Hidden="):
|
|
183
|
+
hidden = line.split("=", 1)[1].strip().lower() == "true"
|
|
184
|
+
if not hidden and exec_cmd:
|
|
185
|
+
items.append({
|
|
186
|
+
"name": name,
|
|
187
|
+
"command": exec_cmd,
|
|
188
|
+
"location": str(d),
|
|
189
|
+
"file": str(f),
|
|
190
|
+
"enabled": True,
|
|
191
|
+
})
|
|
192
|
+
except Exception:
|
|
193
|
+
pass
|
|
194
|
+
|
|
195
|
+
return items
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def create_btrfs_snapshot(source: str, destination: str) -> bool:
|
|
199
|
+
"""Create a Btrfs snapshot if the filesystem supports it."""
|
|
200
|
+
if shutil.which("btrfs"):
|
|
201
|
+
result = run_command(["btrfs", "subvolume", "snapshot", source, destination], check=False)
|
|
202
|
+
return result.returncode == 0
|
|
203
|
+
return False
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def get_linux_temp_paths() -> dict[str, Path]:
|
|
207
|
+
"""Return standard Linux junk, log, and cache directories."""
|
|
208
|
+
paths = {
|
|
209
|
+
"System Temp": Path("/tmp"),
|
|
210
|
+
"Variable Temp": Path("/var/tmp"),
|
|
211
|
+
}
|
|
212
|
+
user_cache = Path.home() / ".cache"
|
|
213
|
+
if user_cache.is_dir():
|
|
214
|
+
paths["User Cache"] = user_cache
|
|
215
|
+
|
|
216
|
+
user_trash = Path.home() / ".local" / "share" / "Trash"
|
|
217
|
+
if user_trash.is_dir():
|
|
218
|
+
paths["User Trash"] = user_trash
|
|
219
|
+
|
|
220
|
+
var_log = Path("/var/log")
|
|
221
|
+
if var_log.is_dir():
|
|
222
|
+
paths["System Logs"] = var_log
|
|
223
|
+
|
|
224
|
+
return paths
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def drop_linux_page_caches() -> bool:
|
|
228
|
+
"""
|
|
229
|
+
Sync and drop page caches / dentries on Linux (requires root).
|
|
230
|
+
"""
|
|
231
|
+
try:
|
|
232
|
+
run_command(["sync"], check=False)
|
|
233
|
+
drop_path = Path("/proc/sys/vm/drop_caches")
|
|
234
|
+
if drop_path.exists():
|
|
235
|
+
with open(drop_path, "w") as f:
|
|
236
|
+
f.write("3\n")
|
|
237
|
+
return True
|
|
238
|
+
except (PermissionError, OSError):
|
|
239
|
+
pass
|
|
240
|
+
return False
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
# sysmaint/utils/parsers.py
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def parse_clamscan_output(output: str) -> list[dict[str, Any]]:
|
|
10
|
+
"""
|
|
11
|
+
Parse clamscan output and return a list of infected file records.
|
|
12
|
+
|
|
13
|
+
Expected line format: `/path/to/file: MalwareName FOUND`
|
|
14
|
+
"""
|
|
15
|
+
threats = []
|
|
16
|
+
for line in output.splitlines():
|
|
17
|
+
line = line.strip()
|
|
18
|
+
if "FOUND" in line and not line.startswith("Known viruses"):
|
|
19
|
+
parts = line.split(":", 1)
|
|
20
|
+
if len(parts) == 2:
|
|
21
|
+
file_path = parts[0].strip()
|
|
22
|
+
malware_part = parts[1].replace("FOUND", "").strip()
|
|
23
|
+
threats.append({
|
|
24
|
+
"file": file_path,
|
|
25
|
+
"malware": malware_part,
|
|
26
|
+
"engine": "clamav",
|
|
27
|
+
})
|
|
28
|
+
return threats
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def parse_sfc_output(output: str) -> dict[str, Any]:
|
|
32
|
+
"""
|
|
33
|
+
Parse SFC console output on Windows.
|
|
34
|
+
"""
|
|
35
|
+
out_lower = output.lower()
|
|
36
|
+
corruptions_found = "found corrupt files" in out_lower or "resource protection found corrupt" in out_lower
|
|
37
|
+
repaired = "successfully repaired" in out_lower
|
|
38
|
+
clean = "did not find any integrity violations" in out_lower
|
|
39
|
+
reboot_required = "reboot" in out_lower or "restart" in out_lower
|
|
40
|
+
|
|
41
|
+
status = "Clean"
|
|
42
|
+
if clean:
|
|
43
|
+
status = "Clean"
|
|
44
|
+
elif corruptions_found and repaired:
|
|
45
|
+
status = "Repaired"
|
|
46
|
+
elif corruptions_found and not repaired:
|
|
47
|
+
status = "Corrupted (Unrepaired)"
|
|
48
|
+
elif "could not perform" in out_lower:
|
|
49
|
+
status = "Error"
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
"status": status,
|
|
53
|
+
"clean": clean,
|
|
54
|
+
"corruptions_found": corruptions_found,
|
|
55
|
+
"repaired": repaired,
|
|
56
|
+
"reboot_required": reboot_required,
|
|
57
|
+
"raw": output.strip(),
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def parse_dism_output(output: str) -> dict[str, Any]:
|
|
62
|
+
"""
|
|
63
|
+
Parse Windows DISM check/scan/restore health output.
|
|
64
|
+
"""
|
|
65
|
+
out_lower = output.lower()
|
|
66
|
+
healthy = "no component store corruption detected" in out_lower
|
|
67
|
+
repairable = "the component store is repairable" in out_lower
|
|
68
|
+
repaired = "the restore operation completed successfully" in out_lower
|
|
69
|
+
corrupted = "component store has been corrupted" in out_lower or repairable
|
|
70
|
+
|
|
71
|
+
status = "Healthy"
|
|
72
|
+
if healthy:
|
|
73
|
+
status = "Healthy"
|
|
74
|
+
elif repaired:
|
|
75
|
+
status = "Repaired Successfully"
|
|
76
|
+
elif repairable:
|
|
77
|
+
status = "Corruption Detected (Repairable)"
|
|
78
|
+
elif corrupted:
|
|
79
|
+
status = "Corrupted"
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
"status": status,
|
|
83
|
+
"healthy": healthy,
|
|
84
|
+
"corrupted": corrupted,
|
|
85
|
+
"repaired": repaired,
|
|
86
|
+
"raw": output.strip(),
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def parse_sfc_log(log_text: str) -> list[str]:
|
|
91
|
+
"""
|
|
92
|
+
Extract repaired file references from SFC CBS log snippets.
|
|
93
|
+
Looks for lines containing '[SR]'.
|
|
94
|
+
"""
|
|
95
|
+
repaired_files = []
|
|
96
|
+
for line in log_text.splitlines():
|
|
97
|
+
if "[SR]" in line and "Repairing corrupted file" in line:
|
|
98
|
+
repaired_files.append(line.strip())
|
|
99
|
+
return repaired_files
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def parse_fsck_output(output: str) -> dict[str, Any]:
|
|
103
|
+
"""Extract basic summary from fsck output."""
|
|
104
|
+
clean = "clean" in output.lower()
|
|
105
|
+
errors = re.findall(r"error|corrupt|inconsistent", output, re.IGNORECASE)
|
|
106
|
+
return {
|
|
107
|
+
"clean": clean,
|
|
108
|
+
"error_count": len(errors),
|
|
109
|
+
"errors_found": len(errors) > 0,
|
|
110
|
+
"raw": output.strip(),
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def parse_driverquery_output(output: str) -> list[dict[str, str]]:
|
|
115
|
+
"""
|
|
116
|
+
Parse Windows driverquery /v CSV or tabular output.
|
|
117
|
+
"""
|
|
118
|
+
drivers = []
|
|
119
|
+
lines = output.splitlines()
|
|
120
|
+
for line in lines:
|
|
121
|
+
if line.startswith('"') and '","' in line:
|
|
122
|
+
# CSV format
|
|
123
|
+
parts = [p.strip('"') for p in line.split('","')]
|
|
124
|
+
if len(parts) >= 4 and parts[0] != "Module Name":
|
|
125
|
+
drivers.append({
|
|
126
|
+
"name": parts[0],
|
|
127
|
+
"display_name": parts[1] if len(parts) > 1 else parts[0],
|
|
128
|
+
"driver_type": parts[2] if len(parts) > 2 else "",
|
|
129
|
+
"state": parts[4] if len(parts) > 4 else "Running",
|
|
130
|
+
"status": parts[5] if len(parts) > 5 else "OK",
|
|
131
|
+
})
|
|
132
|
+
elif len(line.split()) >= 3 and not line.startswith("==") and not line.startswith("Module"):
|
|
133
|
+
parts = line.split(None, 3)
|
|
134
|
+
drivers.append({
|
|
135
|
+
"name": parts[0],
|
|
136
|
+
"display_name": parts[1],
|
|
137
|
+
"driver_type": parts[2],
|
|
138
|
+
"state": parts[3] if len(parts) > 3 else "Running",
|
|
139
|
+
"status": "OK",
|
|
140
|
+
})
|
|
141
|
+
return drivers
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def parse_ping_output(output: str) -> dict[str, Any]:
|
|
145
|
+
"""
|
|
146
|
+
Parse ping command output across Windows and Linux.
|
|
147
|
+
"""
|
|
148
|
+
avg_match = re.search(r"Average = (\d+)ms|avg = [\d\.]+/([\d\.]+)/", output)
|
|
149
|
+
loss_match = re.search(r"(\d+)% loss", output)
|
|
150
|
+
|
|
151
|
+
latency = None
|
|
152
|
+
if avg_match:
|
|
153
|
+
val = avg_match.group(1) or avg_match.group(2)
|
|
154
|
+
if val:
|
|
155
|
+
try:
|
|
156
|
+
latency = float(val)
|
|
157
|
+
except ValueError:
|
|
158
|
+
pass
|
|
159
|
+
|
|
160
|
+
packet_loss = 100
|
|
161
|
+
if loss_match:
|
|
162
|
+
try:
|
|
163
|
+
packet_loss = int(loss_match.group(1))
|
|
164
|
+
except ValueError:
|
|
165
|
+
pass
|
|
166
|
+
|
|
167
|
+
return {
|
|
168
|
+
"reachable": packet_loss < 100,
|
|
169
|
+
"packet_loss_percent": packet_loss,
|
|
170
|
+
"avg_latency_ms": latency,
|
|
171
|
+
}
|