-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.py
More file actions
159 lines (132 loc) · 4.22 KB
/
Copy pathmonitor.py
File metadata and controls
159 lines (132 loc) · 4.22 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
import argparse
import config
def positive_int(value):
"""Validator argparse: pastikan nilai bilangan bulat positif."""
try:
value = int(value)
except ValueError:
raise argparse.ArgumentTypeError("harus bilangan bulat")
if value <= 0:
raise argparse.ArgumentTypeError("harus lebih besar dari 0")
return value
def parse_args(argv=None):
parser = argparse.ArgumentParser(
prog="monitor",
description="Server Monitor - TUI untuk monitoring Linux server.",
)
parser.add_argument(
"--refresh",
type=positive_int,
default=config.REFRESH_RATE,
metavar="DETIK",
help=(
"Interval refresh widget System/Network dalam detik "
f"(default: {config.REFRESH_RATE})"
),
)
parser.add_argument(
"--slow-refresh",
type=positive_int,
default=config.SLOW_REFRESH_RATE,
metavar="DETIK",
help=(
"Interval refresh Docker/Kubernetes/Services dalam detik "
f"(default: {config.SLOW_REFRESH_RATE})"
),
)
parser.add_argument(
"--interface",
default=config.NETWORK_INTERFACE,
metavar="NAMA",
help="Interface jaringan yang dipantau (default: deteksi otomatis)",
)
parser.add_argument(
"--gateway",
default=config.GATEWAY,
metavar="IP",
help="Alamat gateway (default: dari config.py)",
)
parser.add_argument(
"--kubeconfig",
default=config.KUBECONFIG_PATH,
metavar="PATH",
help=(
"Path kubeconfig untuk memantau Kubernetes/K3s "
"(default: ~/.kube/config atau variabel KUBECONFIG)"
),
)
parser.add_argument(
"--no-docker",
action="store_true",
help="Sembunyikan widget Docker",
)
parser.add_argument(
"--no-kubernetes",
action="store_true",
help="Sembunyikan widget Kubernetes",
)
parser.add_argument(
"--no-services",
action="store_true",
help="Sembunyikan widget Services",
)
return parser.parse_args(argv)
def apply_args(args):
"""Terapkan argumen CLI ke konfigurasi runtime.
Dipanggil SEBELUM import widget, karena widget membaca nilai config
saat module-nya diimpor.
"""
config.REFRESH_RATE = args.refresh
config.SLOW_REFRESH_RATE = args.slow_refresh
config.NETWORK_INTERFACE = args.interface
config.GATEWAY = args.gateway
config.KUBECONFIG_PATH = args.kubeconfig
config.SHOW_DOCKER = not args.no_docker
config.SHOW_KUBERNETES = not args.no_kubernetes
config.SHOW_SERVICES = not args.no_services
apply_args(parse_args())
from textual.app import App, ComposeResult
from textual.containers import Container, Horizontal, Vertical
from textual.widget import Widget
from textual.widgets import Header, Footer
from widgets.banner import BannerWidget
from widgets.system import SystemWidget
from widgets.network import NetworkWidget
from widgets.docker import DockerWidget
from widgets.kubernetes import KubernetesWidget
from widgets.services import ServicesWidget
from config import (
SHOW_DOCKER,
SHOW_KUBERNETES,
SHOW_SERVICES,
)
class ServerMonitor(App):
TITLE = "Server Monitor"
CSS_PATH = "monitor.css"
BINDINGS = [
("q", "quit", "Quit"),
("r", "refresh", "Refresh"),
]
def compose(self) -> ComposeResult:
yield Header()
yield BannerWidget()
with Container(id="dashboard"):
with Horizontal(id="top-row"):
yield SystemWidget()
with Vertical(id="right-column"):
if SHOW_SERVICES:
yield ServicesWidget()
yield NetworkWidget()
if SHOW_KUBERNETES:
yield KubernetesWidget()
if SHOW_DOCKER:
yield DockerWidget()
yield Footer()
def action_refresh(self):
# Paksa semua widget mengambil data baru, bukan sekadar refresh layout.
for widget in self.query(Widget):
refresh = getattr(widget, "refresh_data", None)
if callable(refresh):
refresh()
if __name__ == "__main__":
ServerMonitor().run()