[how to] fan speed override and fine tuning (v2)

sksbir
Posts: 395
youtube meble na wymiar Warszawa
Joined: Tue Aug 25, 2015 9:23 pm

[how to] fan speed override and fine tuning (v2)

Post by sksbir »

Hi
[edit 14/6/2017]
- script force_fanspeed.sh : fixed fan speed and limited time execution. script is here in 1st reply
- script smart_fanspeed.sh : using disks temp sensor to smooth discs temperature by actively acting on the fan speed., script is here
- startup script for smart_fanspeed.sh : in order to start script at boot. see here.

In order to launch and install these scripts, you must be connected to your NAS with ssh, and with root user.


Here is a little script I wrote in response of FAN control on AS6204T

- This script is successfully tested on AS5002T and AS3102T. I cannot guarantee that it will works on other models, specially if they are using more that one fan.
- script is intended for people who knows about using ssh terminal on their NAS.

- Copy paste the script on the NAS, on location of your choice, in a file named force_fanspeed.sh, and make it executable ( chmod +x )
- script syntax is
..../force_fanspeed.sh SPEED DURATION
SPEED : from 30 to 255, 75-80 are optimal : less noisy than "medium", but cooling enough, far more than "auto".
DURATION : in minutes.

script is intended to use has less resources has possible on your NAS. the basic command ( fanctrl -set fanpwm ) works only for 10 seconds, some other process is working here and reverts fan speed every 10 seconds to settings stored in ADM.
That's why this script first make a pause of 9 seconds, and then actively scan when fan speed setting is changed to reinstall it's own setting just 1/10 second after.
script needs about 1mn to really override default fan speed. Don't worry if fan speed reverts temporarily to default settings during the first minute.
once script is breaked or ended, fan speed reverts to default setting stored in ADM.

sample invocation : crontab line, increase fan speed nights, during backup ( ADM setting is "auto" ) :

Code: Select all

00 23 * * * /bin/sh /share/Syst_interne/force_fanspeed.sh 75 480

script content :

Code: Select all

#!/bin/sh

mysynerror() {
        echo $0 SPEED DURATION
        echo with SPEED from 30 to 255
        echo duration in minutes
        echo script needs about 1mn to really override fanspeed
        exit 6
}

if [ "$2" = "" ]
then
        mysynerror
fi

DESIREDPOWER="$1"
DURATION="$2"

FANID=0
##DESIREDPOWER=75 # from about 30 to 255

if [ \( "$DESIREDPOWER" -lt 30 \) -o \( "$DESIREDPOWER" -gt 255 \) ]
then
        mysynerror
fi
if [ "$DURATION" -le 0 ]
then
        mysynerror
fi

COUNTDWN=`expr $DURATION \* 6`

while [ $COUNTDWN -gt 0 ]
do
        CURRPOWER=`fanctrl -getfanspeed|awk ' { print $NF } ' `
        if [ $DESIREDPOWER -ne $CURRPOWER ]
        then
                fanctrl -setfanpwm $FANID $DESIREDPOWER
                ## echo $CURRPOWER to $DESIREDPOWER CPT : $CPT
                CPT=0
                COUNTDWN=`expr $COUNTDWN - 1`
                sleep 9
        else
                sleep 0.1
                CPT=`expr $CPT + 1`
        fi
done
Last edited by sksbir on Wed Oct 18, 2017 4:37 pm, edited 9 times in total.
User avatar
orion
Posts: 3485
Joined: Wed May 29, 2013 11:09 am

Re: [how to] fan speed override and fine tuning

Post by orion »

Bravo! Thanks for sharing. :D
MikeG.6.5
Posts: 917
Joined: Fri May 15, 2015 1:56 am

Re: [how to] fan speed override and fine tuning

Post by MikeG.6.5 »

Would love to see this with an implementation to read the temp settings from both the CPU sensor and from the HDD's sensors. Then the script could be used instead of auto, where the temps are read and the fan changes according to the cooling needed.

Other than that, this is fantastic. New ways to manipulate our NASes and make things better! Thanks!
sksbir
Posts: 395
Joined: Tue Aug 25, 2015 9:23 pm

smart fanspeed override is here !

Post by sksbir »

MikeG.6.5 wrote:Would love to see this with an implementation to read the temp settings from both the CPU sensor and from the HDD's sensors. Then the script could be used instead of auto, where the temps are read and the fan changes according to the cooling needed....
I heard you, but I was not able to find the way to retrieve CPU temp.

Here is a script which is doing what you say, but just with disk(s) temp. It take the hottest disk into account. The fanspeed override loop is the same has force_fanspeed.sh ( in 1st reply ).

I tryed it on my 2 NAS with success.

copy-paste the content in a file named "smart_fanspeed.sh" in location of your choice, and make it executable ( chmod +x )
- once started, script will never end.
- script will output log on standard output with "-v" option.

running example:

Code: Select all

# ./smart_fanspeed.sh -v
/dev/sda added to list with recognized temp of 43
/dev/sdb added to list with recognized temp of 38
Retained devices for temperature check : /dev/sda /dev/sdb
20170613_22:21:08 hottest disk changed from 15 to 43,raising fanpower from 31 to 92
20170613_23:06:30 hottest disk changed from 43 to 42,raising fanpower from 92 to 90
20170613_23:09:30 hottest disk changed from 42 to 43,raising fanpower from 90 to 92
20170613_23:21:31 hottest disk changed from 43 to 42,raising fanpower from 92 to 90
20170613_23:24:31 hottest disk changed from 42 to 43,raising fanpower from 90 to 92
20170613_23:30:31 hottest disk changed from 43 to 42,raising fanpower from 92 to 90
20170614_00:06:34 hottest disk changed from 42 to 43,raising fanpower from 90 to 92
20170614_00:18:34 hottest disk changed from 43 to 42,raising fanpower from 92 to 90
20170614_02:15:40 hottest disk changed from 42 to 43,raising fanpower from 90 to 92
20170614_02:18:41 hottest disk changed from 43 to 42,raising fanpower from 92 to 90
20170614_03:27:45 hottest disk changed from 42 to 41,raising fanpower from 90 to 88
20170614_03:30:45 hottest disk changed from 41 to 42,raising fanpower from 88 to 90
20170614_05:15:51 hottest disk changed from 42 to 41,raising fanpower from 90 to 88
20170614_05:18:51 hottest disk changed from 41 to 42,raising fanpower from 88 to 90
20170614_05:27:52 hottest disk changed from 42 to 41,raising fanpower from 90 to 88
20170614_05:30:52 hottest disk changed from 41 to 42,raising fanpower from 88 to 90
Script source code: ( Last edit : 22 april 2022 )

Code: Select all

#!/bin/sh
# designed by sksbir 
# https://forum.asustor.com/viewtopic.php?f=97&t=8948
# 20220422 : added nvme device, using smartctl --scan for device recognition , managing multiple fans, more agressive fan reaction in case of temperature elevation.

if [ "$1" = "-v" ]
then
        VERBOSE=1
fi

DEF_DESIREDPOWER=75 # from about 30 to 255
CYCLETEMPCHECK=12 # every how many 10" cycles we check for temp changes. 6 = every mn
#
# temperature to power grid : GRIDxx=yy : we want power of yy if temp is xx
GRID15=30 ; GRID16=30 ; GRID17=30 ; GRID18=31 ; GRID19=33
GRID20=35 ; GRID21=37 ; GRID22=39 ; GRID23=41 ; GRID24=44
GRID25=47 ; GRID26=50 ; GRID27=54 ; GRID28=59 ; GRID29=65
GRID30=68 ; GRID31=70 ; GRID32=72 ; GRID33=74 ; GRID34=75
GRID35=77 ; GRID36=79 ; GRID37=82 ; GRID38=84 ; GRID39=85
GRID40=86 ; GRID41=88 ; GRID42=90 ; GRID43=92 ; GRID44=94
GRID45=96 ; GRID46=105 ; GRID47=110 ; GRID48=135 ; GRID49=160
GRID50=170 ; GRID51=175 ; GRID52=180 ; GRID53=185 ; GRID54=190
GRID55=195 ; GRID56=200 ; GRID57=210 ; GRID58=220 ; GRID59=230
GRID60=250 ; GRID61=250 ; GRID62=250 ; GRID63=250 ; GRID64=250



#
# let's learn devices which have temperature sensors
#
HOTTESTDISKTEMP=15
LSTDEVICES=""
for DSKDEV in `smartctl --scan|awk ' { print $1 } ' ` 
do
        echo $DSKDEV|grep nvm >/dev/null 2>&1
        if [ $? -eq 0 ]
        then
                TSTTEMP=`smartctl --all $DSKDEV|awk '/^Temperature:.*Celsius/ { print $2 } '`
        else
                TSTTEMP=`smartctl --all $DSKDEV|awk '/^194/ { print $10 } '`
        fi
        if [ "$TSTTEMP" != "" ]
        then
                if [ \( "$TSTTEMP" -gt 15 \) -a \( "$TSTTEMP" -lt 70 \) ]
                then
                        [ $VERBOSE ] && echo $DSKDEV added to list with recognized temp of $TSTTEMP
                        LSTDEVICES="$LSTDEVICES $DSKDEV"
                fi
        fi
done
[ $VERBOSE ] && echo Retained devices for temperature check : $LSTDEVICES

FANLIST=`fanctrl -getfanspeed|awk ' { print $1 } '`
[ $VERBOSE ] && echo valid fanlist : $FANLIST


# DESIREDPOWER=$DEF_DESIREDPOWER # from about 30 to 255
DESIREDPOWER=`fanctrl -getfanspeed|awk ' { print $NF } '|head -1`
COUNTDWN=0
CPT=0
DELAY=1 # initial loop = no delay

while :
do
#
# temperature check
#
        if [ $COUNTDWN -le 0 ]
        then
                COUNTDWN=$CYCLETEMPCHECK
                # we almost cancel pause because this loop consumes time.
                DELAY=1
                NEW_HOTTESTDISKTEMP=15
                for DSKDEV in $LSTDEVICES
                do
                        echo $DSKDEV|grep nvm >/dev/null 2>&1
                        if [ $? -eq 0 ]
                        then
                                TSTTEMP=`smartctl --all $DSKDEV|awk '/^Temperature:.*Celsius/ { print $2 } '`
                        else
                                TSTTEMP=`smartctl --all $DSKDEV|awk '/^194/ { print $10 } '`
                        fi
                        if [ "$TSTTEMP" -gt $NEW_HOTTESTDISKTEMP ]
                        then
                                NEW_HOTTESTDISKTEMP=$TSTTEMP
                        fi
                done
                NEW_DESIREDPOWER=`eval echo \\\$GRID$NEW_HOTTESTDISKTEMP`
                if [ "$NEW_DESIREDPOWER" = "" ]
                then
                        NEW_DESIREDPOWER=$DEF_DESIREDPOWER
                fi
                if [ $HOTTESTDISKTEMP != $NEW_HOTTESTDISKTEMP ]
                then
                        if [ $DESIREDPOWER != $NEW_DESIREDPOWER ]
                        then
                                [ $VERBOSE ] && echo `date +%Y%m%d_%T` hottest disk changed from $HOTTESTDISKTEMP to $NEW_HOTTESTDISKTEMP,raising fanpower from $DESIREDPOWER to $NEW_DESIREDPOWER
                                DESIREDPOWER=$NEW_DESIREDPOWER
                        else
                                [ $VERBOSE ] && echo `date +%Y%m%d_%T` hottest disk changed from $HOTTESTDISKTEMP to $NEW_HOTTESTDISKTEMP,fanpower leaved unchanged at $DESIREDPOWER 
                        fi
                        HOTTESTDISKTEMP=$NEW_HOTTESTDISKTEMP
                ## else
                        ## [ $VERBOSE ] && echo `date +%Y%m%d_%T` hottest disk leaved unchanged at $HOTTESTDISKTEMP ,fanpower leaved unchanged at $DESIREDPOWER 
                fi
        fi

#
# fan override loop
#
        CURRPOWER=`fanctrl -getfanspeed|awk ' { print $NF } ' `
        if [ $DESIREDPOWER -ne $CURRPOWER ]
        then
                for FANID in $FANLIST
                do
                        fanctrl -setfanpwm $FANID $DESIREDPOWER
                done
                ## echo $CURRPOWER to $DESIREDPOWER CPT : $CPT
                CPT=0
                COUNTDWN=`expr $COUNTDWN - 1`
                sleep $DELAY
                DELAY=9
        else
                sleep 0.1
                CPT=`expr $CPT + 1`
        fi
done
Last edited by sksbir on Sat Apr 23, 2022 5:24 am, edited 5 times in total.
MikeG.6.5
Posts: 917
Joined: Fri May 15, 2015 1:56 am

Re: [how to] fan speed override and fine tuning (v2)

Post by MikeG.6.5 »

Went to test this and got a slew of errors. Line 6, then a bunch of line 42's and then a whole bunch of 106's...

Too tired to dig into it tonight, though. Will try messing with it tomorrow to see what the problem could be.

Many thanks for working on this, though. Any way to get more controls... :)
sksbir
Posts: 395
Joined: Tue Aug 25, 2015 9:23 pm

Re: [how to] fan speed override and fine tuning (v2)

Post by sksbir »

MikeG.6.5 wrote:Went to test this and got a slew of errors. Line 6, then a bunch of line 42's and then a whole bunch of 106's... ....
Hi Mike. I think you were too tired , and made a mistake. The script is running perfectly on my two NAS since several days. There is nothing in 6 1st lines of script which can explains your problem.
The best way to create script is to copy-paste from here directly to vi in ssh session on your NAS.

- I have changed the code of smart_fanspeed.sh : adding a test in order to exclude USB disks ( those who are mounted on /share/USBx )
- I disabled the verbose output when nothing changed, so the output remains short and interesting, even over several days.
- I added a startup script so the script is started when NAS is rebooting:
File /volume1/.@plugins/etc/init.d/S50_smartfanspeed :

Code: Select all

#!/bin/sh
LOGFILE=<YOUR_PATH_HERE>/fanspeed.log

case "$1" in
    start)
fuser $LOGFILE
if [ $? -eq 1 ]
then
        <YOUR PATH_HERE>/smart_fanspeed.sh -v > $LOGFILE 2>&1 &
fi
        ;;

esac
My advice is to set fanspeed in ADM to "medium" or "high". Just in case script is killed, you will not overheat your disks.
raidsm
Posts: 4
Joined: Sun Dec 04, 2016 8:57 am

Re: [how to] fan speed override and fine tuning (v2)

Post by raidsm »

Hi!

great to see this script but I wonder if there's a way to modify the source code instead of overide it with a script? (I know yours is for a certains amount of time)

I know that on synology nases there's a modification possible on the DSM fan control script and you just can change some values depending on temps.

Is there a way to do this on ADM? As I have a SSD I would like to slow down fan speed a bit in auto mode.

I did a search and did'nt find anything useful.
sksbir
Posts: 395
Joined: Tue Aug 25, 2015 9:23 pm

Re: [how to] fan speed override and fine tuning (v2)

Post by sksbir »

raidsm wrote:Hi!
great to see this script but I wonder if there's a way to modify the source code instead of overide it with a script? (I know yours is for a certains amount of time)
I know that on synology nases there's a modification possible on the DSM fan control script and you just can change some values depending on temps.
Is there a way to do this on ADM? As I have a SSD I would like to slow down fan speed a bit in auto mode.
I did a search and did'nt find anything useful.
Hi
I never found anything about changing source. That's why I wrote this 2 scripts. take a look at second script ( smart_fanspeed.sh ) , link is in first reply : this script works endless.
MikeG.6.5
Posts: 917
Joined: Fri May 15, 2015 1:56 am

Re: [how to] fan speed override and fine tuning (v2)

Post by MikeG.6.5 »

I honestly haven't had any time to dig into this.

Lately I seem to spend most of my time deleting spam posts and banning the offending accounts, that I seldom have time to look at any of the posts on the forums. And when I do, I find that in many cases it's a rehash of things that have already been covered.

My apologies. I didn't mean to make a negative comment about these scripts and then abandon them. As fan control hasn't been an issue for me, nor temps on my NAS, they haven't been a priority. RL things have taken the top of the pile and the forums are a sad secondary. (Except for the spammers. I seem to spend an inordinate amount of time dealing with those!)
dergilb
Posts: 10
Joined: Sat Aug 19, 2017 8:15 pm

Re: [how to] fan speed override and fine tuning (v2)

Post by dergilb »

hmmm is it possible to set the fan to 0? in case that hdds are in hibernate and cpu is idle? or is fan off not supportet by hardware?
Post Reply

Return to “[Official] For AS50xx/51xx Series”