SHARE
TWEET

setup-armbian-monitoring-environment

a guest Jan 28th, 2016 233 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. #
  3. # setup-armbian-monitoring-environment
  4. #
  5. # This script should be called from armhwinfo (relying on the
  6. # following exported variables -- if not called from armhwinfo
  7. # these should be defined/exported the same way armhwinfo does):
  8. #
  9. # export HARDWARE DISTROID DISTROCODE ARCH KERNELID MACHINE ID
  10. #
  11. # The purpose is to create an environment for rpimonitord so that
  12. # relevant hardware informations can be monitored easily. This
  13. # script starts uses configuration files in /etc/armbianmonitor/,
  14. # collects data sources below /etc/armbianmonitor/datasources/ and
  15. # adjusts templates for RPi-Monitor on the fly. Only if the file
  16. # /etc/armbianmonitor/start-monitoring exists this script relinks
  17. # /etc/rpimonitor/data.conf and starts rpimonitord if not already
  18. # running.
  19. #
  20. # In case the script detects that not all necessary data sources
  21. # are available through sysfs it will act as a daemon to collect
  22. # data on its own and to write it to the approriate file. As an
  23. # example the SoC's temperature: Running on an AXP209 based board
  24. # the script will check /sys/class/thermal/thermal_zone0/temp and
  25. # if existing link it to /etc/armbianmonitor/datasources/soctemp.
  26. # In case it does not exist the script will create a normal file
  27. # at this location and writes the thermal value to it using the
  28. # sunxi_axp209_temp binary in an endless loop.
  29. #
  30. # At least the following files/symlinks will be provided below
  31. # /etc/armbianmonitor/datasources/ depending on SoC/PMIC in question:
  32. #
  33. # soctemp (SoC's internal temp in degree Celsius * 1000)
  34. # pmictemp (PMIC's internal temp in degree Celsius * 1000)
  35. # ac_voltage (DC-IN voltage in V * 1000000)
  36. # usb_voltage (USB OTG voltage in V * 1000000)
  37. # battery_voltage (battery voltage in V * 1000000)
  38. # ac_current (DC-IN current in A * 1000000)
  39. # usb_current (USB OTG current in A * 1000000)
  40. # battery_current (battery current in A * 1000000)
  41. #
  42. # When extended debugging has been chosen (using _armbian-monitoring_
  43. # this can be configured. Then /etc/armbianmonitor/start-monitoring
  44. # contains DEBUG) this script will also provide a few more files:
  45. #
  46. # cpustat (cpu_stat,system_stat,user_stat,nice_stat,iowait_stat,irq_stat
  47. #          collected through /proc/cpustat in daemon mode)
  48. # cpu_count (number of active CPU cores)
  49. # vcorevoltage (Vcore in V * 1000 based on sysfs or script.bin/.dtb)
  50. #
  51. # Disk monitoring: For configured disks the following parameters can be
  52. # monitored: temperature, S.M.A.R.T. health, load cycle count and CRC
  53. # errors indicating connection/cable problems. The config file used is
  54. # /etc/armbianmonitoring/disks.conf
  55. #
  56. # Filesystem monitoring: In /etc/armbianmonitoring/filesystems.conf
  57. # mountpoints and trigger values can be defined that will be used to
  58. # create the template stuff for these fs at startup of this script.
  59. #
  60. # The behaviour of this script can be configured through another tool
  61. # called armbian-monitoring. The latter will also check for connected
  62. # disks, get their name and GUID and let the user choose whether the
  63. # disk should be monitored or not. The information has to be stored in
  64. # /etc/armbianmonitor/disks.conf relying on the GUIDs of the disks.
  65.  
  66.  # define some variables:
  67. CheckInterval=7.5      # time in seconds between two checks
  68. DiskCheckInterval=60   # time in seconds between disk checks
  69.  
  70. Main() {
  71.     PreRequisits
  72.    
  73.     case ${ID} in
  74.         Cubieboard|Cubietruck|Orange|"Lamobo R1"|"Lime 2"|Lime|Banana|Micro)
  75.             DealWithAXP209
  76.             ;;
  77.         "Banana M2")
  78.             DealWithNewBoard
  79.             # DealWithAXP221
  80.             ;;
  81.         "Cubietruck Plus"|"Banana Pi M3"|"pcDuino8 Uno")
  82.             DealWithNewBoard
  83.             # DealWithAXP818
  84.             ;;
  85.         Guitar|"Roseapple Pi")
  86.             DealWithNewBoard
  87.             # DealWithS500
  88.             ;;
  89.         "Odroid XU4"|"Odroid XU3")
  90.             DealWithNewBoard
  91.             # DealWithExynos4
  92.             ;;
  93.         "Odroid C1")
  94.             DealWithNewBoard
  95.             # DealWithS805
  96.             ;;
  97.         Clearfog|"Turris Omnia")
  98.             DealWithNewBoard
  99.             # DealWithArmada38x
  100.             ;;
  101.         "Cubox i4"|"HB i2eX"|"Cubox i2eX"|"HB i1"|"HB i2"|"Wandboard")
  102.             DealWithNewBoard
  103.             # DealWithiMX6
  104.             ;;
  105.         "Orange Pi PC"|"Orange Pi Plus"|"Orange Pi 2"|"Orange Pi One"|"Orange Pi Lite")
  106.             DealWithNewBoard
  107.             # DealWithH3
  108.             ;;
  109.         Geekbox)
  110.             DealWithNewBoard
  111.             # DealWithRK3368
  112.             ;;
  113.         *)
  114.             # No templates exist now. Combine sane defaults with some guessing
  115.             DealWithNewBoard
  116.             ;;
  117.     esac
  118.    
  119.     # Create the Armbian templates
  120.     CreateTemplates
  121.  
  122.     exit 0
  123.    
  124.     # Decide depending on existence of /etc/armbianmonitor/start-monitoring
  125.     ShouldMonitoringBeStarted
  126.    
  127.     # Provide missing data in daemon mode
  128.     LoopEndlessly
  129. } # Main
  130.  
  131. DealWithAXP209() {
  132.     # check existence of sysfs nodes
  133.     if [ -f /sys/devices/virtual/thermal/thermal_zone0/temp ]; then
  134.         ln -fs /sys/devices/virtual/thermal/thermal_zone0/temp /etc/armbianmonitor/datasources/soctemp
  135.     else
  136.         if [ -L /etc/armbianmonitor/datasources/soctemp ]; then
  137.             rm -f /etc/armbianmonitor/datasources/soctemp
  138.         fi
  139.         echo -n 25000 >/etc/armbianmonitor/datasources/soctemp
  140.         export GetSoCTemp="${ARCH}-${ID}"
  141.     fi
  142.     if [ -d /sys/devices/platform/soc@01c00000/1c2ac00.i2c/i2c-0/0-0034 ]; then
  143.         # mainline kernel and 'axp209 mainline sysfs interface' patch applied
  144.         ln -fs /sys/power/axp_pmu/ac/voltage /etc/armbianmonitor/datasources/ac_voltage
  145.         ln -fs /sys/power/axp_pmu/ac/amperage /etc/armbianmonitor/datasources/ac_current
  146.         ln -fs /sys/power/axp_pmu/vbus/voltage /etc/armbianmonitor/datasources/usb_voltage
  147.         ln -fs /sys/power/axp_pmu/vbus/amperage /etc/armbianmonitor/datasources/usb_current
  148.         ln -fs /sys/power/axp_pmu/battery/voltage /etc/armbianmonitor/datasources/battery_voltage
  149.         ln -fs /sys/power/axp_pmu/battery/amperage /etc/armbianmonitor/datasources/battery_current
  150.         ln -fs /sys/power/axp_pmu/pmu/temp /etc/armbianmonitor/datasources/pmictemp
  151.         ln -fs /sys/power/axp_pmu/battery/capacity /etc/armbianmonitor/datasources/battery_percent
  152.         ln -fs /sys/power/axp_pmu/battery/charging /etc/armbianmonitor/datasources/battery_charging
  153.         ln -fs /sys/power/axp_pmu/charger/amperage /etc/armbianmonitor/datasources/charger_current
  154.         ln -fs /sys/power/axp_pmu/battery/connected /etc/armbianmonitor/datasources/battery_connected
  155.     fi
  156.     if [ -d /sys/devices/platform/sunxi-i2c.0/i2c-0/0-0034/axp20-supplyer.28/power_supply ]; then
  157.         # sunxi 3.4 kernel
  158.         SysFSPrefix=/sys/devices/platform/sunxi-i2c.0/i2c-0/0-0034/axp20-supplyer.28/power_supply
  159.         ln -fs "${SysFSPrefix}"/ac/voltage_now /etc/armbianmonitor/datasources/ac_voltage
  160.         ln -fs "${SysFSPrefix}"/ac/current_now /etc/armbianmonitor/datasources/ac_current
  161.         ln -fs "${SysFSPrefix}"/usb/voltage_now /etc/armbianmonitor/datasources/usb_voltage
  162.         ln -fs "${SysFSPrefix}"/usb/current_now /etc/armbianmonitor/datasources/usb_current
  163.         ln -fs "${SysFSPrefix}"/battery/voltage_now /etc/armbianmonitor/datasources/battery_voltage
  164.         ln -fs "${SysFSPrefix}"/battery/current_now /etc/armbianmonitor/datasources/battery_current
  165.         ln -fs /sys/devices/platform/sunxi-i2c.0/i2c-0/0-0034/temp1_input /etc/armbianmonitor/datasources/pmictemp
  166.         ln -fs /sys/class/power_supply/battery/capacity /etc/armbianmonitor/datasources/battery_percent
  167.         ln -fs /sys/class/power_supply/battery/charging /etc/armbianmonitor/datasources/battery_charging
  168.         ln -fs /sys/class/power_supply/battery/connected /etc/armbianmonitor/datasources/battery_connected
  169.     fi
  170.     # relink template
  171.     if [ "X${DebugMode}" = "XDEBUG" ]; then
  172.         ln -sf /etc/armbianmonitor/templates/axp209_template_debug.conf /etc/armbianmonitor/templates/cpu_pmic.conf
  173.     else
  174.         ln -sf /etc/armbianmonitor/templates/axp209_template.conf /etc/armbianmonitor/templates/cpu_pmic.conf
  175.     fi
  176. } # DealWithAXP209
  177.  
  178. DealWithNewBoard() {
  179.     # check existence of sysfs nodes
  180.     if [ -f /sys/devices/virtual/thermal/thermal_zone0/temp ]; then
  181.         ln -fs /sys/devices/virtual/thermal/thermal_zone0/temp /etc/armbianmonitor/datasources/soctemp
  182.     elif [ -f /sys/devices/virtual/thermal/thermal_zone1/temp ]; then
  183.         ln -fs /sys/devices/virtual/thermal/thermal_zone1/temp /etc/armbianmonitor/datasources/soctemp
  184.     fi
  185.     # relink template
  186.     if [ "X${DebugMode}" = "XDEBUG" ]; then
  187.         ln -sf /etc/armbianmonitor/templates/unknown_board_template_debug.conf /etc/armbianmonitor/templates/cpu_pmic.conf
  188.     else
  189.         ln -sf /etc/armbianmonitor/templates/unknown_board_template.conf /etc/armbianmonitor/templates/cpu_pmic.conf
  190.     fi
  191. } # DealWithNewBoard
  192.  
  193. CreateTemplates() {
  194.     # check whether templates we write aren't symlinks to somewhere else
  195.     for i in armbian.conf uptime.conf version.conf ; do
  196.         if [ -L /etc/armbianmonitor/templates/${i} ]; then
  197.             rm /etc/armbianmonitor/templates/${i}
  198.             touch /etc/armbianmonitor/templates/${i}
  199.             chmod 711 /etc/armbianmonitor/templates/${i}
  200.         fi
  201.     done
  202.  
  203.     # check whether our logo is available
  204.     if [ ! -f /usr/share/rpimonitor/web/img/armbian.png ]; then
  205.         if [ -L /usr/share/rpimonitor/web/img/armbian.png ]; then
  206.             rm /usr/share/rpimonitor/web/img/armbian.png
  207.         fi
  208.         cp -p /etc/armbianmonitor/templates/armbian.png /usr/share/rpimonitor/web/img/
  209.     fi
  210.    
  211.     # create main template
  212.     echo "web.page.icon='img/armbian.png'
  213. web.page.menutitle='RPi-Monitor  <sub>('+data.hostname+')</sub>'
  214. web.page.pagetitle='RPi-Monitor ('+data.hostname+')'
  215. web.status.1.name=$ID
  216. web.statistics.1.name=$ID
  217. web.addons.1.name=Addons
  218. web.addons.1.addons=about
  219. include=/etc/armbianmonitor/templates/version.conf
  220. include=/etc/armbianmonitor/templates/uptime.conf
  221. include=/etc/armbianmonitor/templates/cpu_pmic.conf
  222. include=/etc/rpimonitor/template/memory.conf" >/etc/armbianmonitor/armbian.conf
  223.    
  224.     # remove firmware line in version info:
  225.     grep -v "Firmware" /etc/rpimonitor/template/version.conf | sed 's|line.5|line.4|' >/etc/armbianmonitor/templates/version.conf  
  226.    
  227.     # uptime template with correct machine name:
  228.     sed "s/Raspberry Pi/$ID/" < /etc/rpimonitor/template/uptime.conf >/etc/armbianmonitor/templates/uptime.conf
  229.  
  230.     # check swap settings. In case we're using swap then add template
  231.     HowManySwapDevices=$(swapon -s | wc -l)
  232.     if [[ ${HowManySwapDevices} -gt 1 ]]; then
  233.         echo "include=/etc/rpimonitor/template/swap.conf" >>/etc/armbianmonitor/armbian.conf
  234.     fi
  235.    
  236.     echo "include=/etc/armbianmonitor/template/filesystems.conf
  237. include=/etc/armbianmonitor/template/disks.conf
  238. include=/etc/rpimonitor/template/network.conf" >>/etc/armbianmonitor/armbian.conf
  239.  
  240.     UpdateFileSystems
  241.     UpdateDisks
  242. } #
  243.  
  244. UpdateFileSystems() {
  245.     # Generates /etc/armbianmonitor/template/filesystems.conf dynamically
  246.     # based on the contents of /etc/armbianmonitoring/filesystems.conf
  247.    
  248.     # if not existing, create config file with single entry for /
  249.     if [ ! -f /etc/armbianmonitoring/filesystems.conf ]; then
  250.         echo '/' >/etc/armbianmonitoring/filesystems.conf
  251.         chmod 711 /etc/armbianmonitoring/filesystems.conf
  252.     fi
  253.    
  254.     # Update template:
  255.    
  256. } # UpdateFileSystems
  257.  
  258. UpdateDisks() {
  259.     # Generates /etc/armbianmonitor/template/disks.conf dynamically
  260.     # based on the contents of /etc/armbianmonitor/disks.conf. The
  261.     # current mapping between GUIDs and /dev/sd* nodes will be stored
  262.     # in /etc/armbianmonitor/datasources/disk-by-guid
  263.    
  264.     # ensure lookup file is empty:
  265.     if [ -L /etc/armbianmonitor/datasources/disk-by-guid ]; then
  266.         rm -f /etc/armbianmonitor/datasources/disk-by-guid
  267.     fi 
  268.     echo -n "" >/etc/armbianmonitor/datasources/disk-by-guid
  269.     chmod 711 /etc/armbianmonitor/datasources/disk-by-guid
  270.    
  271.     OIFS=${IFS}
  272.     IFS=:
  273.     cat /etc/armbianmonitor/disks.conf | while read ; do
  274.         IFS=:
  275.         set ${REPLY}
  276.         GUID="$1"
  277.         DiskName="$2"
  278.         SMARTPrefix="$3"
  279.         TempCommand="$4"
  280.         CRCAttribute="$5"
  281.         LCCAttribute="$6"
  282.         IFS=${OIFS}
  283.        
  284.         # try to resolve device node (when not present, then disk's
  285.         # not mounted -- we then create the entry without device node)
  286.         DeviceNode="$(ResolveGUID ${GUID})"
  287.         echo -e "${GUID}\t${DiskName}\t${DeviceNode}" >>/etc/armbianmonitor/datasources/disk-by-guid
  288.  
  289.         # create template stuff for every listed disk
  290.         :
  291.     done
  292.     IFS=${OIFS}
  293. } # UpdateDisks
  294.  
  295. ResolveGUID() {
  296.     # function that will be supplied with a GUID and returns the device node, eg.
  297.     # translating 637B7677-18E7-4C7B-8DF3-CFED96EA55C3 to /dev/sdb
  298.  
  299.     # check whether disks are existent
  300.     ls /sys/block/sd* >/dev/null 2>&1 || return
  301.    
  302.     for i in /sys/block/sd* ; do
  303.         DeviceNode=/dev/${i##*/}
  304.         GUID=$(gdisk -l ${DeviceNode} | awk -F" " '/^Disk identifier/ {print $4}')
  305.         if [ "X${GUID}" = "X${1}" ]; then
  306.             echo -n ${DeviceNode}
  307.             break
  308.         fi
  309.     done
  310. } # GetGUIDforDisk
  311.  
  312. PreRequisits() {
  313.     export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
  314.     unset LANG
  315.     LastDiskCheck=0
  316.  
  317.     # we need the informations gathered from armhwinfo. In case we're not called
  318.     # from there, get the variables on our own
  319.     if [ "X${ID}" = "X" ]; then
  320.         . /etc/init.d/armhwinfo start >/dev/null 2>&1
  321.     fi
  322.    
  323.     # check/create /etc/armbianmonitor and /etc/armbianmonitor/datasources
  324.     if [ ! -d /etc/armbianmonitor ]; then
  325.         mkdir -p -m 755 /etc/armbianmonitor
  326.     else
  327.         chmod 755 /etc/armbianmonitor
  328.     fi
  329.     if [ ! -d /etc/armbianmonitor/datasources ]; then
  330.         mkdir -p -m 755 /etc/armbianmonitor/datasources
  331.     else
  332.         chmod 755 /etc/armbianmonitor/datasources
  333.     fi
  334.    
  335.     # check whether we should do debug monitoring or normal
  336.     read DebugMode </etc/armbianmonitor/start-monitoring
  337. } # PreRequisits
  338.  
  339. Main
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top