#!/bin/sh

info_fn=/proc/driver/nvidia/gpus/0/information
if test ! -e $info_fn ; then
  # No NVidia card or not using NVidia driver
  exit 0
fi

# Get GPU model
gpu_model=`grep Model: $info_fn | awk -F: '{print $2}' | sed -e 's/^[ \t]*//g'`
if test "$gpu_model" = "GeForce 210" ; then
  # GT 210 maximum GPU temperature is 105 C
  max_temp=105
elif test "$gpu_model" = "GeForce 220" ; then
  # GT 220 maximum GPU temperature is 105 C
  max_temp=105
elif test "$gpu_model" = "GeForce 240" ; then
  # GT 240 maximum GPU temperature is 105 C
  max_temp=105
elif test "$gpu_model" = "GeForce 430" ; then
  # GT 430 maximum GPU temperature is  98 C
  max_temp=98
elif test "$gpu_model" = "GeForce GTS 450" ; then
  # GTS 450 maximum GPU temperature is  100 C
  max_temp=100
elif test "$gpu_model" = "GeForce GT 520" ; then
  # GT 520 maximum GPU temperature is  102 C
  max_temp=102
else
  # Assume 100 is the maximum
  max_temp=100
fi

crit_temp=`expr $max_temp - 5`
warn_temp=`expr $crit_temp - 10`

if test -x /usr/bin/nvidia-smi ; then
  temp=`nvidia-smi -q -g 0 -d TEMPERATURE | grep -Ei 'Gpu[[:space:]]+:[[:space:]]+[[:digit:]]+ C'`
  gpu_temp=`echo $temp | sed -e 's/[^0-9]*//g'`
  #echo "GPUTEMP:$gpu_temp" > /tmp/gputemp.txt
  if test -n "$gpu_temp" ; then
    # Have some type of number
    if test $gpu_temp -lt $warn_temp ; then
      gpu_color=green
    elif test $gpu_temp -lt $crit_temp ; then
      gpu_color=yellow
    else
      gpu_color=red
    fi
    #echo gputemp $gpu_temp $gpu_color 
    #echo $XYMON $XYMSRV "status $MACHINE.gputemp $gpu_color `date`
    #`cat /tmp/gputemp.txt`
    #"
    $XYMON $XYMSRV "status $MACHINE.gputemp $gpu_color `date`
    GPUTEMP:$gpu_temp
    "
  fi
fi

exit 0
