MonkeyBrains.net/~rudy/example Random examples

raid_status.sh
< /table>

What good is your raid if you don't know when it goes bad?

I've been working on a cronjob that checks the raid status for various raid setups for a few years. This script is a 'universal' one in that it runs on Linux and FreeBSD and checks software raids, 3ware raids, promise raid arrays, and some other types. I've mostly developed it for FreeBSD, so be sure to test this out if you plan on using it anywhere.
#!/bin/sh

# raid_status - check the state of the RAID. 

# This script works for various types of RAID devices.  (Currently, 3Ware,
# gmirror, BSd 'ar0' raids, zpool)
# WARNING: Install the proper CLI program for your 3ware card, if you use 3ware.

# Set up a cronjob like this:
# */16 * * * * /home/rudy/bin/raid_status CRON

### Copyright (c) 2006, Rudy Rucker All rights reserved.
### Redistribution and use of script, with or without modification, is
### permitted provided that the following condition is met:
###    Redistributions of source code must retain the above copyright
###    notice, this list of conditions and the following disclaimer.
### THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
### ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
### IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
### ARE DISCLAIMED.

# ----------- Change Log ------------
# Mon Oct 11 15:20:37 PDT 2004 - rudy
#  Original script.
# Tue Feb  7 01:28:07 PST 2006 - rudy
#  Added 9500 and 9550 support
# Fri Jun  9 10:38:33 PDT 2006 - rudy
#  works for 'ar' and 'tw' mirrored arrays
# Tue Sep 12 10:23:13 PDT 2006 - rudy
#  Added gmirror and realized that not all 3ware's are the same...
# Fri Jan 18 00:46:13 PST 2008 - rudy
#  going to add support for multiple geom mirrors. gm0s1, gm0s2, etc...
# Fri Jan 18 01:03:25 PST 2008 - rudy
#  added zpool status checking.  untested  :)
# Fri Jan 18 01:23:54 PST 2008
#  can check machines with multiple array types... got zfs and gmirror?
#  tested a machine with zfs and gmirror
# ---------------------------------------------------------------------

MODE=$1

TWCLI="/usr/local/sbin/tw_cli"
GMIRROR="/sbin/gmirror"
ATACONTROL="/sbin/atacontrol"
MEGA="/usr/local/sbin/MegaCli"


AWK="/usr/bin/awk"
GREP="/usr/bin/grep"
MAIL="/usr/bin/mail"
SORT="/usr/bin/sort"
ZPOOL="/sbin/zpool"

EMAIL="noc@example.com"

STATUS="none_tested"

report_status () {
	# Okay, we checked the raid status and know what the return code should be.
	if [ "$STATUS" = "$VALID" ]; then
		if [ "$MODE" != "CRON" ]; then
			echo "OK condition"; 
			$ESTATUS_CMD
	                echo
		fi
	else
		# ERROR!  Either print to TTY or send an email, based on MODE (which is arg[1])
		if [ "$MODE" = "CRON" ]; then
			$ESTATUS_CMD | $MAIL -s "[ERROR] Raid array on $HOST returned $STATUS" $EMAIL
		else
			echo "ERROR condition"
			$ESTATUS_CMD
		fi
        fi
        STATUS="UNKNOWN"
}

# if this is not a 3ware card, check the atacontol
if [ -c /dev/twed0 ] && [ -x $TWCLI ]; then
	# Tested: 3ware card ... 8000 series
	STATUS=`$TWCLI info c0 u0 | $GREP "^Status" | $AWK {'print $2'}`;
	VALID='OK'
	ESTATUS_CMD="$TWCLI info c0 u0";
	# double check the 3ware output incase it returned nada...
	# Umm... this is the only raid card I have witness this bug
	if [ "X$STATUS" = "X" ]; then
		sleep 1;
		STATUS=`$TWCLI info c0 u0 | $GREP "^Status" | $AWK {'print $2'}`;
	fi
        report_status
elif [ -c /dev/mfi0 ] && [ -x $MEGA ]; then
    # LSI MegaRaid
    # tested: SAS 2108 [Liberator]
    STATUS=`$MEGA -LDInfo -Lall -aALL | $GREP ^State | $AWK -F ': *' '{print $2}'`
    VALID='Optimal'
    ESTATUS_CMD="$MEGA -LDInfo -Lall -aALL"
    report_status
elif [ -c /dev/da0 ] && [ -x $TWCLI ]; then
	# Note, there are plenty of other device names that use da0... 
	# if you have the TWCLI installed, assume da0 is a RAID.
	# Tested: 3ware 9550SX, 9500S, 9650
	STATUS=`$TWCLI info c0 | $GREP "^u0" | $AWK '{print $3}'`;
	VALID='OK'
	ESTATUS_CMD="$TWCLI info c0 u0"
        report_status
fi
if [ -d /dev/mirror ] && [ -x $GMIRROR ]; then
	# gmirror /dev/mirror/gm0 gm0s1 gm0s2 etc...
	# the 'sort -u' allows this to test multiple arrays.  Neat hack.  :)
	STATUS=`$GMIRROR status | $GREP "^mirror" | $AWK {'print $2'} | $SORT -u`;
	VALID='COMPLETE'
	ESTATUS_CMD="$GMIRROR list";
        report_status
fi
if [ -c /dev/ar0 ] && [ -x $ATACONTROL ]; then
	# Motherboard promise and others
	STATUS=`$ATACONTROL status ar0 | $GREP "status" | $AWK -F 'status: ' '{print $2}'`;
	VALID='READY'
	ESTATUS_CMD="/sbin/atacontrol status ar0"
        report_status
fi
if [ -c /dev/zfs ] && [ -x $ZPOOL ]; then
	# Tested:  zpool mirror on FreeBSD 7.0
	STATUS=`$ZPOOL status -x`;
	VALID='all pools are healthy'
	ESTATUS_CMD="$ZPOOL status -v";
        report_status
fi
if  [ "$STATUS" = "none_tested" ]; then
	echo "Unknown Raid type.... ";
	if [ -x $TWCLI ]; then
		echo " + found $TWCLI";
	else
		echo " - can't exec $TWCLI";
	fi
	if [ -x $ATACONTROL ]; then
		echo " + found $ATACONTROL";
	else
		echo " - can't exec $ATACONTROL";
	fi
	if [ -x $ZPOOL ]; then
		echo " + found $ZPOOL";
	else
		echo " - can't exec $ZPOOL";
	fi
	if [ -x $GMIRROR ]; then
		echo " + found $GMIRROR";
	else
		echo " - can't exec $GMIRROR";
	fi
	exit;
fi

  • Resource for FreeBSD gmirror software RAID [Note from the year 2017: don't use this, use ZFS]
  • Resource for Linux software RAID [Note from 2017: there are newer tools/examples, this is old]
  • Resource for 3ware RAID cards [Note from 2017: 3ware got bought by a couple of different companies, does it still exist?]
  • MegaCLI usage guide
    Final note from the year 2017: if you are on FreeBSD, use ZFS.