-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdr-metrics-server.py
More file actions
executable file
·117 lines (92 loc) · 3.72 KB
/
dr-metrics-server.py
File metadata and controls
executable file
·117 lines (92 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python3
"""
Disaster Recovery Metrics HTTP Server for Prometheus
Exposes disaster recovery metrics via HTTP on port 9101
Prometheus can scrape this endpoint directly
Usage:
python3 dr-metrics-server.py [--port PORT] [--host HOST]
Author: InsightLearn DevOps Team
Version: 1.0.0
"""
import os
import subprocess
import time
from http.server import BaseHTTPRequestHandler, HTTPServer
from pathlib import Path
# Configuration
DEFAULT_PORT = 9101
DEFAULT_HOST = "0.0.0.0"
METRICS_SCRIPT = Path(__file__).parent / "export-dr-metrics.sh"
class MetricsHandler(BaseHTTPRequestHandler):
"""HTTP handler for Prometheus metrics endpoint"""
def do_GET(self):
"""Handle GET request for /metrics endpoint"""
if self.path == "/metrics":
self.send_metrics()
elif self.path == "/health":
self.send_health()
else:
self.send_error(404, "Not Found - Use /metrics or /health")
def send_metrics(self):
"""Generate and send Prometheus metrics"""
try:
# Execute metrics export script with OUTPUT_STDOUT=1
env = os.environ.copy()
env["OUTPUT_STDOUT"] = "1"
env["METRICS_FILE"] = "/tmp/dr_metrics_temp.prom"
result = subprocess.run(
["bash", str(METRICS_SCRIPT)],
capture_output=True,
text=True,
timeout=30,
env=env
)
if result.returncode == 0:
metrics_data = result.stdout
# Send response
self.send_response(200)
self.send_header("Content-type", "text/plain; version=0.0.4")
self.end_headers()
self.wfile.write(metrics_data.encode())
else:
self.send_error(500, f"Metrics script failed: {result.stderr}")
except subprocess.TimeoutExpired:
self.send_error(504, "Metrics script timeout")
except Exception as e:
self.send_error(500, f"Error generating metrics: {str(e)}")
def send_health(self):
"""Health check endpoint"""
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
self.wfile.write(b"OK\n")
def log_message(self, format, *args):
"""Log HTTP requests with timestamp"""
timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
print(f"[{timestamp}] {self.address_string()} - {format % args}")
def run_server(host=DEFAULT_HOST, port=DEFAULT_PORT):
"""Start HTTP server"""
server_address = (host, port)
httpd = HTTPServer(server_address, MetricsHandler)
print(f"Starting Disaster Recovery Metrics Server on {host}:{port}")
print(f"Metrics endpoint: http://{host}:{port}/metrics")
print(f"Health endpoint: http://{host}:{port}/health")
print(f"Metrics script: {METRICS_SCRIPT}")
print("Press Ctrl+C to stop\n")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nShutting down server...")
httpd.shutdown()
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="DR Metrics HTTP Server for Prometheus")
parser.add_argument("--port", type=int, default=DEFAULT_PORT, help=f"Port to listen on (default: {DEFAULT_PORT})")
parser.add_argument("--host", type=str, default=DEFAULT_HOST, help=f"Host to bind to (default: {DEFAULT_HOST})")
args = parser.parse_args()
# Check if metrics script exists
if not METRICS_SCRIPT.exists():
print(f"ERROR: Metrics script not found: {METRICS_SCRIPT}")
print("Please ensure export-dr-metrics.sh is in the same directory")
exit(1)
run_server(host=args.host, port=args.port)