#!/bin/ksh
#
# check_paging
#

PAGWLEVEL=20  # AIX = Paging space used (supplementary alarm)
PAGELEVEL=30
MEMWLEVEL=90  # Memory used alarms, AIX = AVM, Solaris = Memory used
MEMELEVEL=95  # AIX = AVM
OS=$(uname)
HOSTNAME=$(hostname)

SCRIPTNAME=`basename $0`
LASTEFILE=/var/tmp/$SCRIPTNAME.lasterr

usage () {
  echo "$(basename $0) [-w <warning> ] [-e <error] [-d]"
}

debug () {
  if [ -n "$DEBUG" ]
  then
    echo $*
  fi
}

verbose () {
  if [ -n "$VERBOSE" ]
  then
    echo $*
  fi
}

if ! set -- $(getopt "w:e:dv" $*)
then
  usage
  exit 4
fi

while [ "$1" != "--" ]
do
  case $1 in
    -e) ELEVEL=$2; shift;;
    -w) WLEVEL=$2; shift;;
    -d) DEBUG=-d;VERBOSE=-v;;
    -v) VERBOSE=-d;;
    *) echo "Error: Flag '$1' not implemented"; usage; exit 4;;
  esac
  shift
done
shift

# check for overrides in exception file
grep "$HOSTNAME" "$0.exception" | read H P1 P2 P3 P4 DUMMY
if [ -n "$P2" ]
then
  MEMWLEVEL=$P1
  MEMELEVEL=$P2
  if [ -n "$P3" ]
  then
    PAGWLEVEL=$P3
    PAGELEVEL=$P4
  fi
  verbose "Alarm level overrides found: Warn=$P1, Err=$P2, PagW=$P3, PagE=$P4"
fi

case "$OS" in
  AIX)
    AVM=$(vmstat | tail -1 | awk '{print $3}')
    MEM=$(vmstat -v | awk '/memory pages/{print $1}')
    let "MEMUSED = 100 * $AVM / $MEM"
    if [ "$MEMUSED" -gt "$MEMWLEVEL" ]; then WARN=1; EMSG="Warning: avm is ${MEMUSED}% (>${MEMWLEVEL}%)"; fi
    if [ "$MEMUSED" -gt "$MEMELEVEL" ]; then ERR=2; EMSG="Error: avm is ${MEMUSED}% (>${MEMELEVEL}%)"; fi
    PUSED=$(lsps -ls | awk '/%/{print $2}')
    PUSED=${PUSED%\%}
    PSTAT=$(lsps -a)
    if [ "$PUSED" -gt "$PAGWLEVEL" ]; then WARN=1; EMSG="Warning: paging space ${PUSED}% full (>$PAGWLEVEL)"; fi
    if [ "$PUSED" -gt "$PAGELEVEL" ]; then ERR=2; EMSG="Error: paging space ${PUSED}% full (>$PAGELEVEL)"; fi
    ;;
  SunOS)
    if [ "$(uname -r)" == "5.7" ]
    then
      debug "Hack for SunOS 7"
      df -k /tmp >/dev/null; sleep 1
      df -k /tmp >/dev/null; sleep 1
      df -k /tmp >/dev/null; sleep 1
      df -k /tmp >/dev/null; sleep 1
    fi
    MEMTOT=$(prtconf | awk '/Memory/{print $3}')
    SWAPTOT=$(swap -l | awk '/\/dev/{TOT+=$4} END{printf "%.0f",TOT/2048}')
    TMPSIZ=$(df -k /tmp | awk '/swap/{printf "%.0f",$2/1024}')
    let "MEMUSE = $MEMTOT + $SWAPTOT - $TMPSIZ"
    let "PUSED = 100 * $MEMUSE / $MEMTOT"
    PSTAT="Total Memory: $MEMTOT  (prtconf)"
    PSTAT="$PSTAT\nTotal Swap  : $SWAPTOT  (swap -l)"
    PSTAT="$PSTAT\nTmp Size    : $TMPSIZ  (df -k /tmp)"
    PSTAT="$PSTAT\nUsed Memory : $MEMUSE  (mem+swap-tmp)"
    PSTAT="$PSTAT\nMemory Percent: $PUSED"
    PSTAT="$PSTAT\n$(swap -l)"
    if [ "$PUSED" -gt "$MEMWLEVEL" ]; then WARN=1; EMSG="Warning: memory ${PUSED}% used (>$MEMWLEVEL)"; fi
    if [ "$PUSED" -gt "$MEMELEVEL" ]; then ERR=2; EMSG="Error: memory ${PUSED}% used (>$MEMELEVEL)"; fi
    ;;
  Linux)
    PUSED=1
    EMSG="Linux not implemented"
    PSTAT="Linux not implemented"
    ;;
esac


[ -z "$ERR$WARN" ] && { echo "Pagingspace OK"; echo "$PSTAT"; exit 0; }

#
# Check if the same message has already appeared within the same hour
# If so, just exit. Otherwise, update the statusfile
#
STAMP="`date +%Y%m%d%H` $EMSG"
[ "`cat $LASTEFILE 2>/dev/null`"  =  "$STAMP"  ] && {  echo "Pagingspace bad but already reported"; exit 0; }

echo "$STAMP" >$LASTEFILE

[ -n "$ERR" ]   && { echo "$EMSG"; echo "$PSTAT"; exit 2; }
[ -n "$WARN" ]  && { echo "$EMSG"; echo "$PSTAT"; exit 1; }

echo "Pagingspace status undefined"; echo "$PSTAT"; exit 3;
