If you cant explain it simply, you dont understand it well enough

获取cpu load的小脚本

#! /bin/bash

# Print alarm json messages
function print_json()
{
    local json=`echo "$1" | sed -r 's/,{2,}/,/g;s/(^,)|(,$)//g;'`

    cat <<EOF
{
    "collection_flag":0,
    "MSG": [$json]
}
EOF
}

function check_load()
{
    local cpu_num=`cat /proc/cpuinfo | grep processor | wc -l`
    local load_arr=(`uptime | awk -Faverage: '{print $2}' | tr -d ','`)
    local load1 load5 load15 load1_per_cpu load5_per_cpu load15_per_cpu
    load1=${load_arr[0]}
    load5=${load_arr[1]}
    load15=${load_arr[2]}
    load1_per_cpu=$(echo $load1 $cpu_num | awk '{print $1/$2}')
    load5_per_cpu=$(echo $load5 $cpu_num | awk '{print $1/$2}')
    load15_per_cpu=$(echo $load15 $cpu_num | awk '{print $1/$2}')

    local D_proc_cnt D_proc_msg
    D_proc_cnt=$(ps uax | grep ' [D] ' | wc -l)

    echo "{\"cpu_num\":$cpu_num,\"load1\":$load1,\"load5\":$load5,\"load15\":$load15,\"load1_per_cpu\":$load1_per_cpu,\"load5_per_cpu\":$load5_per_cpu,\"load15_per_cpu\":$load15_per_cpu,\"D_proc_cnt\":$D_proc_cnt}"
}

function main()
{
    local msg=$(check_load)
    print_json $msg
}

main
posted @ 2015-12-23 12:22  zk47  阅读(557)  评论(0编辑  收藏  举报

I am a stupid bird, and I need to work hard