]> git.sven.stormbind.net Git - sven/mpt-status.git/blob - mpt-status.init
Add author and remove some cruft
[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
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 . /lib/lsb/init-functions
45
46 if [ $RUN_DAEMON = "no" ] ; then
47         log_failure_msg $"mpt-statusd is disabled in /etc/default/mpt-statusd, not starting."
48         exit 0
49 fi
50
51
52 #Try to blindly load the mptctl module
53 modprobe mptctl || true
54
55 if ! [ -e "/proc/mpt/version" ] ; then
56         log_failure_msg "The mptctl module is missing."
57         exit 0
58 fi
59
60 check_mpt() {
61     echo $$ > $PIDFILE.new && mv $PIDFILE.new $PIDFILE
62     while true ; do
63         # Check ever $PERIOD seconds, send email on every status
64         # change and repeat ever $REMIND seconds if the raid is still
65         # bad.
66         if (mpt-status -i $ID) |grep -q 'state OPTIMAL' ; then
67             BADRAID=false
68         else
69             BADRAID=true
70             logger -t mpt-statusd "detected non-optimal RAID status"
71         fi
72         STATUSCHANGE=false
73         if [ true = "$BADRAID" ] ; then
74             # RAID not OK
75             (mpt-status -i $ID) > $STATUSFILE.new
76             if [ ! -f $STATUSFILE ] ; then # RAID just became broken
77                 STATUSCHANGE=true
78                 mv $STATUSFILE.new $STATUSFILE
79             elif cmp -s $STATUSFILE $STATUSFILE.new ; then
80                 # No change.  Should we send reminder?
81                 LASTTIME="`stat -c '%Z' $STATUSFILE`"
82                 NOW="`date +%s`"
83                 SINCELAST="`expr $NOW - $LASTTIME`"
84                 if [ $REMIND -le "$SINCELAST" ]; then
85                     # Time to send reminder
86                     STATUSCHANGE=true
87                     mv $STATUSFILE.new $STATUSFILE
88                 else
89                     rm $STATUSFILE.new
90                 fi
91             else
92                 STATUSCHANGE=true
93                 mv $STATUSFILE.new $STATUSFILE
94             fi
95         else
96             # RAID OK
97             if [ -f $STATUSFILE ] ; then
98                 rm $STATUSFILE
99                 STATUSCHANGE=true
100             fi
101         fi
102                 
103         if [ true = "$STATUSCHANGE" ]; then
104             hostname="`uname -n`"
105             (
106                 cat <<EOF 
107 This is a RAID status update from mpt-statusd.  The mpt-status
108 program reports that one of the RAIDs changed state:
109
110 EOF
111                 if [ -f $STATUSFILE ] ; then
112                     cat $STATUSFILE
113                 else
114                     (mpt-status -i $ID)
115                 fi
116                 echo
117                 echo "Report from $0 on $hostname"
118             ) | mail -s "info: mpt raid status change on $hostname" $MAILTO
119         fi
120         sleep $PERIOD
121     done
122 }
123
124 check_daemon() {
125         # Let's check if there is a daemon which is really running and not timing out
126         DAEMON_RUN=`ps aux | grep "/etc/init.d/mpt-statusd check_mpt" | grep -v grep | grep -v daemon`
127         if [ -n "$DAEMON_RUN" ] ; then
128                 return 1;
129         else
130                 return 0;
131         fi
132 }
133
134 #
135 #       Function that starts the daemon/service.
136 #
137 d_start() {
138     [ -f $PIDFILE ] && PID="`cat $PIDFILE`"
139     if [ "$PID" ] ; then
140         log_warning_msg "Daemon already running. Refusing to start another"
141         return 0
142     elif check_daemon ; then
143         # Use daemonize to turn it into a daemon and start it with daemon().
144         daemon --pidfile $PIDFILE /usr/sbin/daemonize $SCRIPTNAME check_mpt
145         return 0
146     else
147         log_warning_msg "Daemon is already running. Refusing to start another"
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 -p $PIDFILE $SCRIPTNAME > /dev/null 2>&1 
158                 rm -f $PIDFILE
159         else
160                 log_warning_msg "Daemon is already stopped."
161                 return 0
162         fi
163 }
164
165 # This is a workaround function which does not directly exit and
166 # therefore can be used by a restart
167 d_stop_by_restart() {
168         if [ -f $PIDFILE ] ; then
169                 killproc -p $PIDFILE $SCRIPTNAME > /dev/null 2>&1
170                 rm -f $PIDFILE
171         else
172                 log_warning_msg "Daemon is already stopped."
173         fi
174 }
175
176 case "$1" in
177   start)
178         echo -n $"Starting $DESC: $NAME"
179         d_start
180         ;;
181   stop)
182         echo -n $"Stopping $DESC: $NAME"
183         d_stop
184         ;;
185   check_mpt)
186         check_mpt
187         ;;
188   restart|force-reload)
189         echo -n "Restarting $DESC: $NAME"
190         d_stop_by_restart
191         sleep 1
192         d_start
193         ;;
194   *)
195         echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
196         exit 1
197         ;;
198 esac
199
200 exit 0