000b83a2d6628d1b59c7e63ee40d2bb95436fe53
[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                 export PS=$(ps -f -u `whoami`)
102                 if ( ( echo "$PS" |egrep "synfigstudio$" >/dev/null ) || ( echo "$PS" | egrep "synfigstudio " >/dev/null ) ) ; then
103                         #synfigstudio process exist
104                         if [[ $STARTED == 0 ]]; then
105                                 STARTED=1
106                                 RUNTIME=0
107                                 #get version
108                                 if ( echo "$PS" |egrep "synfigstudio$" >/dev/null ) ; then
109                                         P=$( echo "$PS" |egrep "synfigstudio$"| tr -s ' '| cut -d ' ' -f 8)
110                                 else
111                                         P=$( echo "$PS" |egrep "synfigstudio "| tr -s ' '| cut -d ' ' -f 8)
112                                 fi
113                                 echo 
114                                 if [ ! -e $P ]; then
115                                         P=`which $P`
116                                 fi
117                                 P=`dirname $P`
118                                 VERSION=`$P/synfig --info|head -n 1|cut -d '-' -f 2`
119                                 RELEASE=`$P/synfig --info|egrep "Revision:"|cut -d ' ' -f 2`
120                                 BRANCH=`$P/synfig --info|egrep "Branch:"|cut -d ' ' -f 2-3`
121                                 REVISION_ID=`$P/synfig --info|egrep "Revision ID:"|cut -d ' ' -f 3`
122                                 if [[ $BRANCH == '(no branch)' ]]; then
123                                         BRANCH=$REVISION_ID
124                                 fi
125                                 echo `date +%H:%M` "Synfig $VERSION.$RELEASE.$BRANCH.$REVISION_ID started."
126                                 echo `date +%H:%M` "Assuming Synfig installed in $P."
127                         else
128                                 let RUNTIME=$RUNTIME+1
129                         fi
130                 else
131                         #no synfigstudio process exist
132                         if [[ $STARTED == 1 ]]; then
133                                 #detect crash
134                                 if [ -e ~/.synfig/fifo ]; then
135                                         CRASH=1
136                                 fi
137                                 writelog
138                                 CRASH=0
139                                 STARTED=0
140                         fi
141                 fi
142                 sleep 1
143         done
144 }
145
146 init
147 mainloop