#!/bin/sh # Script to control mdadm raid array monitoring # Written by John Jenkins mrgoblin@sastk.org # $Id: rc.mdadm 214 2013-06-02 11:04:14Z mrgoblin $ # # Please Note: /etc/rc.d/rc.local is likely the best place to call # this script from. If you do call this at start-up please remember # to also add it to your shutdown scripts or you will be left with # a stale PID file. Using the "fstop" parameter will remove this. # # Set some variables MDPID="" PIDFILE="/var/run/mdadm" CONFIG="/etc/mdadm.conf" PROG="/sbin/mdadm" # you may list each device seperated by spaces DEVICES=/dev/md[0-9] # Sanity Checks if [ ! -f ${CONFIG} -o ! -x ${PROG} ]; then echo 'Either your mdadm binary or config file is missing! ' echo 'Is Mdadm installed? ' echo exit 1 fi # Get current PID if [ -f /var/run/mdadm ];then MDPID=$(cat /var/run/mdadm) fi # The functions start() { if [ "X${MDPID}" = "X" ];then ${PROG} --monitor --daemonise ${DEVICES} > ${PIDFILE} else echo "Mdadm Monitor already running : " if [ "${MDPID}" != "$(pidof mdadm)" ]; then echo "Removing stale PID file : " if [ -f "${PIDFILE}" ]; then rm -f "${PIDFILE}" fi ${PROG} --monitor --daemonise "${DEVICES}" > ${PIDFILE} fi exit 1 fi } stop() { if [ "X${MDPID}" = "X" ];then echo "Mdadm Monitor not running :" exit 1 else echo -n "Stopping Mdadm monitor :" kill -15 ${MDPID} && rm ${PIDFILE} && echo " Success " || echo " Failed to stop Mdadm Monitor " unset MDPID fi } status() { if [ "X${MDPID}" = "X" ];then echo "Mdadm Monitor not running :" ${PROG} -D ${DEVICES} |awk '/(^\/dev|State\ :|Raid\ Level|Array Size|Failed)/ { print $0 }' echo else echo "Mdadm Monitor running on PID ${MDPID} :" ${PROG} -D ${DEVICES} |awk '/(^\/dev|State\ :|Raid\ Level|Array Size|Failed)/ { print $0 }' echo fi } check() { err_msg() { echo 'Option "check" requires a valid md device as an argument! ' echo 'Valid devices are: ' #for DEV in $(echo $DEVICES |sed 's,/dev/,,g'); do ls -1 ${DEVICES} |sed 's,/dev/,,g' |while read DEV; do if [ -w /sys/block/${DEV}/md/sync_action ]; then echo -n "Device ${DEV}: " cat /sys/block/${DEV}/md/level fi done exit 1 } if [ ! -z "${1}" ]; then case ${1} in all) ls -1 ${DEVICES} |sed 's,/dev/,,g' |while read DEV; do if [ -w /sys/block/${DEV}/md/sync_action ]; then echo 'check' > /sys/block/${DEV}/md/sync_action 2>/dev/null && \ echo "Initiating check on: ${DEV}" || \ echo "Sync already in progress for: ${DEV}" else echo "Unable to initiate sync for: ${DEV}" err_msg fi done ;; md*) if [ "$(echo ${DEVICES} |fgrep ${1})" ]; then if [ -w "/sys/block/${1}/md/sync_action" ]; then echo 'check' > /sys/block/${1}/md/sync_action 2>/dev/null && \ echo "Initiating check on: ${1}" || \ echo "Sync already in progress for: ${1}" else echo "Unable to initiate sync for: ${1}" err_msg fi else echo "No such Device: ${1}" err_msg fi ;; *) err_msg ;; esac else err_msg fi } forced_stop() { echo "Forcibly stopping Mmdadm Monitor :" /bin/killall -9 mdadm if [ -f "${PIDFILE}" ]; then rm -f "${PIDFILE}" fi unset MDPID } # Parse input case "$1" in start) start ;; stop) stop ;; restart) stop start ;; check) check $2 ;; fstop) forced_stop ;; status) status ;; *) echo "*** Usage: $0 {start|stop|check|fstop|restart|status}" exit 1 esac exit 0 # $Id: rc.mdadm 214 2013-06-02 11:04:14Z mrgoblin $ # EOF