#!/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 30–60 seconds for samples.')