feat: add deploy script for mcp-prometheus and mcp-loki with auto-detection

This commit is contained in:
Claude AI
2026-03-02 09:06:52 +00:00
parent 94eabc8e2c
commit aeda338e2f

View File

@@ -0,0 +1,135 @@
#!/usr/bin/env bash
# ============================================================
# deploy-monitoring-mcps.sh
# Builds and deploys mcp-prometheus + mcp-loki
# Run from: apps/ollama-mcp/
# ============================================================
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; CYAN='\033[0;36m'; NC='\033[0m'
info() { echo -e "${CYAN}[INFO]${NC} $*"; }
success() { echo -e "${GREEN}[OK]${NC} $*"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
die() { echo -e "${RED}[ERROR]${NC} $*" >&2; exit 1; }
# ── Pre-flight ───────────────────────────────────────────────
command -v docker >/dev/null 2>&1 || die "docker not found"
command -v docker compose >/dev/null 2>&1 || die "docker compose not found"
[[ -f .env ]] || {
warn ".env not found — copying from .env.example"
cp .env.example .env
echo ""
warn "Edit .env and set PROMETHEUS_URL and LOKI_URL, then re-run."
warn " PROMETHEUS_URL=http://<node-ip>:<nodeport>"
warn " LOKI_URL=http://<node-ip>:<nodeport>"
echo ""
exit 1
}
source .env
# ── Detect Prometheus & Loki URLs ───────────────────────────
info "Checking Prometheus URL: ${PROMETHEUS_URL:-not set}"
info "Checking Loki URL: ${LOKI_URL:-not set}"
if [[ -z "${PROMETHEUS_URL:-}" ]]; then
warn "PROMETHEUS_URL is not set in .env"
echo ""
info "Trying to auto-detect from kubectl..."
if command -v kubectl >/dev/null 2>&1; then
PROM_PORT=$(kubectl -n monitoring get svc kube-prometheus-stack-prometheus \
-o jsonpath='{.spec.ports[0].nodePort}' 2>/dev/null || echo "")
NODE_IP=$(kubectl get nodes -o jsonpath='{.items[0].status.addresses[?(@.type=="InternalIP")].address}' 2>/dev/null || echo "")
if [[ -n "$PROM_PORT" && -n "$NODE_IP" ]]; then
success "Auto-detected Prometheus: http://${NODE_IP}:${PROM_PORT}"
sed -i "s|PROMETHEUS_URL=.*|PROMETHEUS_URL=http://${NODE_IP}:${PROM_PORT}|" .env
PROMETHEUS_URL="http://${NODE_IP}:${PROM_PORT}"
else
die "Could not auto-detect. Set PROMETHEUS_URL manually in .env"
fi
else
die "kubectl not found. Set PROMETHEUS_URL manually in .env"
fi
fi
if [[ -z "${LOKI_URL:-}" ]]; then
warn "LOKI_URL is not set in .env"
info "Trying to auto-detect from kubectl..."
if command -v kubectl >/dev/null 2>&1; then
LOKI_PORT=$(kubectl -n monitoring get svc loki \
-o jsonpath='{.spec.ports[0].nodePort}' 2>/dev/null || echo "")
NODE_IP=$(kubectl get nodes -o jsonpath='{.items[0].status.addresses[?(@.type=="InternalIP")].address}' 2>/dev/null || echo "")
if [[ -n "$LOKI_PORT" && -n "$NODE_IP" ]]; then
success "Auto-detected Loki: http://${NODE_IP}:${LOKI_PORT}"
sed -i "s|LOKI_URL=.*|LOKI_URL=http://${NODE_IP}:${LOKI_PORT}|" .env
LOKI_URL="http://${NODE_IP}:${LOKI_PORT}"
else
die "Could not auto-detect. Set LOKI_URL manually in .env"
fi
else
die "kubectl not found. Set LOKI_URL manually in .env"
fi
fi
# ── Build & Deploy ───────────────────────────────────────────
echo ""
info "Building mcp-prometheus..."
docker compose build mcp-prometheus
success "mcp-prometheus built"
info "Building mcp-loki..."
docker compose build mcp-loki
success "mcp-loki built"
info "Starting containers..."
docker compose up -d mcp-prometheus mcp-loki
success "Containers started"
# ── Health checks ────────────────────────────────────────────
echo ""
info "Waiting for health checks..."
sleep 5
PROM_PORT="${MCP_PROMETHEUS_PORT:-3003}"
LOKI_PORT_MCP="${MCP_LOKI_PORT:-3005}"
check_health() {
local name=$1 port=$2
if curl -sf "http://localhost:${port}/health" >/dev/null 2>&1; then
success "${name} is healthy → http://localhost:${port}"
else
warn "${name} health check failed — check logs:"
echo " docker compose logs ${name}"
fi
}
check_health "mcp-prometheus" "$PROM_PORT"
check_health "mcp-loki" "$LOKI_PORT_MCP"
# ── Summary ──────────────────────────────────────────────────
echo ""
echo -e "${GREEN}═══════════════════════════════════════════════════${NC}"
echo -e "${GREEN} MCP Monitoring Stack Deployed${NC}"
echo -e "${GREEN}═══════════════════════════════════════════════════${NC}"
echo ""
echo " Prometheus MCP → http://localhost:${PROM_PORT}"
echo " Loki MCP → http://localhost:${LOKI_PORT_MCP}"
echo ""
echo -e "${CYAN}Example prompts for Ollama:${NC}"
echo ' "Which pods have the most CPU usage right now?"'
echo ' "Show errors from the argocd namespace last 30 minutes"'
echo ' "Find any OOMKilled events in the last 24 hours"'
echo ' "What are the top memory-consuming pods in monitoring?"'
echo ' "Show firing alerts"'
echo ""
echo -e "${CYAN}Useful commands:${NC}"
echo " docker compose logs -f mcp-prometheus"
echo " docker compose logs -f mcp-loki"
echo " docker compose ps"
echo " docker compose restart mcp-prometheus mcp-loki"
echo ""