mirror of
https://github.com/samjage/metro-warden.git
synced 2026-06-06 02:50:42 +00:00
Initial commit: Metro Warden TUI network operations center
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
"""Metro Warden plugins package."""
|
||||
|
||||
from .base import BasePlugin
|
||||
|
||||
__all__ = ["BasePlugin"]
|
||||
Binary file not shown.
Binary file not shown.
+105
@@ -0,0 +1,105 @@
|
||||
"""
|
||||
Metro Warden Base Plugin — abstract base class for all plugins.
|
||||
|
||||
Every plugin must subclass :class:`BasePlugin` and define the class-level
|
||||
attributes ``name``, ``version``, and ``description``. Lifecycle hooks
|
||||
``on_load``, ``on_unload``, and ``on_event`` can be overridden as needed.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import abc
|
||||
import asyncio
|
||||
import logging
|
||||
from typing import Any, Optional
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class BasePlugin(abc.ABC):
|
||||
"""
|
||||
Abstract base class for Metro Warden plugins.
|
||||
|
||||
Subclasses must declare:
|
||||
name = "my-plugin" # unique identifier
|
||||
version = "1.0.0"
|
||||
description = "Does something useful"
|
||||
tags = ["category"] # optional
|
||||
|
||||
Lifecycle::
|
||||
|
||||
on_load() — called once after instantiation
|
||||
on_unload() — called before the plugin is removed
|
||||
on_event(topic, data) — called for bus events the plugin subscribes to
|
||||
"""
|
||||
|
||||
# Class-level attributes — subclasses MUST override these
|
||||
name: str = ""
|
||||
version: str = "0.0.0"
|
||||
description: str = ""
|
||||
tags: list = []
|
||||
|
||||
def __init__(self, bus=None, state=None) -> None:
|
||||
if not self.name:
|
||||
raise ValueError(f"{type(self).__name__} must define a 'name' attribute")
|
||||
self._bus = bus
|
||||
self._state = state
|
||||
self._sub_ids: list[str] = []
|
||||
self._log = logging.getLogger(f"plugin.{self.name}")
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Lifecycle hooks
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def on_load(self) -> None:
|
||||
"""Called once when the plugin is loaded by the registry."""
|
||||
self._log.info("plugin %r loaded (v%s)", self.name, self.version)
|
||||
|
||||
def on_unload(self) -> None:
|
||||
"""Called when the plugin is being unloaded. Clean up resources here."""
|
||||
self._unsubscribe_all()
|
||||
self._log.info("plugin %r unloaded", self.name)
|
||||
|
||||
def on_event(self, topic: str, data: Any) -> None:
|
||||
"""
|
||||
Called for every bus event whose topic this plugin has subscribed to.
|
||||
Override in subclasses to handle specific events.
|
||||
"""
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Helper utilities
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def subscribe(self, topic_pattern: str) -> Optional[str]:
|
||||
"""Subscribe this plugin's on_event handler to *topic_pattern*."""
|
||||
if self._bus is None:
|
||||
self._log.warning("no bus — cannot subscribe to %r", topic_pattern)
|
||||
return None
|
||||
sub_id = self._bus.subscribe(topic_pattern, self.on_event)
|
||||
self._sub_ids.append(sub_id)
|
||||
return sub_id
|
||||
|
||||
def publish(self, topic: str, data: Any = None) -> None:
|
||||
"""Publish an event to the bus."""
|
||||
if self._bus is None:
|
||||
return
|
||||
self._bus.publish_sync(topic, data)
|
||||
|
||||
def state_get(self, key: str, default: Any = None) -> Any:
|
||||
if self._state is None:
|
||||
return default
|
||||
return self._state.get(key, default)
|
||||
|
||||
def state_set(self, key: str, value: Any) -> None:
|
||||
if self._state is not None:
|
||||
self._state.set(key, value)
|
||||
|
||||
def _unsubscribe_all(self) -> None:
|
||||
if self._bus is None:
|
||||
return
|
||||
for sid in self._sub_ids:
|
||||
self._bus.unsubscribe(sid)
|
||||
self._sub_ids.clear()
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<{type(self).__name__} name={self.name!r} v{self.version}>"
|
||||
@@ -0,0 +1,5 @@
|
||||
"""DNS monitoring plugin package."""
|
||||
|
||||
from .plugin import DNSPlugin
|
||||
|
||||
__all__ = ["DNSPlugin"]
|
||||
@@ -0,0 +1,130 @@
|
||||
"""
|
||||
DNS Plugin — monitors DNS resolution, configured resolvers, and query health.
|
||||
|
||||
Publishes to:
|
||||
dns.resolvers — list of configured nameservers
|
||||
dns.health — last query round-trip time and result
|
||||
dns.query.result — result of a DNS query
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import socket
|
||||
import time
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from plugins.base import BasePlugin
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_POLL_INTERVAL = 30.0
|
||||
HEALTH_CHECK_HOST = "one.one.one.one"
|
||||
|
||||
|
||||
def _read_resolvers() -> List[str]:
|
||||
"""Parse /etc/resolv.conf for nameserver entries."""
|
||||
resolvers: List[str] = []
|
||||
try:
|
||||
with open("/etc/resolv.conf") as fh:
|
||||
for line in fh:
|
||||
line = line.strip()
|
||||
if line.startswith("nameserver"):
|
||||
parts = line.split()
|
||||
if len(parts) >= 2:
|
||||
resolvers.append(parts[1])
|
||||
except OSError:
|
||||
pass
|
||||
return resolvers
|
||||
|
||||
|
||||
def _check_dns_health(host: str) -> Dict:
|
||||
"""Perform a synchronous DNS health check and return timing info."""
|
||||
start = time.monotonic()
|
||||
error: Optional[str] = None
|
||||
resolved: Optional[str] = None
|
||||
try:
|
||||
resolved = socket.gethostbyname(host)
|
||||
except socket.gaierror as exc:
|
||||
error = str(exc)
|
||||
elapsed_ms = (time.monotonic() - start) * 1000.0
|
||||
return {
|
||||
"host": host,
|
||||
"resolved": resolved,
|
||||
"elapsed_ms": round(elapsed_ms, 2),
|
||||
"error": error,
|
||||
"healthy": error is None,
|
||||
}
|
||||
|
||||
|
||||
class DNSPlugin(BasePlugin):
|
||||
"""
|
||||
Monitors DNS resolver configuration and performs periodic health checks.
|
||||
Publishes resolver info and query health to the event bus.
|
||||
"""
|
||||
|
||||
name = "dns"
|
||||
version = "1.0.0"
|
||||
description = "Monitors DNS resolver configuration and query health"
|
||||
tags = ["network", "dns"]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
bus=None,
|
||||
state=None,
|
||||
poll_interval: float = DEFAULT_POLL_INTERVAL,
|
||||
health_host: str = HEALTH_CHECK_HOST,
|
||||
) -> None:
|
||||
super().__init__(bus=bus, state=state)
|
||||
self._poll_interval = poll_interval
|
||||
self._health_host = health_host
|
||||
self._task: asyncio.Task | None = None
|
||||
self._running = False
|
||||
|
||||
def on_load(self) -> None:
|
||||
super().on_load()
|
||||
self.subscribe("dns.query") # allow other plugins to request queries
|
||||
self._running = True
|
||||
try:
|
||||
loop = asyncio.get_running_loop()
|
||||
self._task = loop.create_task(self._poll_loop())
|
||||
except RuntimeError:
|
||||
self._log.debug("no running event loop at load time; task deferred")
|
||||
|
||||
def on_unload(self) -> None:
|
||||
self._running = False
|
||||
if self._task and not self._task.done():
|
||||
self._task.cancel()
|
||||
super().on_unload()
|
||||
|
||||
async def _poll_loop(self) -> None:
|
||||
self._log.debug("dns poll loop started (interval=%.1fs)", self._poll_interval)
|
||||
while self._running:
|
||||
try:
|
||||
resolvers = await asyncio.to_thread(_read_resolvers)
|
||||
self.state_set("dns.resolvers", resolvers)
|
||||
if self._bus:
|
||||
await self._bus.publish("dns.resolvers", {"nameservers": resolvers})
|
||||
|
||||
health = await asyncio.to_thread(_check_dns_health, self._health_host)
|
||||
self.state_set("dns.health", health)
|
||||
if self._bus:
|
||||
await self._bus.publish("dns.health", health)
|
||||
except asyncio.CancelledError:
|
||||
break
|
||||
except Exception as exc:
|
||||
self._log.error("dns poll error: %s", exc)
|
||||
await asyncio.sleep(self._poll_interval)
|
||||
self._log.debug("dns poll loop stopped")
|
||||
|
||||
def on_event(self, topic: str, data: Any) -> None:
|
||||
if topic == "dns.query" and isinstance(data, dict):
|
||||
host = data.get("host", "")
|
||||
if host:
|
||||
asyncio.ensure_future(self._resolve_and_publish(host))
|
||||
|
||||
async def _resolve_and_publish(self, host: str) -> None:
|
||||
result = await asyncio.to_thread(_check_dns_health, host)
|
||||
if self._bus:
|
||||
await self._bus.publish("dns.query.result", result)
|
||||
@@ -0,0 +1,5 @@
|
||||
"""Firewall monitoring plugin package."""
|
||||
|
||||
from .plugin import FirewallPlugin
|
||||
|
||||
__all__ = ["FirewallPlugin"]
|
||||
@@ -0,0 +1,200 @@
|
||||
"""
|
||||
Firewall Plugin — reads firewall rules from iptables or nftables.
|
||||
|
||||
Publishes to:
|
||||
firewall.backend — detected backend ("iptables", "nftables", "none")
|
||||
firewall.rules — parsed rule list
|
||||
firewall.chains — dict of chains with policy and rule count
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from plugins.base import BasePlugin
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_POLL_INTERVAL = 60.0
|
||||
|
||||
|
||||
def _detect_backend() -> str:
|
||||
"""Detect which firewall backend is available."""
|
||||
if shutil.which("nft"):
|
||||
return "nftables"
|
||||
if shutil.which("iptables"):
|
||||
return "iptables"
|
||||
return "none"
|
||||
|
||||
|
||||
def _run(args: List[str]) -> str:
|
||||
"""Run a subprocess and return stdout. Returns '' on error."""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
args,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=10,
|
||||
)
|
||||
return result.stdout
|
||||
except (subprocess.TimeoutExpired, FileNotFoundError, PermissionError) as exc:
|
||||
log.debug("command %r failed: %s", args, exc)
|
||||
return ""
|
||||
|
||||
|
||||
def _parse_iptables() -> Dict:
|
||||
"""Parse iptables -L -n -v output into structured data."""
|
||||
output = _run(["iptables", "-L", "-n", "-v", "--line-numbers"])
|
||||
chains: Dict[str, Dict] = {}
|
||||
rules: List[Dict] = {}
|
||||
|
||||
current_chain: Optional[str] = None
|
||||
policy_re = re.compile(r"^Chain (\S+) \(policy (\S+)")
|
||||
rule_re = re.compile(
|
||||
r"^\s*(\d+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.*)"
|
||||
)
|
||||
|
||||
for line in output.splitlines():
|
||||
m = policy_re.match(line)
|
||||
if m:
|
||||
current_chain = m.group(1)
|
||||
chains[current_chain] = {
|
||||
"policy": m.group(2),
|
||||
"rule_count": 0,
|
||||
}
|
||||
continue
|
||||
if current_chain and (m := rule_re.match(line)):
|
||||
rule = {
|
||||
"chain": current_chain,
|
||||
"num": int(m.group(1)),
|
||||
"pkts": m.group(2),
|
||||
"bytes": m.group(3),
|
||||
"target": m.group(4),
|
||||
"prot": m.group(5),
|
||||
"in": m.group(7),
|
||||
"out": m.group(8),
|
||||
"source": m.group(9),
|
||||
"destination": m.group(10).strip(),
|
||||
}
|
||||
rules.append(rule)
|
||||
chains[current_chain]["rule_count"] += 1
|
||||
|
||||
return {"chains": chains, "rules": rules, "backend": "iptables"}
|
||||
|
||||
|
||||
def _parse_nftables() -> Dict:
|
||||
"""Parse nft list ruleset output into structured data."""
|
||||
output = _run(["nft", "-j", "list", "ruleset"])
|
||||
chains: Dict[str, Dict] = {}
|
||||
rules: List[Dict] = []
|
||||
|
||||
try:
|
||||
import json
|
||||
data = json.loads(output)
|
||||
for item in data.get("nftables", []):
|
||||
if "chain" in item:
|
||||
c = item["chain"]
|
||||
chains[c["name"]] = {
|
||||
"table": c.get("table", ""),
|
||||
"policy": c.get("policy", ""),
|
||||
"rule_count": 0,
|
||||
}
|
||||
elif "rule" in item:
|
||||
r = item["rule"]
|
||||
chain_name = r.get("chain", "")
|
||||
rule_entry = {
|
||||
"chain": chain_name,
|
||||
"table": r.get("table", ""),
|
||||
"handle": r.get("handle", ""),
|
||||
"expr": str(r.get("expr", "")),
|
||||
}
|
||||
rules.append(rule_entry)
|
||||
if chain_name in chains:
|
||||
chains[chain_name]["rule_count"] += 1
|
||||
except Exception as exc:
|
||||
log.debug("nftables JSON parse failed, falling back: %s", exc)
|
||||
# Plain-text fallback
|
||||
for line in output.splitlines():
|
||||
line = line.strip()
|
||||
if line:
|
||||
rules.append({"chain": "unknown", "expr": line})
|
||||
|
||||
return {"chains": chains, "rules": rules, "backend": "nftables"}
|
||||
|
||||
|
||||
class FirewallPlugin(BasePlugin):
|
||||
"""
|
||||
Reads and monitors firewall rules from iptables or nftables.
|
||||
Automatically detects the available backend.
|
||||
"""
|
||||
|
||||
name = "firewall"
|
||||
version = "1.0.0"
|
||||
description = "Reads firewall rules from iptables or nftables"
|
||||
tags = ["security", "network", "firewall"]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
bus=None,
|
||||
state=None,
|
||||
poll_interval: float = DEFAULT_POLL_INTERVAL,
|
||||
) -> None:
|
||||
super().__init__(bus=bus, state=state)
|
||||
self._poll_interval = poll_interval
|
||||
self._backend: str = "none"
|
||||
self._task: asyncio.Task | None = None
|
||||
self._running = False
|
||||
|
||||
def on_load(self) -> None:
|
||||
super().on_load()
|
||||
self._backend = _detect_backend()
|
||||
self._log.info("firewall backend detected: %s", self._backend)
|
||||
self.state_set("firewall.backend", self._backend)
|
||||
self.subscribe("firewall.refresh")
|
||||
self._running = True
|
||||
try:
|
||||
loop = asyncio.get_running_loop()
|
||||
self._task = loop.create_task(self._poll_loop())
|
||||
except RuntimeError:
|
||||
self._log.debug("no running event loop at load time; task deferred")
|
||||
|
||||
def on_unload(self) -> None:
|
||||
self._running = False
|
||||
if self._task and not self._task.done():
|
||||
self._task.cancel()
|
||||
super().on_unload()
|
||||
|
||||
async def _poll_loop(self) -> None:
|
||||
self._log.debug("firewall poll loop started (interval=%.1fs)", self._poll_interval)
|
||||
while self._running:
|
||||
try:
|
||||
await self._collect_and_publish()
|
||||
except asyncio.CancelledError:
|
||||
break
|
||||
except Exception as exc:
|
||||
self._log.error("firewall poll error: %s", exc)
|
||||
await asyncio.sleep(self._poll_interval)
|
||||
self._log.debug("firewall poll loop stopped")
|
||||
|
||||
async def _collect_and_publish(self) -> None:
|
||||
if self._backend == "iptables":
|
||||
data = await asyncio.to_thread(_parse_iptables)
|
||||
elif self._backend == "nftables":
|
||||
data = await asyncio.to_thread(_parse_nftables)
|
||||
else:
|
||||
data = {"chains": {}, "rules": [], "backend": "none"}
|
||||
|
||||
self.state_set("firewall.rules", data.get("rules", []))
|
||||
self.state_set("firewall.chains", data.get("chains", {}))
|
||||
if self._bus:
|
||||
await self._bus.publish("firewall.rules", data)
|
||||
await self._bus.publish("firewall.chains", data.get("chains", {}))
|
||||
|
||||
def on_event(self, topic: str, data: Any) -> None:
|
||||
if topic == "firewall.refresh":
|
||||
asyncio.ensure_future(self._collect_and_publish())
|
||||
@@ -0,0 +1,5 @@
|
||||
"""Network monitoring plugin package."""
|
||||
|
||||
from .plugin import NetworkPlugin
|
||||
|
||||
__all__ = ["NetworkPlugin"]
|
||||
@@ -0,0 +1,129 @@
|
||||
"""
|
||||
Network Plugin — monitors network interfaces and traffic using psutil.
|
||||
|
||||
Publishes to:
|
||||
network.interfaces — dict of {iface: {status, ip4, ip6, rx_bytes, tx_bytes, ...}}
|
||||
network.stats — aggregate stats snapshot
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import socket
|
||||
from typing import Any, Dict
|
||||
|
||||
try:
|
||||
import psutil
|
||||
_PSUTIL_AVAILABLE = True
|
||||
except ImportError:
|
||||
_PSUTIL_AVAILABLE = False
|
||||
|
||||
from plugins.base import BasePlugin
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
# Polling interval in seconds
|
||||
DEFAULT_POLL_INTERVAL = 5.0
|
||||
|
||||
|
||||
def _get_interfaces() -> Dict[str, Dict]:
|
||||
"""Collect interface statistics from psutil."""
|
||||
if not _PSUTIL_AVAILABLE:
|
||||
return {}
|
||||
|
||||
stats: Dict[str, Dict] = {}
|
||||
io_counters = psutil.net_io_counters(pernic=True)
|
||||
if_stats = psutil.net_if_stats()
|
||||
if_addrs = psutil.net_if_addrs()
|
||||
|
||||
for iface, io in io_counters.items():
|
||||
nic_stat = if_stats.get(iface)
|
||||
addrs = if_addrs.get(iface, [])
|
||||
|
||||
ip4 = ""
|
||||
ip6 = ""
|
||||
mac = ""
|
||||
for addr in addrs:
|
||||
if addr.family == socket.AF_INET:
|
||||
ip4 = addr.address
|
||||
elif addr.family == socket.AF_INET6:
|
||||
ip6 = addr.address
|
||||
elif addr.family == psutil.AF_LINK:
|
||||
mac = addr.address
|
||||
|
||||
stats[iface] = {
|
||||
"status": "UP" if (nic_stat and nic_stat.isup) else "DOWN",
|
||||
"speed": nic_stat.speed if nic_stat else 0,
|
||||
"mtu": nic_stat.mtu if nic_stat else 0,
|
||||
"ip4": ip4,
|
||||
"ip6": ip6,
|
||||
"mac": mac,
|
||||
"rx_bytes": io.bytes_recv,
|
||||
"tx_bytes": io.bytes_sent,
|
||||
"rx_packets": io.packets_recv,
|
||||
"tx_packets": io.packets_sent,
|
||||
"rx_errors": io.errin,
|
||||
"tx_errors": io.errout,
|
||||
"rx_drop": io.dropin,
|
||||
"tx_drop": io.dropout,
|
||||
}
|
||||
|
||||
return stats
|
||||
|
||||
|
||||
class NetworkPlugin(BasePlugin):
|
||||
"""Monitors network interfaces and publishes stats to the event bus."""
|
||||
|
||||
name = "network"
|
||||
version = "1.0.0"
|
||||
description = "Monitors network interfaces and traffic statistics via psutil"
|
||||
tags = ["network", "monitoring"]
|
||||
|
||||
def __init__(self, bus=None, state=None, poll_interval: float = DEFAULT_POLL_INTERVAL) -> None:
|
||||
super().__init__(bus=bus, state=state)
|
||||
self._poll_interval = poll_interval
|
||||
self._task: asyncio.Task | None = None
|
||||
self._running = False
|
||||
|
||||
def on_load(self) -> None:
|
||||
super().on_load()
|
||||
if not _PSUTIL_AVAILABLE:
|
||||
self._log.warning("psutil not available — network monitoring degraded")
|
||||
self._running = True
|
||||
# Schedule the polling loop
|
||||
try:
|
||||
loop = asyncio.get_running_loop()
|
||||
self._task = loop.create_task(self._poll_loop())
|
||||
except RuntimeError:
|
||||
self._log.debug("no running event loop at load time; task deferred")
|
||||
|
||||
def on_unload(self) -> None:
|
||||
self._running = False
|
||||
if self._task and not self._task.done():
|
||||
self._task.cancel()
|
||||
super().on_unload()
|
||||
|
||||
async def _poll_loop(self) -> None:
|
||||
"""Periodically collect interface data and publish to bus."""
|
||||
self._log.debug("network poll loop started (interval=%.1fs)", self._poll_interval)
|
||||
while self._running:
|
||||
try:
|
||||
data = await asyncio.to_thread(_get_interfaces)
|
||||
self.state_set("network.interfaces", data)
|
||||
await self._bus.publish("network.interfaces", data) if self._bus else None
|
||||
await self._bus.publish("network.stats", {
|
||||
"interface_count": len(data),
|
||||
"active_count": sum(1 for v in data.values() if v["status"] == "UP"),
|
||||
}) if self._bus else None
|
||||
except asyncio.CancelledError:
|
||||
break
|
||||
except Exception as exc:
|
||||
self._log.error("poll error: %s", exc)
|
||||
await asyncio.sleep(self._poll_interval)
|
||||
self._log.debug("network poll loop stopped")
|
||||
|
||||
def on_event(self, topic: str, data: Any) -> None:
|
||||
"""Handle requests to refresh immediately."""
|
||||
if topic == "network.refresh":
|
||||
self._log.debug("manual refresh requested")
|
||||
@@ -0,0 +1,5 @@
|
||||
"""System monitoring plugin package."""
|
||||
|
||||
from .plugin import SystemPlugin
|
||||
|
||||
__all__ = ["SystemPlugin"]
|
||||
@@ -0,0 +1,164 @@
|
||||
"""
|
||||
System Plugin — monitors CPU, memory, disk, and load via psutil.
|
||||
|
||||
Publishes to:
|
||||
system.cpu — per-core and overall CPU usage %
|
||||
system.memory — RAM and swap usage
|
||||
system.disk — disk partition usage
|
||||
system.load — 1/5/15 minute load averages
|
||||
system.snapshot — combined full snapshot
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import os
|
||||
from typing import Any, Dict, List
|
||||
|
||||
try:
|
||||
import psutil
|
||||
_PSUTIL_AVAILABLE = True
|
||||
except ImportError:
|
||||
_PSUTIL_AVAILABLE = False
|
||||
|
||||
from plugins.base import BasePlugin
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_POLL_INTERVAL = 3.0
|
||||
|
||||
|
||||
def _collect_cpu() -> Dict:
|
||||
if not _PSUTIL_AVAILABLE:
|
||||
return {}
|
||||
per_core = psutil.cpu_percent(interval=None, percpu=True)
|
||||
freq = psutil.cpu_freq()
|
||||
return {
|
||||
"percent": psutil.cpu_percent(interval=None),
|
||||
"per_core": per_core,
|
||||
"core_count": psutil.cpu_count(logical=False),
|
||||
"logical_count": psutil.cpu_count(logical=True),
|
||||
"freq_mhz": round(freq.current, 1) if freq else 0,
|
||||
"freq_max_mhz": round(freq.max, 1) if freq else 0,
|
||||
}
|
||||
|
||||
|
||||
def _collect_memory() -> Dict:
|
||||
if not _PSUTIL_AVAILABLE:
|
||||
return {}
|
||||
vm = psutil.virtual_memory()
|
||||
sw = psutil.swap_memory()
|
||||
return {
|
||||
"total": vm.total,
|
||||
"available": vm.available,
|
||||
"used": vm.used,
|
||||
"percent": vm.percent,
|
||||
"swap_total": sw.total,
|
||||
"swap_used": sw.used,
|
||||
"swap_percent": sw.percent,
|
||||
}
|
||||
|
||||
|
||||
def _collect_disk() -> List[Dict]:
|
||||
if not _PSUTIL_AVAILABLE:
|
||||
return []
|
||||
partitions = []
|
||||
for part in psutil.disk_partitions(all=False):
|
||||
try:
|
||||
usage = psutil.disk_usage(part.mountpoint)
|
||||
partitions.append({
|
||||
"device": part.device,
|
||||
"mountpoint": part.mountpoint,
|
||||
"fstype": part.fstype,
|
||||
"total": usage.total,
|
||||
"used": usage.used,
|
||||
"free": usage.free,
|
||||
"percent": usage.percent,
|
||||
})
|
||||
except PermissionError:
|
||||
continue
|
||||
return partitions
|
||||
|
||||
|
||||
def _collect_load() -> Dict:
|
||||
try:
|
||||
avg = os.getloadavg()
|
||||
return {"load1": avg[0], "load5": avg[1], "load15": avg[2]}
|
||||
except AttributeError:
|
||||
return {"load1": 0.0, "load5": 0.0, "load15": 0.0}
|
||||
|
||||
|
||||
def _collect_snapshot() -> Dict:
|
||||
return {
|
||||
"cpu": _collect_cpu(),
|
||||
"memory": _collect_memory(),
|
||||
"disk": _collect_disk(),
|
||||
"load": _collect_load(),
|
||||
}
|
||||
|
||||
|
||||
class SystemPlugin(BasePlugin):
|
||||
"""Monitors CPU, memory, disk, and load averages via psutil."""
|
||||
|
||||
name = "system"
|
||||
version = "1.0.0"
|
||||
description = "Monitors system resources: CPU, memory, disk, and load averages"
|
||||
tags = ["system", "monitoring"]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
bus=None,
|
||||
state=None,
|
||||
poll_interval: float = DEFAULT_POLL_INTERVAL,
|
||||
) -> None:
|
||||
super().__init__(bus=bus, state=state)
|
||||
self._poll_interval = poll_interval
|
||||
self._task: asyncio.Task | None = None
|
||||
self._running = False
|
||||
|
||||
def on_load(self) -> None:
|
||||
super().on_load()
|
||||
if not _PSUTIL_AVAILABLE:
|
||||
self._log.warning("psutil not available — system monitoring degraded")
|
||||
self._running = True
|
||||
try:
|
||||
loop = asyncio.get_running_loop()
|
||||
self._task = loop.create_task(self._poll_loop())
|
||||
except RuntimeError:
|
||||
self._log.debug("no running event loop at load time; task deferred")
|
||||
|
||||
def on_unload(self) -> None:
|
||||
self._running = False
|
||||
if self._task and not self._task.done():
|
||||
self._task.cancel()
|
||||
super().on_unload()
|
||||
|
||||
async def _poll_loop(self) -> None:
|
||||
self._log.debug("system poll loop started (interval=%.1fs)", self._poll_interval)
|
||||
while self._running:
|
||||
try:
|
||||
snapshot = await asyncio.to_thread(_collect_snapshot)
|
||||
|
||||
self.state_set("system.cpu", snapshot["cpu"])
|
||||
self.state_set("system.memory", snapshot["memory"])
|
||||
self.state_set("system.disk", snapshot["disk"])
|
||||
self.state_set("system.load", snapshot["load"])
|
||||
|
||||
if self._bus:
|
||||
await self._bus.publish("system.cpu", snapshot["cpu"])
|
||||
await self._bus.publish("system.memory", snapshot["memory"])
|
||||
await self._bus.publish("system.disk", snapshot["disk"])
|
||||
await self._bus.publish("system.load", snapshot["load"])
|
||||
await self._bus.publish("system.snapshot", snapshot)
|
||||
|
||||
except asyncio.CancelledError:
|
||||
break
|
||||
except Exception as exc:
|
||||
self._log.error("system poll error: %s", exc)
|
||||
await asyncio.sleep(self._poll_interval)
|
||||
self._log.debug("system poll loop stopped")
|
||||
|
||||
def on_event(self, topic: str, data: Any) -> None:
|
||||
if topic == "system.refresh":
|
||||
asyncio.ensure_future(self._poll_loop())
|
||||
Reference in New Issue
Block a user