mdalerts, mdcheck

Here below are two useful Bash scripts to monitor mdadm software RAIDs on a GNU/Linux system.

The mdalerts script is intented to output events detected by mdadm –monitor, thus it should be set in the PROGRAM line of the mdadm.conf file in order to be run by mdmadm. Arguments are passed by mdmadm.
When mdadm detect any event, the script output the event details (using echo) and also send a notification (using notify-send) to desktop applets (panels and notifiers).

The mdcheck script is intented to check and output the status of mdadm devices. It check and output (using echo) the status of mdmadm RAID devices and also send a notification (using notify-send) to desktop applets (panels and notifiers) when a device is not clean.

For both scripts, the mdadm devices list should be configured inside the code. Superuser rights are not required.

For more details and settings, please look inside the code. See also: man mdadm and man mdadm.conf.

The mdalerts and the mdcheck are free software, released under the terms of the GNU General Public License version 3 or (at your option) any later version.

Comments, suggestions and bug reports are welcome.

mdalerts

#!/bin/bash
#
# NAME
# 	mdalerts
# 
# SYNOPSIS
# 	Output (using echo and notify-send) events detected by mdadm monitor.
# 
# DESCRIPTION
# 	This script is intented to output events detected by 'mdadm --monitor', 
# 	thus it should be set in the PROGRAM line of the 'mdadm.conf' file in order 
# 	to be run by mdmadm. Arguments are passed by mdadm.
# 	When mdadm detect any event, the script output the event details (using 
# 	echo) and also send a notification (using notify-send) to desktop applets 
# 	(panels and notifiers).
# 	The mdadm devices list should be configured inside the script itself.
# 	The script do not need superuser rights.
# 	See also: 'man mdadm' and 'man mdadm.conf'.
# 
# VERSION
# 	1.0 (03/06/2017)
# 
# AUTHOR
# 	Alexus
# 	
# COPYRIGHT
# 	Copyright (c) 2016+ Alexus
# 	License: GPLv3+ (GNU GPL version 3 or later)
# 	This program is free software: you can redistribute it and/or modify
# 	it under the terms of the GNU General Public License as published by
# 	the Free Software Foundation, either version 3 of the License, or
# 	(at your option) any later version.
# 	This program is distributed in the hope that it will be useful,
# 	but WITHOUT ANY WARRANTY; without even the implied warranty of
# 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# 	GNU General Public License for more details.
# 	You should have received a copy of the GNU General Public License
# 	along with this program.  If not, see .
# 	

## @file
## @name       mdalerts
## @brief      Output events detected by 'mdadm --monitor'
## @author     alexus
## @copyright  GPLv3+
## @version    1.0
## @date       03/06/2017
## @pre        mdadm, notify-send
## @note       Do not need superuser rights
## @param      string  $1  event (passed by mdadm)
## @param      string  $2  md device (passed by mdadm)
## @param      string  $3  related component device (possibly passed by mdadm)
## @return     int     1   parent command was not mdadm

# Declarations
declare -i alarm_ms  # milliseconds
declare    parent_cmd='mdadm'
declare    msg

# ### Script configuration ###
alarm_ms=10000
# ### End script configuration ###

# Check parent command
if [[ $parent_cmd != "$(ps -o comm= $PPID)" ]] ; then echo -e "ERROR! This script should be run by mdadm.\nSet it in the PROGRAM line of the 'mdadm.conf' file.\See: 'man mdadm'.\n"; exit 1 ;fi

# Outputs and notifications
msg="Event: $1, Device: $2"
if [[ -n "$3" ]] ; then msg+=", Component: $3" ; fi
echo "MD MONITOR EVENT!" "$msg$"
notify-send -u "normal" -t "$alarm_ms" "MD MONITOR EVENT!" "$msg"

# Exit
exit 0

mdcheck

#!/bin/bash
#
# NAME
# 	mdcheck
# 
# SYNOPSIS
# 	Check and output mdadm devices status.
# 
# DESCRIPTION
# 	This script is intented to check and output the status of mdadm devices.
# 	The script check and output (using echo) the status of mdadm devices and 
# 	also send a notification (using notify-send) to desktop applets (panels and 
# 	notifiers) when a device is not clean.
# 	The mdadm devices list should be configured inside the script itself.
# 	The script do not need superuser rights.
# 	See also: 'man mdadm' and 'man mdadm.conf'.
# 
# VERSION
# 	1.0 (03/06/2017)
# 
# AUTHOR
# 	Alexus
# 	
# COPYRIGHT
# 	Copyright (c) 2016+ Alexus
# 	License: GPLv3+ (GNU GPL version 3 or later)
# 	This program is free software: you can redistribute it and/or modify
# 	it under the terms of the GNU General Public License as published by
# 	the Free Software Foundation, either version 3 of the License, or
# 	(at your option) any later version.
# 	This program is distributed in the hope that it will be useful,
# 	but WITHOUT ANY WARRANTY; without even the implied warranty of
# 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# 	GNU General Public License for more details.
# 	You should have received a copy of the GNU General Public License
# 	along with this program.  If not, see .
# 	

## @file
## @name       mdcheck
## @brief      Output the status of mdadm devices for monitoring applets
## @author     alexus
## @copyright  GPLv3+
## @version    1.0
## @date       03/06/2017
## @pre        mdadm, notify-send
## @note       Do not need superuser rights
## @return     int  1  one or mode mdadm devices is not clean working

# Declarations
declare raid
declare raid_status='ok'
declare raids
declare unit_state
declare unit_recovery
declare unit_resync
declare out_pre
declare out_post
declare -i alarm_ms  # milliseconds
declare -i exitCode=0

# ### Script configuration ###
raids="md1 md2 md3 md4"
alarm_ms=5000
out_pre='| MD:'
out_post=''
# ### End script configuration ###

# Checking...
for raid in $raids ; do
	mdstat="$(grep -A2 ^$raid "/proc/mdstat")"
	if [[ $(echo -e $mdstat | grep '\[.*F.*\]') ]] ; then unit_state="failed" ; raid_status='KO'
	elif [[ $(echo -e $mdstat | grep '\[.*_.*\]') ]] ; then unit_state="degraded" ; raid_status='KO'
	else unit_state="working" ; fi
	unit_recovery=$(echo -e $mdstat | grep recovery | awk '{print $16}')
	unit_resync=$(echo $mdstat | grep resync | awk '{print $16}')
	if [[ $raid_status == "KO" ]] ; then 
		notify-send -u "critical" -t "$alarm_ms" "MD RAID ALARM!" "${0##*/} - Device '$raid' has $unit_state unit(s). Recovery: $unit_recovery. Resync: $unit_resync."
		exitCode=1
	fi
done

# Output
echo "$out_pre$raid_status$out_post"

# Exit
exit $exitCode

Leave a Reply