Files
Distributed-Prometheus/edge-agent/config/csv-to-targets.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

130 lines
3.4 KiB
Bash
Executable File
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
# 统一目标配置转换脚本
# 从 targets.csv 生成 onvif-targets.json 和 ping-targets.json
# 使用方法: ./csv-to-targets.sh targets.csv
set -e
CSV_FILE=${1:-"targets.csv"}
# 检查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 " Fedora: sudo dnf install jq"
exit 1
fi
# 检查CSV文件是否存在
if [ ! -f "$CSV_FILE" ]; then
echo "❌ CSV文件 $CSV_FILE 不存在"
exit 1
fi
echo "🔄 正在从 $CSV_FILE 生成配置文件..."
echo ""
# 临时文件
ONVIF_TEMP=$(mktemp)
PING_TEMP=$(mktemp)
# 处理CSV文件跳过注释行和标题行
tail -n +2 "$CSV_FILE" | grep -v '^#' | while IFS=',' read -r type ip device group network device_type model location username password onvif_port; do
# 去除空格
type=$(echo "$type" | xargs)
ip=$(echo "$ip" | xargs)
# 跳过空行
if [ -z "$type" ] || [ -z "$ip" ]; then
continue
fi
if [ "$type" = "onvif" ]; then
# 处理 ONVIF 设备
device_type=$(echo "$device_type" | xargs)
model=$(echo "$model" | xargs)
location=$(echo "$location" | xargs)
username=$(echo "$username" | xargs)
password=$(echo "$password" | xargs)
onvif_port=$(echo "$onvif_port" | xargs)
labels="{
\"device_type\": \"$device_type\",
\"model\": \"$model\",
\"location\": \"$location\",
\"username\": \"$username\",
\"password\": \"$password\""
if [ "$onvif_port" != "80" ] && [ -n "$onvif_port" ]; then
labels="$labels,
\"onvif_port\": \"$onvif_port\""
fi
labels="$labels
}"
echo "{
\"targets\": [\"$ip\"],
\"labels\": $labels
}" >> "$ONVIF_TEMP"
elif [ "$type" = "ping" ]; then
# 处理 Ping 目标
device=$(echo "$device" | xargs)
group=$(echo "$group" | xargs)
network=$(echo "$network" | xargs)
labels="{
\"device\": \"$device\""
if [ -n "$group" ]; then
labels="$labels,
\"group\": \"$group\""
fi
if [ -n "$network" ]; then
labels="$labels,
\"network\": \"$network\""
fi
labels="$labels
}"
echo "{
\"targets\": [\"$ip\"],
\"labels\": $labels
}" >> "$PING_TEMP"
fi
done
# 生成 JSON 文件
if [ -s "$ONVIF_TEMP" ]; then
jq -s '.' "$ONVIF_TEMP" > onvif-targets.json
ONVIF_COUNT=$(jq length onvif-targets.json)
echo "✅ 生成 ONVIF 设备配置: $ONVIF_COUNT 个设备"
else
echo "[]" > onvif-targets.json
echo "⚠️ 未找到 ONVIF 设备,生成空配置"
fi
if [ -s "$PING_TEMP" ]; then
jq -s '.' "$PING_TEMP" > ping-targets.json
PING_COUNT=$(jq length ping-targets.json)
echo "✅ 生成 Ping 目标配置: $PING_COUNT 个目标"
else
echo "[]" > ping-targets.json
echo "⚠️ 未找到 Ping 目标,生成空配置"
fi
# 清理临时文件
rm -f "$ONVIF_TEMP" "$PING_TEMP"
echo ""
echo "✅ 配置文件生成完成!"
echo "📁 生成的文件:"
echo " - onvif-targets.json"
echo " - ping-targets.json"
echo ""