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

62 lines
1.5 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
# CSV转Ping JSON脚本 - 将Ping目标CSV表格转换为Prometheus监控JSON配置
# 使用方法: ./csv-to-ping-json.sh ping-targets.csv > ping-targets.json
set -e
CSV_FILE=${1:-"ping-targets.csv"}
OUTPUT_FILE=${2:-"ping-targets.json"}
# 检查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
# 检查CSV文件是否存在
if [ ! -f "$CSV_FILE" ]; then
echo "❌ CSV文件 $CSV_FILE 不存在"
exit 1
fi
echo "🔄 正在转换 $CSV_FILE$OUTPUT_FILE..."
# 使用jq将CSV转换为JSON
tail -n +2 "$CSV_FILE" | while IFS=',' read -r ip device group network; do
# 构建labels对象
labels="{
\"device\": \"$device\""
# 添加可选的group标签
if [ -n "$group" ]; then
labels="$labels,
\"group\": \"$group\""
fi
# 添加可选的network标签
if [ -n "$network" ]; then
labels="$labels,
\"network\": \"$network\""
fi
labels="$labels
}"
# 输出JSON对象
echo "{
\"targets\": [\"$ip\"],
\"labels\": $labels
}"
done | jq -s '.' > "$OUTPUT_FILE"
echo "✅ 转换完成!"
echo "📊 生成了 $(jq length "$OUTPUT_FILE") 个Ping目标配置"
echo "📁 输出文件: $OUTPUT_FILE"
echo ""
echo "🔍 预览生成的JSON:"
jq . "$OUTPUT_FILE"