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