Files
Distributed-Prometheus/edge-agent/deploy.sh
Super User 95a09fd9d8 完善中央与边缘部署、远程写入与监控文档
- 增加中央与边缘完整配置和部署脚本
- 引入 VictoriaMetrics 数据源与 remote_write 故障排查说明
- 新增 edge-agent 配置脚本、ONVIF 自建 exporter 与 ping 监控示例

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-25 04:24:40 -05:00

156 lines
4.3 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# 分布式Prometheus边缘代理部署脚本
# 适用于Linux系统 (玩客云等设备)
set -e
echo "=== 分布式Prometheus边缘代理部署脚本 ==="
echo ""
# 检查Docker是否安装
if ! command -v docker &> /dev/null; then
echo "❌ Docker未安装请先安装Docker"
exit 1
fi
# 检查Docker Compose (优先检查V2然后检查V1)
DOCKER_COMPOSE_CMD=""
if docker compose version &> /dev/null; then
DOCKER_COMPOSE_CMD="docker compose"
echo "✅ 检测到 Docker Compose V2"
elif command -v docker-compose &> /dev/null; then
DOCKER_COMPOSE_CMD="docker-compose"
echo "✅ 检测到 Docker Compose V1"
else
echo "❌ Docker Compose未安装请先安装Docker Compose"
exit 1
fi
echo "✅ Docker环境检查通过"
echo ""
# 检查jq是否安装
if ! command -v jq &> /dev/null; then
echo "❌ jq未安装请先安装jq:"
echo " Ubuntu/Debian: sudo apt-get install jq"
echo " CentOS/RHEL: sudo yum install jq"
echo " Alpine: apk add jq"
exit 1
fi
# 检查并生成配置文件
echo "🔄 检查并生成配置文件..."
if [ -f "config/devices.csv" ]; then
echo "📱 从CSV生成ONVIF设备配置..."
cd config
chmod +x *.sh
./update-configs.sh
cd ..
else
echo "⚠️ config/devices.csv 不存在使用默认JSON配置"
fi
if [ ! -f "config/onvif-targets.json" ]; then
echo "❌ 配置文件 config/onvif-targets.json 不存在"
exit 1
fi
if [ ! -f "config/ping-targets.json" ]; then
echo "❌ 配置文件 config/ping-targets.json 不存在"
exit 1
fi
if [ ! -f "prometheus-edge/prometheus.yml" ]; then
echo "❌ 配置文件 prometheus-edge/prometheus.yml 不存在"
exit 1
fi
echo "✅ 配置文件检查通过"
echo ""
# 创建环境变量文件
if [ ! -f ".env" ]; then
if [ -f "env.example" ]; then
cp env.example .env
echo "📝 已创建 .env 文件,请编辑其中的配置"
echo " 特别是 CENTRAL_SERVER_HOST 和 CENTRAL_SERVER_PORT"
echo ""
read -p "按回车键继续,或 Ctrl+C 取消..."
else
echo "❌ env.example 文件不存在"
exit 1
fi
fi
# 从 .env 生成 prometheus.yml使 remote_write 指向中央服务器)
if [ -f ".env" ]; then
set -a
source .env
set +a
CENTRAL_SERVER_HOST=${CENTRAL_SERVER_HOST:-192.168.1.10}
CENTRAL_SERVER_PORT=${CENTRAL_SERVER_PORT:-8428}
if [ -f "prometheus-edge/prometheus.yml.template" ]; then
echo "📝 根据 .env 生成 prometheus.yml (中央: ${CENTRAL_SERVER_HOST}:${CENTRAL_SERVER_PORT})..."
export CENTRAL_SERVER_HOST CENTRAL_SERVER_PORT
envsubst '${CENTRAL_SERVER_HOST} ${CENTRAL_SERVER_PORT}' < prometheus-edge/prometheus.yml.template > prometheus-edge/prometheus.yml
echo "✅ prometheus.yml 已生成"
fi
fi
# 创建数据目录
mkdir -p prometheus-edge/data
echo "✅ 数据目录创建完成"
echo ""
# 停止现有服务
echo "🛑 停止现有服务..."
$DOCKER_COMPOSE_CMD down 2>/dev/null || true
# 拉取最新镜像
echo "📥 拉取Docker镜像..."
if ! $DOCKER_COMPOSE_CMD pull; then
echo ""
echo "⚠️ 镜像拉取失败,尝试继续启动(如果本地已有镜像)..."
echo ""
fi
# 启动服务
echo "🚀 启动服务..."
$DOCKER_COMPOSE_CMD up -d
# 等待服务启动
echo "⏳ 等待服务启动..."
sleep 10
# 检查服务状态
echo ""
echo "📊 服务状态检查:"
$DOCKER_COMPOSE_CMD ps
echo ""
echo "📋 服务日志:"
$DOCKER_COMPOSE_CMD logs --tail=20
echo ""
echo "✅ 部署完成!"
echo ""
echo "🔗 访问地址:"
echo " - Prometheus UI: http://localhost:9092"
echo " - 目标状态: http://localhost:9092/targets"
echo ""
echo "📝 管理命令:"
echo " - 查看日志: $DOCKER_COMPOSE_CMD logs -f"
echo " - 重启服务: $DOCKER_COMPOSE_CMD restart"
echo " - 停止服务: $DOCKER_COMPOSE_CMD down"
echo ""
echo "🔄 配置更新:"
echo " - 编辑CSV: nano config/devices.csv"
echo " - 生成JSON: cd config && ./update-configs.sh"
echo " - 热重载: 等待5分钟自动重载或重启prometheus-edge"
echo ""
echo "⚠️ 请确保:"
echo " 1. 已正确配置 .env 文件中的服务器地址"
echo " 2. 已更新 config/devices.csv 中的设备信息"
echo " 3. 网络连接正常可以访问ONVIF设备"