Alternative temperature monitor

and fan control, if you have the two-disk version

Introduction

The default fan on the MBWE II is small and noisy and it's spinning when it shouldn't, especially if you have installed the Smart spindown script.

With the default temperature monitoring service on the MBWE, you can't set the limits for when the fan should be spinning how fast. With my simple replacement shell script you can.

Also, you can log the temperatures easily for debugging or other purposes.

Prerequisites

Enabled SSH access.

A text editor and the know-how to use it. I recommend installing nano, but vi will also do just fine.

An up-to-date version of hdparm. See Smart spindown for installation instructions.

Note: if you own the MBWE I i.e. the single disk version, this script is pretty much useless for anything else but temperature logging, since the single disk version doesn't have a fan.

The how-to

  1. Make sure you are in superuser mode.

    # su
  2. Disable the original temperature monitoring service. Edit the file /etc/init.d/S15wdc-heat-monitor. Comment out this line:

    $HEAT_MON &

    Then stop the service

    # /etc/init.d/S15wdc-heat-monitor stop

    Download my shell script and the script used to start it as a service, move them to right places and set treir access rights:

    # wget http://kyyhkynen.net/stuff/mybook/temperature_monitor
    # mv temperature_monitor /usr/sbin
    # chmod 0700 /usr/sbin/temperature_monitor
    # chown root:root /usr/sbin/temperature_monitor
    # wget http://kyyhkynen.net/stuff/mybook/S51temperature_monitor
    # mv S51temperature_monitor /etc/init.d
    # chmod 0700 /etc/init.d/S51temperature_monitor
    # chown root:root /etc/init.d/S51temperature_monitor
    # ln -s /etc/init.d/S51temperature_monitor /etc/init.d/K51temperature_monitor

    The scripts in the directory /etc/init.d/ starting with S[number] are automatically run during system startup with parameter start. The number defines the order the scripts are run. Likewise, scripts starting with K[number] are run when the system is shutting down with parameter stop.

  3. Edit the settings in the beginning of the script (see comments in the script for details).

    #!/bin/sh
    # An alternative temperature monitoring and fan control script for 
    # WD MyBook World Edition external hard drives
    #
    # Copyright (C) 2007 by kyyhkynen at gmail.com
    #
    # You may do with this file (and parts thereof) whatever you want, as long
    # as my copyright notice is retained.
    # last updated 2008/04/23
    #
    #######################################################################
    #
    # Configuration settings
    #
    # Disk(s) to monitor.
    # If you have only one disk, leave DISK2 empty
    # In that case, however, this script is pretty much useless.
    DISK1=sda
    DISK2=sdb
    
    # hdparm location. If you are using the optware version,
    # you have to change this into /opt/sbin/hdparm
    HDPARM=/sbin/hdparm
    
    # Temperature limits for speeds.
    # When the temperature of the disk(s) drop below TEMP_LIMIT_0, the fan speed is set to 0
    # When the temperature of the disk(s) is between the limits, the fan speed is set to 50
    # When the temperature of the disk(s) is above TEMP_LIMIT_50, the fan speed is set to 100
    TEMP_LIMIT_0=43
    TEMP_LIMIT_50=55
    
    # The amount of time (in minutes) to wait before turning the fan off after the disk(s) have been spun down.
    SPINDOWN_WAIT=10
    
    # Logging stuff
    LOG_ENABLED=false
    LOG_INTERVAL=5
    LOGGER=echo
    
    #
    # End of configuration. You shouldn't need to touch lines below this :)
    #
    #######################################################################
    
    # Device name(s) for the disk(s).
    DEVNAME1=/dev/${DISK1}
    DEVNAME2=/dev/${DISK2}
    
    # The file controlling the fan speed
    FAN_SPEED=/sys/devices/platform/wdc-fan/speed
    
    # Get current fan speed
    CURRENT_FAN_SPEED=`cat ${FAN_SPEED}` ;
    
    LOG_COUNTER=0
    SPINDOWN_COUNTER=0
    
    while [[ /bin/true ]]; do
    	
    	TEMPERATURE_DISK1=$(smartctl -d ata -A ${DEVNAME1}|grep Tempera|cut -c 88-90) ;
    	
    	$HDPARM -C $DEVNAME1 |grep active >/dev/null
    	if [ "$?" == "0" ] ; then
    		STATUS_DISK1=u ;
    	else
    		STATUS_DISK1=d ;
    	fi ;
    
    	if [ -z "$DISK2" ] ; then
    		TEMPERATURE_DISK2=$TEMPERATURE_DISK1 ;
    		STATUS_DISK2=$STATUS_DISK1 ;
    	else
    		TEMPERATURE_DISK2=$(smartctl -d ata -A ${DEVNAME2}|grep Tempera|cut -c 88-90) ;
    		$HDPARM -C $DEVNAME2 |grep active >/dev/null
    		if [ "$?" == "0" ] ; then
    			STATUS_DISK2=u ;
    		else
    			STATUS_DISK2=d ;
    		fi ;
    	fi ;
    
    	if [ $STATUS_DISK1 = "d" -a $STATUS_DISK2 = "d" ] ; then
    		if [ $CURRENT_FAN_SPEED -gt 0 ] ; then
    			SPINDOWN_COUNTER=$((SPINDOWN_COUNTER+1)) ;
    			if [[ $SPINDOWN_COUNTER -ge $SPINDOWN_WAIT ]] ; then
    				echo 0 > $FAN_SPEED ;
    				CURRENT_FAN_SPEED=0 ;
    				SPINDOWN_COUNTER=0 ;
    			fi ;
    		fi ;
    	else
    		if [ $TEMPERATURE_DISK1 -ge $TEMP_LIMIT_0 -o $TEMPERATURE_DISK2 -ge $TEMP_LIMIT_0 ] ; then
    			if [ $TEMPERATURE_DISK1 -ge $TEMP_LIMIT_50 -o $TEMPERATURE_DISK2 -ge $TEMP_LIMIT_50 ] ; then
    				echo 100 > $FAN_SPEED ;
    				CURRENT_FAN_SPEED=100 ;
    			else
    				if [ $CURRENT_FAN_SPEED -lt 50 ] ; then
    					echo 100 > $FAN_SPEED ;
    					sleep 2 ;
    				fi ;
    				echo 50 > $FAN_SPEED ;
    				CURRENT_FAN_SPEED=50 ;
    			fi ;
    		else
    			echo 0 > $FAN_SPEED ;
    			CURRENT_FAN_SPEED=0 ;
    		fi ;
    	fi ;
    
    	LOG_COUNTER=$((LOG_COUNTER+1));
    	if [[ $LOG_COUNTER -ge $LOG_INTERVAL ]] ; then
    		$LOG_ENABLED && ${LOGGER} "${TEMPERATURE_DISK1}${STATUS_DISK1} ${TEMPERATURE_DISK2}${STATUS_DISK2} ${CURRENT_FAN_SPEED}" ;
    		LOG_COUNTER=0 ;
    	fi ;
    	sleep 60 ;
    done
    
  4. Start the service.

    # /etc/init.d/S51temperature_monitor start

  5. You're done.

    If you want to tweak your settings, just edit the script and restart the service afterwards with

    # /etc/init.d/S51temperature_monitor restart
  6. As a bonus, if you turn on the logging you can build nice graphs of your drive temperature with the data (see example).

     

    A quick how-to on that:

    The output format of the temperature_monitor is like this:

    [disk 1 temperature][disk 1 status] [disk 2 temperature][disk 2 status] [current fan speed]

    In other words, when the disk 1 temperature is 42°C and the disk 2 temperature 47°C, both disks are up and the fan is spinning at 50% speed, the output is

    42u 47u 50

    In order to create a pretty graph with this, we'll just have to log the output, parse the log line by line and draw some stuff accordingly.

     

  7. If you're too lazy to do this on your own, here is my version:

    First, you'll need to have PHP installed, because I'm using it to parse the log and to create the graph.

     

    A good how-to on that can be found here. This link seems to be down at the moment. You can try the google cache version instead or follow these, almost identical instructions.

    All the guides on installing PHP on MBWE seem to be dead at the moment...

    Of course, you can also use the precompiled binaries from the optware feed, which is way faster (and smarter) than compiling stuff manually :)

    Because the other guides are down, I'll show you how to do it with optware. Or you can try to build it yourself from source.

    First, install optware if you haven't done so already. Then:

    # ipkg install php php-fcgi

    After installation is done, you'll have to edit your /etc/lighttpd/lighttpd.conf. First find this line:

    index-file.names = ( "nasMaster.pl" )

    And change it into this:

    index-file.names = ( "nasMaster.pl", "index.html", "index.php" )

    Then find this:

    "nasMaster.pl" => ((  "socket" => "/tmp/lighttpd.fcgi.socket",
      "check-local" => "disable",
    ))

    And change it into this:

    "nasMaster.pl" => ((  "socket" => "/tmp/lighttpd.fcgi.socket",
      "check-local" => "disable",
    )),
    ".php" => (( "bin-path" => "/opt/bin/php-fcgi",
      "socket" => "/tmp/php.socket",
    ))

    Note that if you built PHP from source, the path to php-fcgi (or php-cgi) will be different.

    Now restart lighttpd with

    # /etc/init.d/lighttpd.sh restart

    You can test PHP by creating a test script:

    # echo "<? phpinfo();?>" > /usr/www/lib/test.php

    ...and navigating with your web browser of choice to http://[path-to-your-mbwe]/test.php. The page should output information on PHP.

     

    We'll need to log timestamps with the temperatures. I have put together a small logging script to do just that and keep the log file small. Just download it and put it in /usr/sbin:

    # su
    # cd /usr/sbin
    # wget http://kyyhkynen.net/stuff/mybook/simple_logger
    # chmod 0700 simple_logger

     

    Then, set the temperature_monitorto use the logging script by editing these lines:

    LOG_ENABLED=false
    LOG_INTERVAL=5
    LOGGER=echo

    to

    LOG_ENABLED=true
    LOG_INTERVAL=5
    LOGGER="/usr/sbin/simple_logger /mnt/ramdisk/temperature.log 1000"

    This tells the simple_logger script to write the log to /mnt/ramdisk/temperature.log and keep the log file 1000 lines long.

    You can change the LOG_INTERVAL if you want to, it just tells the script how often (in minutes) the temperature should be logged.

    Remember to restart the temperature monitor after making changes in it by

    # /etc/init.d/S51temperature_monitor restart

    Wait a while for the log to get some entries :)

     

    Next, get the PHP script and put it in your web server's document root.

    Since I'm using the defult lighttpd, I'm putting it in /usr/www/lib/:

    # cd /usr/www/lib
    # wget http://kyyhkynen.net/stuff/mybook/temperature.phps
    # mv temperature.phps temperature.php

    Edit the PHP script and change the variables in the beginning to fit your purposes.

    Navigate to http://your-mbwe/temperature.php with your favorite web browser and enjoy.

    (Note: the output is tested to work properly only on IE7, Firefox 2.x and Safari 3.x; feel free to edit the css to make the output fit your purposes :)