#!/bin/bash
#
# $Id$
#
# checks if a 3ware RAID reports enough OK drives
#

# prepare

. /usr/lib/montools/mt-prepare

RESNAME="HW-RAID (3Ware)"


# work

PATH="/etc/scripts:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"


# FIXME make these configurable too
export NRUNITS="1"
export NRBBUS="1"

CNR=$(tw_cli show |egrep "^c[0-9]" |cut -f1 -d' ')
#logDebug "Detected controller to be '${CNR}'"

OKDRIVES=$(tw_cli /${CNR} show drivestatus |egrep "^p[0-9]" |grep -c "OK")

if [ "$OKDRIVES" != "$DISKS_IN_RAID" ] ; then
  add2criticals \
    "Only $OKDRIVES of $DISKS_IN_RAID hard discs are OK."
else
  add2infos "All $DISKS_IN_RAID hard discs seem OK."
fi


OKUNITS=$(tw_cli /${CNR} show unitstatus |egrep "^u[0-9]" |grep -c "OK")

if [ "$OKUNITS" != "$NRUNITS" ] ; then
  WARNUNITS=$(tw_cli /${CNR} show unitstatus |egrep "^u[0-9]" |egrep -c "OK|VERIFYING")
  if [ "$WARNUNITS" != "$NRUNITS" ] ; then
    add2criticals \
      "Only $OKUNITS of $NRUNITS 3Ware Units are OK or VERIFYING." \
      "For details re-call tw_cli:" \
      "tw_cli /${CNR} show unitstatus"
  else
    # a verification lasts ~10 days, so don't bother to send e-mails
    #add2warnings \
    logNormal "Only $OKUNITS of $NRUNITS 3Ware Units are OK, others are VERIFYING."
    logNormal "For details re-call tw_cli:"
    logNormal "tw_cli /${CNR} show unitstatus"
  fi
else
  add2infos "All $NRUNITS 3Ware Units seem OK."
fi


OKBBUS=$(tw_cli /${CNR} show bbustatus |egrep "^bbu" |grep -c "OK")

if [ "$OKBBUS" != "$NRBBUS" ] ; then
  add2criticals \
    "Only $OKBBUS of $NRBBUS 3Ware BBUs are OK."
else
  add2infos "All $NRBBUS 3Ware BBUs seem OK."
fi


# notifications

. /usr/lib/montools/mt-notify-exit

