Files
homelabscripts/proxmox/fan_control/fan_control.sh
2024-05-01 14:49:42 -04:00

92 lines
2.9 KiB
Bash

#!/bin/bash
check_start_time() {
local start_time="$1"
local current_time=$(date +%s)
local time_diff=$((current_time - start_time))
if [ $time_diff -ge 60 ]; then
echo 0
else
echo 1
fi
}
check_temperature() {
temp_lines=$(ipmitool -I lanplus -H "$HOST" -U "$USER" -P "$PASS" sdr type temperature 2>/dev/null | grep -e degrees | awk '{print $(NF-2)}')
temp_arr=""
readarray -t temp_arr <<< "$temp_lines"
sorted_temps=($(printf "%s\n" "${temp_arr[@]}" | sort -nr))
max_temp=${sorted_temps[0]}
if [ "$max_temp" -le 60 ] && [ "$CURRENT_PROFILE" != "MANUAL" ]; then
# enable manual fan control
ipmitool -I lanplus -H "$HOST" -U "$USER" -P "$PASS" raw 0x30 0x30 0x01 0x00 &> /dev/null
# set initial fan to 20%
ipmitool -I lanplus -H "$HOST" -U "$USER" -P "$PASS" raw 0x30 0x30 0x02 0xff 0x14 &> /dev/null
LOW_TO_HIGH=false
CURRENT_PROFILE="MANUAL"
elif [ "$max_temp" -le 65 ] && [ "$CURRENT_PROFILE" != "MANUAL2" ]; then
# enable manual fan control
ipmitool -I lanplus -H "$HOST" -U "$USER" -P "$PASS" raw 0x30 0x30 0x01 0x00 &> /dev/null
# set initial fan to 30%
ipmitool -I lanplus -H "$HOST" -U "$USER" -P "$PASS" raw 0x30 0x30 0x02 0xff 0x1e &> /dev/null
# set low_to_high used to determine wait time
if [[ "$CURRENT_PROFILE" == "MANUAL"]]; then
LOW_TO_HIGH=true
else
LOW_TO_HIGH=false
fi
CURRENT_PROFILE="MANUAL2"
elif [ "$max_temp" -gt 65 ] && [ "$CURRENT_PROFILE" != "AUTO" ]; then
# enable Dell fan control
ipmitool -I lanplus -H "$HOST" -U "$USER" -P "$PASS" raw 0x30 0x30 0x01 0x01 &> /dev/null
# set low_to_high used to determine wait time
if [[ "$CURRENT_PROFILE" == "MANUAL2"]]; then
LOW_TO_HIGH=true
else
LOW_TO_HIGH=false
fi
CURRENT_PROFILE="AUTO"
fi
}
disable_third_party_mode (){
# disable 3rd party pcie fan mode
output=$(sshpass -p "$PASS" ssh -o StrictHostKeyChecking=no "$USER@$HOST" "racadm get system.pcieslotlfm" 2>/dev/null)
pcie_ports_count=$(echo "$output" | grep -c 'System.pcieslotlfm')
# Iterate through each PCIe port and disable third party mode
for ((i=1; i<=pcie_ports_count; i++)); do
sshpass -p "$PASS" ssh -o StrictHostKeyChecking=no "$USER@$HOST" "racadm set system.pcieslotlfm.$i.lfmmode disabled" &> /dev/null
done
}
HOST=""
USER=""
PASS=""
VMID=""
CURRENT_PROFILE=""
LOW_TO_HIGH=false
start_time=$(ps -o lstart= -p $(cat /var/run/qemu-server/$VMID.pid))
start_time=$(date -d "$start_time" +%s)
while [[ $(check_start_time "$start_time") -eq 1 ]]
do
sleep 5
done
disable_third_party_mode
while true; do
check_temperature
if [[ "$CURRENT_PROFILE" == "AUTO" || "$LOW_TO_HIGH" == true]]; then
sleep 180
else
sleep 60
fi
done