查看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 ~]# 

  

  

转载于:https://www.cnblogs.com/standby/p/8360516.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值