check_mem.sh内存状态脚本

  sre

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

swap_total_mem_min=99999999990
swap_total_mem_max=99999999999
swap_total_mem=0

swap_used_mem_min=
swap_used_mem_max=
swap_used_mem=0

swap_free_mem_min=
swap_free_mem_max=
swap_free_mem=0

swap_use_per_min=20
swap_use_per_max=70
swap_use_per=0

total_mem_min=99999999990
total_mem_max=99999999999
total_mem=0

used_mem_min=
used_mem_max=
used_mem=0

free_mem_min=
free_mem_max=
free_mem=0

mem_used_per_min=20
mem_used_per_max=70
mem_used_per=0


load1min=
load1max=
load1=0
load5min=
load5max=
load5=0
load15min=
load15max=
load15=0

StatStr=
CURNUM=             #当前的性能参数

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

quota=
QUOTASTR=

function gotErr(){
    if [ "1" -eq 2 ];then        result="{'cpname':'mem',DataResultStr}"
        result=`echo {result//\'/\"}`
        echoresult
    elif [ "1" -eq 3 ];then        echo "无法获取到指标,疑似组件故障,请确认并请检查监控脚本和运维平台配置"
    fi

    exit1
}

function contain(){
    result=(echo1 | grep "2")
    if [[ "result" != "" ]]
    then
        return 1
    else
        return 0
    fi
}


swap_total_mem_value=0
swap_used_mem_value=0
swap_free_mem_value=0

total_mem_value=0
used_mem_value=0
free_mem_value=0
#依据传入的参数值来判断我们要监控的服务是什么
#然后通过各个函数获取到具体的值
#值之间的
function analysisStat(){
    mem_info=`free -m`
    total_mem_value=`echo "mem_info" |awk 'NR==2{print2}'`
    used_mem_value=`echo "mem_info" |awk 'NR==2{print3}'`
    free_mem_value=`echo "mem_info" |awk 'NR==2{print4}'`
    title=`echo "mem_info" | head -n 1`
    if [[ "title" =~ "buff/cache" ]];then
        swap_total_mem_value=`echo "mem_info"|awk 'NR==3{print2}'`
        swap_used_mem_value=`echo "mem_info" |awk 'NR==3{print3}'`
        swap_free_mem_value=`echo "mem_info" |awk 'NR==3{print4}'`
        buffers_cached_value=`echo "mem_info" |awk 'NR==2{print6}'`
#        used_mem_value=((used_mem_value - buffers_cached_value ))
#        free_mem_value=(( used_mem_value +buffers_cached_value ))
    else
        swap_total_mem_value=`echo "mem_info"|awk 'NR==4{print2}'`
        swap_used_mem_value=`echo "mem_info" |awk 'NR==4{print3}'`
        swap_free_mem_value=`echo "mem_info" |awk 'NR==4{print4}'`
        buffers_mem_value=`echo "mem_info" |awk 'NR==2{print6}'`
        buffers_cached_value=`echo "mem_info" |awk 'NR==2{print7}'`
        used_mem_value=((used_mem_value - buffers_cached_value -buffers_mem_value ))
        free_mem_value=((used_mem_value + buffers_cached_value +buffers_mem_value ))
    fi




    CURNUM=swap_total_mem_value
    parseFileds "swap_total_mem" 


    CURNUM=swap_used_mem_value
    parseFileds "swap_used_mem" 

    CURNUM=swap_free_mem_value
    parseFileds "swap_free_mem" 

    if [ "swap_total_mem_value" -eq 0 ];then
        CURNUM=0
    else
        CURNUM=`awk 'BEGIN{printf "%.2f\n",('swap_used_mem_value'/'swap_total_mem_value')*100}'`
    fi
    parseFileds "swap_use_per" 

    CURNUM=total_mem_value
    parseFileds "total_mem"

    CURNUM=used_mem_value
    parseFileds "used_mem" 

    CURNUM=free_mem_value
    parseFileds "free_mem" 

    CURNUM=`awk 'BEGIN{printf "%.2f\n",('used_mem_value'/'total_mem_value')*100}'`
    parseFileds "mem_used_per" 

}

#CURNUM            当前值
#WARNLIMIT         告警的上限
#ERRORLIMIT    异常的上限
#判断以上三个值并拼接结果字符串
function parseFileds(){
    if [[ "DataResultStr" != "" ]];then
        DataResultStr=DataResultStr","
    fi
    DataResultStr="DataResultStr'1':'CURNUM'"

}

#分析结果 给出给出状态
function analysisResult(){
        gotErr 2
}

#输出错误信息并且退出程序
function showHelp(){
    if [ "1" != "" ];then
        echo "请输入1"
    fi
    echo "check_mem.sh 可以监听本地的memory的状态"
    exit 1
}

while getopts "h" arg
do
    case $arg in
        h)
            showHelp
            ;;
        ?) 
            showHelp
            ;;
    esac
done


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

使用:

[root@sre ~]# bash check_mem.sh 
{"cpname":"mem","swap_total_mem":"2027","swap_used_mem":"73","swap_free_mem":"1954","swap_use_per":"3.60","total_mem":"1837","used_mem":"1150","free_mem":"89","mem_used_per":"62.6
0"}

LEAVE A COMMENT

Captcha Code