From 2c5a1849a3b9097f13e748bb8c5c7310665e5e65 Mon Sep 17 00:00:00 2001 From: zebra Date: Tue, 19 Mar 2024 12:21:25 -0700 Subject: [PATCH] Added new scripts --- activitystats.ps1 | 0 nvgpuload.sh | 70 +++++++++++++++++++++++++++++++++++++++++++++++ nvgputemp.sh | 59 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+) mode change 100755 => 100644 activitystats.ps1 create mode 100644 nvgpuload.sh create mode 100644 nvgputemp.sh diff --git a/activitystats.ps1 b/activitystats.ps1 old mode 100755 new mode 100644 diff --git a/nvgpuload.sh b/nvgpuload.sh new file mode 100644 index 0000000..5a23877 --- /dev/null +++ b/nvgpuload.sh @@ -0,0 +1,70 @@ +#!/bin/bash +#Zebra - 3/18/2024 +#Uses nvidia-smi to query the GPU load 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 GPU driver not found" + exit 0 +fi + +# Run nvidia-smi command and store the output +output=$(nvidia-smi) + +# Check if there is a fan installed. (it messes up the output) Unless "N/A" exits on line 10 of the output, +# Then skip the first occurrence of numbers with percentage values. (AKA The fan speed percentage) +if echo "$output" | awk 'NR==10' | grep -q "N/A"; then + # Extract GPU loads using grep and awk without skipping + gpu_loads=$(echo "$output" | grep -oP '\d+%' | awk '{print substr($0, 1, length($0)-1)}') +else + # Extract GPU loads using grep and awk, skipping the first occurrence + gpu_loads=$(echo "$output" | grep -oP '\d+%' | awk '{print substr($0, 1, length($0)-1)}' | tail -n +2) +fi + +# If the command above returns with no results, a GPU must not be installed or detected. +if [ -z "$gpu_loads" ]; then + echo "No GPUs found" + exit 1 +fi + +# If multiple GPUs exist, we concatenate GPU indices and load into a single line +gpu_info="" +gpu_index=0 +for load in $gpu_loads; do + if [ $gpu_index == 0 ]; then + gpu_info+="$load" + ((gpu_index++)) + else + gpu_info+="gpu$gpu_index: $load | " + ((gpu_index++)) + fi +done + +# Remove trailing "| " from the concatenated string +gpu_info="${gpu_info% | }" + +#Re-add the Percent sign +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 LOAD "$gpu_info%" '{"custom_fields":[{"field":20,"string_value":$LOAD}]}') + + +# 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" >/dev/null + + +# Check GPU load and exit with appropriate exit code +if echo "$gpu_loads" | grep -q -E '100%'; then + exit 3 +elif echo "$gpu_loads" | grep -q -E '90%'; then + exit 2 +else + exit 0 +fi \ No newline at end of file diff --git a/nvgputemp.sh b/nvgputemp.sh new file mode 100644 index 0000000..128f809 --- /dev/null +++ b/nvgputemp.sh @@ -0,0 +1,59 @@ +#!/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 LOAD "$gpu_info%" '{"custom_fields":[{"field":20,"string_value":$LOAD}]}') + +# 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 \ No newline at end of file