rmm-scripts/nvgputemp.sh

59 lines
1.6 KiB
Bash

#!/bin/bash
#Zebra - 3/18/2024
#Uses nvidia-smi to query the GPU Temperature of one or more installed NVIDIA GPUs on linux.
# Check if nvidia-smi command is available
if ! command -v nvidia-smi &> /dev/null; then
echo "NVIDIA driver not found"
exit 0
fi
# Run nvidia-smi command and store the output
output=$(nvidia-smi)
# Extract GPU temperatures using grep and awk
gpu_temps=$(echo "$output" | grep -oP '(\d+)C' | awk 'NR%3==1')
# Check if no GPU temperatures were found
if [ -z "$gpu_temps" ]; then
echo "No GPUs found"
exit 0
fi
# If multiple GPUs exist, we concatenate GPU indices and load into a single line
gpu_info=""
gpu_index=0
for temp in $gpu_temps; do
if [ $gpu_index == 0 ]; then
gpu_info+="$temp"
((gpu_index++))
else
gpu_info+=" | gpu$gpu_index: $temp"
((gpu_index++))
fi
done
# Remove trailing "| " from the concatenated string
gpu_info="${gpu_info% | }"
# Output the concatenated GPU info
echo "$gpu_info"
#Build CURL variables - Specific to TacticalRMM
AGENTID=$(cat /etc/tacticalagent | jq -r .agentid)
URL="https://your.RMM.api.here/agents
DATA=$(echo {}|jq -n --arg TEMP "$gpu_temps" '{"custom_fields":[{"field":18,"string_value":$TEMP}]}')
# Make sure to declare an API key in a secure fashion.
curl -s -X PUT -H "Content-Type: application/json" -H "X-API-KEY:$API" $URL --data "$DATA" >NUL
# Check GPU temperature and exit with appropriate exit code
if echo "$gpu_temps" | grep -q -E '([8-9][5-9]|[9][0-9])'; then
exit 3
elif echo "$gpu_temps" | grep -q -E '[7-9][5-9]'; then
exit 2
else
exit 0
fi