用于检查oracle中的ASM磁盘组空间并发送邮件的Shell脚本

时间:2016-04-05 12:32:01

标签: oracle shell if-statement passwords database-connection

我正在尝试编写一个shell脚本来监视ORACLE中的ASM实例,并将相应的人员邮寄到邮件列表中。我写了以下脚本。

#/bin/sh    
#set -x
    USER=xxx
    PASS=yyy
    CC_OFFSHORE="xxx@hang.com , yyy@hang.com , zzz@hang.com"
    CC_TEAM="$CC_OFFSHORE , special_recipient@hang.com"
    . /home/oracle/.TESTenv             #(NOT MANDATORY. DEPENDS ON YOUR ENVIRONMENT)
    sqlplus -s $USER/$PASS << EOF
    spool /home/oracle/SABARISH/RETVAL.log
    set linesize 140
    SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF
    col group_number format 999
    col diskgroup  format a20
    col total_mb  format 999,999,999
    col free_mb  format 999,999,999
    col tot_used format 999,999,999
    col pct_used format 999
    col pct_free format 999
    select group_number,
           name diskgroup,
           total_mb,
           free_mb,
           total_mb-free_mb tot_used,
           pct_used,
           pct_free
      from (select group_number,name,total_mb,free_mb,
                 round(((total_mb-nvl(free_mb,0))/decode(total_mb,0,1,total_mb))*100) pct_used,
                 round((free_mb/total_mb)*100) pct_free
          from v\$asm_diskgroup
          where total_mb >0
          order by pct_free
         )
    /
    spool off;
    EOF

    while read -r values
    do
        USED_PCT=$(echo $values | awk '{print $6}')
        DISKGROUP_NAME=$(echo $values | awk '{print $2}')
        WARNING_LIMIT=60
        CRITICAL_LIMIT=70
        if [ ${USED_PCT} -ge ${WARNING_LIMIT} ] && [ ${USED_PCT} -lt ${CRITICAL_LIMIT} ]
        then
            echo "WARNING ALERT. $DISKGROUP_NAME disk has used $USED_PCT%" | mailx -s "(WARNING ALERT $USED_PCT% used)" $CXC_OFFSHORE
        elif [ ${USED_PCT} -ge ${CRITICAL_LIMIT} ]
        then
            echo "CRITICAL ALERT.$DISKGROUP_NAME disk has used $USED_PCT%" | mailx -s "(CRITICAL ALERT $USED_PCT% used)" $CXC_TEAM
        fi
    done < /home/oracle/SABARISH/RETVAL.log

以上脚本运作完美。

我需要做的就是在脚本中添加更多细节。在执行ASM实例查询之前,我们需要检查数据库是否已启动并运行。

并检查特定用户的密码是否已过期或已激活。

然后运行ASM查询。如果上述两个约束都失败,则它不应该执行ASM实例查询。它应该退出。

我如何实现它?

1 个答案:

答案 0 :(得分:0)

这样的事情:

echo "select 1 from dual "|sqlplus yourUsername/YourPass@ADMDB  >/dev/null 2>&1

if [ $? -eq 0 ]
then 
echo "Connected"
else 
echo "not connected "
fi