How to parse and process Linux Software RAID events

It is an interesting thing to know how to parse and process Linux Software RAID events. It is also very useful as you can automatically take appropriate action immediately after a certain event occurs.

Shell script

Create mdmonitor.sh shell script to parse Linux Software RAID events.

#!/bin/sh
# Parse and process software raid [mdadm] events

# Script requires an action and parameter to get started
if [ "$#" -lt 2 ]; then
  echo "Illegal number of parameters"
  exit 1;
fi

# Get action
action=$1;
shift;

# Parse action
case "$action" in
  "Fail")
    # Sample: Fail /dev/md1 /dev/sdb2
    # Action requires 2 parameters
    if [ "$#" -ne 2 ]; then
      logger --priority "local0.debug" --tag "mdadm" \
             "Action [Fail] Illegal number of parameters: $*";
      exit 1;
    fi
    # Perform custom action
    logger --priority "local0.notice" --tag "mdadm" "Device $2 failed on array $1";
    ;;
  "FailSpare")
    # Sample: FailSpare /dev/md1 /dev/sdb2
    # Action requires 2 parameters
    if [ "$#" -ne 2 ]; then
      logger --priority "local0.debug" --tag "mdadm" \
             "Action [FailSpare] Illegal number of parameters: $*";
      exit 1;
    fi
    # Perform custom action
    logger --priority "local0.notice" --tag "mdadm" "Spare device $2 failed on array $1";
    ;;
  "RebuildStarted")
    # Sample: RebuildStarted /dev/md1
    # Action requires 1 parameter
    if [ "$#" -ne 1 ]; then
      logger --priority "local0.debug" --tag "mdadm" \
             "Action [RebuildStarted] Illegal number of parameters: $*";
      exit 1;
    fi
    # Perform custom action
    logger --priority "local0.notice" --tag "mdadm" "Rebuild started on array $1";
    ;;
  "RebuildFinished")
    # Sample: RebuildFinished /dev/md1
    # Action requires 1 parameter
    if [ "$#" -ne 1 ]; then
      logger --priority "local0.debug" --tag "mdadm" \
             "Action [RebuildFinished] Illegal number of parameters: $*";
      exit 1;
    fi
    # Perform custom action
    logger --priority "local0.notice" --tag "mdadm" "Rebuild finished on array $1";
    ;;
  "SpareActive")
    # Sample: SpareActive /dev/md1 /dev/sdb2
    # Action requires 2 parameters
    if [ "$#" -ne 2 ]; then
      logger --priority "local0.debug" --tag "mdadm" \
             "Action [SpareActive] Illegal number of parameters: $*";
      exit 1;
    fi
    # Perform custom action
    logger --priority "local0.notice" --tag "mdadm" "Spare device $2 activated on array $1";
    ;;
  "DeviceDisappeared")
    # Sample: DeviceDisappeared /dev/md/1
    # Action requires 1 parameter
    if [ "$#" -ne 1 ]; then
      logger --priority "local0.debug" --tag "mdadm" \
             "Action [DeviceDisappeared] Illegal number of parameters: $*";
      exit 1;
    fi
    # Perform custom action
    logger --priority "local0.notice" --tag "mdadm" "Array $1 disappeared";
    ;;
  "NewArray")
    # Sample: NewArray /dev/md1
    # Action requires 1 parameter
    if [ "$#" -ne 1 ]; then
      logger --priority "local0.debug" --tag "mdadm" \
             "Action [NewArray] Illegal number of parameters: $*";
      exit 1;
    fi
    # Perform custom action
    logger --priority "local0.notice" --tag "mdadm" "New array $1 has been detected";
    ;;
  *)
    # Perform test action
    logger --priority "local0.debug" --tag "mdadm" "Unknow action $action $*";
    ;;
esac

Replace logger command that makes entries in the system log with your own solution. You can create database entry or just pass information to your own monitoring application.

Monitoring service

Create systemd service file to monitor Linux Software RAID events.

$ sudo cat > /etc/systemd/system/mdmonitor.service << EOF
[Unit]
Description=Linux Software RAID monitor

[Service]
Type=forking
ExecStart=mdadm --monitor --scan --program /path/to/script/mdmonitor.sh --daemonize --pid-file /var/run/mdadm/monitor.pid --no-sharing
PIDFile=/var/run/mdadm/monitor.pid

[Install]
WantedBy=multi-user.target
EOF
Remember to replace /path/to/script/mdmonitor.sh.

Reload systemd manager configuration.

$ sudo systemctl daemon-reload

Enable service at boot time.

$ sudo systemctl enable mdmonitor.service

Start service immediately.

$ sudo systemctl start mdmonitor.service

References

systemd.service — Service unit configuration [freedesktop.org]

mdadm - a tool for managing Linux Software RAID arrays [neil.brown.name]

Milosz Galazka's Picture

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system.

Gdansk, Poland https://sleeplessbeastie.eu