host-metrics/scripts/register-prometheus.py
2026-09-17 22:31:35 +02:00

27 lines
1.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""Register the SQL plugin's Prometheus connector, preserving existing definitions."""
import json
import urllib.request
BASE = 'http://127.0.0.1:9200/_plugins/_query/_datasources'
with urllib.request.urlopen(BASE, timeout=30) as response:
existing = json.load(response)
def has_name(value):
if isinstance(value, dict):
return value.get('name') == 'host_prometheus' or any(has_name(v) for v in value.values())
if isinstance(value, list):
return any(has_name(v) for v in value)
return False
if has_name(existing):
print('Preserving existing host_prometheus data source.')
else:
payload = {'name': 'host_prometheus', 'connector': 'prometheus',
'properties': {'prometheus.uri': 'http://prometheus:9090'}}
request = urllib.request.Request(BASE, data=json.dumps(payload).encode(),
headers={'Content-Type': 'application/json'}, method='POST')
with urllib.request.urlopen(request, timeout=30) as response:
print(response.read().decode())
print('Open Observability → Metrics and select host_prometheus. Allow 3060 seconds for samples.')