#!/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文件(跳过注释行和标题行) # 列顺序: type,ip,name,role,parent,uplink_type,network,device_type,model,location,username,password,onvif_port,lat,lon tail -n +2 "$CSV_FILE" | grep -v '^#' | while IFS=',' read -r type ip name role parent uplink_type network device_type model location username password onvif_port lat lon; do # 去除空格 type=$(echo "$type" | xargs) ip=$(echo "$ip" | xargs) # 跳过空行 if [ -z "$type" ]; 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) lat=$(echo "$lat" | xargs) lon=$(echo "$lon" | 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 if [ -n "$lat" ]; then labels="$labels, \"lat\": \"$lat\"" fi if [ -n "$lon" ]; then labels="$labels, \"lon\": \"$lon\"" fi labels="$labels }" echo "{ \"targets\": [\"$ip\"], \"labels\": $labels }" >> "$ONVIF_TEMP" elif [ "$type" = "ping" ]; then # 处理 Ping 目标 device=$(echo "$name" | xargs) # name 作为 device 标签 role=$(echo "$role" | xargs) parent=$(echo "$parent" | xargs) uplink_type=$(echo "$uplink_type" | xargs) network=$(echo "$network" | xargs) labels="{ \"device\": \"$device\"" if [ -n "$uplink_type" ]; then labels="$labels, \"uplink_type\": \"$uplink_type\"" fi if [ -n "$network" ]; then labels="$labels, \"network\": \"$network\"" fi if [ -n "$role" ]; then labels="$labels, \"role\": \"$role\"" fi if [ -n "$parent" ]; then labels="$labels, \"parent\": \"$parent\"" 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 ""