]> git.sven.stormbind.net Git - sven/mpt-status.git/blob - mpt-status.init
Add the Debian mpt-status init script to the repo.
[sven/mpt-status.git] / mpt-status.init
1 #! /bin/sh
2
3 # Author: Petter Reinholdtsen <pere@hungry.com>
4 # License: GNU General Public License v2 or later
5 #
6 ### BEGIN INIT INFO
7 # Provides:          mpt-statusd
8 # Required-Start:    $remote_fs $syslog
9 # Required-Stop:     $remote_fs $syslog
10 # Default-Start:     2 3 4 5
11 # Default-Stop:      0 1 6
12 # Short-Description: Check mpt-status values in the background.
13 ### END INIT INFO
14
15 PATH=/sbin:/bin:/usr/sbin:/usr/bin
16 DESC="mpt-status monitor"
17 NAME=mpt-statusd
18 PIDFILE=/var/run/$NAME.pid
19 STATUSFILE=/var/run/$NAME.status
20 SCRIPTNAME=/etc/init.d/$NAME
21
22
23 # Do not touch you can configure this in /etc/default/mpt-statusd
24 MAILTO=root   # Where to report problems
25 PERIOD=600    # Seconds between each check    (default 10 minutes)
26 REMIND=7200   # Seconds between each reminder (default 2 hours)
27 RUN_DAEMON=yes
28 ID=0
29
30 [ -e /etc/default/mpt-statusd ] && . /etc/default/mpt-statusd
31
32 # Gracefully exit if the package has been removed.
33 test -x /usr/sbin/mpt-status || exit 0
34
35 . /lib/lsb/init-functions
36 [ -e /etc/default/rcS ] && . /etc/default/rcS
37
38 if [ $RUN_DAEMON = "no" ] ; then
39         log_begin_msg "mpt-statusd is disabled in /etc/default/mpt-statusd, not starting."
40         log_end_msg 0
41         exit 0
42 fi
43
44 if ! [ -e "/proc/mpt/version" ] ; then
45         log_failure_msg "The mptctl module is missing. Please have a look at the README.Debian.gz."
46         log_end_msg 0
47         exit 0
48 fi
49
50 check_mpt() {
51     echo $$ > $PIDFILE.new && mv $PIDFILE.new $PIDFILE
52     while true ; do
53         # Check ever $PERIOD seconds, send email on every status
54         # change and repeat ever $REMIND seconds if the raid is still
55         # bad.
56         if (mpt-status -i $ID) |grep -q 'state OPTIMAL' ; then
57             BADRAID=false
58         else
59             BADRAID=true
60             logger -t mpt-statusd "detected non-optimal RAID status"
61         fi
62         STATUSCHANGE=false
63         if [ true = "$BADRAID" ] ; then
64             # RAID not OK
65             (mpt-status -i $ID) > $STATUSFILE.new
66             if [ ! -f $STATUSFILE ] ; then # RAID just became broken
67                 STATUSCHANGE=true
68                 mv $STATUSFILE.new $STATUSFILE
69             elif cmp -s $STATUSFILE $STATUSFILE.new ; then
70                 # No change.  Should we send reminder?
71                 LASTTIME="`stat -c '%Z' $STATUSFILE`"
72                 NOW="`date +%s`"
73                 SINCELAST="`expr $NOW - $LASTTIME`"
74                 if [ $REMIND -le "$SINCELAST" ]; then
75                     # Time to send reminder
76                     STATUSCHANGE=true
77                     mv $STATUSFILE.new $STATUSFILE
78                 else
79                     rm $STATUSFILE.new
80                 fi
81             else
82                 STATUSCHANGE=true
83                 mv $STATUSFILE.new $STATUSFILE
84             fi
85         else
86             # RAID OK
87             if [ -f $STATUSFILE ] ; then
88                 rm $STATUSFILE
89                 STATUSCHANGE=true
90             fi
91         fi
92                 
93         if [ true = "$STATUSCHANGE" ]; then
94             hostname="`uname -n`"
95             (
96                 cat <<EOF 
97 This is a RAID status update from mpt-statusd.  The mpt-status
98 program reports that one of the RAIDs changed state:
99
100 EOF
101                 if [ -f $STATUSFILE ] ; then
102                     cat $STATUSFILE
103                 else
104                     (mpt-status -i $ID)
105                 fi
106                 echo
107                 echo "Report from $0 on $hostname"
108             ) | mail -s "info: mpt raid status change on $hostname" $MAILTO
109         fi
110         sleep $PERIOD
111     done
112 }
113
114 check_daemon() {
115         # Let's check if there is a daemon which is really running and not timing out
116         DAEMON_RUN=`ps aux | grep "/etc/init.d/mpt-statusd check_mpt" | grep -v grep | grep -v daemon`
117         if [ -n "$DAEMON_RUN" ] ; then
118                 return 1;
119         else
120                 return 0;
121         fi
122 }
123
124 #
125 #       Function that starts the daemon/service.
126 #
127 d_start() {
128     [ -f $PIDFILE ] && PID="`cat $PIDFILE`"
129     if [ "$PID" ] ; then
130         log_progress_msg "Daemon already running. Refusing to start another"
131         return 0
132     elif check_daemon ; then
133         # Use the daemon package to turn this script into a daemon
134         start-stop-daemon --start --quiet --pidfile $PIDFILE \
135                 --oknodo --exec /usr/bin/daemon /usr/bin/daemon $SCRIPTNAME check_mpt
136         return 0
137     else
138         log_progress_msg "Daemon is already running. Refusing to start another"
139         return 0
140     fi
141 }
142
143 #
144 #       Function that stops the daemon/service.
145 #
146 d_stop() {
147         if [ -f $PIDFILE ] ; then
148                 start-stop-daemon --stop --oknodo --quiet --pidfile $PIDFILE > /dev/null 2>&1 
149                 rm -f $PIDFILE
150         else
151                 log_progress_msg "Daemon is already stopped."
152                 return 0
153         fi
154 }
155
156 # This is a workaround function which does not directly exit and
157 # therefore can be used by a restart
158 d_stop_by_restart() {
159         if [ -f $PIDFILE ] ; then
160                 start-stop-daemon --oknodo --stop --quiet --pidfile $PIDFILE
161                 rm -f $PIDFILE
162                 log_end_msg 0
163         else
164                 log_progress_msg "Daemon is already stopped."
165                 log_end_msg 0
166         fi
167 }
168
169 case "$1" in
170   start)
171         echo -n ""
172         log_begin_msg "Starting $DESC: $NAME"
173         d_start ; CODE=$?
174         log_end_msg $CODE
175         ;;
176   stop)
177         log_begin_msg "Stopping $DESC: $NAME"
178         d_stop ; CODE=$?
179         log_end_msg $CODE
180         ;;
181   check_mpt)
182         check_mpt
183         ;;
184   restart|force-reload)
185         log_begin_msg "Restarting $DESC: $NAME"
186         d_stop_by_restart
187         sleep 1
188         d_start || CODE=$?
189         log_end_msg $CODE
190         ;;
191   *)
192         # echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
193         echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
194         exit 1
195         ;;
196 esac
197
198 exit 0