Last active
April 1, 2024 00:31
-
-
Save changbowen/5d0b8b3ca0a960d6b952f7075273aa8e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
temp_threshold_low=40 | |
temp_threshold_mid=50 | |
temp_threshold_high=60 | |
temp_threshold_max=70 | |
power_threshold_low=180 | |
power_threshold_mid=300 | |
power_threshold_high=500 | |
power_threshold_max=800 | |
# process args | |
while getopts "v" opt; do | |
case $opt in | |
v) | |
opt_verbose=true | |
;; | |
esac | |
done | |
log() { | |
if [ $opt_verbose ]; then | |
if [ "$1" == "-n" ]; then | |
echo -n "$2" | |
else | |
echo "$1" | |
fi | |
fi | |
} | |
dec_to_hex() { | |
if [ -n "$1" ] && [ "$1" -eq "$1" ] 2>/dev/null; then | |
echo $(printf '0x%02x' $1) | |
fi | |
} | |
manual_fan_control() { | |
if [ -z "$1" ]; then | |
echo "Pass 1 or 0 to enable or disable manual control" | |
else | |
if [ "$1" -eq "1" ]; then | |
log "Enabling manual control..." | |
ipmitool raw 0x30 0x30 0x01 0x00 > /dev/null | |
else | |
log "Disabling manual control..." | |
ipmitool raw 0x30 0x30 0x01 0x01 > /dev/null | |
fi | |
fi | |
} | |
# Examples | |
# Set all fans to 20% speed: | |
# fan_speed 20 | |
# | |
# Set CPU fans to 100% speed: | |
# fan_speed 100 cpu | |
# | |
# Set PCI fans to lowst speed: | |
# fan_speed 0 pci | |
fan_speed() { | |
local hex_speed=$(dec_to_hex $1) | |
if [ -z "$hex_speed" ]; then | |
ipmitool sdr type Fan | grep RPM | |
echo "Pass percentage number to set fan speed." | |
return 1 | |
fi | |
if [ -z "$2" ]; then | |
log "Setting all fans speed to $1% ($hex_speed)..." | |
ipmitool raw 0x30 0x30 0x02 0xff $hex_speed > /dev/null | |
elif [ "$2" == "cpu" ]; then | |
log "Setting CPU fans speed to $1% ($hex_speed)..." | |
ipmitool raw 0x30 0x30 0x02 0x00 $hex_speed > /dev/null # fan #1 | |
ipmitool raw 0x30 0x30 0x02 0x01 $hex_speed > /dev/null # fan #2 | |
ipmitool raw 0x30 0x30 0x02 0x03 $hex_speed > /dev/null # fan #4 | |
ipmitool raw 0x30 0x30 0x02 0x04 $hex_speed > /dev/null # fan #5 | |
elif [ "$2" == "pci" ]; then | |
log "Setting PCI fans speed to $1% ($hex_speed)..." | |
ipmitool raw 0x30 0x30 0x02 0x02 $hex_speed > /dev/null # fan #3 | |
ipmitool raw 0x30 0x30 0x02 0x05 $hex_speed > /dev/null # fan #6 | |
fi | |
} | |
get_temps_cpu() { | |
local cpu_temps=$(ipmitool sdr type temperature | grep "3\." | grep -Po '\d+(?= degrees C)') | |
cpu1_temp=$(echo $cpu_temps | awk '{print $1;}') | |
cpu2_temp=$(echo $cpu_temps | awk '{print $2;}') | |
cpu_temp=$(( cpu1_temp > cpu2_temp ? cpu1_temp : cpu2_temp )) | |
} | |
get_power_draw() { | |
power_draw=$(ipmitool sdr type current | grep "7\." | grep -Po '\d+(?= Watts)') | |
} | |
# 1st param is CPU temp, 2nd is power draw. | |
update_fan_speed() { | |
if [ -n "$1" ]; then | |
if ([ "$1" -lt "$temp_threshold_low" ]); then | |
fan_speed 10 cpu | |
elif ([ "$1" -ge "$temp_threshold_low" ] && [ "$1" -lt "$temp_threshold_mid" ]); then | |
fan_speed 20 cpu | |
elif ([ "$1" -ge "$temp_threshold_mid" ] && [ "$1" -lt "$temp_threshold_high" ]); then | |
fan_speed 50 cpu | |
elif ([ "$1" -ge "$temp_threshold_high" ] && [ "$1" -lt "$temp_threshold_max" ]); then | |
fan_speed 80 cpu | |
else | |
fan_speed 100 cpu | |
fi | |
fi | |
if [ -n "$2" ]; then | |
if ([ "$2" -lt "$power_threshold_low" ]); then | |
fan_speed 10 pci | |
elif ([ "$2" -ge "$power_threshold_low" ] && [ "$2" -lt "$power_threshold_mid" ]); then | |
fan_speed 20 pci | |
elif ([ "$2" -ge "$power_threshold_mid" ] && [ "$2" -lt "$power_threshold_high" ]); then | |
fan_speed 40 pci | |
elif ([ "$2" -ge "$power_threshold_high" ] && [ "$2" -lt "$power_threshold_max" ]); then | |
fan_speed 60 pci | |
else | |
fan_speed 100 pci | |
fi | |
else | |
echo "Pass in CPU and power draw as parameters." | |
fi | |
} | |
# main procedures | |
echo "Starting T620 fan control script..." | |
manual_fan_control 1 | |
fan_speed 50 | |
# control fan speeds based on temperatures | |
while true; do | |
sleep 10 | |
get_temps_cpu | |
get_power_draw | |
log "CPU1: $cpu1_temp °C; CPU2: $cpu2_temp °C; Power: $power_draw" | |
update_fan_speed $cpu_temp $power_draw | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment