MySQL监控脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL监控脚本相关的知识,希望对你有一定的参考价值。

#!/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监控脚本的主要内容,如果未能解决你的问题,请参考以下文章

Python脚本监控mysql数据库,Python脚本监控mongo数据库

mysql主从同步脚本监控脚本

mysql主从同步监控脚本

MySQL的keepalived高可用监控脚本

Shell开发MySQL主从监控脚本

python 监控mysql脚本