76f77f4638a4c0380484b94966b18d47e5a368cb
[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 "Synfig 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 echo `date +%H:%M` "Synfig Crash Monitor started."
24 STARTED=0
25 RUNTIME=0
26 VERSION=''
27 RELEASE=''
28 BRANCH=''
29 REVISION_ID=''
30 CRASH=0
31 [ ! -d ~/.synfig/cph ] && mkdir -p ~/.synfig/cph || true
32 }
33
34 writelog()
35 {
36         if [[ $STARTED != 0 ]]; then
37                 if [[ $CRASH == 0 ]]; then
38                         echo `date +%H:%M` "Synfig exited normally. Session time: $RUNTIME."
39                 else
40                         echo `date +%H:%M` "Crash detected. Version $VERSION.$RELEASE.$BRANCH, session time: $RUNTIME."
41                 fi
42                 if [ -e ~/.synfig/cph/log ]; then
43                         #check if dump needed
44                         CURRENTDATE=`date +%Y-%m-%d`
45                         LOGMODDATE=`stat -c %y ~/.synfig/cph/log`
46                         LOGMODDATE=${LOGMODDATE%% *}
47                         if [[ $LOGMODDATE != $CURRENTDATE ]]; then
48                                 dumpstats
49                         fi
50                 fi
51                 #write log
52                 echo $VERSION/$BRANCH/$RELEASE/$REVISION_ID $RUNTIME $CRASH >> ~/.synfig/cph/log
53                 CRASH=0
54                 RUNTIME=0
55         else
56                 echo
57         fi
58 }
59
60 dumpstats()
61 {
62         echo `date +%H:%M` 'Dumping stats for previous session...'
63         LOGMODDATE=`stat -c %y ~/.synfig/cph/log`
64         LOGMODDATE=${LOGMODDATE%% *}
65         #get versions
66         VERSIONS=''
67         while read LINE; do
68                 FOUND=0
69                 for VER in $VERSIONS; do
70                         if [[ $VER == ${LINE%% *} ]]; then
71                                 FOUND=1
72                                 break
73                         fi
74                 done
75                 [[ $FOUND == 0 ]] && VERSIONS="$VERSIONS ${LINE%% *}"
76         done < ~/.synfig/cph/log
77         echo "   Logged versions: ${VERSIONS}"
78         for VER in $VERSIONS; do
79                 #generating random record ID
80                 ID=$( echo `date` `ps` | md5sum | md5sum )
81                 ID="${ID:2:16}"
82                 #summarizing time and counting crashes
83                 CRASHCOUNT=0
84                 TIMECOUNT=0
85                 while read LINE; do
86                         if [[ ${LINE%% *} == $VER ]]; then
87                                 TIMECOUNT=`expr $TIMECOUNT + $(echo $LINE| cut -f 2 -d ' ')` || true
88                                 CRASHCOUNT=`expr $CRASHCOUNT + $(echo $LINE| cut -f 3 -d ' ')` || true
89                         fi
90                 done < ~/.synfig/cph/log
91                 echo "   $LOGMODDATE $ID $VER $TIMECOUNT $CRASHCOUNT"
92                 echo "$LOGMODDATE $ID $VER $TIMECOUNT $CRASHCOUNT" >> ~/.synfig/cph/stats
93         done
94         rm -f ~/.synfig/cph/log
95         echo '   Done.'
96 }
97
98 mainloop()
99 {
100         while true; do
101                 if ( ps -f -u `whoami`|egrep "synfigstudio$" >/dev/null ) ; then
102                         #synfigstudio process exist
103                         if [[ $STARTED == 0 ]]; then
104                                 STARTED=1
105                                 RUNTIME=0
106                                 #get version
107                                 P=$(ps -f -u `whoami`|egrep "synfigstudio$"| tr -s ' '| cut -d ' ' -f 8)
108                                 echo 
109                                 if [ ! -e $P ]; then
110                                         P=`which $P`
111                                 fi
112                                 P=`dirname $P`
113                                 VERSION=`$P/synfig --info|head -n 1|cut -d '-' -f 2`
114                                 RELEASE=`$P/synfig --info|egrep "Revision:"|cut -d ' ' -f 2`
115                                 BRANCH=`$P/synfig --info|egrep "Branch:"|cut -d ' ' -f 2-3`
116                                 REVISION_ID=`$P/synfig --info|egrep "Revision ID:"|cut -d ' ' -f 3`
117                                 if [[ $BRANCH == '(no branch)' ]]; then
118                                         BRANCH=$REVISION_ID
119                                 fi
120                                 echo `date +%H:%M` "Synfig $VERSION.$RELEASE.$BRANCH.$REVISION_ID started."
121                                 echo `date +%H:%M` "Assuming Synfig installed in $P."
122                         else
123                                 let RUNTIME=$RUNTIME+1
124                         fi
125                 else
126                         #no synfigstudio process exist
127                         if [[ $STARTED == 1 ]]; then
128                                 #detect crash
129                                 if [ -e ~/.synfig/fifo ]; then
130                                         CRASH=1
131                                 fi
132                                 writelog
133                                 CRASH=0
134                                 STARTED=0
135                         fi
136                 fi
137                 sleep 1
138         done
139 }
140
141 init
142 mainloop