Full implementation of multi-type server management across GUI and CLI: New clients: SQLClient (MariaDB/MSSQL/PostgreSQL), RedisClient, GrafanaClient, PrometheusClient, TelnetSession, WinRMClient, RemoteDesktopLauncher. New GUI tabs: QueryTab (SQL editor + Treeview), RedisTab (console + history), GrafanaTab (dashboards + alerts), PrometheusTab (PromQL + targets), PowershellTab (PS/CMD), LaunchTab (RDP/VNC external client). Infrastructure: TAB_REGISTRY for conditional tabs per server type, adaptive server_dialog fields, colored type badges in sidebar, status checker for all types (SSH/TCP/SQL/Redis/HTTP), 100+ i18n keys. CLI: ssh.py extended with --sql, --redis, --grafana-*, --prom-*, --ps, --cmd. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
203 lines
8.2 KiB
Python
203 lines
8.2 KiB
Python
"""
|
|
Grafana tab — dashboards browser and alerts overview.
|
|
"""
|
|
|
|
import threading
|
|
import webbrowser
|
|
from tkinter import ttk
|
|
|
|
import customtkinter as ctk
|
|
from core.grafana_client import GrafanaClient
|
|
from core.i18n import t
|
|
|
|
|
|
class GrafanaTab(ctk.CTkFrame):
|
|
def __init__(self, master, store):
|
|
super().__init__(master, fg_color="transparent")
|
|
self.store = store
|
|
self._current_alias: str | None = None
|
|
self._client: GrafanaClient | None = None
|
|
self._dashboards: list[dict] = []
|
|
|
|
self._build_ui()
|
|
|
|
def _build_ui(self):
|
|
# ── Header + Refresh ──
|
|
header_frame = ctk.CTkFrame(self, fg_color="transparent")
|
|
header_frame.pack(fill="x", padx=15, pady=(15, 5))
|
|
|
|
title = ctk.CTkLabel(header_frame, text=t("grafana_title"),
|
|
font=ctk.CTkFont(size=18, weight="bold"))
|
|
title.pack(side="left")
|
|
|
|
self._refresh_btn = ctk.CTkButton(header_frame, text=t("grafana_refresh"), width=100,
|
|
command=self._refresh)
|
|
self._refresh_btn.pack(side="right")
|
|
|
|
# ── Dashboards section ──
|
|
dash_label = ctk.CTkLabel(self, text=t("grafana_dashboards"),
|
|
font=ctk.CTkFont(size=14, weight="bold"), anchor="w")
|
|
dash_label.pack(fill="x", padx=15, pady=(10, 3))
|
|
|
|
dash_frame = ctk.CTkFrame(self, fg_color="transparent")
|
|
dash_frame.pack(fill="both", expand=True, padx=15, pady=(0, 5))
|
|
|
|
columns = ("uid", "title", "folder")
|
|
self._dash_tree = ttk.Treeview(dash_frame, columns=columns, show="headings",
|
|
selectmode="browse", height=8)
|
|
self._dash_tree.heading("uid", text="UID")
|
|
self._dash_tree.heading("title", text=t("grafana_dash_title"))
|
|
self._dash_tree.heading("folder", text=t("grafana_dash_folder"))
|
|
self._dash_tree.column("uid", width=120, minwidth=80)
|
|
self._dash_tree.column("title", width=300, minwidth=150)
|
|
self._dash_tree.column("folder", width=150, minwidth=80)
|
|
self._dash_tree.pack(side="left", fill="both", expand=True)
|
|
|
|
dash_scroll = ttk.Scrollbar(dash_frame, orient="vertical", command=self._dash_tree.yview)
|
|
dash_scroll.pack(side="right", fill="y")
|
|
self._dash_tree.configure(yscrollcommand=dash_scroll.set)
|
|
|
|
self._dash_tree.bind("<Double-1>", self._on_dashboard_click)
|
|
|
|
# ── Alerts section ──
|
|
alerts_label = ctk.CTkLabel(self, text=t("grafana_alerts"),
|
|
font=ctk.CTkFont(size=14, weight="bold"), anchor="w")
|
|
alerts_label.pack(fill="x", padx=15, pady=(10, 3))
|
|
|
|
alerts_frame = ctk.CTkFrame(self, fg_color="transparent")
|
|
alerts_frame.pack(fill="both", expand=True, padx=15, pady=(0, 5))
|
|
|
|
alert_columns = ("state", "name", "severity")
|
|
self._alerts_tree = ttk.Treeview(alerts_frame, columns=alert_columns, show="headings",
|
|
selectmode="browse", height=6)
|
|
self._alerts_tree.heading("state", text=t("grafana_alert_state"))
|
|
self._alerts_tree.heading("name", text=t("grafana_alert_name"))
|
|
self._alerts_tree.heading("severity", text=t("grafana_alert_severity"))
|
|
self._alerts_tree.column("state", width=100, minwidth=60)
|
|
self._alerts_tree.column("name", width=300, minwidth=150)
|
|
self._alerts_tree.column("severity", width=100, minwidth=60)
|
|
self._alerts_tree.pack(side="left", fill="both", expand=True)
|
|
|
|
alerts_scroll = ttk.Scrollbar(alerts_frame, orient="vertical", command=self._alerts_tree.yview)
|
|
alerts_scroll.pack(side="right", fill="y")
|
|
self._alerts_tree.configure(yscrollcommand=alerts_scroll.set)
|
|
|
|
# ── Status bar ──
|
|
self._status_bar = ctk.CTkLabel(self, text=t("grafana_no_server"), anchor="w",
|
|
font=ctk.CTkFont(size=11), text_color="#9ca3af")
|
|
self._status_bar.pack(fill="x", padx=15, pady=(5, 10))
|
|
|
|
# ── Public API ──
|
|
|
|
def set_server(self, alias: str | None):
|
|
"""Called when user selects a server in sidebar."""
|
|
self._current_alias = alias
|
|
self._client = None
|
|
self._dashboards.clear()
|
|
self._clear_tables()
|
|
|
|
if alias:
|
|
self._set_status(t("grafana_connected").format(alias=alias), "#22c55e")
|
|
self._refresh()
|
|
else:
|
|
self._set_status(t("grafana_no_server"), "#9ca3af")
|
|
|
|
# ── Refresh ──
|
|
|
|
def _refresh(self):
|
|
if not self._current_alias:
|
|
self._set_status(t("no_server_selected"), "#ef4444")
|
|
return
|
|
|
|
self._refresh_btn.configure(state="disabled", text=t("grafana_loading"))
|
|
self._set_status(t("grafana_loading"), "#ccaa00")
|
|
|
|
def _do():
|
|
try:
|
|
client = self._get_client()
|
|
|
|
dashboards = client.list_dashboards()
|
|
alerts = client.list_alerts()
|
|
|
|
self.after(0, lambda: self._populate_dashboards(dashboards))
|
|
self.after(0, lambda: self._populate_alerts(alerts))
|
|
self.after(0, lambda: self._set_status(
|
|
t("grafana_loaded").format(
|
|
dashboards=len(dashboards), alerts=len(alerts)
|
|
), "#22c55e"))
|
|
except Exception as e:
|
|
self.after(0, lambda: self._set_status(f"(error) {e}", "#ef4444"))
|
|
finally:
|
|
self.after(0, lambda: self._refresh_btn.configure(
|
|
state="normal", text=t("grafana_refresh")))
|
|
|
|
threading.Thread(target=_do, daemon=True).start()
|
|
|
|
def _get_client(self) -> GrafanaClient:
|
|
if self._client is None:
|
|
self._client = GrafanaClient(self._current_alias, self.store)
|
|
return self._client
|
|
|
|
# ── Table population ──
|
|
|
|
def _populate_dashboards(self, dashboards: list[dict]):
|
|
self._dash_tree.delete(*self._dash_tree.get_children())
|
|
self._dashboards = dashboards
|
|
for d in dashboards:
|
|
uid = d.get("uid", "")
|
|
title = d.get("title", "")
|
|
folder = d.get("folderTitle", d.get("folder", "General"))
|
|
self._dash_tree.insert("", "end", values=(uid, title, folder))
|
|
|
|
def _populate_alerts(self, alerts: list[dict]):
|
|
self._alerts_tree.delete(*self._alerts_tree.get_children())
|
|
for a in alerts:
|
|
state = a.get("state", a.get("status", "unknown"))
|
|
name = a.get("name", a.get("title", ""))
|
|
severity = a.get("severity", a.get("labels", {}).get("severity", "—"))
|
|
tag = ""
|
|
if state in ("alerting", "firing"):
|
|
tag = "alerting"
|
|
elif state in ("ok", "normal", "inactive"):
|
|
tag = "ok"
|
|
self._alerts_tree.insert("", "end", values=(state, name, severity), tags=(tag,))
|
|
|
|
# Color-code alert states
|
|
self._alerts_tree.tag_configure("alerting", foreground="#ef4444")
|
|
self._alerts_tree.tag_configure("ok", foreground="#22c55e")
|
|
|
|
def _clear_tables(self):
|
|
self._dash_tree.delete(*self._dash_tree.get_children())
|
|
self._alerts_tree.delete(*self._alerts_tree.get_children())
|
|
|
|
# ── Events ──
|
|
|
|
def _on_dashboard_click(self, _event):
|
|
"""Open dashboard URL in browser on double-click."""
|
|
selection = self._dash_tree.selection()
|
|
if not selection:
|
|
return
|
|
item = self._dash_tree.item(selection[0])
|
|
uid = item["values"][0] if item["values"] else None
|
|
if not uid:
|
|
return
|
|
|
|
# Find the dashboard data to get the URL
|
|
for d in self._dashboards:
|
|
if d.get("uid") == uid:
|
|
url = d.get("url", "")
|
|
if url:
|
|
try:
|
|
client = self._get_client()
|
|
full_url = client.get_dashboard_url(url)
|
|
webbrowser.open(full_url)
|
|
except Exception:
|
|
# Fallback: just open relative URL
|
|
webbrowser.open(url)
|
|
break
|
|
|
|
# ── Helpers ──
|
|
|
|
def _set_status(self, text: str, color: str = "#9ca3af"):
|
|
self._status_bar.configure(text=text, text_color=color)
|