栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

基于pidstat 内存检测脚本

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于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 $@

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/962008.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号