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

#WARNLIMIT=-1      #警告值 超过该值则认为是需要告警
#ERRORLIMIT=-1         #错误值 超过该值认为是错误

PORT=
LIMIT=
CONN=0              #是否检查连接数
CONNMIN=
CONNMAX=
MEMORY=0            #是否检查内存
MEMORYMIN=
MEMORYMAX=
CPU=0               #是否检查CPU
CPUMIN=
CPUMAX=

SLOW_QUERY_MIN=
SLOW_QUERY_MAX=
SLOW_QUERY=

CURNUM=             #当前的性能参数

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

FLAGEID=
#是否输出FLAGPID
FLAGEOPID=1
#是否输出RUNTIME
RUNTIME=1
#是否输出STARTTIME
STARTTIME=0
VERBOSE=0
CORENUM=1
QUOTASTR=
quota=
redis_acquisition_json=""

#redis认证密码
AUTHEN=""
BASEDIR=/iflytek/redis-3.2.9

function gotErr(){
    if [ "1" -eq 3 ];then        echo "无法获取到指标,疑似组件故障,请确认并请检查监控脚本和运维平台配置"
    elif [ "1" -eq 4 ];then
        echo "该组件进程为僵尸进程,请确认并请检查该组件状态"
    fi

    exit 1
}

#返回是否服务是否存活
function isAlive(){
    CORENUM=`cat /proc/cpuinfo |grep "physical id"|wc -l`
    FLAGEID=(netstat -lnp | grep -w PORT | grep LISTEN | grep 0.0.0.0 |awk '{for(i=1;i<=NF;i++){if(i ~ /redis-/) print i}}'| awk -F '/' '{print1}' |head -n 1)
    if [[ "FLAGEID" == "" ]];then
        gotErr 3
    fi
    ZOMBIE=(ps -A -ostat,pid | grep -e '^[Zz]' | grep FLAGEID)
    if [[ "ZOMBIE" != "" ]];then
       gotErr 4
    fi
    redis_acquisition_json="redis_acquisition_json,'FLAGEPID':FLAGEID"
}


#处理连接数
function getConnNum(){
    CURNUM=(netstat -apn | grepFLAGEID | grep ESTABLISHED  | wc -l)
    redis_acquisition_json="redis_acquisition_json,'ConnectionNum':CURNUM"
}

#ps aux 4是内存 累加4的值
function getMemory(){
    CURNUM=(ps aux | grepFLAGEID |awk 'BEGIN{sum=0}{sum+=4}END{print sum}')
    redis_acquisition_json="redis_acquisition_json,'Memory':CURNUM"
}

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

#function get_redis_stat {
#  if (( VERBOSE == 1 )) ; then
#      REDIS_STAT=(./cmd/linux/redis-stat.sh -f "BASEDIR" -p "PORT" -v)
#  else
#      REDIS_STAT=(./cmd/linux/redis-stat.sh -f "BASEDIR" -p "PORT")
#  fi
#}

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

#ps -eo pid,lstart是查询进程的开始时间
function getStarttime(){
    CURNUM=(ps -eo pid,lstart | grep FLAGEID |awk '{if(1=='FLAGEID') print2 ,3 ,4 ,5 ,6}')
    redis_acquisition_json="redis_acquisition_json,'STARTTIME':'CURNUM'"
}
#服务器每秒钟执行的命令数量
function getInstantaneous_ops_per_sec(){
    CURNUM=`cat redis_{PORT}.txt | grep instantaneous_ops_per_sec |sed "s/[^0-9\.]//g"`
    if [[ "CURNUM" == "" ]];then
        CURNUM=0
    fi
    redis_acquisition_json="redis_acquisition_json,'instantaneous_ops_per_sec':'CURNUM'"
}

#获取role的值
function getRole(){
    role=`cat redis_{PORT}.txt |  grep role | awk '{split(0,a,"[:]");print a[2]}'| sed s/[[:space:]]//g`
    if [ "{role}" = "master" ];then
        CURNUM=0
    else
        CURNUM=1
    fi
    redis_acquisition_json="redis_acquisition_json,'role':'CURNUM'"
}

function getredis_version(){
CURNUM=`cat redis_{PORT}.txt | grep redis_version |sed "s/[^0-9\.]//g"`
    if [[ "CURNUM" == "" ]];then
        CURNUM=0
    fi
    redis_acquisition_json="redis_acquisition_json,'redis_version':'CURNUM'"
}
function getredis_mode(){
CURNUM=`cat redis_{PORT}.txt | grep redis_mode |awk -F ':' '{print 2}'| sed s/[[:space:]]//g`
    if [[ "CURNUM" == "standalone" ]];then
        CURNUM=0
    else
        CURNUM=1
    fi
    redis_acquisition_json="redis_acquisition_json,'redis_mode':'CURNUM'"
}
function gettcp_port(){
tcpPort=`cat redis_{PORT}.txt | grep tcp_port |awk -F ':' '{print2}'|tr -d "\r"`
    CURNUM={tcpPort}
    if [[ "CURNUM" == "" ]];then
        CURNUM=0
    fi
    redis_acquisition_json="redis_acquisition_json,'tcp_port':'CURNUM'"
}
function getuptime_in_days(){
CURNUM=`cat redis_{PORT}.txt | grep uptime_in_days |awk -F ':' '{print2}'|tr -d "\r"`
    if [[ "CURNUM" == "" ]];then
        CURNUM=0
    fi
    redis_acquisition_json="redis_acquisition_json,'uptime_in_days':'CURNUM'"
}
function getkeyspace_hits(){
CURNUM=`cat redis_{PORT}.txt | grep keyspace_hits |awk -F ':' '{print 2}'|tr -d "\r"`
    if [[ "CURNUM" == "" ]];then
        CURNUM=0
    fi
    redis_acquisition_json="redis_acquisition_json,'keyspace_hits':'CURNUM'"
}
function getkeyspace_hit_ratio(){
a=`cat redis_{PORT}.txt | grep -w keyspace_hits |awk -F ':' '{print2}'|sed 's/[^0-9]//g'`
b=`cat redis_{PORT}.txt | grep -w keyspace_misses |awk -F ':' '{print2}'|sed 's/[^0-9]//g'`
    if [ "b" -eq 0 ];then
        CURNUM=0
    else
        CURNUM=`awk -v awk_a="a" -v awk_b="b" 'BEGIN{printf "%.2f\n",(awk_a/awk_b)}'`
    fi
    redis_acquisition_json="redis_acquisition_json,'keyspace_hit_ratio':'CURNUM'"
}
function gettotal_net_input_bytes(){
CURNUM=`cat redis_{PORT}.txt | grep total_net_input_bytes |awk -F ':' '{print 2}'|tr -d "\r"`
    if [[ "CURNUM" == "" ]];then
        CURNUM=0
    fi
    redis_acquisition_json="redis_acquisition_json,'total_net_input_bytes':'CURNUM'"
}
function gettotal_net_output_byte(){
CURNUM=`cat redis_{PORT}.txt | grep total_net_output_byte |awk -F ':' '{print2}'|tr -d "\r"`
    if [[ "CURNUM" == "" ]];then
        CURNUM=0
    fi
    redis_acquisition_json="redis_acquisition_json,'total_net_output_byte':'CURNUM'"
}
function getinstantaneous_input_kbps(){
CURNUM=`cat redis_{PORT}.txt | grep instantaneous_input_kbps |awk -F ':' '{print 2}'|tr -d "\r"`
    if [[ "CURNUM" == "" ]];then
        CURNUM=0
    fi
    redis_acquisition_json="redis_acquisition_json,'instantaneous_input_kbps':'CURNUM'"
}
function getinstantaneous_output_kbps(){
CURNUM=`cat redis_{PORT}.txt | grep instantaneous_output_kbps |awk -F ':' '{print2}'|tr -d "\r"`
    if [[ "CURNUM" == "" ]];then
        CURNUM=0
    fi
    redis_acquisition_json="redis_acquisition_json,'instantaneous_output_kbps':'CURNUM'"
}
function getslowlog_len(){
CURNUM=`BASEDIR/redis-cli -h localhost -p PORT slowlog len`
    if [[ "CURNUM" == "" ]];then
        CURNUM=0
    fi
    redis_acquisition_json="redis_acquisition_json,'slowlog_len':'CURNUM'"
}
function getslowlog_max_time(){
CURNUM=`BASEDIR/redis-cli -h localhost -pPORT slowlog get|awk 'BEGIN{FS="\n";RS="\n[ \t\n]*\n|^\n+"}{print 3}'|sort -n -r|awk 'NR==1{print1}'`
    if [[ "CURNUM" == "" ]];then
        CURNUM=0
    fi
    redis_acquisition_json="redis_acquisition_json,'slowlog_max_time':'CURNUM'"
}
function getrdb_last_bgsave_status(){
CURNUM=`cat redis_{PORT}.txt | grep rdb_last_bgsave_status |awk -F ':' '{print 2}'| sed s/[[:space:]]//g`
    if [[ "CURNUM" = "ok" ]];then
        CURNUM=1
     else
        CURNUM=0
    fi
    redis_acquisition_json="redis_acquisition_json,'rdb_last_bgsave_status':'CURNUM'"
}
function getrdb_last_bgsave_time_sec(){
CURNUM=`cat redis_{PORT}.txt | grep rdb_last_bgsave_time_sec |awk -F ':' '{print2}'|tr -d "\r"`
    if [[ "CURNUM" == "" ]];then
        CURNUM=0
    fi
    redis_acquisition_json="redis_acquisition_json,'rdb_last_bgsave_time_sec':'CURNUM'"
}
function getrdb_changes_since_last_save(){
CURNUM=`cat redis_{PORT}.txt | grep rdb_changes_since_last_save |awk -F ':' '{print 2}'|tr -d "\r"`
    if [[ "CURNUM" == "" ]];then
        CURNUM=0
    fi
    redis_acquisition_json="redis_acquisition_json,'rdb_changes_since_last_save':'CURNUM'"
}
function getrdb_last_save_time(){
CURNUM=`cat redis_{PORT}.txt | grep rdb_last_save_time |awk -F ':' '{print2}'|tr -d "\r"`
    if [[ "CURNUM" == "" ]];then
        CURNUM=0
    fi
    redis_acquisition_json="redis_acquisition_json,'rdb_last_save_time':'CURNUM'"
}
function getconnected_clients(){
CURNUM=`cat redis_{PORT}.txt | grep connected_clients |awk -F ':' '{print 2}'|tr -d "\r"`
    if [[ "CURNUM" == "" ]];then
        CURNUM=0
    fi
    redis_acquisition_json="redis_acquisition_json,'connected_clients':'CURNUM'"
}
function getconnected_clients_pct(){
a=`cat redis_{PORT}.txt | grep connected_clients |awk -F ':' '{print2}'`
b=`BASEDIR/redis-cli -h localhost -pPORT config get maxclients | sed -n '2p'`
    if [[ "a" == ""|| "b" == "" || "b" -eq 0 ]];then
        CURNUM=0
    else
        CURNUM=`awk 'BEGIN{printf "%.2f\n",('{a}'/'{b}')}'`
    fi
    redis_acquisition_json="redis_acquisition_json,'connected_clients_pct':'CURNUM'"
}
function getrejected_connections(){
CURNUM=`cat redis_{PORT}.txt | grep rejected_connections |awk -F ':' '{print 2}'|tr -d "\r"`
    if [[ "CURNUM" == "" ]];then
        CURNUM=0
    fi
    redis_acquisition_json="redis_acquisition_json,'rejected_connections':'CURNUM'"
}
function gettotal_connections_received(){
CURNUM=`cat redis_{PORT}.txt | grep total_connections_received |awk -F ':' '{print2}'|tr -d "\r"`
    if [[ "CURNUM" == "" ]];then
        CURNUM=0
    fi
    redis_acquisition_json="redis_acquisition_json,'total_connections_received':'CURNUM'"
}
function getused_memory(){
CURNUM=`cat redis_{PORT}.txt | grep -w used_memory |awk -F ':' '{print 2}'|tr -d "\r"`
    if [[ "CURNUM" == "" ]];then
        CURNUM=0
    fi
    redis_acquisition_json="redis_acquisition_json,'used_memory':'CURNUM'"
}
function getused_memory_pct(){
a=`cat redis_{PORT}.txt | grep -w used_memory |awk -F ':' '{print2}'`
b=`BASEDIR/redis-cli -h localhost -pPORT config get maxmemory | sed -n '2p'`
    if [[ "a" == ""|| "b" == ""  || "b" -eq 0 ]];then
        CURNUM=0
    else
        CURNUM=`awk 'BEGIN{printf "%.2f\n",('{a}'/'{b}')}'`
    fi
    redis_acquisition_json="redis_acquisition_json,'used_memory_pct':'CURNUM'"
}
function getused_memory_rss(){
CURNUM=`cat redis_{PORT}.txt | grep -w used_memory_rss |awk -F ':' '{print 2}'|tr -d "\r"`
    if [[ "CURNUM" == "" ]];then
        CURNUM=0
    fi
    redis_acquisition_json="redis_acquisition_json,'used_memory_rss':'CURNUM'"
}
function getmem_fragmentation_ratio(){
CURNUM=`cat redis_{PORT}.txt | grep mem_fragmentation_ratio |awk -F ':' '{print2}'|tr -d "\r"`
    if [[ "CURNUM" == "" ]];then
        CURNUM=0
    fi
    redis_acquisition_json="redis_acquisition_json,'mem_fragmentation_ratio':'CURNUM'"
}
function getevicted_keys(){
CURNUM=`cat redis_{PORT}.txt | grep evicted_keys |awk -F ':' '{print 2}'|tr -d "\r"`
    if [[ "CURNUM" == "" ]];then
        CURNUM=0
    fi
    redis_acquisition_json="redis_acquisition_json,'evicted_keys':'CURNUM'"
}
function getexpired_keys(){
CURNUM=`cat redis_{PORT}.txt | grep expired_keys |awk -F ':' '{print2}'|tr -d "\r"`
    if [[ "CURNUM" == "" ]];then
        CURNUM=0
    fi
    redis_acquisition_json="redis_acquisition_json,'expired_keys':'CURNUM'"
}
function getmaster_link_status(){
CURNUM=`cat redis_{PORT}.txt | grep master_link_status |awk -F ':' '{print 2}'|tr -d "\r"`
    if [[ "CURNUM" == "" ]];then
        CURNUM=0
    elif  [[ "CURNUM" == "up" ]];then
        CURNUM=1
    else
        CURNUM=2
    fi
    redis_acquisition_json="redis_acquisition_json,'master_link_status':'CURNUM'"
}
function getmaster_link_down_since_seconds(){
CURNUM=`cat redis_{PORT}.txt | grep master_link_down_since_seconds |awk -F ':' '{print 2}'|tr -d "\r"`
    if [[ "CURNUM" == "" ]];then
      CURNUM=0
    fi
    redis_acquisition_json="redis_acquisition_json,'master_link_down_since_seconds':'CURNUM'"
}
function getmaster_last_io_seconds(){
CURNUM=`cat redis_{PORT}.txt | grep master_last_io_seconds_ago |awk -F ':' '{print2}'|tr -d "\r"`
    if [[ "CURNUM" == "" ]];then
      CURNUM=0
    fi
    redis_acquisition_json="redis_acquisition_json,'master_last_io_seconds':'CURNUM'"
}
function getslave_read_only(){
CURNUM=`cat redis_{PORT}.txt | grep slave_read_only |awk -F ':' '{print 2}'|tr -d "\r"`
    if [[ "CURNUM" == "" ]];then
      CURNUM=0
    fi
    redis_acquisition_json="redis_acquisition_json,'slave_read_only':'CURNUM'"
}
function getconnected_slaves(){
CURNUM=`cat redis_{PORT}.txt | grep connected_slaves |awk -F ':' '{print2}'|tr -d "\r"`
    if [[ "CURNUM" == "" ]];then
      CURNUM=0
    fi
    redis_acquisition_json="redis_acquisition_json,'connected_slaves':'CURNUM'"
}

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

        getConnNum

        getMemory

        getCPU

        CURNUM=FLAGEID

        getRuntime

        getStarttime

    slow_query_script="(cd `dirname0`;pwd)/check_redis_slow_query.sh"
    SLOW_RESULT=`slow_query_script -f "BASEDIR" -t "THRESHOLD" -c "LIMIT" -p "PORT" -a "AUTHEN"`
    RETURN_CODE=?

    if (( RETURN_CODE == 0 )) ; then
        CURNUM=0
        #parseFileds "Redis慢查询"SLOW_QUERY_MIN SLOW_QUERY_MAX
    elif ((RETURN_CODE == 3 )) ; then
        CURNUM=-1
        WranResultStr="{WranResultStr}Redis慢查询无法获取到慢查询的值"
        if [[ {quota} != "" ]]; then
            quota="{quota},"
        fi

         quota="{quota}Redis慢查询"
         QUOTASTR="{QUOTASTR}Redis慢查询=无法获取到慢查询的值"
         SLOW_RESULT=""
    else
        COUNT=(echoSLOW_RESULT| awk -F '<<COUNT>>' '{print 1}')
        SLOW_RESULT=(echo SLOW_RESULT| awk -F '<<COUNT>>' '{print2}')
        CURNUM={COUNT}
        #parseFileds "Redis慢查询"SLOW_QUERY_MIN SLOW_QUERY_MAX
    fi
    ######################待修改

    redis_acquisition_json="redis_acquisition_json,'Redis慢查询':'CURNUM'"

    if [ ! -f "{BASEDIR}/redis-cli" ]; then
        return 0
    fi

    if [[ "AUTHEN" == "" ]];thenBASEDIR/redis-cli -h localhost -p PORT info >redis_{PORT}.txt
    else
      BASEDIR/redis-cli -h localhost -pPORT -a AUTHEN info >redis_{PORT}.txt
    fi

    getInstantaneous_ops_per_sec

    getRole

    getredis_version

    getredis_mode

    gettcp_port

    getuptime_in_days

    getkeyspace_hits

    getkeyspace_hit_ratio

    gettotal_net_input_bytes

    gettotal_net_output_byte

    getinstantaneous_input_kbps

    getinstantaneous_output_kbps

    getslowlog_len

    getslowlog_max_time

    getrdb_last_bgsave_status

    getrdb_last_bgsave_time_sec

    getrdb_changes_since_last_save

    getrdb_last_save_time

    getconnected_clients

    getconnected_clients_pct

    getrejected_connections

    gettotal_connections_received

    getused_memory

    getused_memory_pct

    getused_memory_rss

    getmem_fragmentation_ratio

    getevicted_keys

    getexpired_keys

    getmaster_link_status

    getmaster_link_down_since_seconds

    getmaster_last_io_seconds

    getslave_read_only

    getconnected_slaves

    result="{'cpname':'redis',{redis_acquisition_json#*,}}"
    result=`echo{result//\'/\"}`
    echo result
    #fi
}



#输出错误信息并且退出程序
function showHelp(){
    if [ "1" != "" ];then
        echo "请输入1"
    fi
    echo "check_redis.sh 可以监听本地的redis的状态 参数如下"
    echo "check_redis.sh [-w 连接数,CPU,内存,慢查询] [-c 连接数,CPU,内存,慢查询] [-p <port>] [-a <password>] [-b <path>]"
    echo "-w 表示监控警告的值"
    echo "-c 表示异常值"
    echo "-t 设置记录Redis慢查询语句的执行时间阈值,默认为10000微秒"
    echo "-p 设置监听Redis数据库的端口,默认为6379"
    echo "-l 设置Redis记录慢查询语句的数目,超过此数值将删除最久的记录,默认为128"
    echo "-a 设置Redis认证密码,默认为空"
    echo "-b redis-cli可执行程序的路径"
    echo "-v 设置redis-stat脚本输出详细信息"
    exit 1
}


while getopts "f:t:l:b:p:a:hv" arg
do
    casearg in
        h)
            showHelp
            ;;
        t)
            THRESHOLD=OPTARG
            ;;
        l)             LIMIT=OPTARG
            ;;
        b)
            BASEDIR=OPTARG
            ;;
        p)
            PORT=OPTARG
            ;;
        a)
            AUTHEN=OPTARG
            ;;
        v)
            VERBOSE=1
            ;;
        ?)            showHelp
            ;;
    esac
done

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


#插件正题部分 
#确定存活的
#启动
#分析

#get_redis_stat
isAlive
analysisStat

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

Captcha Code