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