发两个linux下简单统计资源消耗的脚本

总共两个程序, 好久之前写的了, 简单用用还成.

第一个是监控系统整体资源消耗的程序, 其实就是在vmstat -n 时间这个命令上再封装一下. 主要是有两个功能:

1. vmstat中的free, buff, cache这三个字段, 其实都表示的是”空闲内存”, 所以在有的情况下, 需要统计这几个的和值
2. 加上时间戳, 方便统计

#! /bin/bash
vmResult=vmstat.csv;
ioResult=iostat.csv;
vmTemp="${vmResult}.tmp";
ioTemp="${ioResult}.tmp";
# time step
step=1;

# $1: file
function printTime()
{
        echo "Time is `date '+%Y-%m-%d %H:%M:%S'`" >> $1;
}

function start()
{
        echo "Starting the cmd: vmstat, time step: $step second(s)" > $vmTemp;
        printTime $vmTemp;
        vmstat -n $step | awk '{ if ($1~/([0-9])+/) { printf "%s\t",$0; system("date +\"%T\""); } else { print $0; }}' >> $vmTemp &
        echo "Starting the cmd: iostat, time step: $step second(s)" > $ioTemp;
        printTime $ioTemp;
        iostat -x $step >> $ioTemp &
}

function stop()
{
        killall -9 vmstat awk;
        echo "End of cmd: vmstat" >> $vmTemp;
        printTime $vmTemp;
        awk '{ if ($1~/([0-9])+/) { $18=$4+$5+$6; print} else { print $0; }}' $vmTemp > $vmResult;

        killall -9 iostat;
        echo "End of cmd: iostat" >> $ioTemp;
        printTime $ioTemp;
        cat $ioTemp > $ioResult;

        rm $vmTemp $ioTemp;
}
case C"$1" in
        C)
                echo "Usage: $0 start|stop"
                ;;
        Cstart)
                start;
                echo "Start monitor!"
                ;;
        Cstop)
                stop;
                echo "Stop monitor!"
                ;;
        C*)
                echo "Usage: $0 start|stop"
                ;;
esac 

第二个, 是监控系统中某个或某几个特定进程的资源消耗(主要是mem), 也是对系统命令的封装. 这个封装的是ps aux命令, 然后根据指定的进程名称进行筛选和统计.

#! /bin/bash
psResult="ps.list"
# time step
step=1;

# $1: file
function printTime()
{
        echo "Time is `date '+%Y-%m-%d %H:%M:%S'`" >> $1;
}

function start()
{
        echo "Starting the cmd: ps aux, time step: $step second(s)" > $psResult;
        echo "Starting the cmd: ps aux, time step: $step second(s)" > ${psResult}.all;
        echo "USER       PID %CPU %MEM   VSZ  RSS TTY      STAT START   TIME COMMAND" >> $psResult;
        echo "VSZ  RSS TIME" >> ${psResult}.all;
        printTime $psResult;
        printTime ${psResult}.all;
        while (true)
        do
                psAux=`ps aux | grep "$1" | grep -v "grep" | grep -v "control_ps.sh"`
                echo "$psAux" | awk '{ printf "%s\t",$0; system("date +\"%T\""); }' >> $psResult
                echo "$psAux" | awk 'BEGIN{sum5=0;sum6=0}{sum5+=$5;sum6+=$6}END{ printf ("%d\t%d\t", sum5, sum6); system("date +\"%T\""); }' >> ${psResult}.all
                sleep $step
        done &
}

function stop()
{
        killall -9 control_ps.sh ps;
        echo "End of cmd: ps aux" >> $psResult
}
case C"$1" in
        C)
                echo "Usage: $0 start|stop"
                ;;
        Cstart)
                if [[ x"$2" = x ]]; then
                        echo "Usage: $0 start module_name|stop"
                        exit 1
                fi
                start $2;
                echo "Start monitor!"
                ;;
        Cstop)
                stop;
                echo "Stop monitor!"
                ;;
        C*)
                echo "Usage: $0 start module_name|stop"
                ;;
esac

本文链接:https://www.poisonbian.com/post/1121.html 转载需授权!

分享到:
原文链接:,转发请注明来源!
「发两个linux下简单统计资源消耗的脚本」评论列表

发表评论