#! /bin/sh # Author: Petter Reinholdtsen # License: GNU General Public License v2 or later # mpt-statusd - Check mpt-status values in the background. # # chkconfig: 345 60 50 # description: Check mpt-status values in the background \ # using daemonize and the mpt-status utility. ### BEGIN INIT INFO # Provides: mpt-statusd # Required-Start: $remote_fs $syslog # Required-Stop: $remote_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Check mpt-status values in the background. ### END INIT INFO PATH=/sbin:/bin:/usr/sbin:/usr/bin DESC="mpt-status monitor" NAME=mpt-statusd PIDFILE=/var/run/$NAME.pid STATUSFILE=/var/run/$NAME.status SCRIPTNAME=/etc/init.d/$NAME # Do not touch you can configure this in /etc/default/mpt-statusd MAILTO=root # Where to report problems PERIOD=600 # Seconds between each check (default 10 minutes) REMIND=7200 # Seconds between each reminder (default 2 hours) RUN_DAEMON=yes ID=0 [ -e /etc/default/mpt-statusd ] && . /etc/default/mpt-statusd # Gracefully exit if the package has been removed. test -x /usr/sbin/mpt-status || exit 0 # Source function library. . /etc/rc.d/init.d/functions . /lib/lsb/init-functions if [ $RUN_DAEMON = "no" ] ; then log_failure_msg $"mpt-statusd is disabled in /etc/default/mpt-statusd, not starting." exit 0 fi if ! [ -e "/proc/mpt/version" ] ; then log_failure_msg "The mptctl module is missing." exit 0 fi check_mpt() { echo $$ > $PIDFILE.new && mv $PIDFILE.new $PIDFILE while true ; do # Check ever $PERIOD seconds, send email on every status # change and repeat ever $REMIND seconds if the raid is still # bad. if (mpt-status -i $ID) |grep -q 'state OPTIMAL' ; then BADRAID=false else BADRAID=true logger -t mpt-statusd "detected non-optimal RAID status" fi STATUSCHANGE=false if [ true = "$BADRAID" ] ; then # RAID not OK (mpt-status -i $ID) > $STATUSFILE.new if [ ! -f $STATUSFILE ] ; then # RAID just became broken STATUSCHANGE=true mv $STATUSFILE.new $STATUSFILE elif cmp -s $STATUSFILE $STATUSFILE.new ; then # No change. Should we send reminder? LASTTIME="`stat -c '%Z' $STATUSFILE`" NOW="`date +%s`" SINCELAST="`expr $NOW - $LASTTIME`" if [ $REMIND -le "$SINCELAST" ]; then # Time to send reminder STATUSCHANGE=true mv $STATUSFILE.new $STATUSFILE else rm $STATUSFILE.new fi else STATUSCHANGE=true mv $STATUSFILE.new $STATUSFILE fi else # RAID OK if [ -f $STATUSFILE ] ; then rm $STATUSFILE STATUSCHANGE=true fi fi if [ true = "$STATUSCHANGE" ]; then hostname="`uname -n`" ( cat < /dev/null 2>&1 rm -f $PIDFILE else log_warning_msg "Daemon is already stopped." return 0 fi } # This is a workaround function which does not directly exit and # therefore can be used by a restart d_stop_by_restart() { if [ -f $PIDFILE ] ; then killproc -p $PIDFILE $SCRIPTNAME > /dev/null 2>&1 rm -f $PIDFILE else log_warning_msg "Daemon is already stopped." fi } case "$1" in start) echo -n "" echo -n $"Starting $DESC: $NAME" d_start ;; stop) echo -n $"Stopping $DESC: $NAME" d_stop ;; check_mpt) check_mpt ;; restart|force-reload) echo -n "Restarting $DESC: $NAME" d_stop_by_restart sleep 1 d_start ;; *) echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2 exit 1 ;; esac exit 0