Update alloy and prom exporter

This commit is contained in:
weidenwiesel
2025-11-01 17:59:00 +01:00
parent 17fe77fcd8
commit 3fd7dbc8ee
2 changed files with 51 additions and 39 deletions
@@ -1,6 +1,5 @@
#!/usr/bin/python
import subprocess
import requests
import threading
import time
@@ -11,41 +10,23 @@ HOST = '127.0.0.1'
PORT = 10080
base_ip_prefix = "192.42.6."
base_ip_start = 11
base_ip_start = 31
update_interval = 30
aggregated_metrics = {}
expected_backends = [f"http://{base_ip_prefix}{base_ip_start + i}:8282/metrics" for i in range(10)]
expected_container_names = [f"invidious-companion-{i:02d}" for i in range(1, 11)]
def get_containers():
try:
result = subprocess.run(
["docker", "ps", "--format", "{{.Names}}"],
capture_output=True, text=True, check=True
)
containers = [line for line in result.stdout.splitlines() if line.startswith("invidious-companion-")]
containers.sort(key=lambda x: int(x.split('-')[2]))
return containers
except Exception as e:
print(f"Error getting container info: {e}")
return []
aggregated_metrics = {}
def fetch_metrics():
global aggregated_metrics
while True:
containers = get_containers()
backend = []
container_names = []
for i, container in enumerate(containers):
ip = f"{base_ip_prefix}{base_ip_start + i}"
backend.append(f"http://{ip}:8282/metrics")
container_names.append(container)
sums = {}
backend_down = 0
backend_down_list = []
backend_up_list = []
for url, container_name in zip(backend, container_names):
for url, container_name in zip(expected_backends, expected_container_names):
try:
resp = requests.get(url, timeout=5)
if resp.status_code != 200:
@@ -70,9 +51,9 @@ def fetch_metrics():
except Exception as e:
backend_down += 1
backend_down_list.append(container_name)
print(f"Error calling URL '{url}': {e}")
print(f"Fehler beim Abruf von {url}: {e}")
backend_total = len(backend)
backend_total = len(expected_backends)
backend_up = backend_total - backend_down
metrics = {}
@@ -84,6 +65,13 @@ def fetch_metrics():
metrics["invidious_companion_healthy_total"] = backend_up
metrics["invidious_companion_unhealthy_total"] = backend_down
try:
with open('/var/log/inv_network_load.log', 'r') as file:
network_load = file.read().replace('\n', '')
metrics["invidious_companion_network_load_percent"] = network_load
except Exception as e:
metrics["invidious_companion_network_load_percent"] = 0
labeled_metrics = []
for ep in backend_up_list:
labeled_metrics.append((f'invidious_companion_healthy{{container="{ep}"}}', 1))
@@ -101,6 +89,7 @@ class MetricsHandler(BaseHTTPRequestHandler):
for name, value in metrics.items():
response += f"# TYPE {name} gauge\n{name} {value}\n"
for labeled_name, value in labeled_metrics:
# labeled_name enthält schon Label-String, TYPE nicht nötig hier
response += f"{labeled_name} {value}\n"
self.send_response(200)
self.send_header('Content-Type', 'text/plain; version=0.0.4')
@@ -112,7 +101,7 @@ class MetricsHandler(BaseHTTPRequestHandler):
def run_server():
server = HTTPServer((HOST, PORT), MetricsHandler)
print(f'Starting HTTP server for prometheus.scrape under http://{HOST}:{PORT}/metrics')
print(f'Starte HTTP-Server auf http://{HOST}:{PORT}/metrics')
server.serve_forever()
if __name__ == '__main__':