Advertisement
Guest User

lsmonitor

a guest
Aug 5th, 2010
1,556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.07 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. ### BEGIN INIT INFO
  4. # Provides:          lsmonitor
  5. # Required-Start:    $syslog
  6. # Required-Stop:     $syslog
  7. # Default-Start:     2 3 4 5
  8. # Default-Stop:      0 1 6
  9. # Short-Description: Monitor Linkstation LS-WXL
  10. # Description:       Enable service provided by daemon.
  11. ### END INIT INFO
  12.  
  13. #
  14. # lsmonitor:
  15. # - handle ls complete power-on
  16. # - monitor power switch
  17. # - monitor hdd temperature & control fan speed
  18. # - monitor function button (todo)
  19. #
  20. # blstools - Tools for Buffalo Linkstation
  21. # Copyright (C) 2010 Michele Manzato
  22. #
  23. # Credits:
  24. #   Thanks to archonfx on Buffalo NAS Central forum for HDD
  25. #   temperature monitoring command.
  26. #
  27. # Changelog:
  28. #   Modified to work with a Debian kernel on an LS-WXL
  29. #
  30. # This program is free software; you can redistribute it and/or modify
  31. # it under the terms of the GNU General Public License as published by
  32. # the Free Software Foundation; either version 2 of the License, or
  33. # (at your option) any later version.
  34. #
  35. # This program is distributed in the hope that it will be useful,
  36. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  38. # GNU General Public License for more details.
  39. #
  40. # You should have received a copy of the GNU General Public License along
  41. # with this program; if not, write to the Free Software Foundation, Inc.,
  42. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. #
  44.  
  45. # Load settings
  46. . /etc/default/lsmonitor
  47.  
  48. # Location of pid file
  49. PIDFILE=/var/run/lsmonitor_daemon.pid
  50.  
  51. # GPIO's for LS-WXL
  52. GPIO_PWR_SW=42      # Power switch
  53. GPIO_AUT_SW=43      # Auto-Power switch
  54. GPIO_FAN_STAT=40    # Fan low speed enable
  55. GPIO_FAN_LO=48      # Fan low speed enable
  56. GPIO_FAN_HI=47      # Fan high speed enable
  57.  
  58. gpio_config()
  59. {
  60.     if [ $1 == "enable" ]; then
  61.         [ -d /sys/class/gpio/gpio$2 ] || echo $2 > /sys/class/gpio/export
  62.         if [ $3 == "output" ]; then
  63.             echo out > /sys/class/gpio/gpio$2/direction
  64.         else
  65.             echo in > /sys/class/gpio/gpio$2/direction
  66.         fi
  67.     else
  68.         echo $2 > /sys/class/gpio/unexport
  69.     fi
  70. }
  71.  
  72. enable_gpio()
  73. {
  74.     gpio_config enable ${GPIO_PWR_SW} input
  75.     gpio_config enable ${GPIO_AUT_SW} input
  76.     gpio_config enable ${GPIO_FAN_STAT} input
  77.     gpio_config enable ${GPIO_FAN_LO} output
  78.     gpio_config enable ${GPIO_FAN_HI} output
  79. }
  80.  
  81. disable_gpio()
  82. {
  83.     gpio_config disable ${GPIO_PWR_SW}
  84.     gpio_config disable ${GPIO_AUT_SW}
  85.     gpio_config disable ${GPIO_FAN_STAT}
  86.     gpio_config disable ${GPIO_FAN_LO}
  87.     gpio_config disable ${GPIO_FAN_HI}
  88. }
  89.  
  90.  
  91. # Monitor HDD temperature & control fan speed
  92. monitor_temperature()
  93. {
  94.     HDDTEMP1=0
  95.     HDDTEMP2=0
  96.    
  97.     # Retrieve HDD temp
  98.     [ -b /dev/sda ] && HDDTEMP1=$(smartctl /dev/sda --all | awk '$1 == "194" {print $10}')
  99.     [ -b /dev/sdb ] && HDDTEMP2=$(smartctl /dev/sdb --all | awk '$1 == "194" {print $10}')
  100.    
  101.     # Get max temp
  102.     if [ $HDDTEMP1 -gt $HDDTEMP2 ]; then
  103.         HDDTEMP=$HDDTEMP1
  104.     else
  105.         HDDTEMP=$HDDTEMP2
  106.     fi
  107.  
  108.     # Change fan speed accordingly
  109.     if [ $HDDTEMP -le $HDDTEMP0 ] ; then
  110.         # off
  111.         echo 1 > /sys/class/gpio/gpio${GPIO_FAN_LO}/value
  112.         echo 1 > /sys/class/gpio/gpio${GPIO_FAN_HI}/value
  113.     elif [ $HDDTEMP -le $HDDTEMP1 ] ; then
  114.         # slow
  115.         echo 0 > /sys/class/gpio/gpio${GPIO_FAN_LO}/value
  116.         echo 1 > /sys/class/gpio/gpio${GPIO_FAN_HI}/value
  117.     elif [ $HDDTEMP -le $HDDTEMP2 ] ; then
  118.         # medium
  119.         echo 1 > /sys/class/gpio/gpio${GPIO_FAN_LO}/value
  120.         echo 0 > /sys/class/gpio/gpio${GPIO_FAN_HI}/value
  121.     else
  122.         # fast
  123.         echo 0 > /sys/class/gpio/gpio${GPIO_FAN_LO}/value
  124.         echo 0 > /sys/class/gpio/gpio${GPIO_FAN_HI}/value
  125.     fi
  126. }
  127.  
  128.  
  129. # Control LS switch status to power down the unit
  130. lsmonitor_daemon()
  131. {
  132.     COUNT=20
  133.     while [ true ] ; do
  134.         # Check switch status
  135.         PWR_SW=`cat /sys/class/gpio/gpio${GPIO_PWR_SW}/value`
  136.         AUT_SW=`cat /sys/class/gpio/gpio${GPIO_AUT_SW}/value`
  137.  
  138.         # Terminate when in OFF state
  139.         if [ "$PWR_SW" -eq 1 ] && [ "$AUT_SW" -eq 1 ]; then
  140.             break
  141.         fi
  142.  
  143.         # Once per minute monitor HDD temperature
  144.         if [ $COUNT -eq 20 ] ; then
  145.             COUNT=0
  146.             monitor_temperature
  147.         else
  148.             COUNT=$(( $COUNT + 1 ))
  149.         fi
  150.  
  151.         sleep 3
  152.        
  153.     done
  154.  
  155.     # Run the fan at low speed while halting, just in case halt hangs the unit
  156.     echo 0 > /sys/class/gpio/gpio${GPIO_FAN_LO}/value
  157.     echo 1 > /sys/class/gpio/gpio${GPIO_FAN_HI}/value
  158.  
  159.     # blink power led
  160.     echo timer > /sys/devices/platform/leds-gpio/leds/power/trigger
  161.     echo   100 > /sys/devices/platform/leds-gpio/leds/power/delay_on
  162.     echo   100 > /sys/devices/platform/leds-gpio/leds/power/delay_off
  163.    
  164.     # Initiate unit shutdown
  165.     halt
  166. }
  167.  
  168. # Kill the lsmonitor daemon
  169. kill_lsmonitor_daemon()
  170. {
  171.         PID=`cat $PIDFILE`
  172.     if [ "$PID" != "" ] ; then
  173.             kill $PID
  174.         rm $PIDFILE
  175.     fi
  176. }
  177.  
  178.  
  179. case $1 in
  180.     start)
  181.         # Enable the corresponding GPIO's
  182.         enable_gpio
  183.        
  184.         # Start the lsmonitor daemon
  185.         lsmonitor_daemon &
  186.             echo $! > $PIDFILE
  187.         ;;
  188.     stop)
  189.         # Kill the lsmonitor daemon
  190.         kill_lsmonitor_daemon
  191.  
  192.         # Disable the corresponding GPIO's
  193.         disable_gpio
  194.         ;;
  195.  
  196.     restart|force-reload)
  197.         $0 stop && sleep 2 && $0 start
  198.         ;;
  199.  
  200.     *)
  201.         echo "Usage: $0 {start|stop|restart|force-reload}"
  202.         exit 2
  203.         ;;
  204. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement