查看swap占用情况

查看swap被占用的情况

#!/bin/bash
# Get current swap usage for all running processes
# Erik Ljungstrom 27/05/2011
# http://northernmost.org/blog/find-out-what-is-using-your-swap/

SUM=0
OVERALL=0
for DIR in `find /proc/ -maxdepth 1 -type d | egrep "^/proc/[0-9]"` ; do
        PID=`echo $DIR | cut -d / -f 3`
        PROGNAME=`ps -p $PID -o comm --no-headers`
        for SWAP in `grep Swap $DIR/smaps 2>/dev/null| awk '{ print $2 }'`
        do
                let SUM=$SUM+$SWAP
        done
        echo "PID=$PID - Swap used: $SUM - ($PROGNAME )"
        let OVERALL=$OVERALL+$SUM
        SUM=0

done
echo "Overall swap used: $OVERALL"

使用:

[root@standby  ~]# ./swap.sh |sort -nr -k 5 |head -10
PID=13899 - Swap used: 2071424 - ([ET_NET 0] )
PID=4124 - Swap used: 741224 - (nginx )
PID=19965 - Swap used: 741212 - (nginx )
PID=19960 - Swap used: 741212 - (nginx )
PID=19969 - Swap used: 741172 - (nginx )
PID=19968 - Swap used: 741172 - (nginx )
PID=19967 - Swap used: 741172 - (nginx )
PID=19966 - Swap used: 741172 - (nginx )
PID=19964 - Swap used: 741172 - (nginx )
PID=19962 - Swap used: 741172 - (nginx )
[root@standby ~]# 

 

 

查看指定进程的swap占用情况

#!/bin/bash
# Get current swap usage for input process.
# 71standby@gmail.com

function Usage()
{
    echo -e "Usage: /bin/sh $0 keyword \n \
     keyword: which to match the process that you wanna statistics.\n \
     Example: 'traffic' will match the traffic_cop,traffic_manager,traffic_server and so on."
    exit 2
}

function Statistics()
{
    SUM=0
    OVERALL=0
    for PID in `ps -ef |grep $1 |grep -v grep |grep -v $0 |awk '{print $2}'`; do
        PROGNAME=`ps -p $PID -o comm --no-headers`
        DIR=/proc/$PID/smaps
        for SWAP in `grep Swap $DIR 2>/dev/null| awk '{ print $2 }'`; do
            let SUM=$SUM+$SWAP
        done
        echo "PID=$PID - Swap used: $SUM - ($PROGNAME)"
        let OVERALL=$OVERALL+$SUM
        SUM=0
    done
    echo "$1 swap used: $OVERALL"
}

if [ $# -ne 1 ]; then
    Usage
else
    Statistics $1
fi

使用:

[root@standby ~]# ./swap_show.sh traffic
PID=48575 - Swap used: 764 - (traffic_cop)
PID=48577 - Swap used: 12948 - (traffic_manager)
PID=48588 - Swap used: 3823100 - ([ET_NET 0])
traffic swap used: 3836812
[root@standby ~]# 

  

  

posted @ 2018-01-26 17:11  青山应回首  阅读(2245)  评论(0编辑  收藏  举报