#!/bin/bash # ============================================================ # МОНИТОРИНГ: метрики медиасервисов (exportarr) # ============================================================ # Раньше Prometheus видел только сам себя и один узел. Теперь — # очередь, библиотеки, диски и ошибки Sonarr/Radarr/Prowlarr/Bazarr. # ============================================================ set -uo pipefail CT=134 deploy() { local name=$1 app=$2 port=$3 url=$4 key=$5 pct exec $CT -- docker rm -f "$name" >/dev/null 2>&1 pct exec $CT -- docker run -d --name "$name" --restart unless-stopped \ -p "$port:$port" \ -e PORT="$port" -e URL="$url" -e API_KEY="$key" -e LOG_LEVEL=info \ ghcr.io/onedr0p/exportarr:latest "$app" >/dev/null 2>&1 sleep 6 local code code=$(curl -s -o /tmp/m.txt -w '%{http_code}' --max-time 20 "http://192.168.1.134:$port/metrics" 2>/dev/null) printf ' %-18s %-12s порт %-5s метрик: %s\n' "$name" "$app" "$port" "$(grep -c '^[a-z]' /tmp/m.txt 2>/dev/null)" } echo "=== 1. РАЗВЁРТЫВАЮ ЭКСПОРТЁРЫ ===" deploy exportarr-sonarr sonarr 9707 http://192.168.1.117:8989 "$(cat /root/.sonarr-key)" deploy exportarr-radarr radarr 9708 http://192.168.1.118:7878 "$(cat /root/.radarr-key)" deploy exportarr-prowlarr prowlarr 9709 http://192.168.1.119:9696 "$(cat /root/.prowlarr-key)" BAZKEY=$(pct exec 120 -- bash -c "grep -oE '[^<]+' /opt/bazarr/config/config/config.yaml 2>/dev/null | head -1" 2>/dev/null | tr -d '\r') if [ -z "$BAZKEY" ]; then BAZKEY=$(pct exec 120 -- bash -c "grep -rhoE 'apikey: .*' /opt/bazarr/config/config/config.yaml 2>/dev/null | head -1 | cut -d' ' -f2" 2>/dev/null | tr -d '\r') fi echo " ключ Bazarr: ${BAZKEY:0:8}…" deploy exportarr-bazarr bazarr 9710 http://192.168.1.120:6767 "$BAZKEY" echo echo "=== 2. ДОБАВЛЯЮ ЦЕЛИ В PROMETHEUS ===" pct exec $CT -- bash -c "cp /opt/prometheus/prometheus.yml /opt/prometheus/prometheus.yml.bak-\$(date +%H%M%S)" pct pull $CT /opt/prometheus/prometheus.yml /tmp/prom.yml 2>/dev/null python3 - <<'PY' import re p = "/tmp/prom.yml" t = open(p, encoding="utf-8").read() if "exportarr" not in t: jobs = """ - job_name: exportarr scrape_interval: 60s static_configs: - targets: - 127.0.0.1:9707 labels: {app: sonarr} - targets: - 127.0.0.1:9708 labels: {app: radarr} - targets: - 127.0.0.1:9709 labels: {app: prowlarr} - targets: - 127.0.0.1:9710 labels: {app: bazarr} """ # вставляю перед rule_files или scrape_configs концом if "rule_files:" in t: t = t.replace("rule_files:", jobs.strip("\n") + "\n\nrule_files:", 1) else: t = t.rstrip() + "\n" + jobs print(" цели добавлены") else: print(" цели уже есть") open(p, "w", encoding="utf-8").write(t) PY pct push $CT /tmp/prom.yml /opt/prometheus/prometheus.yml --perms 644 2>/dev/null pct exec $CT -- bash -c "docker exec prometheus promtool check config /etc/prometheus/prometheus.yml 2>&1 | tail -3" | sed 's/^/ /'