|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Simple script implementing a temperature dependent fan speed control |
| 4 | +# Supported Linux kernel versions: 2.6.5 and later |
| 5 | +# |
| 6 | +# Version 0.70 |
| 7 | +# |
| 8 | +# Usage: fancontrol [CONFIGFILE] |
| 9 | +# |
| 10 | +# Dependencies: |
| 11 | +# bash, egrep, sed, cut, sleep, readlink, lm_sensors :) |
| 12 | +# |
| 13 | +# Please send any questions, comments or success stories to |
| 14 | +# marius.reiner@hdev.de |
| 15 | +# Thanks! |
| 16 | +# |
| 17 | +# For configuration instructions and warnings please see fancontrol.txt, which |
| 18 | +# can be found in the doc/ directory or at the website mentioned above. |
| 19 | +# |
| 20 | +# |
| 21 | +# Copyright 2003 Marius Reiner <marius.reiner@hdev.de> |
| 22 | +# Copyright (C) 2007-2009 Jean Delvare <khali@linux-fr.org> |
| 23 | +# |
| 24 | +# This program is free software; you can redistribute it and/or modify |
| 25 | +# it under the terms of the GNU General Public License as published by |
| 26 | +# the Free Software Foundation; either version 2 of the License, or |
| 27 | +# (at your option) any later version. |
| 28 | +# |
| 29 | +# This program is distributed in the hope that it will be useful, |
| 30 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 31 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 32 | +# GNU General Public License for more details. |
| 33 | +# |
| 34 | +# You should have received a copy of the GNU General Public License |
| 35 | +# along with this program; if not, write to the Free Software |
| 36 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 37 | +# MA 02110-1301 USA. |
| 38 | +# |
| 39 | +# |
| 40 | + |
| 41 | +PIDFILE="/var/run/fancontrol.pid" |
| 42 | + |
| 43 | +#DEBUG=1 |
| 44 | +MAX=255 |
| 45 | + |
| 46 | +function LoadConfig |
| 47 | +{ |
| 48 | + local fcvcount fcv |
| 49 | + |
| 50 | + echo "Loading configuration from $1 ..." |
| 51 | + if [ ! -r "$1" ] |
| 52 | + then |
| 53 | + echo "Error: Can't read configuration file" >&2 |
| 54 | + exit 1 |
| 55 | + fi |
| 56 | + |
| 57 | + # grep configuration from file |
| 58 | + INTERVAL=`egrep '^INTERVAL=.*$' $1 | sed -e 's/INTERVAL=//g'` |
| 59 | + DEVPATH=`egrep '^DEVPATH=.*$' $1 | sed -e 's/DEVPATH= *//g'` |
| 60 | + DEVNAME=`egrep '^DEVNAME=.*$' $1 | sed -e 's/DEVNAME= *//g'` |
| 61 | + FCTEMPS=`egrep '^FCTEMPS=.*$' $1 | sed -e 's/FCTEMPS=//g'` |
| 62 | + MINTEMP=`egrep '^MINTEMP=.*$' $1 | sed -e 's/MINTEMP=//g'` |
| 63 | + MAXTEMP=`egrep '^MAXTEMP=.*$' $1 | sed -e 's/MAXTEMP=//g'` |
| 64 | + MINSTART=`egrep '^MINSTART=.*$' $1 | sed -e 's/MINSTART=//g'` |
| 65 | + MINSTOP=`egrep '^MINSTOP=.*$' $1 | sed -e 's/MINSTOP=//g'` |
| 66 | + HWMON=$( echo "$DEVPATH" | sed 's/=.*$//g') |
| 67 | + FCDEVPATH=$( echo "$DEVPATH" | sed 's/^.*=//g') |
| 68 | + FCMINTEMP=$MINTEMP |
| 69 | + FCMAXTEMP=$MAXTEMP |
| 70 | + FCMINSTART=$MINSTART |
| 71 | + FCMINSTOP=$MINSTOP |
| 72 | + AFCTEMP_1_LOWER=(00 39 36 41 46 55) |
| 73 | + AFCTEMP_1_UPPER=(39 39 44 49 54 150) |
| 74 | + AFCTEMP_2_LOWER=(00 61 65 69 73 82) |
| 75 | + AFCTEMP_2_UPPER=(63 67 71 75 79 150) |
| 76 | + AFCTEMP_3_LOWER=(00 51 55 59 63 71) |
| 77 | + AFCTEMP_3_UPPER=(53 57 61 65 69 150) |
| 78 | + |
| 79 | + |
| 80 | + FCFANS=`egrep '^FCFANS=.*$' $1 | sed -e 's/FCFANS=//g'` |
| 81 | + |
| 82 | + FCTARGETS=`egrep '^FCTARGETS=.*$' $1 | sed -e 's/FCTARGETS=//g'` |
| 83 | + |
| 84 | + # Check whether all mandatory settings are set |
| 85 | + if [[ -z ${INTERVAL} || -z ${FCTEMPS} || -z ${MINTEMP} || -z ${MAXTEMP} || -z ${MINSTART} || -z ${MINSTOP} ]] |
| 86 | + then |
| 87 | + echo "Some mandatory settings missing, please check your config file!" >&2 |
| 88 | + exit 1 |
| 89 | + fi |
| 90 | + if [ "$INTERVAL" -le 0 ] |
| 91 | + then |
| 92 | + echo "Error in configuration file:" >&2 |
| 93 | + echo "INTERVAL must be at least 1" >&2 |
| 94 | + exit 1 |
| 95 | + fi |
| 96 | + |
| 97 | + # write settings to arrays for easier use and print them |
| 98 | + echo |
| 99 | + echo "Common settings:" |
| 100 | + |
| 101 | + temp_string=$FCTEMPS |
| 102 | + |
| 103 | + let fcvcount=0 |
| 104 | + for fcv in $FCTEMPS |
| 105 | + do |
| 106 | + fcvcount=$((fcvcount+1)) |
| 107 | + AFCTEMP[$fcvcount]=$( echo "$temp_string" | cut -d" " -f $fcvcount ) |
| 108 | + AFCTEMP[$fcvcount]=$( echo "${AFCTEMP[$fcvcount]}" | sed 's/hwmon1/\/sys\/bus\/i2c\/devices/g' ) |
| 109 | + AFCTEMP_PATH[$fcvcount]=$( echo "${AFCTEMP[$fcvcount]}" | sed 's/hwmon1/\/sys\/bus\/i2c\/devices/g' ) |
| 110 | + AFCTEMP[$fcvcount]=$( cat ${AFCTEMP[$fcvcount]} ) |
| 111 | + AFCTEMP[$fcvcount]=$(( AFCTEMP[$fcvcount]/1000 )) |
| 112 | + done |
| 113 | + |
| 114 | + fan_string=$FCFANS |
| 115 | + fcvcount=0 |
| 116 | + for fcv in $FCFANS |
| 117 | + do |
| 118 | + fcvcount=$((fcvcount+1)) |
| 119 | + AFCFAN[$fcvcount]=$( echo "$fan_string" | cut -d" " -f $fcvcount ) |
| 120 | + AFCFAN_PATH[$fcvcount]=$( echo "${AFCFAN[$fcvcount]}" | sed 's/hwmon1/\/sys\/bus\/i2c\/devices/g' ) |
| 121 | + AFCFAN[$fcvcount]=$( cat ${AFCFAN_PATH[$fcvcount]} ) |
| 122 | + done |
| 123 | + |
| 124 | + target_string=$FCTARGETS |
| 125 | + fcvcount=0 |
| 126 | + for fcv in $FCTARGETS |
| 127 | + do |
| 128 | + fcvcount=$((fcvcount+1)) |
| 129 | + AFCTARGET[$fcvcount]=$( echo "$target_string" | cut -d" " -f $fcvcount ) |
| 130 | + AFCFAN_TARGET[$fcvcount]=$( echo "${AFCTARGET[$fcvcount]}" | sed 's/hwmon1/\/sys\/bus\/i2c\/devices/g' ) |
| 131 | + done |
| 132 | + |
| 133 | +} |
| 134 | + |
| 135 | +# Check that all referenced sysfs files exist |
| 136 | +function CheckFiles |
| 137 | +{ |
| 138 | + local outdated=0 fcvcount tsen fan |
| 139 | + if [ $outdated -eq 1 ] |
| 140 | + then |
| 141 | + echo >&2 |
| 142 | + echo "At least one referenced file is missing. Either some required kernel" >&2 |
| 143 | + echo "modules haven't been loaded, or your configuration file is outdated." >&2 |
| 144 | + echo "In the latter case, you should run pwmconfig again." >&2 |
| 145 | + fi |
| 146 | + return $outdated |
| 147 | +} |
| 148 | + |
| 149 | +LoadConfig $1 |
| 150 | + |
| 151 | +# Detect path to sensors |
| 152 | +if [ ! -d $DIR ] |
| 153 | +then |
| 154 | + echo $0: 'No sensors found! (did you load the necessary modules?)' >&2 |
| 155 | + exit 1 |
| 156 | +fi |
| 157 | +cd $DIR |
| 158 | + |
| 159 | +# Check for configuration change |
| 160 | +if [ "$DIR" != "/" ] && [ -z "$DEVPATH" -o -z "$DEVNAME" ] |
| 161 | +then |
| 162 | + echo "Configuration is too old, please run pwmconfig again" >&2 |
| 163 | + exit 1 |
| 164 | +fi |
| 165 | +if [ "$DIR" = "/" -a -n "$DEVPATH" ] |
| 166 | +then |
| 167 | + echo "Unneeded DEVPATH with absolute device paths" >&2 |
| 168 | + exit 1 |
| 169 | +fi |
| 170 | +CheckFiles || exit 1 |
| 171 | + |
| 172 | +if [ -f "$PIDFILE" ] |
| 173 | +then |
| 174 | + echo "File $PIDFILE exists, is fancontrol already running?" >&2 |
| 175 | + exit 1 |
| 176 | +fi |
| 177 | +echo $$ > "$PIDFILE" |
| 178 | + |
| 179 | +# main function |
| 180 | +function UpdateThermalSensors |
| 181 | +{ |
| 182 | + fcvcount=0 |
| 183 | + for fcv in $FCTEMPS |
| 184 | + do |
| 185 | + fcvcount=$((fcvcount+1)) |
| 186 | + AFCTEMP[$fcvcount]=$( cat ${AFCTEMP_PATH[$fcvcount]} ) |
| 187 | + AFCTEMP[$fcvcount]=$(( AFCTEMP[$fcvcount]/1000 )) |
| 188 | + done |
| 189 | +} |
| 190 | + |
| 191 | +function UpdateThermalLevel |
| 192 | +{ |
| 193 | + AFCTEMP_NUM=$((6-${AFCTEMP_LEVEL[$i]})) |
| 194 | + AFCTEMP_UPPER_BUF=AFCTEMP_"$i"_UPPER["$AFCTEMP_NUM"] |
| 195 | + AFCTEMP_LOWER_BUF=AFCTEMP_"$i"_LOWER["$AFCTEMP_NUM"] |
| 196 | + |
| 197 | + AFCTEMP_UPPER=${!AFCTEMP_UPPER_BUF} |
| 198 | + AFCTEMP_LOWER=${!AFCTEMP_LOWER_BUF} |
| 199 | + |
| 200 | + |
| 201 | + if (( ("${AFCTEMP[$i]}" <= "$AFCTEMP_UPPER") && ("${AFCTEMP[$i]}" >= "$AFCTEMP_LOWER") )) ; then |
| 202 | + FLAG=2 |
| 203 | + elif (( "${AFCTEMP[$i]}" > "$AFCTEMP_UPPER" )); then |
| 204 | + AFCTEMP_LEVEL[$i]=$((${AFCTEMP_LEVEL[$i]} - 1)) |
| 205 | + FLAG=1 |
| 206 | + elif (( "${AFCTEMP[$i]}" < "$AFCTEMP_LOWER" )); then |
| 207 | + AFCTEMP_LEVEL[$i]=$((${AFCTEMP_LEVEL[$i]} + 1)) |
| 208 | + FLAG=1 |
| 209 | + else |
| 210 | + AFCTEMP_LEVEL[$i]=1 |
| 211 | + FLAG=2 |
| 212 | + fi |
| 213 | +} |
| 214 | + |
| 215 | +function UpdateFanSpeeds |
| 216 | +{ |
| 217 | + #echo "num tmp lev F L H" |
| 218 | + #Update level |
| 219 | + for i in 1 2 3 |
| 220 | + do |
| 221 | + #echo "----------------------" |
| 222 | + FLAG=0 |
| 223 | + #FLAG=0 : initial flag |
| 224 | + #FLAG=1 : update level |
| 225 | + #FLAG=2 : final level |
| 226 | + while [ $FLAG -ne 2 ] |
| 227 | + do |
| 228 | + UpdateThermalLevel |
| 229 | + #echo " $i ${AFCTEMP[$i]} ${AFCTEMP_LEVEL[$i]} $FLAG $AFCTEMP_LOWER $AFCTEMP_UPPER " |
| 230 | + done |
| 231 | + done |
| 232 | + |
| 233 | + min=${AFCTEMP_LEVEL[0]} |
| 234 | + for j in "${AFCTEMP_LEVEL[@]}"; do |
| 235 | + (( j < min )) && min=$j |
| 236 | + done |
| 237 | + |
| 238 | + if (($min == 1 || $min == 2)); then |
| 239 | + FAN_PERCENTAGE=100 |
| 240 | + elif (($min == 3)); then |
| 241 | + FAN_PERCENTAGE=80 |
| 242 | + elif (($min == 4)); then |
| 243 | + FAN_PERCENTAGE=60 |
| 244 | + elif (($min == 5)); then |
| 245 | + FAN_PERCENTAGE=40 |
| 246 | + elif (($min == 6)); then |
| 247 | + FAN_PERCENTAGE=30 |
| 248 | + else |
| 249 | + FAN_PERCENTAGE=100 |
| 250 | + fi |
| 251 | + echo "The lowest level of thermal sensors: $min " |
| 252 | + echo "Trying to set fan speed to $FAN_PERCENTAGE %" |
| 253 | + #Set speed to fan1~fan10 |
| 254 | + FAN_PERCENTAGE=`expr $FAN_PERCENTAGE \* 255 / 100` |
| 255 | + let fcvcount=0 |
| 256 | + for fcv in $FCFANS |
| 257 | + do |
| 258 | + fcvcount=$(( fcvcount + 1 )) |
| 259 | + echo $FAN_PERCENTAGE > ${AFCFAN_TARGET[$fcvcount]} |
| 260 | + AFCFAN[$fcvcount]=$( cat ${AFCFAN_PATH[$fcvcount]} ) |
| 261 | + |
| 262 | + echo "FAN fan$fcvcount = ${AFCFAN[$fcvcount]} (rpm)" |
| 263 | + done |
| 264 | + |
| 265 | + rm -f "$PIDFILE" |
| 266 | +} |
| 267 | + |
| 268 | +# main loop calling the main function at specified intervals |
| 269 | +AFCTEMP_LEVEL=(9 4 4 4) #inttial level |
| 270 | +while true |
| 271 | +do |
| 272 | + UpdateThermalSensors |
| 273 | + UpdateFanSpeeds |
| 274 | + echo "Sleep $INTERVAL seconds ..." |
| 275 | + echo |
| 276 | + # Sleep while still handling signals |
| 277 | + sleep $INTERVAL & |
| 278 | + wait $! |
| 279 | +done |
0 commit comments