Oracle ASM DiskGroup空间使用情况监控脚本

用于nagios监控Oracle ASM DiskGroup空间使用情况。

#!/usr/bin/bash
#
# Nagios plugin to check Oracle ASM DiskGroup usage.
#
# $Id: check_asmdg_usage.sh,v 1.0 2010/11/10 $
#
# Copyright (C) 2010-2011  Yu jun / BII-ERG Ltd.
#
# The purpose of this program is to check Oracle ASM diskgroup usage.
# The Program will connect to the database instance instead of ASM.
#
# For this program to run correctly, you should create a database account
# named "db_check",and the password also should be "db_check". If you
# need to change the username and password, you should modify this
# program. You also need to grant below priviledges to account "db_check"
#  1. create session
#  2. select on sys.v_$asm_diskgroup

# Set Environment Variable
export ORACLE_HOME=/app/oracle/product/10.2
export PATH=$PATH:$ORACLE_HOME/bin


# External commands
CMD_AWK="/usr/bin/awk"

# Nagios plugin return values
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_TEXT=""

# Default values
DG_USAGE=0
DG_WARN_MIN=-1
DG_WARN_MAX=101
DG_CRIT_MIN=-1
DG_CRIT_MAX=101
WARN_TRIGGER=0
CRIT_TRIGGER=0
VERBOSE=0

# ------------------------------ FUNCTIONS -------------------------------------

printInfo() {
    echo "Nagios plugin to check Oracle ASM DiskGroup usage."
    echo "Copyright (C) 2010-2011  Yu jun / BII-ERG Ltd."
}

printHelp() {
    echo
    echo "Usage: $(basename $0) [-s ] [-g ][-w ] [-c ]"
    echo
    echo "  -s  Oracle database connect string, should be set in file tnsnames.ora"
    echo "  -g  Oracle ASM diskgroup name"
    echo "  -w  ASM DiskGroup usage% warning safe range ([min]:[max]) as integers"
    echo "  -c  ASM DiskGroup usage% critical safe range ([min]:[max]) as integers"
    echo "  -h  this help screen"
    echo "  -v  verbose output (for debugging)"
    echo "  -V  version info"
    echo
    echo "Note about safe ranges: You can give the minimum (min:) or maximum (:max)"
    echo "DG usage percentage or both (min:max). Values must be integers between"
    echo "0-100 and mins must be less than maxes."
    echo
    echo "Example: $(basename $0) -s ORCL -g DATADG -w :60 -c :75"
    echo "This will return CRITICAL if DG usage is above 75%, WARNING if"
    echo "DG usage is above 60% for DATADG and OK otherwise. In case of errors"
    echo "plugin returns UNKNOWN."
    echo
}

printVersion() {
    echo
    echo "\$Id: check_asmdg_usage.sh,v 1.0 2010/11/10 $"
    echo
}

# Checks command line options (pass $@ as parameter).
checkOptions() {
    if [ $# -lt 6 ]; then
        echo "Error: Must input at least 6 parameters."
        printInfo
        printHelp
        exit $STATE_UNKNOWN
    fi

    while getopts s:g:w:c:lhvV OPT $@; do
            case $OPT in
                s) DBConnStr=$OPTARG
                   ;;
                g) DiskGroup=$OPTARG
                   ;;
                w) # warning range
                   opt_warn_min=$(echo $OPTARG | $CMD_AWK 'BEGIN{FS=":"}{print $1}')
                   opt_warn_max=$(echo $OPTARG | $CMD_AWK 'BEGIN{FS=":"}{print $2}')
                   WARN_TRIGGER=1
                   ;;
                c) # critical range
                   opt_crit_min=$(echo $OPTARG | $CMD_AWK 'BEGIN{FS=":"}{print $1}')
                   opt_crit_max=$(echo $OPTARG | $CMD_AWK 'BEGIN{FS=":"}{print $2}')
                   CRIT_TRIGGER=1
                   ;;
                h) printInfo
                   printHelp
                   exit $STATE_UNKNOWN
                   ;;
                v) VERBOSE=1
                   ;;
                V) printInfo
                   printVersion
                   exit $STATE_UNKNOWN
                   ;;
                ?) printInfo
                   printHelp
                   exit $STATE_UNKNOWN
                   ;;
            esac
    done
    range_error=0
    if [ $WARN_TRIGGER -eq 1 ]; then
        if [ "$opt_warn_min" != "" ]; then
            if [ "$(echo $opt_warn_min | grep '^[0-9]*$')" = "" ]; then
                range_error=1
            elif [ $opt_warn_min -lt 0 ] || [ $opt_warn_min -gt 100 ]; then
                range_error=1
            fi
            DG_WARN_MIN=$opt_warn_min
        fi
        if [ "$opt_warn_max" != "" ]; then
            if [ "$(echo $opt_warn_max | grep '^[0-9]*$')" = "" ]; then
                range_error=1
            elif [ $opt_warn_max -lt 0 ] || [ $opt_warn_max -gt 100 ]; then
                range_error=1
            fi
            DG_WARN_MAX=$opt_warn_max
        fi
        if [ $DG_WARN_MIN -ge $DG_WARN_MAX ]; then
            range_error=1
        fi
    fi
    if [ $CRIT_TRIGGER -eq 1 ]; then
        if [ "$opt_crit_min" != "" ]; then
            if [ "$(echo $opt_crit_min | grep '^[0-9]*$')" = "" ]; then
                range_error=1
            elif [ $opt_crit_min -lt 0 ] || [ $opt_crit_min -gt 100 ]; then
                range_error=1
            fi
            DG_CRIT_MIN=$opt_crit_min
        fi
        if [ "$opt_crit_max" != "" ]; then
            if [ "$(echo $opt_crit_max | grep '^[0-9]*$')" = "" ]; then
                range_error=1
            elif [ $opt_crit_max -lt 0 ] || [ $opt_crit_max -gt 100 ]; then
                range_error=1
            fi
            DG_CRIT_MAX=$opt_crit_max
        fi
        if [ $DG_CRIT_MIN -ge $DG_CRIT_MAX ]; then
            range_error=1
        fi
    fi
    if [ $range_error -eq 1 ]; then
        echo "Error: Invalid safe range values."
        printInfo
        printHelp
        exit $STATE_UNKNOWN
    fi
    if [ -z "$DBConnStr" ]; then
        echo "Error: Invalid Database Connect String."
        printInfo
        printHelp
        exit $STATE_UNKNOWN       
    fi
    if [ -z "$DiskGroup" ]; then
        echo "Error: Invalid DiskGroup."
        printInfo
        printHelp
        exit $STATE_UNKNOWN       
    fi
}

# ----------------------------- MAIN PROGRAM -----------------------------------
checkOptions $@

if [ $VERBOSE -eq 1 ]; then
    set -vx
fi

DGTEXT=$(sqlplus -silent "db_check/db_check@$DBConnStr" << EOF
set pages 0 feedback off verify off heading off echo off
select total_mb||':'||free_mb from v\$asm_diskgroup where name=upper('$DiskGroup');
exit;
EOF)

if [ $? -ne 0 ] ; then
  echo "$DGTEXT"
  printInfo
  printHelp
  exit $STATE_UNKNOWN
fi

if [ -z "$DGTEXT" ] ; then
  echo "Error: You maybe input the wrong ASM diskgroup name."
  printInfo
  printHelp
  exit $STATE_UNKNOWN
fi

TotalMB=$(echo $DGTEXT | $CMD_AWK 'BEGIN{FS=":"}{print $1}')
if [ -z "$TotalMB" ]; then
  echo "Error: The database return a NULL for the Total_MB from v$asm_diskgroup."
  exit $STATE_UNKNOWN
else
    if [ "$(echo $TotalMB | grep '^[0-9]*$')" = "" ]; then
        echo "Error: The database return a invalid value for the Total_MB from v$asm_diskgroup."
        echo "The return Total_MB is : $TotalMB."
        exit $STATE_UNKNOWN
    elif [ $TotalMB -le 0 ] ; then
        echo "Error: The Total_MB for ASM diskgroup $DiskGroup <= 0."
        exit $STATE_UNKNOWN
    fi
fi

FreeMB=$(echo $DGTEXT | $CMD_AWK 'BEGIN{FS=":"}{print $2}')
if [ -z "$FreeMB" ]; then
  echo "Error: The database return a NULL for the Free_MB from v$asm_diskgroup."
  exit $STATE_UNKNOWN  
else
    if [ "$(echo $FreeMB | grep '^[0-9]*$')" = "" ]; then
        echo "Error: The database return a invalid value for the Free_MB from v$asm_diskgroup."
        echo "The return Free_MB is : $FreeMB."
        exit $STATE_UNKNOWN
    fi
fi

UsedMB=$((TotalMB - FreeMB))
DG_USAGE=$((UsedMB*100/TotalMB))

STATE_TEXT="- $DG_USAGE% (TotalMB=$TotalMB UsedMB=$UsedMB FreeMB=$FreeMB)"

if [ $CRIT_TRIGGER -eq 1 ]; then
    if [ $DG_USAGE -lt $DG_CRIT_MIN ] || [ $DG_USAGE -gt $DG_CRIT_MAX ]; then
        echo "DG $DiskGroup USAGE CRITICAL $STATE_TEXT"
        exit $STATE_CRITICAL
    fi
fi
if [ $WARN_TRIGGER -eq 1 ]; then
    if [ $DG_USAGE -lt $DG_WARN_MIN ] || [ $DG_USAGE -gt $DG_WARN_MAX ]; then
        echo "DG $DiskGroup USAGE WARNING $STATE_TEXT"
        exit $STATE_WARNING
    fi
fi

echo "DG $DiskGroup USAGE OK $STATE_TEXT"
exit $STATE_OK

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/228190/viewspace-682941/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/228190/viewspace-682941/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值