#!/bin/ksh
#
# XARY aka Andrei.Ryjov@spasu.net Fri Sep 26 11:02:25 CEST 2008
#
# check if $RESERVE gb are available in all LVM diskgroups (except root)
#
uname -s | egrep -s AIX || exit 0       # Run on AIX only

TODAY=`date +%Y%m%d`
LASTRUNFILE=/var/tmp/`basename $0`.lastrun
touch $LASTRUNFILE
[ x"`cat $LASTRUNFILE`" = x"$TODAY" ] && exit 0 # Reduce the noise bu running once a day only
echo $TODAY >$LASTRUNFILE

DEFAULT_THRESHOLD=150 # gigabytes
HOSTNAME=`uname -n`
#
# Some groups may have different $RESERVE threshold
#
EXCEPTIONS="

    Hostname   VG or FS-in-VG                           Threshold
	       one FS per VG only, please,
	       FS threshold applies to the whole VG!



    *           rootvg                                      1           # for rootvg, the exception threshold is in MB, for others GB

    nim-cal     /export/dump                                2           # We will never grow the FS in rush here
    nim-cor     /export/dump                                2           # We will never grow the FS in rush here


    cerf        *                                           0           # As of Nov.2008, cerf needs no reserve in any VG (management desigion)

    puma        *                                           0           # New reserve policy since Fri Jan 23 11:21:47 WEST 2009
    tisserin    *                                           0           # New reserve policy since Fri Jan 23 11:21:47 WEST 2009
    triton      *                                           0           # New reserve policy since Fri Jan 23 11:21:47 WEST 2009


    canard      *                                          25           # App server, growth is more predictable than with DB
    nette       *                                          25           # App server, growth is more predictable than with DB

"

RC=0 # Return Code

for i in `lsvg -L`; do

  MPOINTS=`lsvg -Ll $i | awk '$2=="jfs2" && $NF~/^\// {print $NF}'`

  #
  # See if the group and/or host is in the exception list
  # Try to match the hostname + vgname first (highest priority),
  # then try hostname+mountpoint-within-this-group
  # then try hostname+wildcard, then widcard+VG,
  # and then fallback to default
  #
  NEW_THRESHOLD=`echo "$EXCEPTIONS" | awk '$1=="'$HOSTNAME'" && $2=="'$i'" {print $3}' | tail -1`

  echo $NEW_THRESHOLD | egrep -s '^[0-9]+$' || {
    #
    # OK, host+VG didnt match.
    # In the exception list, select the mountpoint(s) that belong to current host & VG
    #
    MPOINTS=`(echo "$MPOINTS"; echo "$EXCEPTIONS" | awk '$1=="'$HOSTNAME'" && $2~/^\// {print $2}') | sort | uniq -c | awk '$1>1 {print $2}'`
    #
    # If (accidentially) multiple matching mount points from this HOST/VG
    # found in the exception list, take the largetst threshold
    #
    NEW_THRESHOLD=`for m in $MPOINTS; do echo "$EXCEPTIONS" | awk '$1=="'$HOSTNAME'" && $2=="'$m'" {print $3}'; done | sort -n | tail -1`
  }

  #
  # Try hostname and any VG (wildcard)
  #
  echo $NEW_THRESHOLD | egrep -s '^[0-9]+$' || NEW_THRESHOLD=`echo "$EXCEPTIONS" | awk '$1=="'$HOSTNAME'" && $2=="*" {print $3}' | tail -1`
  #
  # No match so far - try any host (wildcard) + specific VG name (good for rootvg, for instance. Other VGs may be dangerous to mix among hosts!)
  #
  echo $NEW_THRESHOLD | egrep -s '^[0-9]+$' || NEW_THRESHOLD=`echo "$EXCEPTIONS" | awk '$1=="*" && $2=="'$i'" {print $3}' | tail -1`


  #
  # No match in Exceptions? Fallback to default
  #
  echo $NEW_THRESHOLD | egrep -s '^[0-9]+$' || NEW_THRESHOLD=$DEFAULT_THRESHOLD


  CURRENT_RESERVE=`lsvg -L $i | awk '/FREE/ {print $(NF-1)}' | sed 's/(//' | awk '{printf("%d", $1)}'`

  [ x"$i" != x"rootvg" ] && CURRENT_RESERVE=`expr $CURRENT_RESERVE / 1024` # for rootvg, the exception threshold is in MB, for others GB

  #
  # Calculate the PP per LV ratio, and divide $CURRENT_RESERVE by it
  #
  # However, alerting for not fully mirrored groups is done in check_Mirrors
  # (for instance, mirrored VGs  need double reserve)
  #
  R=`for v in \`lsvg -lL $i | awk '$NF~/\// {print $1}'\`; do
    lslv -L \$v | awk '/^LPs:/ {print $4/$2}'
  done | sort -u | sort -n | head -1` # we take the minimal ratio here, to get alerts earlier

  echo $R | egrep -s '[0-9]' && {

    #
    # Adjust threshold to take in account mirroring
    #
    CURRENT_RESERVE=`expr $CURRENT_RESERVE / $R`

    #
    # Make sure mirrored VGs hav quorum disabled,
    # and unmirrored - enabled
    #
    QUORUM=`lsvg -L $i | awk '/QUORUM:/ {print $NF}'`
    [ $R -eq 1  -a  x"$QUORUM" = x"(Enabled)"  ] || [ $R -gt 1  -a  x"$QUORUM" = x"(Disabled)" ] || { printf "Wrong QUORUM: $i\n"; RC=1; }
  }

  #
  # Now, finally, check if we are below the threshol
  #
  [ $CURRENT_RESERVE -lt $NEW_THRESHOLD ] && {
    RC=1
    MESSAGE="$MESSAGE\n`printf \"%-10s %10d gb left\" $i $CURRENT_RESERVE"
  }
done

[ $RC -eq 0 ] && exit 0


printf "Disk space reserve below threshold\n\n$MESSAGE\n"
exit $RC
