查看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 
 
     keyword: which to match the process that you wanna statistics.
 
     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 ~]# 

  

  

作者:Standby一生热爱名山大川、草原沙漠,还有妹子
出处:http://www.cnblogs.com/standby/

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/standby/p/8360516.html