👌 IMPROVE: Update Hetzner configuration files and scripts to include new targets, enhance health check metrics, and organize file paths for better maintainability

This commit is contained in:
2025-05-22 10:28:08 +00:00
parent 61d056dcab
commit 945cae3908
7 changed files with 508 additions and 37 deletions

View File

@@ -17,10 +17,20 @@ if not HETZNER_API_TOKEN:
raise ValueError("❌ HETZNER_API_TOKEN is missing! Make sure it's set in the .env file.")
# 📂 Paths to output files
PROMETHEUS_TARGETS_FILE = "/opt/phx/main/config/hetzner_targets.json"
ERROR_LOG_FILE = "/opt/phx/main/config/hetzner_error_servers.json"
EXCLUDED_SERVERS_FILE = "/opt/phx/main/config/hetzner_excluded_servers.json"
DNS_MAPPING_FILE = "/opt/phx/main/config/hetzner_dns_mapping.json"
# PROMETHEUS_TARGETS_FILE = "/opt/phx/main/config/hetzner_targets.json"
# ERROR_LOG_FILE = "/opt/phx/main/config/hetzner_error_servers.json"
# EXCLUDED_SERVERS_FILE = "/opt/phx/main/config/hetzner_excluded_servers.json"
# DNS_MAPPING_FILE = "/opt/phx/main/config/hetzner_dns_mapping.json"
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
PROMETHEUS_TARGETS_FILE = os.path.join(BASE_DIR, "config/hetzner_targets.json")
PHX_SYSTEM_FILE = os.path.join(BASE_DIR, "config/phoenix_system_hetzner_targets.json")
PHX_WORKER_FILE = os.path.join(BASE_DIR, "config/phoenix_worker_hetzner_targets.json")
DNS_MAPPING_FILE = os.path.join(BASE_DIR, "config/hetzner_dns_mapping.json")
ERROR_LOG_FILE = os.path.join(BASE_DIR, "config/hetzner_error_servers.json")
EXCLUDED_SERVERS_FILE = os.path.join(BASE_DIR, "config/hetzner_excluded_servers.json")
# 📌 Hetzner API URL
HETZNER_API_URL = os.getenv("HETZNER_API_URL")
@@ -30,7 +40,7 @@ if not HETZNER_API_URL:
raise ValueError("❌ HETZNER_API_URL is missing! Make sure it's set in the .env file.")
# 🛑 List of server names to exclude (DARKLIST)
DARKLISTED_SERVERS = ["docuvita"]
DARKLISTED_SERVERS = ["docuvita", "teamcity", "gitea"]
# 📡 Fetch Hetzner server list with pagination support and Rate Limiting handling
def get_hetzner_servers():
@@ -78,6 +88,8 @@ def get_hetzner_servers():
def generate_prometheus_sd_config():
servers = get_hetzner_servers()
targets = []
phx_system_targets = []
phx_worker_targets = []
error_servers = []
excluded_servers = []
dns_mappings = [] # New list for storing DNS-IP mappings
@@ -97,6 +109,7 @@ def generate_prometheus_sd_config():
continue # Skip adding to Prometheus targets
if ipv4:
# Add to DNS mapping file
targets.append({
"targets": [f"{ipv4}:9100", f"{ipv4}:9113"],
"labels": {
@@ -104,7 +117,37 @@ def generate_prometheus_sd_config():
"datacenter": datacenter
}
})
# Add to DNS mapping file
# This is with Python Flask server for health checks
targets.append({
"targets": [f"{ipv4}:9800"],
"labels": {
"instance": f"{server_name}",
"datacenter": datacenter,
"job": "health-exporter"
}
})
# Phoenix System metrics (port 3000)
# phx_system_targets.append({
# "targets": [f"{ipv4}:3000"],
# "labels": {
# "instance": f"{server_name}",
# "datacenter": datacenter,
# "__metrics_path__": "/health/metrics"
# }
# })
# # Phoenix Worker metrics (port 3001)
# phx_worker_targets.append({
# "targets": [f"{ipv4}:3001"],
# "labels": {
# "instance": f"{server_name}",
# "datacenter": datacenter,
# "__metrics_path__": "/health/metrics"
# }
# })
dns_mappings.append({
"dns_name": server_name,
"ip_address": ipv4
@@ -125,6 +168,14 @@ def generate_prometheus_sd_config():
print(f"✅ Updated Prometheus targets in {PROMETHEUS_TARGETS_FILE}")
with open(PHX_SYSTEM_FILE, "w") as f:
json.dump(phx_system_targets, f, indent=4)
print(f"✅ phoenix-system targets saved to {PHX_SYSTEM_FILE}")
with open(PHX_WORKER_FILE, "w") as f:
json.dump(phx_worker_targets, f, indent=4)
print(f"✅ phoenix-worker targets saved to {PHX_WORKER_FILE}")
# Save DNS Mappings file
with open(DNS_MAPPING_FILE, "w") as f:
json.dump(dns_mappings, f, indent=4)