Advertisement
Guest User

sunxi-temp-daemon.sh

a guest
Jul 30th, 2015
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.17 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # A20/AXP209 SoC/HDD/PMU temperature daemon. Writes the current temperatures
  4. # to /tmp/soctemp, /tmp/pmutemp and /tmp/disktemp (since we're experiencing
  5. # always timeouts under heavy load when trying to get the temperatures
  6. # directly from within RPi-Monitor.
  7. #
  8. # Copyright 2015 - Thomas Kaiser - http://kaiser-edv.de/
  9. #
  10. # This program is free software: you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation, either version 3 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License
  21. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  22.  
  23. SoCTempAdjustment=1447 # default for A20
  24. CheckInterval=5        # time in seconds between two checks
  25. DiskCheckInterval=60   # time in seconds between disk checks
  26. CheckAllDisks=FALSE    # if set to anything else than FALSE you've to adjust
  27.                        # your data collection settings: both name and regexp. And
  28.                        # display/graph settings should also match the count of
  29.                        # disks since the values for all found disks will be written
  30.                        # space delimited to the temp file. With 4 disks you might
  31.                        # use
  32.                        # dynamic.12.name=hddtemp1,hddtemp2,hddtemp3,hddtemp4
  33.                        # dynamic.12.source=/tmp/disktemp
  34.                        # dynamic.12.regexp=^(\S+)\s(\S+)\s(\S+)\s(\S+)
  35.  
  36. export PATH=/usr/local/bin:/usr/bin:/bin:
  37. unset LANG
  38. LastDiskCheck=0
  39.  
  40. Main() {
  41.     # Ensure that we're running as root since otherwise querying SATA/USB disks won't work
  42.     if [ "$(id -u)" != "0" ]; then
  43.         echo "This script must be run as root" >&2
  44.         exit 1
  45.     fi
  46.    
  47.     # ensure we're writing to files instead of symlinks
  48.     CreateTempDir
  49.    
  50.     while true ; do
  51.         # check disk temperature(s). We execute this only every ${DiskCheckInterval} since it's
  52.         # a bit costly (S.M.A.R.T. queries). We check either /dev/sda or all available block
  53.         # devices -- see above for contents/consequences of $CheckAllDisks
  54.         TimeNow=$(( $(date "+%s") / ${DiskCheckInterval} ))
  55.         if [[ ${TimeNow} -gt ${LastDiskCheck} ]]; then
  56.             # time for a disk check. If ${CheckAllDisks} is FALSE and /dev/sda exists we
  57.             # only query this device otherwise all available (might be none)
  58.             if [ "X${CheckAllDisks}" = "XFALSE" -a -L /sys/block/sda ]; then
  59.                 DiskTemp=$(GetDiskTemp /dev/sda)
  60.                 SanitizeValue ${DiskTemp} >/tmp/disktemp
  61.             else
  62.                 DiskTemp=""
  63.                 for diskdevice in /sys/block/sd* ; do
  64.                     RawDiskTemp=$(GetDiskTemp /dev/${diskdevice##*/})
  65.                     DiskTemp="${DiskTemp}$(SanitizeValue ${RawDiskTemp}) "
  66.                 done
  67.                 echo "${DiskTemp}" >/tmp/disktemp
  68.             fi
  69.             # update check timestamp
  70.             LastDiskCheck=${TimeNow}
  71.         fi
  72.        
  73.         # Soc and PMU temp -- depends on the kernel we're running
  74.         case $(uname -r) in
  75.             3.4.*)
  76.                 if [ -x /usr/share/rpimonitor/scripts/sunxi_tp_temp ]; then
  77.                     SoCTemp=$(/usr/share/rpimonitor/scripts/sunxi_tp_temp ${SoCTempAdjustment} | awk '{printf ("%0.0f",$1*1000); }')
  78.                 fi
  79.                 read PMUTemp </sys/devices/platform/sunxi-i2c.0/i2c-0/0-0034/temp1_input
  80.                 ;;
  81.             4.*)
  82.                 # mainline kernel 4.0 or above, SoC temp should be available
  83.                 read SoCTemp </sys/class/thermal/thermal_zone0/temp
  84.         esac
  85.        
  86.         # check whether PMU value could be read before
  87.         if [ "X${PMUTemp}" = "X" ]; then
  88.             # Maybe the patches from http://sunxi.montjoie.ovh are applied and lm-sensors installed
  89.             PMUTemp=$(sensors | awk -F" " '/CHIP: / {printf ("%0.0f",$2*1000); }')
  90.         fi
  91.         SanitizeValue ${SoCTemp} >/tmp/soctemp
  92.         SanitizeValue ${PMUTemp} >/tmp/pmutemp
  93.         sleep ${CheckInterval}
  94.     done
  95. } # Main
  96.  
  97. GetDiskTemp() {
  98.     # get disk temperate using hddtemp (doesn't wake up sleeping disks). The commented
  99.     # smartctl call is for USB disks in external enclosures that are able to answer
  100.     # S.M.A.R.T. queries since they're SAT capable:
  101.     # /usr/sbin/smartctl -d sat -a ${1} | awk -F" " '/Temperature_Cel/ {printf ("%0.0f",$10*1000); }'
  102.     /usr/sbin/hddtemp -n ${1} 2>/dev/null | awk '{printf ("%0.0f",$1*1000); }'
  103. } # GetDiskTemp
  104.  
  105. SanitizeValue() {
  106.     # keep thermal values in the range of 0°C-100°C
  107.     if [[ ${1} -lt 0 ]]; then
  108.         echo -n 0
  109.     elif [[ ${1} -gt 100000 ]]; then
  110.         echo -n 100000
  111.     else
  112.         echo -n ${1}
  113.     fi
  114. } # SanitizeValue
  115.  
  116. CreateTempDir() {
  117.     # create a safe temporary dir with the three files and symlinks to them below /tmp/
  118.     MyTempDir=$(mktemp -d /tmp/rpimonitor.XXXXXX)
  119.     if [ ! -d "${MyTempDir}" ]; then
  120.         MyTempDir=/tmp/rpimonitor.$RANDOM.$RANDOM.$RANDOM.$$
  121.         (umask 077 && mkdir ${MyTempDir}) || (echo "Failed to create temp dir. Aborting" >&2 ; exit 1)
  122.     fi
  123.     for file in soctemp disktemp pmutemp ; do
  124.         touch "${MyTempDir}/${file}"
  125.         chmod 644 "${MyTempDir}/${file}"
  126.         chown root "${MyTempDir}/${file}"
  127.         ln -f -s ${MyTempDir}/${file} /tmp/${file}
  128.     done
  129.     chmod 711 "${MyTempDir}"
  130. } #CreateTempFiles
  131.  
  132. Main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement