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