#!/bin/ksh
#****************************************************************************
#
# (C) Copyright CPIC. 2008
#
# File name: check_db_alert.sh
# Project: Database Alerts Check
#
# Parameter : app_name -- name of db application(=oracle_sid)
# ipaddress -- ip of the db server
#
# Creation Date: Aug 19, 2008
# Origin Author: Shangkun Peng
#
#
# Prerequisites:
# before using this shell script, directory $DAILY_CHECK must be created
# and this shell script should be put into it.
# for each application, environment file $DBENVFILE must be put into it.
# also, variables for auto ftp must be set
#
# Limitations:
# this shell script can be run at most once an hour
# additional job can be added but variable JOB_LIST must be modified
# and relative function must be coded too
#
#****************************************************************************
# record start date and time
echo "------------------------------------------------------------"
echo "database information collect started at:"; date
# check necessary parameter(s)
if [ $# -ne 2 ]
then
echo "ERROR: parameter(s) not supplied correctly"
echo "USAGE: check_db_alert.sh app_name ipaddress"
echo "WHERE:"
echo "app_name -- name of db application"
echo "ipaddress -- ip of the db server"
exit 1
fi
# -----------------------------------------------------------------------------------------------------------
# DEFINE VALUES FOR VARIABLES IN THIS SHELL
# general variables, can not be modified
APP_NAME=$1 # name of db application
IPADDRESS=$2 # ip of db server
DAILY_CHECK=/tmp/db_dailycheck # directory for information collection of db application
DBENVFILE=$APP_NAME".dbenv" # name of environment file
DATE=`date +%y%m%d` # records shell running date
HOUR=`date +%H` # records shell running hour
# configuration variables, can be modified
RETENTION=30 # days to retain result files
SUFFIX=pre # suffix for result files before ftp
SUFFIX1=bak # suffix for result files after ftp
FORMAT=$DATE"."$HOUR"."$SUFFIX # name format for result files before ftp
# auto ftp variables
FTP_DEST_HOST=10.193.16.86 # ip address of information repository server
FTP_USER=ftpuser # ftp user name on information repository server
FTP_PWD=1qaz2wsx # corresponding password for ftp user above
FTP_DEST_DIR=//tbdb_check/db_dailycheck/$DATE # directory for receiving on information repository server
# job list variables
# include jobs in double quotation marks and separate them with space,
# when add a job, add explanations here and add relative function below
# job1: get the errors of alert_$ORACLE_SID.log within retention
# job2: get the tablespaces which useage>90% and freeage<5G
# job3: get the alert.log,listener.log,sqlnet.log size
JOB_LIST="job1 job2 job3 job4 job5" # all the jobs need to be done
# -----------------------------------------------------------------------------------------------------------
# check if necessory environment file $DBENVFILE exists
# and the shell has not be run in the same hour already
if [ ! -f $DAILY_CHECK"/"$DBENVFILE ]
then
echo "ERROR: necessory environment file ("$DBENVFILE") does not exist in "$DAILY_CHECK" directory"
exit 1
elif [ -f $DAILY_CHECK"/"$APP_NAME.*.$FORMAT* ]
then
echo "ERROR: this shell script has already been run in this hour, it can NOT be run more than once an hour"
exit 1
fi
# -----------------------------------------------------------------------------------------------------------
# get into directory for information collection of db application
# and set db environment variables before beginning information collection
cd $DAILY_CHECK
. $DAILY_CHECK"/"$DBENVFILE
# sqlplus varivale
SQLUSER="/ as sysdba"
ALERTFILET=$DAILY_CHECK/alertfilet.txt
sqlplus -S "$SQLUSER" <<EOF
set echo off
set feedback off
set heading off
set pagesize 0
set linesize 100
set trimspool on
spool $ALERTFILET
select a.value||'/alert_'||b.value||'.log' from v/$parameter a,v/$parameter b
where a.name='background_dump_dest' and b.name='instance_name';
spool off
set echo on
set feedback on
set heading on
set pagesize 1000
set linesize 1000
set trimspool off
EOF
ALTERFILE=`head -1 $ALERTFILET`
rm -f $ALERTFILET
# -----------------------------------------------------------------------------------------------------------
# delete outdated files
find $DAILY_CHECK -name $APP_NAME".*."$SUFFIX1 -mtime +$RETENTION -exec rm -f {} /;
# -----------------------------------------------------------------------------------------------------------
# FUNCTION AREA
# function to collect capacity information
# job1: get the errors of alert_$ORACLE_SID.log within retention
# job2: get the tablespaces which useage>90% and freeage<5G
# job3: get the alert.log,listener.log,sqlnet.log size
job1()
{
#job1 variables
today=`date +%a`
username="/ as sysdba"
datefile=./date.txt
alertfile=$ALTERFILE
cd $DAILY_CHECK
touch date.txt
touch alert
echo $IPADDRESS"."$APP_NAME > $APP_NAME"."$FORMAT
echo "/n" >> $APP_NAME"."$FORMAT
echo "alert{SID}.log is processing..."
echo "1.alert.log check" >> $APP_NAME"."$FORMAT
echo '/n/n--------------------------------------------------/n/n' >> $APP_NAME"."$FORMAT
sqlplus -S "$username" << EOF
set echo off
set feedback off
set heading off
set pagesize 0
set linesize 100
set trimspool on
spool $datefile
select to_char(sysdate-1,'yyyy') from dual;
select to_char(sysdate-1,'Mon') from dual;
select to_char(sysdate-1,'dd') from dual;
select initcap(substr(to_char(sysdate-1,'day'),1,3)) from dual;
select to_char(sysdate-3,'yyyy') from dual;
select to_char(sysdate-3,'Mon') from dual;
select to_char(sysdate-3,'dd') from dual;
select initcap(substr(to_char(sysdate-3,'day'),1,3)) from dual;
spool off
set echo on
set feedback on
set heading on
set pagesize 1000
set linesize 1000
set trimspool off
EOF
if [ $today != "Mon" ]; then
year=`sed -n '1p' date.txt`
month=`sed -n '2p' date.txt`
day=`sed -n '3p' date.txt`
weekday=`sed -n '4p' date.txt`
else
year=`sed -n '5p' date.txt`
month=`sed -n '6p' date.txt`
day=`sed -n '7p' date.txt`
weekday=`sed -n '8p' date.txt`
fi
if [ "$day" -lt "10" ]; then
nday=`echo $day | cut -c2`
sed -n "/$weekday $month $nday [0-9][0-9]:[0-9][0-9]:[0-9][0-9] $year/=" $alertfile > temp
else
sed -n "/$weekday $month $day [0-9][0-9]:[0-9][0-9]:[0-9][0-9] $year/=" $alertfile > temp
fi
if [ -s temp ]; then
beginline=`head -1 temp`
tail +"$beginline" $alertfile /
| sed 's/Mon [A-Z][a-z][a-z] /*sdmei* &/p' | uniq /
| sed 's/Tue [A-Z][a-z][a-z] /*sdmei* &/p' | uniq /
| sed 's/Wed [A-Z][a-z][a-z] /*sdmei* &/p' | uniq /
| sed 's/Thu [A-Z][a-z][a-z] /*sdmei* &/p' | uniq /
| sed 's/Fri [A-Z][a-z][a-z] /*sdmei* &/p' | uniq /
| sed 's/Sat [A-Z][a-z][a-z] /*sdmei* &/p' | uniq /
| sed 's/Sun [A-Z][a-z][a-z] /*sdmei* &/p' | uniq /
> alert
else
tail -5000 $alertfile /
| sed 's/Mon [A-Z][a-z][a-z] /*sdmei* &/p' | uniq /
| sed 's/Tue [A-Z][a-z][a-z] /*sdmei* &/p' | uniq /
| sed 's/Wed [A-Z][a-z][a-z] /*sdmei* &/p' | uniq /
| sed 's/Thu [A-Z][a-z][a-z] /*sdmei* &/p' | uniq /
| sed 's/Fri [A-Z][a-z][a-z] /*sdmei* &/p' | uniq /
| sed 's/Sat [A-Z][a-z][a-z] /*sdmei* &/p' | uniq /
| sed 's/Sun [A-Z][a-z][a-z] /*sdmei* &/p' | uniq /
> alert
fi
if [ `cat alert | grep ORA- | wc -l` -gt "0" ]; then
point1=1
point2=2
sed -n '/*sdmei*/=' alert > templine
totline=`cat templine | wc -l`
while [ $point1 -lt $totline ]
do
firstline=`sed -n "${point1}p" templine`
secondline=`sed -n "${point2}p" templine`
secondline2=`expr $secondline - 1`
oraline=`sed -n "${firstline},${secondline2}p" alert | grep ORA- | wc -l`
if [ $oraline -gt "0" ]; then
sed -n "${firstline},${secondline2}p" alert | sed 's//*sdmei/*/----------/g' >> $APP_NAME"."$FORMAT
fi
point1=`expr $point1 + 1`
point2=`expr $point2 + 1`
done
rm -f templine
else
echo "no errors" >> $APP_NAME"."$FORMAT
fi
rm -f alert
rm -f date.txt
rm -f temp
echo '/n/n--------------------------------------------------/n/n' >> $APP_NAME"."$FORMAT
}
job2()
{
echo '2.表空间可用空间信息 ' >> $APP_NAME"."$FORMAT
echo '/n/n--------------------------------------------------/n/n' >> $APP_NAME"."$FORMAT
CHECKSQL=$DAILY_CHECK/checkspace.sql
echo "set linesize 180" > $CHECKSQL
echo "col tablespace_name for a60" >> $CHECKSQL
echo "set pagesize 9999" >> $CHECKSQL
echo "set wrap off" >> $CHECKSQL
echo "select" >> $CHECKSQL
echo "tablespace_name," >>$CHECKSQL
echo "megs_alloc init_sizeM," >>$CHECKSQL
echo "max total_sizeM," >>$CHECKSQL
echo "max-megs_used free_sizeM," >>$CHECKSQL
echo "megs_used used_sizeM," >>$CHECKSQL
echo "round((max-megs_used)/(max+0.001) *100,1) free_rate" >>$CHECKSQL
echo "from" >>$CHECKSQL
echo "(select a.tablespace_name," >>$CHECKSQL
echo "round(a.bytes_alloc / 1024 / 1024, 2) megs_alloc," >>$CHECKSQL
echo "round(nvl(b.bytes_free, 0) / 1024 / 1024, 2) megs_free," >>$CHECKSQL
echo "round((a.bytes_alloc - nvl(b.bytes_free, 0)) / 1024 / 1024, 2) megs_used," >>$CHECKSQL
echo "round(maxbytes/1048576,2) Max" >>$CHECKSQL
echo "from ( select f.tablespace_name," >>$CHECKSQL
echo " sum(f.bytes) bytes_alloc," >>$CHECKSQL
echo " sum(decode(f.autoextensible, 'YES',f.maxbytes,'NO', f.bytes)) maxbytes" >>$CHECKSQL
echo " from dba_data_files f" >>$CHECKSQL
echo " group by tablespace_name) a," >>$CHECKSQL
echo "( select f.tablespace_name," >>$CHECKSQL
echo " sum(f.bytes) bytes_free" >>$CHECKSQL
echo " from dba_free_space f" >>$CHECKSQL
echo " group by tablespace_name) b" >>$CHECKSQL
echo "where a.tablespace_name = b.tablespace_name (+)" >>$CHECKSQL
echo "union all" >>$CHECKSQL
echo "select tablespace_name," >>$CHECKSQL
echo " round(sum(AllowUseSize_MB),2) megs_alloc," >>$CHECKSQL
echo " round(sum(FreeSize_MB),2) megs_free," >>$CHECKSQL
echo " round(sum(UsedSize_MB),2) megs_used," >>$CHECKSQL
echo " round(sum(MaxSize_MB),2) max" >>$CHECKSQL
echo " from" >>$CHECKSQL
echo " ( select d.tablespace_name, d.file_name," >>$CHECKSQL
echo " round(decode(d.autoextensible, 'YES', greatest(d.maxbytes, d.bytes), d.bytes)/1024/1024,2) MaxSize_MB," >>$CHECKSQL
echo " round(d.bytes/1024/1024,2) TotalSize_MB," >>$CHECKSQL
echo " nvl(round((d.bytes-p.bytes_cached)/1024/1024,2),0) FreeSize_MB," >>$CHECKSQL
echo " nvl(round(p.bytes_cached/1024/1024,2),0) UsedSize_MB," >>$CHECKSQL
echo " nvl(round((decode(d.autoextensible, 'YES', greatest(d.maxbytes, d.bytes), d.bytes) - p.bytes_cached)/1024/1024,2),0) AllowUseSize_MB," >>$CHECKSQL
echo " --round((user_bytes/bytes)*100,2) UsedPercent," >>$CHECKSQL
echo " user_blocks * ts.block_size/1024/1024 Fragment_MB" >>$CHECKSQL
echo " from dba_temp_files d, v/$temp_extent_pool p, dba_tablespaces ts" >>$CHECKSQL
echo " where d.tablespace_name = p.tablespace_name(+)" >>$CHECKSQL
echo " and d.file_id = p.file_id(+)" >>$CHECKSQL
echo " and d.tablespace_name = ts.tablespace_name(+))" >>$CHECKSQL
echo " group by tablespace_name)" >>$CHECKSQL
echo "order by 6" >>$CHECKSQL
echo "/" >> $CHECKSQL
echo "exit" >>$CHECKSQL
sqlplus "$SQLUSER" @$CHECKSQL >> $APP_NAME"."$FORMAT
rm -f $CHECKSQL
}
job3()
{
echo '/n/n' >> $APP_NAME"."$FORMAT
#3、数据库的alert.log大小检查
echo " alert.log checking processing..."
echo "/n" >> $APP_NAME"."$FORMAT
echo '3. check alert_$ORACLE_SID.log size(M)' >> $APP_NAME"."$FORMAT
echo '--------------------------------------------------/n' >> $APP_NAME"."$FORMAT
if [ -f $ALTERFILE ]
then
ls -l $ALTERFILE|awk '{print $5}'|awk '{printf("%.2fM/n",$1/1048576);}' >> $APP_NAME"."$FORMAT
fi
echo '/n--------------------------------------------------/n' >> $APP_NAME"."$FORMAT
#4、数据库的listener.log大小检查
echo "listener.log is processing..."
echo 'check listener.log size(M)' >> $APP_NAME"."$FORMAT
echo "/n" >> $APP_NAME"."$FORMAT
if [ -f $ORACLE_HOME/network/log/$ORACLE_SID.log ]
then
ls -l $ORACLE_HOME/network/log/$ORACLE_SID.log|awk '{print $5}'|awk '{printf("%.2fM/n",$1/1048576);}' >> $APP_NAME"."$FORMAT
echo "/n" >> $APP_NAME"."$FORMAT
fi
if [ -f $ORACLE_HOME/network/log/listener.log ]
then
ls -l $ORACLE_HOME/network/log/listener.log|awk '{print $5}'|awk '{printf("%.2fM/n",$1/1048576);}' >> $APP_NAME"."$FORMAT
fi
echo '/n--------------------------------------------------/n' >> $APP_NAME"."$FORMAT
#5、数据库sqlnet.log大小检查
echo " sqlnet.log checking processing..."
echo 'check sqlnet.log size(M)' >> $APP_NAME"."$FORMAT
echo "/n" >> $APP_NAME"."$FORMAT
if [ -f $ORACLE_HOME/network/log/sqlnet.log ]
then
ls -l $ORACLE_HOME/network/log/sqlnet.log|awk '{print $5}'|awk '{printf("%.2fM/n",$1/1048576);}' >> $APP_NAME"."$FORMAT
else
echo "no sqlnet.log on this database" >> $APP_NAME"."$FORMAT
fi
echo '/n--------------------------------------------------/n' >> $APP_NAME"."$FORMAT
#6.delete outdated trc and core and aud file
echo "delete .trc file processing..."
find $ORACLE_BASE/admin -name "*.trc" -mtime +15 -exec rm -f {} /;
find $ORACLE_BASE/admin -name "*.aud" -mtime +15 -exec rm -f {} /;
find $ORACLE_BASE/admin -name "*.trw" -mtime +15 -exec rm -f {} /;
find $ORACLE_BASE/admin -name "core_*" -mtime +15 -exec rm -rf {} /;
echo 'delete 15 days off .trc file' >> $APP_NAME"."$FORMAT
echo 'delete successfully!!! ' >> $APP_NAME"."$FORMAT
}
job4()
{
echo '/n----------------------------------------------------------------------------------------------------------------------/n' >> $APP_NAME"."$FORMAT
echo "database check running......"
echo "database check running......" >> $APP_NAME"."$FORMAT
JOB4SQL=$DAILY_CHECK/job4.sql
echo "prompt ----------------------------------------------------------------------------------------------------------------------" >$JOB4SQL
echo "/n/n/nprompt #################### invalid objects ####################" >>$JOB4SQL
echo "set linesize 220 pagesize 9999" >>$JOB4SQL
echo "col owner for a27" >>$JOB4SQL
echo "break on owner skip page on report" >>$JOB4SQL
echo "compute sum label 'sum of owner cnt' of cnt on owner" >>$JOB4SQL
echo "compute sum label 'sum of all cnt' of cnt on report " >>$JOB4SQL
echo "" >>$JOB4SQL
echo "select owner,object_type,status,count(*) cnt from dba_objects" >>$JOB4SQL
echo "where owner not in" >>$JOB4SQL
echo "('ANONYMOUS','CTXSYS','DBSNMP','DIP','DMSYS','EXFSYS','LBACSYS','MDDATA','MDSYS','MGMT_VIEW','OLAPSYS'," >>$JOB4SQL
echo "'ORACLE_OCM','ORDPLUGINS','ORDSYS','OUTLN','SCOTT','PUBLIC','SI_INFORMTN_SCHEMA','SYS','SYSMAN','SYSTEM'," >>$JOB4SQL
echo "'TSMSYS','WMSYS','WKSYS','XDB') " >>$JOB4SQL
echo "group by owner,object_type,status order by 1,2,3;" >>$JOB4SQL
echo "" >>$JOB4SQL
echo "prompt ----------------------------------------------------------------------------------------------------------------------" >>$JOB4SQL
echo "/n/n/n prompt #################### appuser statistics ####################" >>$JOB4SQL
echo "--#2 应用账号的统计更新情况" >>$JOB4SQL
echo "select owner,min(LAST_ANALYZED),max(LAST_ANALYZED) from dba_tables" >>$JOB4SQL
echo "where owner not in ('ANONYMOUS','CTXSYS','DBSNMP','DIP','DMSYS','EXFSYS','LBACSYS','MDDATA','MDSYS','MGMT_VIEW'," >>$JOB4SQL
echo "'OLAPSYS','ORACLE_OCM','ORDPLUGINS','ORDSYS','OUTLN','SCOTT','PUBLIC','SI_INFORMTN_SCHEMA','SYS','SYSMAN','SYSTEM'," >>$JOB4SQL
echo "'TSMSYS','WMSYS','WKSYS','XDB') group by owner;" >>$JOB4SQL
echo "" >>$JOB4SQL
echo "prompt ----------------------------------------------------------------------------------------------------------------------" >>$JOB4SQL
echo "/n/n/n prompt #################### datafile check #################### /n" >>$JOB4SQL
echo "--#3 数据文件状态" >>$JOB4SQL
echo "SELECT a.file_name,a.status,b.status" >>$JOB4SQL
echo "FROM dba_data_files a,v/$datafile b" >>$JOB4SQL
echo "WHERE a.file_id=b.file# and a.status !='AVAILABLE'" >>$JOB4SQL
echo "UNION" >>$JOB4SQL
echo "SELECT a.file_name,a.status,b.status" >>$JOB4SQL
echo "FROM dba_data_files a,v/$datafile b" >>$JOB4SQL
echo "WHERE a.file_id=b.file# and b.status not in('SYSTEM','ONLINE');" >>$JOB4SQL
echo "" >>$JOB4SQL
echo "" >>$JOB4SQL
echo "prompt ----------------------------------------------------------------------------------------------------------------------" >>$JOB4SQL
echo "/n/n/n prompt #################### corrupt block ####################" >>$JOB4SQL
echo "--#4 数据库坏块情况" >>$JOB4SQL
echo "SELECT a.file_name,b.file#,b.block#,b.blocks,b.corruption_change#, b.CORRUPTION_TYPE" >>$JOB4SQL
echo "FROM dba_data_files a,v/$database_block_corruption b" >>$JOB4SQL
echo "WHERE a.file_id=b.file#;" >>$JOB4SQL
echo "" >>$JOB4SQL
echo "" >>$JOB4SQL
echo "prompt ----------------------------------------------------------------------------------------------------------------------" >>$JOB4SQL
echo "/n/n/n prompt #################### archived log ####################" >>$JOB4SQL
echo "--#5 数据库日日志产生量" >>$JOB4SQL
echo "set linesize 220" >>$JOB4SQL
echo "set pagesize 9999" >>$JOB4SQL
echo "break on hour on report" >>$JOB4SQL
echo "compute sum label 'sum of archivelog' of sizeM on report" >>$JOB4SQL
echo "select trunc(a.FIRST_TIME,'HH') hour,a.thread#,count(distinct a.sequence#) cnt_archivedlog," >>$JOB4SQL
echo "sum(round(b.BLOCKS*B.BLOCK_SIZE/1024/1024,2)) sizeM" >>$JOB4SQL
echo "from v/$log_history a,v/$archived_log b" >>$JOB4SQL
echo "where a.FIRST_TIME between trunc(sysdate-1,'DD') and trunc(sysdate,'DD')" >>$JOB4SQL
echo " and a.thread#=b.thread#" >>$JOB4SQL
echo " and a.sequence#=b.sequence#" >>$JOB4SQL
echo " and (b.dest_id=10 or b.dest_id=1)" >>$JOB4SQL
echo "group by trunc(a.FIRST_TIME,'HH'),a.thread#" >>$JOB4SQL
echo "order by 1,2;" >>$JOB4SQL
echo "" >>$JOB4SQL
echo "prompt ----------------------------------------------------------------------------------------------------------------------" >>$JOB4SQL
echo "/n/n/n prompt #################### 2G or 2KWrows table ####################" >>$JOB4SQL
echo "--#6 超过2G或大于2kw记录的表" >>$JOB4SQL
echo "set linesize 220" >>$JOB4SQL
echo "col owner for a22" >>$JOB4SQL
echo "col table_name for a30" >>$JOB4SQL
echo "select a.owner,a.table_name,a.num_rows,a.last_analyzed,a.PARTITIONED,sum(round(b.bytes/1024/1024,2)) sizeM" >>$JOB4SQL
echo "from dba_tables a,dba_segments b" >>$JOB4SQL
echo "where a.table_name=b.segment_name" >>$JOB4SQL
echo " and a.owner=b.owner" >>$JOB4SQL
echo " --and a.PARTITIONED='NO'" >>$JOB4SQL
echo " and a.num_rows>20000000" >>$JOB4SQL
echo "group by a.owner,a.table_name,a.num_rows,a.last_analyzed,a.PARTITIONED" >>$JOB4SQL
echo "--having sum(round(b.bytes/1024/1024,2))>2*1024*1024*1024" >>$JOB4SQL
echo "order by 1,5;" >>$JOB4SQL
echo "" >>$JOB4SQL
echo "prompt ----------------------------------------------------------------------------------------------------------------------" >>$JOB4SQL
echo "/n/n/n prompt #################### row_chain table ####################" >>$JOB4SQL
echo "--#7 行迁移行链接表" >>$JOB4SQL
echo "set linesize 220" >>$JOB4SQL
echo "select owner,table_name,num_rows,last_analyzed,chain_cnt from dba_tables where chain_cnt>0;" >>$JOB4SQL
echo "" >>$JOB4SQL
echo "" >>$JOB4SQL
echo "prompt ----------------------------------------------------------------------------------------------------------------------" >>$JOB4SQL
echo "/n/n/n prompt #################### appuser auth ####################" >>$JOB4SQL
echo "#8 应用账号权限检查" >>$JOB4SQL
echo "break on grantee skip page on report " >>$JOB4SQL
echo "set linesize 180 pagesize 9999" >>$JOB4SQL
echo "col grantee for a23" >>$JOB4SQL
echo "col privilege for a26" >>$JOB4SQL
echo " select grantee,privilege from dba_sys_privs where grantee not in ('ANONYMOUS','CTXSYS','DBSNMP','DIP','DMSYS','EXFSYS'," >>$JOB4SQL
echo " 'LBACSYS','MDDATA','MDSYS','MGMT_VIEW','OLAPSYS','ORACLE_OCM','ORDPLUGINS','ORDSYS','OUTLN','SCOTT','SI_INFORMTN_SCHEMA'," >>$JOB4SQL
echo " 'SYS','SYSMAN','SYSTEM','TSMSYS','WMSYS','WKSYS','XDB') and grantee not in (select role from dba_roles)" >>$JOB4SQL
echo "union" >>$JOB4SQL
echo "select grantee,GRANTED_ROLE privilege from dba_role_privs where grantee not in ('ANONYMOUS','CTXSYS','DBSNMP','DIP','DMSYS'," >>$JOB4SQL
echo "'EXFSYS','LBACSYS','MDDATA','MDSYS','MGMT_VIEW','OLAPSYS','ORACLE_OCM','ORDPLUGINS','ORDSYS','OUTLN','SCOTT','SI_INFORMTN_SCHEMA'," >>$JOB4SQL
echo "'SYS','SYSMAN','SYSTEM','TSMSYS','WMSYS','WKSYS','XDB') and grantee not in (select role from dba_roles)" >>$JOB4SQL
echo "order by 1,2;" >>$JOB4SQL
echo "exit;" >> $JOB4SQL
sqlplus "$SQLUSER" @$JOB4SQL >> $APP_NAME"."$FORMAT
rm -f $JOB4SQL
}
job5()
{
echo '5.表空间可用空间信息 ' >> $APP_NAME"."$FORMAT
echo '/n/n#################### free space < 5% ####################/n/n' >> $APP_NAME"."$FORMAT
CHECKSQL=$DAILY_CHECK/checkspace.sql
echo "set linesize 180" > $CHECKSQL
echo "col tablespace_name for a60" >> $CHECKSQL
echo "set pagesize 9999" >> $CHECKSQL
echo "set wrap off" >> $CHECKSQL
echo "select" >> $CHECKSQL
echo "tablespace_name," >>$CHECKSQL
echo "megs_alloc init_sizeM," >>$CHECKSQL
echo "max total_sizeM," >>$CHECKSQL
echo "max-megs_used free_sizeM," >>$CHECKSQL
echo "megs_used used_sizeM," >>$CHECKSQL
echo "round((max-megs_used)/(max+0.001) *100,1) free_rate" >>$CHECKSQL
echo "from" >>$CHECKSQL
echo "(select a.tablespace_name," >>$CHECKSQL
echo "round(a.bytes_alloc / 1024 / 1024, 2) megs_alloc," >>$CHECKSQL
echo "round(nvl(b.bytes_free, 0) / 1024 / 1024, 2) megs_free," >>$CHECKSQL
echo "round((a.bytes_alloc - nvl(b.bytes_free, 0)) / 1024 / 1024, 2) megs_used," >>$CHECKSQL
echo "round(maxbytes/1048576,2) Max" >>$CHECKSQL
echo "from ( select f.tablespace_name," >>$CHECKSQL
echo " sum(f.bytes) bytes_alloc," >>$CHECKSQL
echo " sum(decode(f.autoextensible, 'YES',f.maxbytes,'NO', f.bytes)) maxbytes" >>$CHECKSQL
echo " from dba_data_files f" >>$CHECKSQL
echo " group by tablespace_name) a," >>$CHECKSQL
echo "( select f.tablespace_name," >>$CHECKSQL
echo " sum(f.bytes) bytes_free" >>$CHECKSQL
echo " from dba_free_space f" >>$CHECKSQL
echo " group by tablespace_name) b" >>$CHECKSQL
echo "where a.tablespace_name = b.tablespace_name (+)" >>$CHECKSQL
echo "union all" >>$CHECKSQL
echo "select tablespace_name," >>$CHECKSQL
echo " round(sum(AllowUseSize_MB),2) megs_alloc," >>$CHECKSQL
echo " round(sum(FreeSize_MB),2) megs_free," >>$CHECKSQL
echo " round(sum(UsedSize_MB),2) megs_used," >>$CHECKSQL
echo " round(sum(MaxSize_MB),2) max" >>$CHECKSQL
echo " from" >>$CHECKSQL
echo " ( select d.tablespace_name, d.file_name," >>$CHECKSQL
echo " round(decode(d.autoextensible, 'YES', greatest(d.maxbytes, d.bytes), d.bytes)/1024/1024,2) MaxSize_MB," >>$CHECKSQL
echo " round(d.bytes/1024/1024,2) TotalSize_MB," >>$CHECKSQL
echo " nvl(round((d.bytes-p.bytes_cached)/1024/1024,2),0) FreeSize_MB," >>$CHECKSQL
echo " nvl(round(p.bytes_cached/1024/1024,2),0) UsedSize_MB," >>$CHECKSQL
echo " nvl(round((decode(d.autoextensible, 'YES', greatest(d.maxbytes, d.bytes), d.bytes) - p.bytes_cached)/1024/1024,2),0) AllowUseSize_MB," >>$CHECKSQL
echo " --round((user_bytes/bytes)*100,2) UsedPercent," >>$CHECKSQL
echo " user_blocks * ts.block_size/1024/1024 Fragment_MB" >>$CHECKSQL
echo " from dba_temp_files d, v/$temp_extent_pool p, dba_tablespaces ts" >>$CHECKSQL
echo " where d.tablespace_name = p.tablespace_name(+)" >>$CHECKSQL
echo " and d.file_id = p.file_id(+)" >>$CHECKSQL
echo " and d.tablespace_name = ts.tablespace_name(+))" >>$CHECKSQL
echo " group by tablespace_name)" >>$CHECKSQL
echo "where free_rate<5.0" >>$CHECKSQL
echo "/" >> $CHECKSQL
echo "exit" >>$CHECKSQL
sqlplus "$SQLUSER" @$CHECKSQL >> $APP_NAME"."$FORMAT
rm -f $CHECKSQL
}
# -----------------------------------------------------------------------------------------------------------
# collect database information by functions
for JOB in $JOB_LIST
do
$JOB
done
echo "ended successfully" >> $APP_NAME"."$FORMAT
# -----------------------------------------------------------------------------------------------------------
# send result files by ftp including those failed to be sent before
# and get the newest version of this shell script
cd $DAILY_CHECK
ftp -in <<!
open $FTP_DEST_HOST
user $FTP_USER $FTP_PWD
cd $FTP_DEST_DIR
bin
mput $APP_NAME".*."$SUFFIX
cd ..
get check_db_alert.sh
bye
!
if [ $? != "0" ]
then
echo "ERROR: send result files by ftp to destination failed"
exit 1
fi
# -----------------------------------------------------------------------------------------------------------
# rename result file after successful ftp
for FILENAME in $DAILY_CHECK"/"$APP_NAME.*.$SUFFIX
do
mv $FILENAME $FILENAME"."$SUFFIX1
done
find $DAILY_CHECK -name "*.log" -mtime +15 -exec rm -f {} /;
# -----------------------------------------------------------------------------------------------------------
# record finish date and time
echo 'database information collect finished at:'; date
echo "------------------------------------------------------------"
#crontab -e
#05 08,16 * * * sh /tmp/db_dailycheck/check_db_alert.sh ryxdb 10.203.4.180 >> /tmp/db_dailycheck/check_db_alert.log 2>&1