ITPub博客

首页 > 自动化运维 > DevOps > shell监控mysql 5.7数据库

shell监控mysql 5.7数据库

原创 DevOps 作者:z597011036 时间:2023-02-14 17:30:16 0 删除 编辑

脚本结果:


脚本内容:

#!/bin/bash
# Mysql Version 5.7.30
host='localhost'
user=root
password=System135
port=3306
#Mysql数据收集
cmd1="select version();"
cmd2="show master status;"
cmd3="show global status like 'Connections';"
cmd4="show global status like 'Questions';"
cmd5="show global status like 'Uptime';"
cmd6="show global status like 'Com_Commit';"
cmd7="show global status like 'Com_Rollback';"
cmd8="show global status like 'key_reads';"
cmd9="show global status like 'key_read_requests';"
cmd10="show global status like 'key_writes';"
cmd11="show global status like 'key_write_requests';"
cmd12="show global status like 'innodb_buffer_pool_reads';"
cmd13="show global status like 'innodb_buffer_pool_read_requests';"
cmd14="show global status like 'Qcache_hits';"
cmd15="show global status like 'Qcache_inserts';"
cmd16="show global status like 'Threads_created';"
cmd17="show global status like 'Connections';"
cmd18="select count(*) from information_schema.innodb_trx;"
cmd19="select count(*) from information_schema.innodb_locks;"
cmd20="select count(*) from information_schema.innodb_lock_waits;"
cmd21="select count(*) from information_schema.processlist;"
#基本信息
Basic() {
  version_number=`mysql -h$host -P$port -u$user -p$password -e "$cmd1" 2>&1 | grep -v version | grep -v "Using a password on the command"`
  master_status=`mysql -h$host -P$port -u$user -p$password -e "show master status;" 2>&1 | grep -v File | grep -v "Using a password on the command" | wc -l`
  slave_status=`mysql -h$host -P$port -u$user -p$password -e "show slave status\G;" 2>&1 | grep -v "Using a password on the command" | wc -l`
  echo "Mysql Version: $version_number"
  if [ $slave_status -gt 2 ];then
    echo "Mysql Role: Slave"
  elif [ $master_status == 1 ] && [ $slave_status -gt 2 ];then
    echo "Mysql Role: Master and Slave"
  else
    echo "Mysql Role: Master"
  fi
}
#Mysql端口检查
Port() {
  process=`ps -ef | grep mysql | grep -v grep | grep sock | awk '{print $2}'`
  port=`ss -tulp | grep $process | wc -l`
  port_number=`ss -tulp | grep $process | awk '{print $5}' | awk -F":" '{print $4}'`
  if [ $process ] && [ $port == 1 ];then
    echo "---------------------------------Mysql端口和进程都正常[端口:$port_number]"
  else
    echo -e "\033[31m---------------------------------Mysql端口和进程都异常,退出脚本\033[0m"
    exit
  fi
}
#Mysql进程检查
Process() {
  process_number=`mysql -h$host -P$port -u$user -p$password -e "$cmd21" 2>&1 | grep -v 'count' | grep -v "Using a password on the command"`
  if [ $process_number -le 100 ];then
    echo "---------------------------------Mysql当前进程数小于等于100个正常[进程数:$process_number]"
  elif [ $process_number -gt 100 ] && [ $process_number -lt 200 ];then
    echo -e "\033[33m---------------------------------Mysql当前进程数大于100小于200个警告[进程数:$process_number]\033[0m"
  else
    echo -e "\033[31m---------------------------------Mysql当前进程数大于200个异常[进程数:$process_number]\033[0m"
  fi
}
#Mysql连接数
Connect() {
   connect_number=`mysql -h$host -P$port -u$user -p$password -e "$cmd3" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'`
   if [ $connect_number -lt 100 ];then
     echo "---------------------------------Mysql当前连接数小于等于100个正常[连接数:$connect_number]"
   elif [ $connect_number -gt 100 ] && [ $connect_number -lt 200 ];then
     echo -e "\033[33m---------------------------------Mysql当前连接数大于100小于200个警告[连接数:$connect_number]\033[0m"
  else
     echo -e "\033[31m---------------------------------Mysql当前连接数大于200个异常[连接数:$connect_number]\033[0m"
  fi
}
#统计Mysql的QPS
Qps() {
  Questions=`mysql -h$host -P$port -u$user -p$password -e "$cmd4" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'`
  Uptime=`mysql -h$host -P$port -u$user -p$password -e "$cmd5" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'`
  qps=$(echo "scale=0;$Questions / $Uptime"|bc)
  #qps=$(echo "scale=2;$Questions / $Uptime"|bc)
  if [ $qps -lt 1000 ];then
    echo "---------------------------------Mysql当前Qps小于等于1000个正常[Qps数:$qps]"
  elif [ $qps -gt 1000 ] && [ $qps -lt 2000 ];then
    echo -e "\033[33m---------------------------------Mysql当前Qps大于1000小于2000个警告[Qps数:$qps]\033[0m"
 else
    echo -e "\033[31m---------------------------------Mysql当前Qps大于2000个异常[Qps数:$qps]\033[0m"
 fi
}
#统计Mysql的Tps
Tps() {
  Com_Commit=`mysql -h$host -P$port -u$user -p$password -e "$cmd6" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'`
  Com_Rollback=`mysql -h$host -P$port -u$user -p$password -e "$cmd7" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'`
  Uptime=`mysql -h$host -P$port -u$user -p$password -e "$cmd5" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'`
  #tps=`expr $Com_Commit + $Com_Rollback / $Uptime`
  tps=$(echo "$Com_Commit + $Com_Rollback / $Uptime"|bc)
  if [ $tps -le 100 ];then
    echo "---------------------------------Mysql当前Tps小于等于100个正常[Com_Commit:$Com_Commit  Com_Rollback:$Com_Rollback  Tps:$tps]"
  fi
}
#统计key Buffer命中率
KeyBuffer() {
  key_reads=`mysql -h$host -P$port -u$user -p$password -e "$cmd8" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'`
  key_read_requests=`mysql -h$host -P$port -u$user -p$password -e "$cmd9" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'`
  key_writes=`mysql -h$host -P$port -u$user -p$password -e "$cmd10" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'`
  key_write_requests=`mysql -h$host -P$port -u$user -p$password -e "$cmd11" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'`
  if [ $key_read_requests != 0 ];then
     key_buffer_read_hits=`echo "scale=2;(1 - $key_reads / $key_read_requests)"|bc`
     key_buffer_read_hits=`awk 'BEGIN {printf "%.2f%%\n",('$key_buffer_read_hits' * 100)}'`
     echo "---------------------------------Mysql当前Key Buffer读命中率正常[Key_Buffer_Read_Hits:$key_buffer_read_hits]"
  else
     echo "---------------------------------Mysql当前Key Buffer读命中率为为0]"
  fi
  if [ $key_write_requests != 0 ];then
     key_buffer_write_hits=`echo "scale=2;(1 - $key_writes / $key_write_requests)"|bc`
     key_buffer_write_hits=`awk 'BEGIN {printf "%.2f%%\n",('$key_buffer_write_hits' * 100)}'`
     echo "---------------------------------Mysql当前Key Buffer写命中率正常[Key_Buffer_Write_Hits:$key_buffer_write_hits]"
  else
     echo "---------------------------------Mysql当前Key Buffer写命中率为0]"
  fi
}
#统计Innodb Buffer命中率
InnodbBuffer() {
  innodb_buffer_pool_reads=`mysql -h$host -P$port -u$user -p$password -e "$cmd12" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'`
  innodb_buffer_pool_read_requests=`mysql -h$host -P$port -u$user -p$password -e "$cmd13" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'`
  innodb_buffer_read_hits=`echo "scale=2;(1 - $innodb_buffer_pool_reads / $innodb_buffer_pool_read_requests)"|bc`
  innodb_buffer_read_hits=`awk 'BEGIN {printf "%.2f%%\n",('$innodb_buffer_read_hits' * 100)}'`
  echo "---------------------------------Mysql当前Innodb Buffer命中率正常[Innodb_Buffer_Read_Hits:$innodb_buffer_read_hits]"
}
#统计Query Cache命中率
QueryCache() {
  qcache_hits=`mysql -h$host -P$port -u$user -p$password -e "$cmd14" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'`
  qcache_inserts=`mysql -h$host -P$port -u$user -p$password -e "$cmd15" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'`
#判断qcache_hits+qcache_inserts是否为0,因为0不能做被除数
  if [ `expr $qcache_hits + $qcache_inserts` != 0 ];then
      query_cache_hits=`echo "scale=2;$qcache_hits / ($qcache_hits + $qcache_inserts)"|bc`
      query_cache_hits=`awk 'BEGIN {printf "%.2f%%\n",('$query_cache_hits' * 100)}'`
      echo "---------------------------------Mysql当前Query Cache命中率正常[Query_Cache_Hits:$query_cache_hits]"
  else
      echo "---------------------------------Mysql当前Query Cache命中率为0"
  fi
}
#统计Thread Cache命中率
ThreadCache() {
  threads_created=`mysql -h$host -P$port -u$user -p$password -e "$cmd16" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'`
  connections=`mysql -h$host -P$port -u$user -p$password -e "$cmd17" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'`
  thread_cache_hits=`echo "scale=2;(1- $threads_created / $connections)"|bc`
  thread_cache_hits=`awk 'BEGIN {printf "%.2f%%\n",('$thread_cache_hits' * 100)}'`
  echo "---------------------------------Mysql当前Thread Cache命中率正常[Thread_Cache_Hits:$thread_cache_hits]"
}
#当前运行多少个事物
Trx() {
  trx_number=`mysql -h$host -P$port -u$user -p$password -e "$cmd18" 2>&1 | egrep -v "count|Using a password on the command"`
  echo "---------------------------------Mysql当前Transaction个数[Transaction:$trx_number]"
}
#统计多少个锁
Lock() {
  lock_number=`mysql -h$host -P$port -u$user -p$password -e "$cmd19" 2>&1 | egrep -v "count|Using a password on the command"`
  lock_wait_number=`mysql -h$host -P$port -u$user -p$password -e "$cmd20" 2>&1 | egrep -v "count|Using a password on the command"`
  echo "---------------------------------Mysql当前Lock个数[Lock Number:$lock_number  Lock_Wait_Number:$lock_wait_number]"
}
#判断从库状态
Slave_status() {
  mster_status=`mysql -h$host -P$port -u$user -p$password -e "show master status;" 2>&1 | grep -v File | grep -v "Using a password on the command" | wc -l`
  slave_status=`mysql -h$host -P$port -u$user -p$password -e "show slave status\G;" 2>&1 | grep -v "Using a password on the command" | wc -l`
  if [ $master_status == 1 ];then
    exit
  elif [ $master_status == 0 ] && [ $slave_status -gt 2 ];then
      Slave_IO_Running=`mysql -h$host -P$port -u$user -p$password -e "show slave status \G" 2>&1 | grep -v "Using a password on the command" | grep Slave_IO_Running | awk '{print $2}'`
      Slave_SQL_Running=`mysql -h$host -P$port -u$user -p$password -e "show slave status \G" 2>&1 | grep -v "Using a password on the command" | grep Slave_SQL_Running | grep -v "Slave_SQL_Running_Sta
te" | awk '{print $2}'`      if [ $Slave_IO_Running == "Yes" ] && [ $Slave_SQL_Running == "Yes" ];then
         echo "---------------------------------Mysql从库正常[Slave_IO_Running:$Slave_IO_Running  Slave_SQL_Running:$Slave_SQL_Running]"
      else
         echo -e "\033[31m---------------------------------Mysql从库异常[Slave_IO_Running:$Slave_IO_Running  Slave_SQL_Running:$Slave_SQL_Running]\033[0m"
      fi
      Seconds_Behind_Master=`mysql -h$host -P$port -u$user -p$password -e "show slave status \G" 2>&1 | grep -v "Using a password on the command" | grep "Seconds_Behind_Master" | awk '{print $2}'`
      echo "---------------------------------Mysql主从数据延迟[Seconds_Behind_Master:$Seconds_Behind_Master]"
  fi
}
Basic
Port
Process
Connect
Qps
Tps
KeyBuffer
InnodbBuffer
QueryCache
ThreadCache
Trx
Lock
Slave_status

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/25854343/viewspace-2935315/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-03-31

  • 博文量
    79
  • 访问量
    486678