码迷,mamicode.com
首页 > 数据库 > 详细

MySQL监控脚本

时间:2017-07-07 20:16:26      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:rss   nbsp   sort   $#   types   bsp   share   dev   end   

#!/bin/bash
#
#

usage() {
echo "Usage: $0 [ types... ] [ limit ] "
echo ""
echo "Types are:"
echo "[ Size ] [ Rss ] [ Swap ] [ Private ] [ Shared ]"
echo ""
printf "%-12s%-12s\n" "[ Size ]" "程序映射的内存大小,非实际占用"
printf "%-12s%-12s\n" "[ Rss ]" "实际使用内存大小(包括独占+共享)"
printf "%-12s%-12s\n" "[ Swap ]" "使用的虚拟内存大小"
printf "%-12s%-12s\n" "[ Private ]" "程序独占内存大小"
printf "%-12s%-12s\n" "[ Shared ]" "程序与其他进程共享内存大小"echo ""
echo "For example : $0 Swap 10"
echo ""
}


mem() {
printf "%-20s%-100s%-20s%-20s\n" "PID" "PRO_NAME" "SIZE(m)" "%RATIO"
for pid in `ls /proc/ |grep ^[0-9]`
do

  if [ ${pid} -eq 1 ];then
    continue
  fi

grep -q $1 /proc/${pid}/smaps 2>/dev/null

  if [ $? -eq 0 ];then
    memsize=`free -m |grep "Mem" |awk ‘{print $2}‘`
    swapsize=`free -m |grep "Swap" |awk ‘{print $2}‘`
    size=`cat /proc/$pid/smaps |grep $1 |awk ‘{sum+=$2;} END{print sum/1024}‘`
    pro_name=`ps aux | grep -w "$pid" | grep -v grep  |awk ‘{print $11}‘`
    ratioval() {
    if [ ‘$1‘ = Swap ];then
      ratio=`awk "BEGIN{print $size/$swapsize*100 \"%\"}"`
    else
      ratio=`awk "BEGIN{print $size/$memsize*100 \"%\"}"`
    fi
      echo $ratio
    }
    ratioval
    printf "%-20s%-100s%-20s%-20s\n" "${pid}" "${pro_name}" "${size}" "$ratio"

  fi
done | sort -n -r -k 3  | head -$2
}


if [ $# -eq 0 ]; then 
    usage 
 else
    mem $1 $2
fi

MySQL监控脚本

标签:rss   nbsp   sort   $#   types   bsp   share   dev   end   

原文地址:http://www.cnblogs.com/songyuejie/p/7133563.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!