Initialize observability stack with local monitoring and server134 deployment

This commit is contained in:
qiubaishen
2026-10-15 18:35:05 +08:00
commit f657abeed1
36 changed files with 4644 additions and 0 deletions
+104
View File
@@ -0,0 +1,104 @@
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")/.."
env_value() {
local key="$1"
local fallback="$2"
local value=""
if [[ -f .env ]]; then
value="$(sed -n "s/^${key}=//p" .env | tail -n 1)"
value="${value#\"}"
value="${value%\"}"
fi
printf '%s' "${value:-${fallback}}"
}
if [[ -z "${PORT_CHECK_SPEC:-}" ]]; then
ui_bind_ip="$(env_value UI_BIND_IP 127.0.0.1)"
ingest_bind_ip="$(env_value INGEST_BIND_IP 127.0.0.1)"
debug_bind_ip="$(env_value DEBUG_BIND_IP 127.0.0.1)"
grafana_port="$(env_value GRAFANA_PORT 3000)"
PORT_CHECK_SPEC="$(cat <<EOF
grafana 3000 ${ui_bind_ip} ${grafana_port}
alertmanager 9093 ${ui_bind_ip} 9093
victoriametrics 8428 ${ingest_bind_ip} 8428
victorialogs 9428 ${ingest_bind_ip} 9428
tempo 4317 ${ingest_bind_ip} 4317
tempo 4318 ${ingest_bind_ip} 4318
tempo 3200 ${debug_bind_ip} 3200
vmalert 8880 ${debug_bind_ip} 8880
node-exporter 9100 ${debug_bind_ip} 9100
vmagent 8429 ${debug_bind_ip} 8429
EOF
)"
fi
compose_owns_port() {
local service="$1"
local container_port="$2"
local expected="$3"
local mappings
mappings="$(docker compose port "${service}" "${container_port}" 2>/dev/null || true)"
grep -Fqx -- "${expected}" <<<"${mappings}"
}
docker_publishers() {
local host_port="$1"
docker ps \
--filter "publish=${host_port}" \
--format '{{.ID}} {{.Names}} {{.Ports}}' 2>/dev/null || true
}
probe_port() {
local bind_ip="$1"
local host_port="$2"
python3 - "${bind_ip}" "${host_port}" <<'PY'
import socket
import sys
bind_ip = sys.argv[1]
port = int(sys.argv[2])
family = socket.AF_INET6 if ":" in bind_ip else socket.AF_INET
address = (bind_ip, port, 0, 0) if family == socket.AF_INET6 else (bind_ip, port)
sock = socket.socket(family, socket.SOCK_STREAM)
try:
sock.bind(address)
finally:
sock.close()
PY
}
conflicts=0
while read -r service container_port bind_ip host_port; do
[[ -z "${service:-}" ]] && continue
expected="${bind_ip}:${host_port}"
if compose_owns_port "${service}" "${container_port}" "${expected}"; then
printf 'Port %-21s is already owned by Compose service %s; allowing update.\n' "${expected}" "${service}"
continue
fi
publishers="$(docker_publishers "${host_port}")"
if [[ -n "${publishers}" ]]; then
conflicts=$((conflicts + 1))
printf 'PORT CONFLICT: %s needs %s, already published by another Docker container:\n' "${service}" "${expected}" >&2
printf ' %s\n' "${publishers}" >&2
continue
fi
if probe_error="$(probe_port "${bind_ip}" "${host_port}" 2>&1)"; then
printf 'Port %-21s is available for %s.\n' "${expected}" "${service}"
continue
fi
conflicts=$((conflicts + 1))
printf 'PORT CONFLICT: %s needs %s, but it cannot be bound.\n' "${service}" "${expected}" >&2
printf ' Probe error: %s\n' "${probe_error}" >&2
printf ' Inspect: sudo ss -ltnp "sport = :%s"\n' "${host_port}" >&2
printf ' Docker: docker ps --filter publish=%s --format "table {{.ID}}\\t{{.Names}}\\t{{.Ports}}"\n' "${host_port}" >&2
done <<<"${PORT_CHECK_SPEC}"
if ((conflicts > 0)); then
printf 'Port preflight failed with %d conflict(s). No containers were changed.\n' "${conflicts}" >&2
exit 1
fi
echo "Port preflight passed."