|
| 1 | +import platform |
| 2 | +from subprocess import run |
| 3 | + |
| 4 | +from app.utils import sysinfo_utils |
| 5 | + |
| 6 | + |
| 7 | +class Menu: |
| 8 | + def __init__(self): |
| 9 | + self.distro = sysinfo_utils.detect_distro() |
| 10 | + self.kernel = platform.release() |
| 11 | + self.hostname = platform.node() |
| 12 | + |
| 13 | + # Service statuses |
| 14 | + self.bbr_status = "Unknown" |
| 15 | + self.docker_status = "Unknown" |
| 16 | + self.fail2ban_status = "Unknown" |
| 17 | + self.ufw_status = "Unknown" |
| 18 | + |
| 19 | + self.menu_text = "" |
| 20 | + self.update_status() |
| 21 | + |
| 22 | + def update_status(self): |
| 23 | + """Update all service statuses""" |
| 24 | + self.bbr_status = self._check_bbr() |
| 25 | + self.docker_status = self._check_service("docker") |
| 26 | + self.fail2ban_status = self._check_service("fail2ban") |
| 27 | + self.ufw_status = self._check_ufw() |
| 28 | + self._build_menu_text() |
| 29 | + |
| 30 | + def _check_bbr(self): |
| 31 | + """Check BBR status""" |
| 32 | + try: |
| 33 | + result = run( |
| 34 | + ["sysctl", "net.ipv4.tcp_congestion_control"], |
| 35 | + capture_output=True, |
| 36 | + text=True, |
| 37 | + ) |
| 38 | + if "bbr" in result.stdout: |
| 39 | + return "Enabled" |
| 40 | + return "Disabled" |
| 41 | + except Exception: |
| 42 | + return "Unknown" |
| 43 | + |
| 44 | + def _check_service(self, service_name): |
| 45 | + """Check service status (systemd)""" |
| 46 | + try: |
| 47 | + result = run( |
| 48 | + ["systemctl", "is-active", service_name], capture_output=True, text=True |
| 49 | + ) |
| 50 | + status = result.stdout.strip() |
| 51 | + if status == "active": |
| 52 | + return "Running" |
| 53 | + return "Stopped" |
| 54 | + except Exception: |
| 55 | + return "Not Installed" |
| 56 | + |
| 57 | + def _check_ufw(self): |
| 58 | + """Check UFW status""" |
| 59 | + try: |
| 60 | + result = run(["ufw", "status"], capture_output=True, text=True) |
| 61 | + if "Status: active" in result.stdout: |
| 62 | + return "Active" |
| 63 | + return "Inactive" |
| 64 | + except Exception: |
| 65 | + return "Not Installed" |
| 66 | + |
| 67 | + def _build_menu_text(self): |
| 68 | + """Build menu text""" |
| 69 | + self.menu_text = f""" |
| 70 | +╔════════════════════════════════════════════════════════════╗ |
| 71 | +║ DevOps Automation Scripts v1.0.4 ║ |
| 72 | +╚════════════════════════════════════════════════════════════╝ |
| 73 | +
|
| 74 | +System: {self.distro} | Kernel: {self.kernel} | Host: {self.hostname} |
| 75 | +Services: BBR [{self.bbr_status}] | Docker [{self.docker_status}] | Fail2Ban [{self.fail2ban_status}] | UFW [{self.ufw_status}] |
| 76 | +
|
| 77 | +──────────────────────────────────────────────────────────── |
| 78 | +
|
| 79 | +Выберите скрипт: |
| 80 | +0 - Выход |
| 81 | +1 - BBR |
| 82 | +2 - Docker |
| 83 | +3 - Fail2Ban |
| 84 | +4 - UFW |
| 85 | +
|
| 86 | +Выбор: """ |
| 87 | + |
| 88 | + def display(self): |
| 89 | + """Display menu""" |
| 90 | + print(self.menu_text, end="") |
0 commit comments