Update all hardcoded push monitor names in check files to match the new Title Case With Space format in monitors.yml. The uk_tokens.yml keys are derived from monitor names so the push() calls must match exactly.
50 lines
1.7 KiB
Python
50 lines
1.7 KiB
Python
import time
|
|
import docker
|
|
import logging
|
|
from health_agent.uptime_kuma import push
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
def check_swarm_cluster():
|
|
start_time = time.time()
|
|
try:
|
|
client = docker.from_env()
|
|
nodes = client.nodes.list()
|
|
|
|
ready_nodes = []
|
|
managers = []
|
|
|
|
for node in nodes:
|
|
spec = node.attrs.get('Spec', {})
|
|
status = node.attrs.get('Status', {})
|
|
manager_status = node.attrs.get('ManagerStatus', {})
|
|
|
|
node_name = spec.get('Name', node.id)
|
|
is_ready = status.get('State') == 'ready'
|
|
is_manager = spec.get('Role') == 'manager'
|
|
|
|
if is_ready:
|
|
ready_nodes.append(node_name)
|
|
|
|
if is_manager:
|
|
reachability = manager_status.get('Reachability')
|
|
if reachability == 'reachable':
|
|
managers.append(node_name)
|
|
|
|
total_nodes = len(nodes)
|
|
ready_count = len(ready_nodes)
|
|
|
|
ping_ms = int((time.time() - start_time) * 1000)
|
|
|
|
if ready_count == total_nodes:
|
|
msg = f"{ready_count}/{total_nodes} nodes Ready (managers: {', '.join(managers)})"
|
|
push("Swarm Cluster", "up", msg, ping_ms)
|
|
else:
|
|
msg = f"{ready_count}/{total_nodes} nodes Ready | Managers reachable: {len(managers)}"
|
|
push("Swarm Cluster", "down", msg, ping_ms)
|
|
|
|
except Exception as e:
|
|
ping_ms = int((time.time() - start_time) * 1000)
|
|
logger.error(f"Swarm check failed: {e}")
|
|
push("Swarm Cluster", "down", str(e), ping_ms)
|