#!/bin/bash
# gpu_temp.sh
[[ $SN ]] || SN=$(dmidecode -s system-serial-number |grep -v ^#  | sed s/[[:space:]]//g)
[[ $HOSTNAME ]] || HOSTNAME=$(hostname)
JP='192.168.168.106'
IPADDR=`ip -o r get $JP|sed -n 's/.*src \([0-9.]*\) .*/\1/p'`
[[ $IPADDR ]] || IPADDR=$(hostname -I)
UPTIME=$(uptime -s | awk -F' ' '{cmd="date +%s -d \""$1" "$2" "$3" "$4" "$5"\""; cmd | getline start; close(cmd); print int((systime()-start)/60)}')
LOCALTIME=$(TZ=CST-8 date +%F_%T_%Z)
mapfile -t power < <(timeout 10 nvidia-smi --query-gpu=index,temperature.gpu,power.draw,utilization.gpu,utilization.memory,gpu_name,vbios_version,driver_version --format=csv,noheader )
mapfile -t process_name < <(timeout 10 nvidia-smi --query-compute-apps=gpu_bus_id,gpu_serial,process_name --format=csv,noheader )

for i in "${!power[@]}"; do echo "$HOSTNAME, $UPTIME, $LOCALTIME, $IPADDR, $SN-${power[$i]}, ${process_name[$i]}" ;done



