1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
#!/bin/bash
#
# Simple script implementing a temperature dependent fan speed control
#
# Version 0.66
#
# Usage: fancontrol [CONFIGFILE]
#
# Dependencies:
# bash, egrep, sed, cut, sleep, lm_sensors :)
#
# Please send any questions, comments or success stories to
# marius.reiner@hdev.de
# Thanks!
#
# The latest version of this script is available at:
# http://www.hdev.de/fancontrol/fancontrol.html
# or in the SVN version of lm_sensors
#
# For configuration instructions and warnings please see fancontrol.txt, which
# can be found in the doc/ directory or at the website mentioned above.
#
#
# Copyright 2003 Marius Reiner <marius.reiner@hdev.de>
# Copyright (C) 2007 Jean Delvare <khali@linux-fr.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#
#DEBUG=1
MAX=255
echo $$ > /var/run/fancontrol.pid
declare -i pwmval
function LoadConfig {
echo "Loading configuration from $1 ..."
# grep configuration from file
INTERVAL=`egrep '^INTERVAL=.*$' $1 | sed -e 's/INTERVAL=//g'`
FCTEMPS=`egrep '^FCTEMPS=.*$' $1 | sed -e 's/FCTEMPS=//g'`
MINTEMP=`egrep '^MINTEMP=.*$' $1 | sed -e 's/MINTEMP=//g'`
MAXTEMP=`egrep '^MAXTEMP=.*$' $1 | sed -e 's/MAXTEMP=//g'`
MINSTART=`egrep '^MINSTART=.*$' $1 | sed -e 's/MINSTART=//g'`
MINSTOP=`egrep '^MINSTOP=.*$' $1 | sed -e 's/MINSTOP=//g'`
# optional settings:
FCFANS=`egrep '^FCFANS=.*$' $1 | sed -e 's/FCFANS=//g'`
MINPWM=`egrep '^MINPWM=.*$' $1 | sed -e 's/MINPWM=//g'`
MAXPWM=`egrep '^MAXPWM=.*$' $1 | sed -e 's/MAXPWM=//g'`
# Check whether all mandatory settings are set
if [[ -z ${INTERVAL} || -z ${FCTEMPS} || -z ${MINTEMP} || -z ${MAXTEMP} || -z ${MINSTART} || -z ${MINSTOP} ]]
then
echo "Some mandatory settings missing, please check your config file!"
exit 1
fi
if [ "$INTERVAL" -le 0 ]
then
echo "Error in configuration file:"
echo "INTERVAL must be at least 1"
exit 1
fi
# write settings to arrays for easier use and print them
echo
echo "Common settings:"
echo " INTERVAL=$INTERVAL"
let fcvcount=0
for fcv in $FCTEMPS
do
if ! echo $fcv | egrep -q '='
then
echo "Error in configuration file:"
echo "FCTEMPS value is improperly formatted"
exit 1
fi
AFCPWM[$fcvcount]=`echo $fcv |cut -d'=' -f1`
AFCTEMP[$fcvcount]=`echo $fcv |cut -d'=' -f2`
AFCFAN[$fcvcount]=`echo $FCFANS |sed -e 's/ /\n/g' |egrep "${AFCPWM[$fcvcount]}" |cut -d'=' -f2`
AFCMINTEMP[$fcvcount]=`echo $MINTEMP |sed -e 's/ /\n/g' |egrep "${AFCPWM[$fcvcount]}" |cut -d'=' -f2`
AFCMAXTEMP[$fcvcount]=`echo $MAXTEMP |sed -e 's/ /\n/g' |egrep "${AFCPWM[$fcvcount]}" |cut -d'=' -f2`
AFCMINSTART[$fcvcount]=`echo $MINSTART |sed -e 's/ /\n/g' |egrep "${AFCPWM[$fcvcount]}" |cut -d'=' -f2`
AFCMINSTOP[$fcvcount]=`echo $MINSTOP |sed -e 's/ /\n/g' |egrep "${AFCPWM[$fcvcount]}" |cut -d'=' -f2`
AFCMINPWM[$fcvcount]=`echo $MINPWM |sed -e 's/ /\n/g' |egrep "${AFCPWM[$fcvcount]}" |cut -d'=' -f2`
[ -z "${AFCMINPWM[$fcvcount]}" ] && AFCMINPWM[$fcvcount]=0
AFCMAXPWM[$fcvcount]=`echo $MAXPWM |sed -e 's/ /\n/g' |egrep "${AFCPWM[$fcvcount]}" |cut -d'=' -f2`
[ -z "${AFCMAXPWM[$fcvcount]}" ] && AFCMAXPWM[$fcvcount]=255
# verify the validity of the settings
if [ "${AFCMINTEMP[$fcvcount]}" -ge "${AFCMAXTEMP[$fcvcount]}" ]
then
echo "Error in configuration file (${AFCPWM[$fcvcount]}):"
echo "MINTEMP must be less than MAXTEMP"
exit 1
fi
if [ "${AFCMAXPWM[$fcvcount]}" -gt 255 ]
then
echo "Error in configuration file (${AFCPWM[$fcvcount]}):"
echo "MAXPWM must be at most 255"
exit 1
fi
if [ "${AFCMINSTOP[$fcvcount]}" -ge "${AFCMAXPWM[$fcvcount]}" ]
then
echo "Error in configuration file (${AFCPWM[$fcvcount]}):"
echo "MINSTOP must be less than MAXPWM"
exit 1
fi
if [ "${AFCMINSTOP[$fcvcount]}" -lt "${AFCMINPWM[$fcvcount]}" ]
then
echo "Error in configuration file (${AFCPWM[$fcvcount]}):"
echo "MINSTOP must be greater than or equal to MINPWM"
exit 1
fi
if [ "${AFCMINPWM[$fcvcount]}" -lt 0 ]
then
echo "Error in configuration file (${AFCPWM[$fcvcount]}):"
echo "MINPWM must be at least 0"
exit 1
fi
echo
echo "Settings for ${AFCPWM[$fcvcount]}:"
echo " Depends on ${AFCTEMP[$fcvcount]}"
echo " Controls ${AFCFAN[$fcvcount]}"
echo " MINTEMP=${AFCMINTEMP[$fcvcount]}"
echo " MAXTEMP=${AFCMAXTEMP[$fcvcount]}"
echo " MINSTART=${AFCMINSTART[$fcvcount]}"
echo " MINSTOP=${AFCMINSTOP[$fcvcount]}"
echo " MINPWM=${AFCMINPWM[$fcvcount]}"
echo " MAXPWM=${AFCMAXPWM[$fcvcount]}"
let fcvcount=fcvcount+1
done
echo
}
if [ -f "$1" ]
then
LoadConfig $1
else
LoadConfig /etc/fancontrol
fi
DIR=/proc/sys/dev/sensors
if [ ! -d $DIR ]
then
# For Linux 2.6, detect if config file uses the hwmon class or not yet
if echo "${AFCPWM[0]}" | egrep -q '^hwmon[0-9]'
then
SDIR=/sys/class/hwmon
else
SDIR=/sys/bus/i2c/devices
fi
if [ ! -d $SDIR ]
then
echo $0: 'No sensors found! (did you load the necessary modules?)'
exit 1
else
SYSFS=1
DIR=$SDIR
fi
fi
cd $DIR
# $1 = pwm file name
function pwmdisable()
{
if [ -n "$SYSFS" ]
then
ENABLE=${1}_enable
# No enable file? Just set to max
if [ ! -f $ENABLE ]
then
echo $MAX > $1
return 0
fi
# Try pwmN_enable=0
echo 0 > $ENABLE 2> /dev/null
if [ `cat $ENABLE` -eq 0 ]
then
# Success
return 0
fi
# It didn't work, try pwmN_enable=1 pwmN=255
echo 1 > $ENABLE 2> /dev/null
echo $MAX > $1
if [ `cat $ENABLE` -eq 1 -a `cat $1` -ge 190 ]
then
# Success
return 0
fi
# Nothing worked
echo "$ENABLE stuck to" `cat $ENABLE` >&2
return 1
else
echo $MAX 0 > $1
fi
}
# $1 = pwm file name
function pwmenable()
{
if [ "$SYSFS" = "1" ]
then
ENABLE=${1}_enable
if [ -f $ENABLE ]
then
echo 1 > $ENABLE 2> /dev/null
if [ $? -ne 0 ]
then
return 1
fi
fi
echo $MAX > $1
else
echo $MAX 1 > $1
fi
}
function restorefans()
{
local status=$1
echo 'Aborting, restoring fans...'
let fcvcount=0
while (( $fcvcount < ${#AFCPWM[@]} )) # go through all pwm outputs
do
pwmo=${AFCPWM[$fcvcount]}
pwmdisable $pwmo
let fcvcount=$fcvcount+1
done
echo 'Verify fans have returned to full speed'
exit $status
}
trap 'restorefans 0' SIGQUIT SIGTERM
trap 'restorefans 1' SIGHUP SIGINT
# main function
function UpdateFanSpeeds {
let fcvcount=0
while (( $fcvcount < ${#AFCPWM[@]} )) # go through all pwm outputs
do
#hopefully shorter vars will improve readability:
pwmo=${AFCPWM[$fcvcount]}
tsens=${AFCTEMP[$fcvcount]}
fan=${AFCFAN[$fcvcount]}
mint=${AFCMINTEMP[$fcvcount]}
maxt=${AFCMAXTEMP[$fcvcount]}
minsa=${AFCMINSTART[$fcvcount]}
minso=${AFCMINSTOP[$fcvcount]}
minpwm=${AFCMINPWM[$fcvcount]}
maxpwm=${AFCMAXPWM[$fcvcount]}
read tval < ${tsens}
if [ $? -ne 0 ]
then
echo "Error reading temperature from $DIR/$tsens"
restorefans 1
fi
if [ "$SYSFS" = "1" ]
then
let tval="($tval+500)/1000"
else
tval=`echo ${tval} | cut -d' ' -f3 | cut -d'.' -f1`
fi
read pwmpval < ${pwmo}
if [ $? -ne 0 ]
then
echo "Error reading PWM value from $DIR/$pwmo"
restorefans 1
fi
if [ "$SYSFS" != "1" ]
then
pwmpval=`echo ${pwmpval} | cut -d' ' -f1`
fi
# If fanspeed-sensor output shall be used, do it
if [[ -n ${fan} ]]
then
read fanval < ${fan}
if [ $? -ne 0 ]
then
echo "Error reading Fan value from $DIR/$fan"
restorefans 1
fi
if [ "$SYSFS" != "1" ]
then
fanval=`echo ${fanval} | cut -d' ' -f2`
fi
else
fanval=1 # set it to a non zero value, so the rest of the script still works
fi
# debug info
if [ "$DEBUG" != "" ]
then
echo "pwmo=$pwmo"
echo "tsens=$tsens"
echo "fan=$fan"
echo "mint=$mint"
echo "maxt=$maxt"
echo "minsa=$minsa"
echo "minso=$minso"
echo "minpwm=$minpwm"
echo "maxpwm=$maxpwm"
echo "tval=$tval"
echo "pwmpval=$pwmpval"
echo "fanval=$fanval"
fi
if (( $tval <= $mint ))
then pwmval=$minpwm # below min temp, use defined min pwm
elif (( $tval >= $maxt ))
then pwmval=$maxpwm # over max temp, use defined max pwm
else
# calculate the new value from temperature and settings
pwmval="(${tval}-${mint})*(${maxpwm}-${minso})/(${maxt}-${mint})+${minso}"
if [ $pwmpval -eq 0 -o $fanval -eq 0 ]
then # if fan was stopped start it using a safe value
echo $minsa > $pwmo
# Sleep while still handling signals
sleep 1 &
wait $!
fi
fi
echo $pwmval > $pwmo # write new value to pwm output
if [ $? -ne 0 ]
then
echo "Error writing PWM value to $DIR/$pwmo"
restorefans 1
fi
if [ "$DEBUG" != "" ]
then
echo "new pwmval=$pwmval"
fi
let fcvcount=$fcvcount+1
done
}
echo 'Enabling PWM on fans...'
let fcvcount=0
while (( $fcvcount < ${#AFCPWM[@]} )) # go through all pwm outputs
do
pwmo=${AFCPWM[$fcvcount]}
pwmenable $pwmo
if [ $? -ne 0 ]
then
echo "Error enabling PWM on $DIR/$pwmo"
restorefans 1
fi
let fcvcount=$fcvcount+1
done
echo 'Starting automatic fan control...'
# main loop calling the main function at specified intervals
while true
do
UpdateFanSpeeds
# Sleep while still handling signals
sleep $INTERVAL &
wait $!
done
|