Advertisement
Guest User

sunxi-temp-daemon.sh

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