基于pidstat 内存检测脚本

#!/bin/bash

#set -x
#set -e
#env init

print_usage(){
    printf "
cyberrt_mem_parse
    memory check tool
USAGE: 
    cyber_mem_parse cmd [param]

    NOTE:
    1 must run init cmd, 
    2 must run catch_ps to save pid info, 
    3 must check crontab and set it.
    check crontab with crontab -l, if you not task for crontab , use crontab -e,
    add these txt to crontab:

    */5  * * * * /mnt/nvme/memcheck/memcheck.sh record_pidstat

    cmdlist:
    init 
        init the data dir.
    catch_ps
        catch the ps -aux info for mem parse. 
        app will be restarted by watchdog, pid will change, so catch it.
    check
        check memory info file and pids file exist.
    parse
        parse memory info with pid.
    parse_all
        auto parse memory info with pid file.
    display
        display pid and app.
    help  
        show this help info. 
"
}

MEM_INFO_PATH=/mnt/nvme/memcheck/
MEM_INFO_FILE=$MEM_INFO_PATH/mem.txt
PID_FILE=$MEM_INFO_PATH/ps_aux_info.txt

init()
{
    echo "################# param $@ #################"

    if [ -d $MEM_INFO_PATH ]; then
        echo "$MEM_INFO_PATH exist."
    else
        echo "can't find will mkdir $MEM_INFO_PATH."
        mkdir -p $MEM_INFO_PATH 
        touch $MEM_INFO_FILE
    fi

}

crontab_check()
{
    crontab -l
}

catch_ps_info()
{
    echo "################# param $@ #################"
    ps -aux >$PID_FILE
}

check_file_is_exist()
{
    echo "################# param $@ #################"

    if [ -e $MEM_INFO_FILE ]; then
        echo "$MEM_INFO_FILE exist."
    else
        echo "can't find $MEM_INFO_FILE."
    fi

    if [ -e $PID_FILE ]; then
        echo "$PID_FILE exist."
    else
        echo "can't find $PID_FILE."
    fi
}

record_pidstat()
{
    echo "################# param $@ #################"
    pidstat -r >>$MEM_INFO_FILE
}

mem_parse_by_pid()
{
    echo "################# param $@ #################"
    #PID=`egrep -i "*cyber*" $PID_FILE |awk '{print $2}'`
    egrep $1 $MEM_INFO_FILE
}

display_pid_info()
{
    echo "################# param $@ #################"
    egrep -i "*cyber*" $PID_FILE |awk '{print $2, $11, $12, $13}'
}

auto_parse_all_pids()
{
    echo "################# param $@ #################"
    PIDS=`egrep -i "*cyber*" $PID_FILE |awk '{print $2}'`

    for pid in ${PIDS[@]}; do 
        echo "pid is $pid"
        mem_parse_by_pid $pid
    done
}

main() {

    local cmd=$1

    case $cmd in
        init)
            init
            cp -rf $0 $MEM_INFO_PATH/
        ;;
        timer_set)
            crontab_set
        ;;
        check)
            check_file_is_exist $@
        ;;
        record_pidstat)
            record_pidstat
        ;;
        catch_ps)
            catch_ps_info
        ;;
        parse)
            mem_parse_by_pid $2
        ;;
        parse_all)
            auto_parse_all_pids    
        ;;
        display)
            display_pid_info
        ;;

        help)
            print_usage
        ;;
        *)
            print_usage
       ;;
    esac
}

main $@

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值