Advertisement
Guest User

setup-armbian-monitoring-environment

a guest
Jan 18th, 2016
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.75 KB | None | 0 0
  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. # mounpoints an 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")
  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. DealWithRK3368
  111. ;;
  112. *)
  113. # No templates exist now. Combine sane defaults with some guessing
  114. DealWithNewBoard
  115. ;;
  116. esac
  117.  
  118. # Create the Armbian templates
  119. CreateTemplates
  120.  
  121. exit 0
  122.  
  123. # Decide depending on existence of /etc/armbianmonitor/start-monitoring
  124. ShouldMonitoringBeStarted
  125.  
  126. # Provide missing data in daemon mode
  127. LoopEndlessly
  128. } # Main
  129.  
  130. DealWithAXP209() {
  131. # check existence of sysfs nodes
  132. if [ -f /sys/devices/virtual/thermal/thermal_zone0/temp ]; then
  133. ln -fs /sys/devices/virtual/thermal/thermal_zone0/temp /etc/armbianmonitor/datasources/soctemp
  134. else
  135. if [ -L /etc/armbianmonitor/datasources/soctemp ]; then
  136. rm -f /etc/armbianmonitor/datasources/soctemp
  137. fi
  138. echo -n 25000 >/etc/armbianmonitor/datasources/soctemp
  139. export GetSoCTemp="${ARCH}-${ID}"
  140. fi
  141. if [ -d /sys/devices/platform/soc@01c00000/1c2ac00.i2c/i2c-0/0-0034 ]; then
  142. # mainline kernel and 'axp209 mainline sysfs interface' patch applied
  143. ln -fs /sys/power/axp_pmu/ac/voltage /etc/armbianmonitor/datasources/ac_voltage
  144. ln -fs /sys/power/axp_pmu/ac/amperage /etc/armbianmonitor/datasources/ac_current
  145. ln -fs /sys/power/axp_pmu/vbus/voltage /etc/armbianmonitor/datasources/usb_voltage
  146. ln -fs /sys/power/axp_pmu/vbus/amperage /etc/armbianmonitor/datasources/usb_current
  147. ln -fs /sys/power/axp_pmu/battery/voltage /etc/armbianmonitor/datasources/battery_voltage
  148. ln -fs /sys/power/axp_pmu/battery/amperage /etc/armbianmonitor/datasources/battery_current
  149. ln -fs /sys/power/axp_pmu/pmu/internal_temp /etc/armbianmonitor/datasources/pmictemp
  150. fi
  151. if [ -d /sys/devices/platform/sunxi-i2c.0/i2c-0/0-0034/axp20-supplyer.28/power_supply ]; then
  152. # sunxi 3.4 kernel
  153. SysFSPrefix=/sys/devices/platform/sunxi-i2c.0/i2c-0/0-0034/axp20-supplyer.28/power_supply
  154. ln -fs "${SysFSPrefix}"/ac/voltage_now /etc/armbianmonitor/datasources/ac_voltage
  155. ln -fs "${SysFSPrefix}"/ac/current_now /etc/armbianmonitor/datasources/ac_current
  156. ln -fs "${SysFSPrefix}"/usb/voltage_now /etc/armbianmonitor/datasources/usb_voltage
  157. ln -fs "${SysFSPrefix}"/usb/current_now /etc/armbianmonitor/datasources/usb_current
  158. ln -fs "${SysFSPrefix}"/battery/voltage_now /etc/armbianmonitor/datasources/battery_voltage
  159. ln -fs "${SysFSPrefix}"/battery/current_now /etc/armbianmonitor/datasources/battery_current
  160. ln -fs /sys/devices/platform/sunxi-i2c.0/i2c-0/0-0034/temp1_input /etc/armbianmonitor/datasources/pmictemp
  161. fi
  162. # relink template
  163. if [ "X${DebugMode}" = "XDEBUG" ]; then
  164. ln -sf /etc/armbianmonitor/templates/axp209_template_debug.conf /etc/armbianmonitor/cpu_pmic.conf
  165. else
  166. ln -sf /etc/armbianmonitor/templates/axp209_template.conf /etc/armbianmonitor/cpu_pmic.conf
  167. fi
  168. } # DealWithAXP209
  169.  
  170. DealWithNewBoard() {
  171. # check existence of sysfs nodes
  172. if [ -f /sys/devices/virtual/thermal/thermal_zone0/temp ]; then
  173. ln -fs /sys/devices/virtual/thermal/thermal_zone0/temp /etc/armbianmonitor/datasources/soctemp
  174. elif [ -f /sys/devices/virtual/thermal/thermal_zone1/temp ]; then
  175. ln -fs /sys/devices/virtual/thermal/thermal_zone1/temp /etc/armbianmonitor/datasources/soctemp
  176. fi
  177. # relink template
  178. if [ "X${DebugMode}" = "XDEBUG" ]; then
  179. ln -sf /etc/armbianmonitor/templates/unknown_board_template_debug.conf /etc/armbianmonitor/cpu_pmic.conf
  180. else
  181. ln -sf /etc/armbianmonitor/templates/unknown_board_template.conf /etc/armbianmonitor/cpu_pmic.conf
  182. fi
  183. } # DealWithNewBoard
  184.  
  185. CreateTemplates() {
  186. # check whether templates we write aren't symlinks to somewhere else
  187. for i in armbian.conf uptime.conf version.conf ; do
  188. if [ -L /etc/armbianmonitor/${i} ]; then
  189. rm /etc/armbianmonitor/${i}
  190. touch /etc/armbianmonitor/${i}
  191. chmod 711 /etc/armbianmonitor/${i}
  192. fi
  193. done
  194.  
  195. # check whether our logo is available
  196. if [ ! -f /usr/share/rpimonitor/web/img/armbian.png ]; then
  197. if [ -L /usr/share/rpimonitor/web/img/armbian.png ]; then
  198. rm /usr/share/rpimonitor/web/img/armbian.png
  199. fi
  200. cp -p /etc/armbianmonitor/templates/armbian.png /usr/share/rpimonitor/web/img/
  201. fi
  202.  
  203. # create main template
  204. echo "web.page.icon='img/armbian.png'
  205. web.page.menutitle='RPi-Monitor <sub>('+data.hostname+')</sub>'
  206. web.page.pagetitle='RPi-Monitor ('+data.hostname+')'
  207. web.status.1.name=$ID
  208. web.statistics.1.name=$ID
  209. web.addons.1.name=Addons
  210. web.addons.1.addons=about
  211. include=/etc/armbianmonitor/version.conf
  212. include=/etc/armbianmonitor/uptime.conf
  213. include=/etc/armbianmonitor/cpu_pmic.conf
  214. include=/etc/rpimonitor/template/memory.conf" >/etc/armbianmonitor/armbian.conf
  215.  
  216. # remove firmware line in version info:
  217. grep -v "Firmware" /etc/rpimonitor/template/version.conf | sed 's|line.5|line.4|' >/etc/armbianmonitor/version.conf
  218.  
  219. # uptime template with correct machine name:
  220. sed "s/Raspberry Pi/$ID/" < /etc/rpimonitor/template/uptime.conf >/etc/armbianmonitor/uptime.conf
  221.  
  222. # check swap settings. In case we're using swap then add template
  223. HowManySwapDevices=$(swapon -s | wc -l)
  224. if [[ ${HowManySwapDevices} -gt 1 ]]; then
  225. echo "include=/etc/rpimonitor/template/swap.conf" >>/etc/armbianmonitor/armbian.conf
  226. fi
  227.  
  228. echo "include=/etc/armbianmonitor/template/filesystems.conf
  229. include=/etc/armbianmonitor/template/disks.conf
  230. include=/etc/rpimonitor/template/network.conf" >>/etc/armbianmonitor/armbian.conf
  231.  
  232. UpdateFileSystems
  233. UpdateDisks
  234. } #
  235.  
  236. UpdateFileSystems() {
  237. # Generates /etc/armbianmonitor/template/filesystems.conf dynamically
  238. # based on the contents of /etc/armbianmonitoring/filesystems.conf
  239.  
  240. # if not existing, create config file with single entry for /
  241. if [ ! -f /etc/armbianmonitoring/filesystems.conf ]; then
  242. echo '/' >/etc/armbianmonitoring/filesystems.conf
  243. chmod 711 /etc/armbianmonitoring/filesystems.conf
  244. fi
  245.  
  246. # Update template:
  247.  
  248. } # UpdateFileSystems
  249.  
  250. UpdateDisks() {
  251. # Generates /etc/armbianmonitor/template/disks.conf dynamically
  252. # based on the contents of /etc/armbianmonitor/disks.conf
  253. :
  254. } # UpdateDisks
  255.  
  256. PreRequisits() {
  257. export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
  258. unset LANG
  259. LastDiskCheck=0
  260.  
  261. # we need the informations gathered from armhwinfo. In case we're not called
  262. # from there, get the variables on our own
  263. if [ "X${ID}" = "X" ]; then
  264. . /etc/init.d/armhwinfo start >/dev/null 2>&1
  265. fi
  266.  
  267. # check/create /etc/armbianmonitor and /etc/armbianmonitor/datasources
  268. if [ ! -d /etc/armbianmonitor ]; then
  269. mkdir -p -m 755 /etc/armbianmonitor
  270. else
  271. chmod 755 /etc/armbianmonitor
  272. fi
  273. if [ ! -d /etc/armbianmonitor/datasources ]; then
  274. mkdir -p -m 755 /etc/armbianmonitor/datasources
  275. else
  276. chmod 755 /etc/armbianmonitor/datasources
  277. fi
  278.  
  279. # check whether we should do debug monitoring or normal
  280. read DebugMode </etc/armbianmonitor/start-monitoring
  281. } # PreRequisits
  282.  
  283. Main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement