popufare_monitor 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/bin/bash
  2. #
  3. # Copyright (c) 2019 Clementine Computing LLC.
  4. #
  5. # This file is part of PopuFare.
  6. #
  7. # PopuFare is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # PopuFare is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with PopuFare. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. # This script expects to be called as root from a cron job.
  21. #
  22. # This script will do some basic monitoring of different pieces
  23. # of the system.
  24. # If a problem is discovered, this program will attemp to report
  25. # and, potentially, try to fix them.
  26. #
  27. VERBOSE=2
  28. bindir=/home/bus/bin
  29. cfgdir=/home/bus/config
  30. i2cdev=/dev/i2c-1
  31. piddir=/tmp
  32. ptydir=/tmp
  33. logdir=/tmp
  34. popmonlog=/tmp/popmon.log
  35. if [[ "$VERBOSE" -gt 0 ]] ; then
  36. ts=`date -u +'%s.%N'`
  37. echo "# $ts [$$] popufare_monitor running" >> $popmonlog
  38. fi
  39. function setup_PIU_socat() {
  40. if [[ -e $piddir/socat.pid ]] ; then
  41. _p=`cat $piddir/socat.pid`
  42. if [[ -e /proc/${_p} ]] ; then kill -9 $_p ; fi
  43. rm -f $piddir/socat.pid
  44. fi
  45. rm -f /dev/ttyPIU
  46. rm -f $ptydir/ttyPIUich
  47. if [[ "$VERBOSE" -gt 0 ]] ; then
  48. ts=`date -u +'%s.%N'`
  49. echo "# $ts [$$] popufare_monitor: starting socat" >> $popmonlog
  50. fi
  51. socat -d -d pty,raw,echo=0,link=$ptydir/ttyPIUich pty,raw,echo=0,link=/dev/ttyPIU > $logdir/socat.log 2>&1 &
  52. echo "$!" > $piddir/socat.pid
  53. }
  54. function setup_I2C_PIU_bridge() {
  55. if [[ -e $piddir/ichthyic.pid ]] ; then
  56. _p=`cat $piddir/ichthyic.pid`
  57. if [[ -e /proc/${_p} ]] ; then kill -9 $_p ; fi
  58. rm -f $piddir/ichthyic.pid
  59. fi
  60. if [[ "$VERBOSE" -gt 0 ]] ; then
  61. ts=`date -u +'%s.%N'`
  62. echo "# $ts [$$] popufare_monitor: starting ichthyic i2c to PIU bridge" >> $popmonlog
  63. fi
  64. $bindir/ichthyic-passthrough --i2c $i2cdev --pty $ptydir/ttyPIUich -v -v -L $logdir/ichthyic.log >> $popmonlog 2>&1 &
  65. echo "$!" > $piddir/ichthyic.pid
  66. }
  67. # If we have an appropriate drop file and I2C is enabled,
  68. # make sure socat is running and our shim program is running.
  69. #
  70. if [[ -e $cfgdir/popufare.ichthyic && -e $i2cdev ]] ; then
  71. ## make sure socat is running to tie ttyPIU to ttyPIUich
  72. ## together
  73. ##
  74. if [[ ! -e $piddir/socat.pid ]] ; then
  75. setup_PIU_socat
  76. fi
  77. p=`cat $piddir/socat.pid`
  78. if [[ ! -e /proc/${p} ]] ; then
  79. if [[ "$VERBOSE" -gt 1 ]] ; then
  80. ts=`date -u +'%s.%N'`
  81. echo "## $ts [$$] stale socat.pid file? no process id found for $p" >> $popmonlog
  82. fi
  83. setup_PIU_socat
  84. fi
  85. ## Now run the shim program to connect the I2C to the ttyPIUich line
  86. ##
  87. if [[ ! -e $piddir/ichthyic.pid ]] ; then
  88. setup_I2C_PIU_bridge
  89. fi
  90. p=`cat $piddir/ichthyic.pid`
  91. if [[ ! -e /proc/${p} ]] ; then
  92. if [[ "$VERBOSE" -gt 1 ]] ; then
  93. ts=`date -u +'%s.%N'`
  94. echo "## $ts [$$] stale ichthyic.pid file? no process id found for $p" >> $popmonlog
  95. fi
  96. setup_I2C_PIU_bridge
  97. fi
  98. fi
  99. if [[ "$VERBOSE" -gt 0 ]] ; then
  100. ts=`date -u +'%s.%N'`
  101. echo "# $ts [$$] popufare_monitor finished" >> $popmonlog
  102. fi
  103. exit 0