#!/bin/sh
#
# Contributed by Wulf Dietrich - w_dietrich@hotmail.com 10/2/2000
#
# 08/03/2000: support for Oracle 8.1 added
#             minor changes in msg-handling
#
# check if filesystem size is big enough when having datafiles with
# autoextend enabled
# Parameter in etc/agents file: ORACLE_SID
#
# need to run ora_create_ops_user before using this agent
#
# Usage:
#contrib:ora_aextfs:8-18:1440:0:enabled:tst1 tst2:
#
# developed on AIX

#. /usr/local/bcnu/etc/bcnuenv
. $BCNUHOME/agent/agent_head

sort_by_severity()
{
        sed -e "s/^ok /wok /g" $1 | sort $2 | sed "s/^wok /ok /g"
}

DFSZFILE=$BCNUTMP/${BCNUAGENT}.dfsz
FSSZFILE=$BCNUTMP/${BCNUAGENT}.fssz
DFFSFILE=$BCNUTMP/${BCNUAGENT}.dffs
OUTFILE=$BCNUTMP/${BCNUAGENT}.out
MSGFILE=$BCNUTMP/${BCNUAGENT}.msg
TMPFILE=$BCNUTMP/${BCNUAGENT}.tmp

rm -f $DFSZFILE $FSSZFILE $DFFSFILE $OUTFILE $MSGFILE $TMPFILE
bcnu_check_err "remove temporary files"

OK=1
ERR=""
WARN=""
WSPACE=`printf " \t"`

# we need this for numeric format used by awk (dezimal point) on AIX systems
export LANG=C


for param in $BCNUPARAM ; do

  bcnu_param
  SID=$p1

  ORACLE_HOME=`grep "^$SID:" $ORATAB | cut -d: -f2`
  if [ -z "$ORACLE_HOME" ] ; then
    WARN=1
    printf "%-7s - Database Instance %s:\t ORACLE_SID $SID not found in $ORATAB\n" warning $ORACLE_SID >> $MSGFILE
    bcnu_check_err "write to file $MSGFILE"
    continue
  fi

  export ORACLE_HOME=$ORACLE_HOME
  export PATH=$ORACLE_HOME/bin:$PATH
  export ORACLE_SID=$SID

  if echo $ORACLE_HOME | $GREP "7.3" >/dev/null ; then
    sqlplus -s / > $TMPFILE <<EOF
set pagesize 0
set linesize 160
set heading off
set feedback off
set verify off
select
  value
from
  v\$parameter
where
  name = 'db_block_size'
  ;
quit
EOF
    bcnu_check_err "select db_block_size on SID $SID" $TMPFILE '^ORA-'
    DB_BLOCK_SIZE=`cat $TMPFILE`
    sqlplus -s / >> $DFSZFILE <<EOF
set pagesize 0
set linesize 160
set heading off
set feedback off
set verify off
select
  df.file_name
  || ' ' || '$SID '
  || ' ' || to_char(ae.maxextend*$DB_BLOCK_SIZE/1024/1024, '999999999.999')
from
  dba_data_files df,
  sys.filext\$ ae
where
  df.file_id = ae.file#
  ;
quit
EOF
  bcnu_check_err "select on SID $SID" $DFSZFILE '^ORA-'
  
  elif echo $ORACLE_HOME | $GREP -e "8.0" -e "8.1" >/dev/null ; then
 
    sqlplus -s / >> $DFSZFILE <<EOF
set pagesize 0
set linesize 160
set heading off
set feedback off
set verify off
select
  df.file_name
  || ' ' || '$SID '
  || to_char(df.maxbytes/1024/1024, '999999999.999')
from
  dba_data_files df
where
  df.autoextensible = 'YES'
  ;
quit
EOF
  bcnu_check_err "select on SID $SID" $DFSZFILE '^ORA-'

  else

    WARN=1
    printf "%-7s - Database Instance %s:\t unexpected DB-Version\n" warning $ORACLE_SID >> $MSGFILE
    bcnu_check_err "write to file $MSGFILE"

  fi

done

if ! [ -s $DFSZFILE ] ; then
  # nothing to do
  if [ $WARN ] ; then
    printf "Monitored Database Instances:\n\n" > $OUTFILE
    bcnu_check_err "create output file"

    sort_by_severity $MSGFILE '-k 1' >> $OUTFILE
    bcnu_check_err "sort messages"

    BCNUMSG="$BCNU -m 'warning - autoexted/fs instable' -e $BCNU_WARN -t $BCNUHOSTTYPE -f $OUTFILE $BCNUHOST"
    bcnu_warn_send
  else
    BCNUMSG="$BCNU -m 'ok - autoextend not enabled' -e $BCNU_OK -t $BCNUHOSTTYPE $BCNUHOST"
    bcnu_send
  fi
  exit 0
fi

$DF > $TMPFILE
bcnu_check_err "execute df"

# get the name of filesystem column
let COL=1
FSCOL=""
for FIELD in `egrep -e "^/[$WSPACE]" -e "[$WSPACE]?/$" $TMPFILE` ; do
  if [ "$FIELD" = "/" ] ; then
    let FSCOL=$COL
    break
  fi
  let COL+=1
done
if [ "$FSCOL" = "" ] ; then
  false
  bcnu_check_err "unexpected df-output"
fi


# convert blocks to MBs, works for 512 Byte and 1024 Kbyte Blocks
awk -v FSCOL=$FSCOL '{
  if ( NR == 1 ) {
    BTOMB=1024;
    if ( index($0, 512) != 0 ) {
      BTOMB=2048;
    }
  }
}
/^\// {
  printf "%s %0.3f\n", $FSCOL, $2/BTOMB;
}' $TMPFILE > $FSSZFILE
bcnu_check_err "awk df-output processing "

sort $FSSZFILE -o $FSSZFILE
bcnu_check_err "sort filesystems with sizes"

# find the filesystem for each datafile
for FILE in `cut -d" " -f1 $DFSZFILE` ; do
  FS=`dirname $FILE`
  while [ $FS != "/" ] ; do
    if grep "^$FS " $FSSZFILE >/dev/null ; then
      # I found it
      echo $FILE $FS
      break
    fi
    FS=`dirname $FS`
    if [ $FS = "/" ] ; then
      echo $FILE $FS
    fi
  done
done > $DFFSFILE

sort $DFFSFILE -o $DFFSFILE
bcnu_check_err "sort file1"

sort $DFSZFILE -o $DFSZFILE
bcnu_check_err "sort file2"

join $DFFSFILE $DFSZFILE > $TMPFILE
bcnu_check_err "join files"

sort -k 2 $TMPFILE -o $DFFSFILE
bcnu_check_err "sort by filesystem"

join -1 1 -2 2 $FSSZFILE $DFFSFILE > $TMPFILE
bcnu_check_err "join with filesystem sizes"

$AWK 'BEGIN {OLD_FSYS=""; SZ_NEED=0;} {
FSYS=$1
SZ=$2
DF=$3
SID=$4
DFSZ=$5
  if ( FSYS == OLD_FSYS ) {
    SZ_NEED=SZ_NEED+DFSZ;
    if ( SID != OLD_SID ) {
      SID_LIST=SIDLIST", "SID;
    }
  }
else {
  if ( OLD_FSYS != "" ) {
    if ( OLD_SZ_NEED > OLD_SZ ) {
      SEVERITY="error";
    }
    else {
      SEVERITY="ok";
    }
    printf "%-7s - Database Instance %s:\t Filesystem %-35s Size is %14.3fMB, Need %14.3fMB\n", SEVERITY, SID_LIST, OLD_FSYS, OLD_SZ, OLD_SZ_NEED;
    }
  SZ_NEED=DFSZ;
  SID_LIST=SID
  }
  OLD_FSYS=FSYS;
  OLD_DF=DF;
  OLD_SID=SID;
  OLD_SZ=SZ;
  OLD_SZ_NEED=SZ_NEED;
}
END {
  if ( OLD_SZ_NEED > OLD_SZ ) {
    SEVERITY="error";
  }
  else {
    SEVERITY="ok";
  }
  printf "%-7s - Database Instance %s:\t Filesystem %-35s Size is %14.3fMB, Need %14.3fMB\n", SEVERITY, SID_LIST, OLD_FSYS, OLD_SZ, OLD_SZ_NEED;
}' < $TMPFILE >> $MSGFILE
bcnu_check_err "awk processing"

if grep "^error" $MSGFILE >/dev/null ; then
  OK=""
  ERR=1
fi
if grep "^warning" $MSGFILE >/dev/null ; then
  OK=""
  WARN=1
fi

printf "Monitored Database Instances:\n\n" > $OUTFILE
bcnu_check_err "create output file"

sort_by_severity $MSGFILE '-k 1 -k 5' >> $OUTFILE
bcnu_check_err "sort messages"

printf "\nFiles of Database Instances:\n\n" >> $OUTFILE
bcnu_check_err "append output file"

cat $TMPFILE >> $OUTFILE
bcnu_check_err "append output file"

if [ $OK ] ; then
  BCNUMSG="$BCNU -m 'ok - autoexted/fs stable' -e $BCNU_OK -t $BCNUHOSTTYPE -f $OUTFILE $BCNUHOST"
    bcnu_send
else
  if ! [ $ERR ] ; then
    if [ $WARN ] ; then
      BCNUMSG="$BCNU -m 'warning - autoexted/fs instable' -e $BCNU_WARN -t $BCNUHOSTTYPE -f $OUTFILE $BCNUHOST"
      bcnu_warn_send
    fi
  else
    BCNUMSG="$BCNU -m 'error - autoexted/fs instable' -e $BCNU_ERR -t $BCNUHOSTTYPE -f $OUTFILE $BCNUHOST"
    bcnu_err_send
  fi
fi
