Files
navidrome-litefs/scripts/cluster_status/cluster_aggregator.py

56 lines
1.8 KiB
Python

import consul_client
import litefs_client
import nomad_client
def get_cluster_status(consul_url, job_id="navidrome-litefs"):
"""
Aggregates cluster data from Consul, LiteFS, and Nomad.
"""
consul_nodes = consul_client.get_cluster_services(consul_url)
aggregated_nodes = []
is_healthy = True
primary_count = 0
for node in consul_nodes:
litefs_status = litefs_client.get_node_status(node["address"])
# Merge data
node_data = {
**node,
"litefs_primary": litefs_status.get("is_primary", False),
"uptime": litefs_status.get("uptime", "N/A"),
"advertise_url": litefs_status.get("advertise_url", ""),
"replication_lag": litefs_status.get("replication_lag", "N/A"),
"litefs_error": litefs_status.get("error", None),
"nomad_logs": None
}
if node["status"] != "passing":
is_healthy = False
# Fetch Nomad logs for critical nodes
alloc_id = nomad_client.get_allocation_id(node["node"], job_id)
if alloc_id:
node_data["alloc_id"] = alloc_id
node_data["nomad_logs"] = nomad_client.get_allocation_logs(alloc_id)
if node_data["litefs_primary"]:
primary_count += 1
aggregated_nodes.append(node_data)
# Final health check
health = "Healthy"
if not is_healthy:
health = "Unhealthy"
elif primary_count == 0:
health = "No Primary Detected"
elif primary_count > 1:
health = "Split Brain Detected (Multiple Primaries)"
return {
"health": health,
"nodes": aggregated_nodes,
"primary_count": primary_count
}