#!/bin/ksh

########################################################################################
#
# Name:			tsusage.sh
# Autor:		Alexander Hahnel / Muniqsoft GmbH
# Version:		1.2
# Erstellung:		22.03.2002
# Beschreibung:		Dieses Skript verbindet sich mit der angegebenen Datenbank und
#			berechnet die Auslastung der Tablespaces. Übersteigt der Füllgrad
#			eines Tablespaces den im Skript definierten Schwellwert (NOTIFY),
#			so wird die Ausgabe an die angegebenen Mail-Adressen geschickt.
#
# Aufruf:		tsusage.sh <SID> <mail1> [ <mail2> ... ]
#
# Voraussetzung:  	Die Oracle Umgebung muss bereits korrekt gesetzt sein !
#			(ORACLE_HOME, ORACLE_BASE, NLS_LANG, PATH, ...)
#
#			Zusätzlich müssen folgende Umgebungsvariablen gesetzt sein:
#			LOGDIR	->	Verzeichnis für die Logdatei
#			DBUSER	->	Benutzername mit dem man sich an der Datenbank anmelden will	
#			DBPASS	->	Passwort des Benutzers
#
########################################################################################


# NOTIFY: defines max percent usage - above this value an email will be sent

export NOTIFY=90


HOSTNAME=`hostname`
BASENAME=`basename $0`
USAGE="USAGE: `basename $0` oracle_sid [mailusers]"

if [ `uname` = "DYNIX/ptx" ];then
   GREPPARAM=""
else
   GREPPARAM="-w "
fi

if [ -z "$1" ]; then
   echo $USAGE
   exit 1
fi
shift 1

# store remaining arguments in mailusers
mailusers="$*"
nr_arg="$#"

echo processing database $ORACLE_SID on $HOSTNAME ...

LOGFILE=$LOGDIR/${BASENAME}_${HOSTNAME}_${ORACLE_SID}_`date '+%Y%m%d%H%M%S'`.log
touch $LOGFILE
if [ $? -ne 0 ]; then
   echo "Kann Logdatei $LOGFILE nicht anlegen."
   echo "Abbruch ..."
   exit 1
fi

db_up=`ps -ef | grep $GREPPARAM ora_pmon_${ORACLE_SID} | grep -v "grep ${GREPPARAM}ora_pmon_${ORACLE_SID}" | wc -l`
if  [ $db_up -eq 1 ]; then
   $ORACLE_HOME/bin/sqlplus -s <<EOS >> $LOGFILE
$DBUSER/$DBPASS
      set pages 0
      select '===========================================================================' from dual;
      select 'Wir befinden uns auf der Instanz ' || instance_name || ' - Host ' || host_name ||
             ' - ' || to_char(sysdate,'DD.MM.YYYY HH24:MI') || '.' from v\$instance;
      set pagesize 60
      set linesize 80
      set feedback off
      set tab off

      column gesamtMB head "GESAMT(MB)"
      column usedMB head "BELEGT(MB)"
      column freeMB head "FREI(MB)"
      column pct_used format 990.9

      break on report
      compute sum of gesamtMB on report
      compute sum of usedMB on report
      compute sum of freeMB on report

      set heading on
      select d.tablespace_name Tablespace,
        round(sum(d.bytes/1024/1024))/count(distinct f.file_id||'.'||f.block_id)  gesamtMB,
        round(sum(d.bytes/1024/1024))/count(distinct f.file_id||'.'||f.block_id) -
        round(sum(f.bytes/1024/1024))/count(distinct d.file_id) usedMB,
        round(sum(f.bytes/1024/1024))/count(distinct d.file_id) freeMB,
        (round(sum(d.bytes/1024/1024))/count(distinct f.file_id||'.'||f.block_id) -
        round(sum(f.bytes/1024/1024))/count(distinct d.file_id)) /
        (round(sum(d.bytes/1024/1024))/count(distinct f.file_id||'.'||f.block_id)) * 100 pct_used
          from dba_data_files d,dba_free_space f
        where d.tablespace_name=f.tablespace_name
          group by d.tablespace_name
          order by pct_used desc
      ;

      exit
EOS

   #
   # check SQL output
   #
   SQL_RET=`cat $LOGFILE |
                  nawk ' BEGIN { alloc = 0; notify = ENVIRON["NOTIFY"] }
                         {
                             if ( NF == 5 && substr($0, 1, 10) != "TABLESPACE" && substr($0, 1, 10) != "----------" )
                             {
                                 if ( int($5) > alloc )
                                 {
                                    alloc = int($5)
                                 }
                             }
                         }
                         END { if ( alloc > notify ) print notify } '`

   # ----------------------------------------------------------------------------

   # ... Display the results ...
   #
   if [ ! -z "$SQL_RET" ] ; then
      # ... to all explicit defined mailusers
      if [ $nr_arg -ne 0 ] ; then
         # mail to all explicitly defined users
         for usr in $mailusers
         do
            mailx -s "Message from $BASENAME ($ORACLE_SID on $HOST)" $usr < $LOGFILE
         done
      fi
   fi
fi

