synfigstudio-cph-monitor 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/bin/bash
  2. # Synfig Crash Monitor script
  3. # Copyright (c) 2009-2010 Konstantin Dmitriev
  4. #
  5. # This package is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License as
  7. # published by the Free Software Foundation; either version 2 of
  8. # the License, or (at your option) any later version.
  9. #
  10. # This package is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # General Public License for more details.
  14. set -e
  15. trap writelog INT
  16. init()
  17. {
  18. echo "SynfigStudio Crash Monitor is a tool to collect statistics about synfig crashes."
  19. echo "All information is collected locally in ~/.synfig/cph directory."
  20. echo
  21. STARTED=0
  22. RUNTIME=0
  23. VERSION=''
  24. RELEASE=''
  25. BRANCH=''
  26. REVISION_ID=''
  27. CRASH=0
  28. [ ! -d ~/.synfig/cph ] && mkdir -p ~/.synfig/cph || true
  29. # Detect if crash monitor is already started
  30. PDIR=${0%`basename $0`}
  31. LCK_FILE=~/.synfig/cph/`basename $0`.lck
  32. if [ -f "${LCK_FILE}" ]; then
  33. MYPID=`head -n 1 "${LCK_FILE}"`
  34. if ! ( ps -p ${MYPID} | grep ${MYPID} >/dev/null ); then
  35. # The process is not running
  36. # Echo current PID into lock file
  37. echo $$ > "${LCK_FILE}"
  38. else
  39. echo "`basename $0` is already running [${MYPID}]. Aborting."
  40. sleep 5
  41. exit 0
  42. fi
  43. else
  44. # The process is not running
  45. # Echo current PID into lock file
  46. echo $$ > "${LCK_FILE}"
  47. fi
  48. echo `date +%H:%M` "Synfig Crash Monitor started."
  49. }
  50. writelog()
  51. {
  52. if [[ $STARTED != 0 ]]; then
  53. if [[ $CRASH == 0 ]]; then
  54. echo `date +%H:%M` "Synfig exited normally. Session time: $RUNTIME."
  55. else
  56. echo `date +%H:%M` "Crash detected. Version $VERSION.$RELEASE.$BRANCH, session time: $RUNTIME."
  57. fi
  58. if [ -e ~/.synfig/cph/log ]; then
  59. #check if dump needed
  60. CURRENTDATE=`date +%Y-%m-%d`
  61. LOGMODDATE=`stat -c %y ~/.synfig/cph/log`
  62. LOGMODDATE=${LOGMODDATE%% *}
  63. if [[ $LOGMODDATE != $CURRENTDATE ]]; then
  64. dumpstats
  65. fi
  66. fi
  67. #write log
  68. echo $VERSION/$BRANCH/$RELEASE/$REVISION_ID $RUNTIME $CRASH >> ~/.synfig/cph/log
  69. CRASH=0
  70. RUNTIME=0
  71. else
  72. echo
  73. fi
  74. }
  75. dumpstats()
  76. {
  77. echo `date +%H:%M` 'Dumping stats for previous session...'
  78. LOGMODDATE=`stat -c %y ~/.synfig/cph/log`
  79. LOGMODDATE=${LOGMODDATE%% *}
  80. #get versions
  81. VERSIONS=''
  82. while read LINE; do
  83. FOUND=0
  84. for VER in $VERSIONS; do
  85. if [[ $VER == ${LINE%% *} ]]; then
  86. FOUND=1
  87. break
  88. fi
  89. done
  90. [[ $FOUND == 0 ]] && VERSIONS="$VERSIONS ${LINE%% *}"
  91. done < ~/.synfig/cph/log
  92. echo " Logged versions: ${VERSIONS}"
  93. for VER in $VERSIONS; do
  94. #generating random record ID
  95. ID=$( echo `date` `ps` | md5sum | md5sum )
  96. ID="${ID:2:16}"
  97. #summarizing time and counting crashes
  98. CRASHCOUNT=0
  99. TIMECOUNT=0
  100. while read LINE; do
  101. if [[ ${LINE%% *} == $VER ]]; then
  102. TIMECOUNT=`expr $TIMECOUNT + $(echo $LINE| cut -f 2 -d ' ')` || true
  103. CRASHCOUNT=`expr $CRASHCOUNT + $(echo $LINE| cut -f 3 -d ' ')` || true
  104. fi
  105. done < ~/.synfig/cph/log
  106. echo " $LOGMODDATE $ID $VER $TIMECOUNT $CRASHCOUNT"
  107. echo "$LOGMODDATE $ID $VER $TIMECOUNT $CRASHCOUNT" >> ~/.synfig/cph/stats
  108. done
  109. rm -f ~/.synfig/cph/log
  110. echo ' Done.'
  111. }
  112. mainloop()
  113. {
  114. while true; do
  115. if ( ps --no-headers -f -Csynfigstudio |egrep "^`whoami`" > /dev/null ) ; then
  116. #synfigstudio process exist
  117. if [[ $STARTED == 0 ]]; then
  118. STARTED=1
  119. RUNTIME=0
  120. #get version
  121. P=$(ps --no-headers -Csynfigstudio -o uid,cmd |egrep "^ *`id -u`" | egrep -v "<defunct>" | tr -s ' ' | sed "s|^ *`id -u` *||" | sed "s|/bin/sh *||" | head -n 1)
  122. echo
  123. if [ ! -e $P ]; then
  124. P=`which $P`
  125. fi
  126. P=`dirname $P`
  127. VERSION=`$P/synfig --info|head -n 1|cut -d '-' -f 2`
  128. RELEASE=`$P/synfig --info|egrep "Revision:"|cut -d ' ' -f 2`
  129. BRANCH=`$P/synfig --info|egrep "Branch:"|cut -d ' ' -f 2-3`
  130. REVISION_ID=`$P/synfig --info|egrep "Revision ID:"|cut -d ' ' -f 3`
  131. if [[ $BRANCH == '(no branch)' ]]; then
  132. BRANCH=$REVISION_ID
  133. fi
  134. echo `date +%H:%M` "Synfig $VERSION.$RELEASE.$BRANCH.$REVISION_ID started."
  135. echo `date +%H:%M` "Assuming Synfig installed in $P."
  136. else
  137. let RUNTIME=$RUNTIME+1
  138. fi
  139. else
  140. #no synfigstudio process exist
  141. if [[ $STARTED == 1 ]]; then
  142. #detect crash
  143. if [ -e ~/.synfig/fifo ]; then
  144. CRASH=1
  145. fi
  146. writelog
  147. CRASH=0
  148. STARTED=0
  149. fi
  150. fi
  151. sleep 1
  152. done
  153. }
  154. init
  155. mainloop