#! /bin/bash
export LANG=en_US.UTF-8

keys=(FLAGEPID  CPU MEMORY SESSION RUNTIME instancename gcpolicytime instance_role instance_status database_role
open_mode protection_mode host_name database_status startup_time uptime version archiver session_total session_actives
session_waits dg_stats dg_delay processes physical_reads_persecond physical_writes_persecond physical_read_io_requests_persecond
physical_write_io_requests_persecond os_cpu_wait_time
)
#echo ${#keys[@]} 看看多少个指标,不包含cpname
declare -A map=()

for var in ${keys[@]};  
do  
    map[$var]=""  
done


CURNUM=                 #当前的性能参数
WranResultStr=      #最后形成的告警字符串
ErrorResultStr=     #最后形成的错误字符串
DataResultStr=      #性能呢个参数字符串

#会话数
SESSION=-1
SESSIONMIN=-1
SESSIONMAX=-1
#CPU
CPU=-1
CPUMIN=-1
CPUMAX=-1
#内存
MEMORY=-1
MEMORYMIN=-1
MEMORYMAX=-1
physical_reads_persecond=
physical_writes_persecond=
physical_read_io_requests_persecond=
physical_write_io_requests_persecond=

last_physical_reads_persecond=
last_physical_writes_persecond=
last_physical_read_io_requests_persecond=
last_physical_write_io_requests_persecond=

last_physical_reads_time=
last_physical_writes_time=
last_physical_read_io_requests_time=
last_physical_write_io_requests_time=

PORT=
FLAGEID=
#是否输出FLAGPID
FLAGEOPID=1
#是否输出RUNTIME
RUNTIME=1
INSTANCE=1
GC_POLICY_TIME=1
SOURCE_DB="/ as sysdba"
isEducate=1
cons=
IP=
QUOTASTR=
quota=
CORENUM=1
oracle_acquisition_json=""


function initLast(){
if [ ! -f recordOracle.txt ]; then
 touch recordOracle.txt
fi

last_physical_reads_time=$(cat recordOracle.txt |grep physical_reads|awk '{print $3}')
last_physical_writes_time=$(cat recordOracle.txt |grep physical_writes|awk '{print $3}')
last_physical_read_io_requests_time=$(cat recordOracle.txt |grep physical_read_io_requests|awk '{print $3}')
last_physical_write_io_requests_time=$(cat recordOracle.txt |grep physical_write_io_requests|awk '{print $3}')

last_physical_reads=$(cat recordOracle.txt |grep physical_reads|awk '{print $2}')
last_physical_writes=$(cat recordOracle.txt |grep physical_writes|awk '{print $2}')
last_physical_read_io_requests=$(cat recordOracle.txt |grep physical_read_io_requests|awk '{print $2}')
last_physical_write_io_requests=$(cat recordOracle.txt |grep physical_write_io_requests|awk '{print $2}')

echo "##" > recordOracle.txt

}

function gotErr(){
    if [ "$1" -eq 0 ];then
        echo "mailstatedes=各项指标恢复正常, statedes=各项指标恢复正常|$DataResultStr"
    elif [ "$1" -eq 1 ];then 
        echo "quota=${quota} mailstatedes=$WranResultStr statedes=$WranResultStr ${QUOTASTR} |$DataResultStr"
    elif [ "$1" -eq 2 ];then 
        if [[ "${WranResultStr}" != "" ]]; then
            echo "quota=${quota} mailstatedes=${WranResultStr},${ErrorResultStr} statedes=${WranResultStr},${ErrorResultStr} ${QUOTASTR}|$DataResultStr"
        else
            echo "quota=${quota} mailstatedes=${ErrorResultStr} statedes=${ErrorResultStr} ${QUOTASTR}|$DataResultStr"
        fi

    elif [ "$1" -eq 3 ];then 
        echo "mailstatedes=无法获取到指标,疑似组件故障,请确认并请检查监控脚本和运维平台配置 statedes=无法获取到指标,疑似组件故障,请确认并请检查监控脚本和运维平台配置|$DataResultStr"
    elif [ "$1" -eq 4 ];then
        echo "mailstatedes=无法获取到主机IP statedes=无法获取到主机IP|$DataResultStr"
  fi
    exit $1
}

function isAlive(){
    CORENUM=`cat /proc/cpuinfo |grep "physical id"|wc -l`
    if [[ "$CORENUM" == "" ]];then
        CORENUM=1
    fi
    #NUM=$(netstat -lnp |grep :$PORT | grep tcp |wc -l)
    #if [[ "$NUM" = "1" ]];then
    #   FLAGEID=$(netstat -lnp |grep :$PORT | grep tcp | grep -v "127.0.0.1"|awk '{print $NF}'| awk -F '/' '{print $1}')
    #else
    #  IP=$(ifconfig | grep "inet addr" | grep -v "127.0.0.1" | head -n 1 |awk '{print $2}' | awk -F ':' '{print $2}')
    #  if [[ "$IP" == "" ]];then
    #       gotErr 4
    #  fi
    #  FLAGEID=$(netstat -lnp | grep $IP |grep :$PORT | grep tcp | grep -v "127.0.0.1"|awk '{print $NF}'| awk -F '/' '{print $1}')
    #fi
    FLAGEID=$(netstat -lnp | grep :$PORT | grep tcp | head -n 1 |grep -v "127.0.0.1"|awk '{print $NF}'| awk -F '/' '{print $1}')
    if [[ "$FLAGEID" == "" ]];then
        gotErr 3
    fi
    #oracle_acquisition_json="$oracle_acquisition_json,'FLAGEPID':$FLAGEID"
    map["FLAGEPID"]="$FLAGEID"

}

function instance_role(){
#echo "Current $FUNCNAME, \$FUNCNAME => (${FUNCNAME[@]})"
CURNUM=`execSql 'select INSTANCE_ROLE from v$instance;'`

if [[ "${CURNUM}" == "" ]];then
CURNUM=0
fi
if [[ "${CURNUM}" == "PRIMARY_INSTANCE" ]];then
CURNUM=1
else
CURNUM=2
fi

#oracle_acquisition_json="$oracle_acquisition_json,'instance_role':$CURNUM"

map["instance_role"]="$CURNUM"
}

function instance_status(){
#echo "Current $FUNCNAME, \$FUNCNAME => (${FUNCNAME[@]})"
CURNUM=`execSql 'select status from v$instance;'`
if [[ "${CURNUM}" == "" ]];then
CURNUM=0
fi
if [[ "${CURNUM}" == "OPEN" ]];then
CURNUM=1
else
CURNUM=2
fi

#oracle_acquisition_json="$oracle_acquisition_json,'instance_status':$CURNUM"

map["instance_status"]="$CURNUM"
}

function database_role(){
CURNUM=`execSql 'select DATABASE_ROLE from v$database;'`
if [[ "${CURNUM}" == "" ]];then
CURNUM=0
fi
if [[ "${CURNUM}" == "PRIMARY" ]];then
CURNUM=1
else
CURNUM=2
fi

#oracle_acquisition_json="$oracle_acquisition_json,'database_role':$CURNUM"
map["database_role"]="$CURNUM"
}

function open_mode(){
CURNUM=`execSql 'select OPEN_MODE from v$database;'`
if [[ "${CURNUM}" == "" ]];then
CURNUM=0
fi
if [[ "${CURNUM}" == "READ WRITE" ]];then
CURNUM=1
else
CURNUM=2
fi

#oracle_acquisition_json="$oracle_acquisition_json,'open_mode':$CURNUM"
map["open_mode"]="$CURNUM"
}

function protection_mode(){
CURNUM=`execSql 'select OPEN_MODE from v$database;'`
if [[ "${CURNUM}" == "" ]];then
CURNUM=0
fi
if [[ "${CURNUM}" == "MAXIMUM PERFORMANCE" ]];then
CURNUM=1
else
CURNUM=-1
fi

#oracle_acquisition_json="$oracle_acquisition_json,'protection_mode':$CURNUM"

map["protection_mode"]="$CURNUM"
}

function host_name(){
CURNUM=`execSql 'select HOST_NAME from v$instance;'`

#oracle_acquisition_json="$oracle_acquisition_json,'host_name':'$CURNUM'"

map["host_name"]="$CURNUM"
}

function database_status(){
#ACTIVE
#SUSPENDED
#INSTANCE RECOVERY
CURNUM=`execSql 'select Database_Status from v$instance;'`

if [[ "${CURNUM}" == "ACTIVE" ]];then
CURNUM=1
elif [[ "${CURNUM}" == "SUSPENDED" ]];then
CURNUM=2
elif [[ "${CURNUM}" == "INSTANCE RECOVERY" ]];then
CURNUM=3
else
CURNUM=0
fi

#oracle_acquisition_json="$oracle_acquisition_json,'database_status':$CURNUM"

map["database_status"]="$CURNUM"
}

function startup_time(){
CURNUM=`execSql "select to_char(startup_time,'yyyy-MM-dd HH24:mi:ss') from v\\$instance"`
if [[ "${CURNUM}" == "" ]];then
CURNUM=0
fi

#oracle_acquisition_json="$oracle_acquisition_json,'startup_time':'$CURNUM'"

map["startup_time"]="$CURNUM"
}

function uptime(){
CURNUM=`execSql 'select ROUND(TO_NUMBER(sysdate - startup_time) * 24 * 60 * 60) a FROM v$instance;'|sed 's/[^0-9]//g'`
if [[ "${CURNUM}" == "" ]];then
CURNUM=0
fi

#oracle_acquisition_json="$oracle_acquisition_json,'uptime':$CURNUM"
map["uptime"]="$CURNUM"
}

function version(){
#echo "ORACLEHOME:${ORACLEHOME}"
CURNUM=`execSql 'select VERSION FROM v$instance;'`
if [[ "${CURNUM}" == "" ]];then
CURNUM=0
fi

#oracle_acquisition_json="$oracle_acquisition_json,'version':'$CURNUM'"

map["version"]="$CURNUM"
}

function archiver(){
#STOPPED
#STARTED
#FAILED - Archiver failed to archive a log last time but will try again within 5 minutes
CURNUM=`execSql 'select archiver FROM v$instance;'`
if [[ "${CURNUM}" == "" ]];then
CURNUM=0
fi

#oracle_acquisition_json="$oracle_acquisition_json,'archiver':'$CURNUM'"

map["archiver"]="$CURNUM"
}
function session_total(){
CURNUM=`execSql 'select count(*) from v$session;'|sed 's/[^0-9]//g'`
if [[ "${CURNUM}" == "" ]];then
CURNUM=0
fi

#oracle_acquisition_json="$oracle_acquisition_json,'session_total':$CURNUM"

map["session_total"]="$CURNUM"
}

function session_actives(){
CURNUM=`execSql "select count(*) from v\\$session where username not in('SYS','SYSTEM');"|sed 's/[^0-9]//g'`
if [[ "${CURNUM}" == "" ]];then
CURNUM=0
fi

#oracle_acquisition_json="$oracle_acquisition_json,'session_actives':$CURNUM"
map["session_actives"]="$CURNUM"
}

function session_waits(){
CURNUM=`execSql "select count(*) from v\\$session where event like 'library%' or event like 'cursor%' or event like 'latch%'  or event like 'enq%' or event like 'log file%';"|sed 's/[^0-9]//g'`
if [[ "${CURNUM}" == "" ]];then
CURNUM=0
fi

#oracle_acquisition_json="$oracle_acquisition_json,'session_waits':$CURNUM"
map["session_waits"]="$CURNUM"
}

function dg_stats(){
CURNUM=`execSql 'select STATUS from v$managed_standby;'`
if [[ "${CURNUM}" == "" ]];then
CURNUM=0
fi

#oracle_acquisition_json="$oracle_acquisition_json,'dg_stats':'$CURNUM'"

map["dg_stats"]="$CURNUM"
}

function dg_delay(){
CURNUM=`execSql 'select DELAY_MINS from v$managed_standby;'`
if [[ "${CURNUM}" == "" ]];then
CURNUM=0
fi

#oracle_acquisition_json="$oracle_acquisition_json,'dg_delay':'$CURNUM'"

map["dg_delay"]="$CURNUM"
}

function processes(){
CURNUM=`execSql 'select value from v$parameter where NAME = '"'processes';"`
if [[ "${CURNUM}" == "" ]];then
CURNUM=0
fi

#oracle_acquisition_json="$oracle_acquisition_json,'processes':$CURNUM"

map["processes"]="$CURNUM"
}

function physical_reads_persecond(){
CURNUM=`execSql 'select value from v$sysstat where NAME = '"'physical reads';"`
nowsec=$(date +%s)
if [[ "${last_physical_reads_time}" == "" ]];then
echo "physical_reads ${CURNUM} ${nowsec}" >> recordOracle.txt
CURNUM=0
return
fi
echo "physical_reads ${CURNUM} ${nowsec}" >> recordOracle.txt
diffsec=$((nowsec-last_physical_reads_time))
diff=$((CURNUM-last_physical_reads))
if [ "$diffsec" == "" -o "$diffsec" -eq 0 ];then
CURNUM=0
else
CURNUM=`awk 'BEGIN{printf "%.2f\n",('${diff}'/'${diffsec}')}'`
fi

#oracle_acquisition_json="$oracle_acquisition_json,'physical_reads_persecond':$CURNUM"

map["physical_reads_persecond"]="$CURNUM"
}

function physical_writes_persecond(){
CURNUM=`execSql 'select value from v$sysstat where NAME = '"'physical writes';"`
nowsec=$(date +%s)
if [[ "${last_physical_writes_time}" == "" ]];then
echo "physical_writes ${CURNUM} ${nowsec}" >> recordOracle.txt
CURNUM=0
return
fi
echo "physical_writes ${CURNUM} ${nowsec}" >> recordOracle.txt
diffsec=$((nowsec-last_physical_writes_time))
diff=$((CURNUM-last_physical_writes))
if [ "$diffsec" == "" -o "$diffsec" -eq 0 ];then
CURNUM=0
else
CURNUM=`awk 'BEGIN{printf "%.2f\n",('${diff}'/'${diffsec}')}'`
fi

#oracle_acquisition_json="$oracle_acquisition_json,'physical_writes_persecond':$CURNUM"

map["physical_writes_persecond"]="$CURNUM"
}

function os_cpu_wait_time(){
CURNUM=`execSql 'select value from v$sysstat where NAME = '"'OS CPU Qt wait time';"|sed 's/[^0-9]//g'`
if [[ "${CURNUM}" == "" ]];then
CURNUM=0
fi
#oracle_acquisition_json="$oracle_acquisition_json,'os_cpu_wait_time':$CURNUM"
map["os_cpu_wait_time"]="$CURNUM"
}

function physical_read_io_requests_persecond(){
CURNUM=`execSql 'select value from v$sysstat where NAME = '"'physical read IO requests';"`
nowsec=$(date +%s)
if [[ "${last_physical_read_io_requests_time}" == "" ]];then
echo "physical_read_io_requests ${CURNUM} ${nowsec}" >> recordOracle.txt
CURNUM=0
return
fi
echo "physical_read_io_requests ${CURNUM} ${nowsec}" >> recordOracle.txt
diffsec=$((nowsec-last_physical_read_io_requests_time))
diff=$((CURNUM-last_physical_read_io_requests))
if [ "$diffsec" == "" -o "$diffsec" -eq 0 ];then
CURNUM=0
else
CURNUM=`awk 'BEGIN{printf "%.2f\n",('${diff}'/'${diffsec}')}'`
fi

#oracle_acquisition_json="$oracle_acquisition_json,'physical_read_io_requests_persecond':$CURNUM"

map["physical_read_io_requests_persecond"]="$CURNUM"
}

function physical_write_io_requests_persecond(){
CURNUM=`execSql 'select value from v$sysstat where NAME = '"'physical read IO requests';"`
nowsec=$(date +%s)
if [[ "${last_physical_write_io_requests_time}" == "" ]];then
echo "physical_write_io_requests ${CURNUM} ${nowsec}" >> recordOracle.txt
CURNUM=0
return
fi
echo "physical_write_io_requests ${CURNUM} ${nowsec}" >> recordOracle.txt
diffsec=$((nowsec-last_physical_write_io_requests_time))
diff=$((CURNUM-last_physical_write_io_requests))
if [ "$diffsec" == "" -o "$diffsec" -eq 0 ];then
CURNUM=0
else
CURNUM=`awk 'BEGIN{printf "%.2f\n",('${diff}'/'${diffsec}')}'`
fi

#oracle_acquisition_json="$oracle_acquisition_json,'physical_write_io_requests_persecond':$CURNUM"

map["physical_write_io_requests_persecond"]="$CURNUM"
}

function execSql()
{
    cmd=`su - oracle -c "sqlplus -s $SOURCE_DB"<<EOF
    set heading off;
    set echo off;
    set feedback off;
    set linesize 5000;
    set heading off
    set feedback off;
    set pagesize 0;
    set verify off;
    set echo off;
    $1
    exit;
EOF`
    #echo "cmd:$cmd"
    result=`echo "$cmd" | awk '{{printf"%s",$1}}'`
    CURNUM="$result"
    echo "$result"
}

#处理连接数
function getSessionNum(){
CURNUM=`su - oracle -c "sqlplus -s $SOURCE_DB"<<EOF
set heading off
set feedback off
set pagesize 0
set verify off
set echo off
select value from v\\$parameter where name='sessions';
exit;
EOF`
#oracle_acquisition_json="$oracle_acquisition_json,'SESSION':$CURNUM"

map["SESSION"]="$CURNUM"
}

#处理连接数
function getInstanceName(){
CURNUM=`su - oracle -c "sqlplus -s $SOURCE_DB"<<EOF
set heading off
set feedback off
set pagesize 0
set verify off
set echo off
select instance_name from v\\$instance;
exit;
EOF`
#oracle_acquisition_json="$oracle_acquisition_json,'instancename':'$CURNUM'"

map["instancename"]="$CURNUM"
}

#获取_GC_POLICY_TIME
function getGcPolicyTime(){
CURNUM=`su - oracle -c "sqlplus -s $SOURCE_DB"<<EOF
set heading off
set feedback off
set pagesize 0
set verify off
set echo off
show parameter _GC_POLICY_TIME;
exit;
EOF`
if [ -n "$CURNUM" ]; then
   CURNUM=$(echo $CURNUM | awk '{print $NF}') 
else
   CURNUM=0
fi
#oracle_acquisition_json="$oracle_acquisition_json,'gcpolicytime':'$CURNUM'"

map["gcpolicytime"]="$CURNUM"
}

#ps aux 4是内存 累加4的值
function getMemory(){
    CURNUM=$(ps aux | grep ora_ |awk 'BEGIN{sum=0}{sum+=$4}END{print sum}')
    if [ "$CORENUM" -ne 0 ];then
       CURNUM=$(printf "%.2f" `echo "scale=2;$CURNUM/$CORENUM" | bc`)
    fi
    #oracle_acquisition_json="$oracle_acquisition_json,'MEMORY':$CURNUM"

    map["MEMORY"]="$CURNUM"
}

#ps aux 3是CPU 累加3的值
function getCPU(){
    CURNUM=$(ps aux | grep ora_ |awk 'BEGIN{sum=0}{sum+=$3}END{print sum}')
    if [ "$CORENUM" -ne 0 ];then
       CURNUM=$(printf "%.2f" `echo "scale=2;$CURNUM/$CORENUM" | bc`)
    fi
    #oracle_acquisition_json="$oracle_acquisition_json,'CPU':$CURNUM"

    map["CPU"]="$CURNUM"
}

#ps -eo pid,etime 是查询进程运行时间
function getRuntime(){
    CURNUM=$(ps -eo pid,etime | grep $FLAGEID |awk '{if($1=='$FLAGEID') print $2}')
    #oracle_acquisition_json="$oracle_acquisition_json,'RUNTIME':'$CURNUM'"

    map["RUNTIME"]="$CURNUM"
}


#依据传入的参数值来判断我们要监控的服务是什么
#然后通过各个函数获取到具体的值
#值之间的
function analysisStat(){


    getCPU

    getMemory

    getSessionNum

    getRuntime

    getInstanceName

    getGcPolicyTime

    if [ "$isEducate" == 1 ];

    then
    instance_role

    instance_status

    database_role

    open_mode

    protection_mode

    host_name

    database_status

    startup_time

    uptime

    version

    archiver

    session_total

    session_actives

    session_waits

    dg_stats

    dg_delay

    processes

    physical_reads_persecond

    physical_writes_persecond

    physical_read_io_requests_persecond

    physical_write_io_requests_persecond

    os_cpu_wait_time
    fi

    #result="{'cpname':'oracle',${oracle_acquisition_json#*,}}"
    #result=`echo ${result//\'/\"}`
    #echo $result
}

function parseTimeFileds(){
    val1=$(echo "$CURNUM $2")
    val2=$(echo "$CURNUM $3")
    DataResultStr="$DataResultStr $1=$CURNUM;$2;$3;0;0"
}

#CURNUM            当前值
#WARNLIMIT         告警的上限
#ERRORLIMIT    异常的上限
#判断以上三个值并拼接结果字符串
function parseFileds(){
    val1=$(echo "$CURNUM $2"| awk '{if($1<=$2){print 1}else{print 0}}')
    val2=$(echo "$CURNUM $3"| awk '{if($1<=$2){print 1}else{print 0}}')

    DataResultStr="$DataResultStr $1=$CURNUM;$2;$3;0;0"
    if [ $val1 -eq 1 ];then
        return 0
    elif [[ $val1 -eq 0 && $val2 -eq 1 ]];then
        if [[ ${WranResultStr} != "" ]]; then
            WranResultStr="${WranResultStr},"
        fi

        WranResultStr="${WranResultStr}$1当前值为${CURNUM}超过告警值${2}"

        if [[ ${quota} != "" ]]; then
            quota="${quota},"
        fi

        quota="${quota}$1"

        QUOTASTR="${QUOTASTR}$1=$1当前值为${CURNUM}超过告警值${2} "

        return 1
    else
        if [[ ${ErrorResultStr} != "" ]]; then
            ErrorResultStr="${ErrorResultStr},"
        fi
        ErrorResultStr="${ErrorResultStr}$1当前值为${CURNUM}超过紧急值${3}"

        if [[ ${quota} != "" ]]; then
            quota="${quota},"
        fi

        quota="${quota}$1"

        QUOTASTR="${QUOTASTR}$1=$1当前值为${CURNUM}超过紧急值${3} "

        return 2
    fi
}

#分析结果 给出给出状态
function analysisResult(){
    if [[ "$WranResultStr" == "" && "$ErrorResultStr" == "" ]];then
        gotErr 0
    elif [[ "$ErrorResultStr" == "" ]];then
        gotErr 1
    else
        gotErr 2
    fi
}

#输出错误信息并且退出程序
function showHelp(){
    if [ "$1" != "" ];then
        echo "请输入$1"
    fi
    echo "check_oracle.sh 可以监听本地的Oracle的状态 参数如下"
    echo "check_oracle.sh [-w CPU,内存,会话数] [-c CPU,内存,会话数] [ -p <port>] [ -s <source_db>] [ -u <http_url>]"
  echo "-p 用于标示进程的端口号"
  echo "-s oracle数据库的登陆用户名密码,格式为为username/password@db_name"
  echo "-u oracle集群界面跳转地址"
    exit 1
}

#检查参数是不是数字
function isNumber(){
    isNum=$(echo "$1" | awk '{print($0~/^([0-9])+[\.]?([0-9])?$/)?1:0}')
    return $isNum
}

function isLarge(){

    isNumber $1
    if [[ $? -eq 0 ]]; then
        return 1
    fi

    isNumber $2
    if [[ $? -eq 0 ]]; then
        return 1
    fi

    isLarge=$(echo "$1 $2"| awk '{if($1>=$2){print 1}else{print 0}}')
    if [ $isLarge -eq 1 ];then
        return 0
    else
        return 1
    fi
}

function checkARG(){
    if [[ "$2" != "" && "$3" != "" ]];then
        isLarge "$3" "$2"
        if [[ $? -eq 1 ]]; then
            return 1
        else
            $1=1
        fi
    fi
    return 0
}

#检查参数是否合法
#即参数是否是小数和整数
#对于守护进程的选择 这个值可以不验证
function checkALL(){
    if [[ "$CPUMIN" != "" || "$CPUMAX" != "" ]];then
        if [[ "$CPUMAX" == "" || "$CPUMIN" == "" ]]; then
            return 1
        fi

        isLarge "$CPUMAX" "$CPUMIN"
        if [[ $? -eq 1 ]]; then
            return 1
        else
            CPU=1
        fi
    fi

    if [[ "$MEMORYMIN" != "" || "$MEMORYMAX" != "" ]];then

        if [[ "$MEMORYMAX" == "" || "$MEMORYMIN" == "" ]]; then
            return 1
        fi

        isLarge "$MEMORYMAX" "$MEMORYMIN"
        if [[ $? -eq 1 ]]; then
            return 1
        else
            MEMORY=1
        fi
    fi

    if [[ "$SESSIONMIN" != "" || "$SESSIONMAX" != "" ]];then
        if [[ "$SESSIONMAX" == "" || "$SESSIONMIN" == "" ]]; then
            return 1
        fi

        isLarge "$SESSIONMAX" "$SESSIONMIN"
        if [[ $? -eq 1 ]]; then
            return 1
        else
            SESSION=1
        fi
    fi

    return 0
}

function parseARG(){
    #it mean warning
    if [ $1 -eq 1 ];then
        CPUMIN=$(echo $2 | awk -F ',' '{print $1}' | sed "s/[^0-9]//g")
        MEMORYMIN=$(echo $2 | awk -F ',' '{print $2}' | sed "s/[^0-9]//g")
        SESSIONMIN=$(echo $2 | awk -F ',' '{print $3}' | sed "s/[^0-9]//g")
    else
        CPUMAX=$(echo $2 | awk -F ',' '{print $1}' | sed "s/[^0-9]//g")
        MEMORYMAX=$(echo $2 | awk -F ',' '{print $2}' | sed "s/[^0-9]//g")
        SESSIONMAX=$(echo $2 | awk -F ',' '{print $3}' | sed "s/[^0-9]//g")
    fi

}


function parm(){
    if [[ "$cons" == "" || "$cons" == "<<source_db>>" ]];then
    isEducate=0
else

if [[ "$cons" =~ sys/* ]];
then
  isEducate=1
  SOURCE_DB=$cons$' as sysdba'
else 
 isEducate=1
 SOURCE_DB=$cons$
fi

fi

}

while getopts "p:u:s:hP:" arg
do
    case $arg in
        h)
            showHelp
            ;;
        u)
            ;;          
        p)
            PORT=$OPTARG
            ;;
        s)
            cons=$OPTARG
            ;;
        ?) 
            showHelp
            ;;
    esac
done


if [[ "$PORT" == "" ]];then
    showHelp "特征项"
fi

#checkALL
if [[ $? == 1 ]];then
    showHelp "正确参数"
fi

#插件正题部分 
#确定存活的
#启动
#分析
parm
initLast
isAlive
analysisStat

#analysisResult


echo -e "{\c"
for key in ${keys[@]};  
do  
echo -e "\"${key}\":\"${map[$key]//\'/\"}\",\c" 
done
echo -e "\"cpname\":\"oracle\"\c"
echo -e "}"

发表回复

您的电子邮箱地址不会被公开。

Captcha Code