# 获取主机 SN 号
SN=$(dmidecode -s system-serial-number 2>/dev/null || cat /sys/class/dmi/id/product_serial 2>/dev/null || echo "Unknown_SN")

# 循环执行 R1 到 R4 级别的诊断
for r in 1 2 3 4; do
    cmd="dcgmi diag -r $r"
    outfile="${SN}-dcgmi-diag-r${r}.txt"
    
    # 捕获开始时间
    START_TIME=$(date "+%Y-%m-%d %H:%M:%S")
    
    # 打印页眉：包含 SN、命令、开始时间
    echo "==================================================" | tee $outfile
    echo "Host SN          : $SN" | tee -a $outfile
    echo "Command Executed : $cmd" | tee -a $outfile
    echo "Start Time       : $START_TIME" | tee -a $outfile
    echo "==================================================" | tee -a $outfile
    
    # 执行具体的诊断命令，并追加写入文件
    $cmd | tee -a $outfile
    
    # 捕获结束时间
    END_TIME=$(date "+%Y-%m-%d %H:%M:%S")
    
    # 打印页脚：包含结束时间
    echo "==================================================" | tee -a $outfile
    echo "End Time         : $END_TIME" | tee -a $outfile
    echo "==================================================" | tee -a $outfile
    
    # 停顿 2 秒
    sleep 2
done
