Forbid double run of synfigstudio-cph-monitor.
[synfig.git] / synfig-studio / trunk / synfigstudio-cph-monitor
1 #!/bin/bash
2
3 # Synfig Crash Monitor script
4 # Copyright (c) 2009 Konstantin Dmitriev
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
15 set -e
16 trap writelog INT
17
18 init()
19 {
20 echo "SynfigStudio Crash Monitor is a tool to collect statistics about synfig crashes."
21 echo "All information is collected locally in ~/.synfig/cph directory."
22 echo
23 STARTED=0
24 RUNTIME=0
25 VERSION=''
26 RELEASE=''
27 BRANCH=''
28 REVISION_ID=''
29 CRASH=0
30 [ ! -d ~/.synfig/cph ] && mkdir -p ~/.synfig/cph || true
31
32 # Detect if crash monitor is already started
33 PDIR=${0%`basename $0`}
34 LCK_FILE=~/.synfig/cph/`basename $0`.lck
35 if [ -f "${LCK_FILE}" ]; then
36         MYPID=`head -n 1 "${LCK_FILE}"`
37         if ! ( ps -p ${MYPID} | grep ${MYPID} >/dev/null ); then
38                 # The process is not running
39                 # Echo current PID into lock file
40                 echo $$ > "${LCK_FILE}"
41         else
42                 echo "`basename $0` is already running [${MYPID}]. Aborting."
43                 sleep 5
44                 exit 0
45         fi
46 else
47         # The process is not running
48         # Echo current PID into lock file
49         echo $$ > "${LCK_FILE}"
50 fi
51 echo `date +%H:%M` "Synfig Crash Monitor started."
52 }
53
54 writelog()
55 {
56         if [[ $STARTED != 0 ]]; then
57                 if [[ $CRASH == 0 ]]; then
58                         echo `date +%H:%M` "Synfig exited normally. Session time: $RUNTIME."
59                 else
60                         echo `date +%H:%M` "Crash detected. Version $VERSION.$RELEASE.$BRANCH, session time: $RUNTIME."
61                 fi
62                 if [ -e ~/.synfig/cph/log ]; then
63                         #check if dump needed
64                         CURRENTDATE=`date +%Y-%m-%d`
65                         LOGMODDATE=`stat -c %y ~/.synfig/cph/log`
66                         LOGMODDATE=${LOGMODDATE%% *}
67                         if [[ $LOGMODDATE != $CURRENTDATE ]]; then
68                                 dumpstats
69                         fi
70                 fi
71                 #write log
72                 echo $VERSION/$BRANCH/$RELEASE/$REVISION_ID $RUNTIME $CRASH >> ~/.synfig/cph/log
73                 CRASH=0
74                 RUNTIME=0
75         else
76                 echo
77         fi
78 }
79
80 dumpstats()
81 {
82         echo `date +%H:%M` 'Dumping stats for previous session...'
83         LOGMODDATE=`stat -c %y ~/.synfig/cph/log`
84         LOGMODDATE=${LOGMODDATE%% *}
85         #get versions
86         VERSIONS=''
87         while read LINE; do
88                 FOUND=0
89                 for VER in $VERSIONS; do
90                         if [[ $VER == ${LINE%% *} ]]; then
91                                 FOUND=1
92                                 break
93                         fi
94                 done
95                 [[ $FOUND == 0 ]] && VERSIONS="$VERSIONS ${LINE%% *}"
96         done < ~/.synfig/cph/log
97         echo "   Logged versions: ${VERSIONS}"
98         for VER in $VERSIONS; do
99                 #generating random record ID
100                 ID=$( echo `date` `ps` | md5sum | md5sum )
101                 ID="${ID:2:16}"
102                 #summarizing time and counting crashes
103                 CRASHCOUNT=0
104                 TIMECOUNT=0
105                 while read LINE; do
106                         if [[ ${LINE%% *} == $VER ]]; then
107                                 TIMECOUNT=`expr $TIMECOUNT + $(echo $LINE| cut -f 2 -d ' ')` || true
108                                 CRASHCOUNT=`expr $CRASHCOUNT + $(echo $LINE| cut -f 3 -d ' ')` || true
109                         fi
110                 done < ~/.synfig/cph/log
111                 echo "   $LOGMODDATE $ID $VER $TIMECOUNT $CRASHCOUNT"
112                 echo "$LOGMODDATE $ID $VER $TIMECOUNT $CRASHCOUNT" >> ~/.synfig/cph/stats
113         done
114         rm -f ~/.synfig/cph/log
115         echo '   Done.'
116 }
117
118 mainloop()
119 {
120         while true; do
121                 export PS=$(ps -f -u `whoami`)
122                 if ( ( echo "$PS" |egrep "synfigstudio$" >/dev/null ) || ( echo "$PS" | egrep "synfigstudio " >/dev/null ) ) ; then
123                         #synfigstudio process exist
124                         if [[ $STARTED == 0 ]]; then
125                                 STARTED=1
126                                 RUNTIME=0
127                                 #get version
128                                 if ( echo "$PS" |egrep "synfigstudio$" >/dev/null ) ; then
129                                         P=$( echo "$PS" |egrep "synfigstudio$"| tr -s ' '| cut -d ' ' -f 8)
130                                 else
131                                         P=$( echo "$PS" |egrep "synfigstudio "| tr -s ' '| cut -d ' ' -f 8)
132                                 fi
133                                 echo 
134                                 if [ ! -e $P ]; then
135                                         P=`which $P`
136                                 fi
137                                 P=`dirname $P`
138                                 VERSION=`$P/synfig --info|head -n 1|cut -d '-' -f 2`
139                                 RELEASE=`$P/synfig --info|egrep "Revision:"|cut -d ' ' -f 2`
140                                 BRANCH=`$P/synfig --info|egrep "Branch:"|cut -d ' ' -f 2-3`
141                                 REVISION_ID=`$P/synfig --info|egrep "Revision ID:"|cut -d ' ' -f 3`
142                                 if [[ $BRANCH == '(no branch)' ]]; then
143                                         BRANCH=$REVISION_ID
144                                 fi
145                                 echo `date +%H:%M` "Synfig $VERSION.$RELEASE.$BRANCH.$REVISION_ID started."
146                                 echo `date +%H:%M` "Assuming Synfig installed in $P."
147                         else
148                                 let RUNTIME=$RUNTIME+1
149                         fi
150                 else
151                         #no synfigstudio process exist
152                         if [[ $STARTED == 1 ]]; then
153                                 #detect crash
154                                 if [ -e ~/.synfig/fifo ]; then
155                                         CRASH=1
156                                 fi
157                                 writelog
158                                 CRASH=0
159                                 STARTED=0
160                         fi
161                 fi
162                 sleep 1
163         done
164 }
165
166
167 init
168 mainloop