Chinaunix首页 | 论坛 | 博客
  • 博客访问: 219707
  • 博文数量: 42
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 420
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-09 10:55
个人简介

每天改变一点点,生活充满了惊喜。

文章分类

全部博文(42)

文章存档

2016年(8)

2015年(29)

2014年(5)

我的朋友

分类: Mysql/postgreSQL

2015-03-31 13:45:30

Shell Code :

  1. #! /bin/sh -
  2. ##! @TODO : 统计MySQL服务器状态,包括 查询、链接、Innodb行锁、SQL执行状态。
  3. ##! @VERSION : 1.0.0
  4. ##! @AUTHOR : SunnyChan sunny__chan@126.com
  5. ##! @FILEIN : mysql_monitor
  6. ##! @FILEOUT : log/status.log , log/processlist.log
  7. ##! @DATE : 2015/03/30

  8. export PATH="/usr/bin:/bin"
  9. PROGRAM=$( basename $0 )
  10. VERSION="1.0.0"

  11. MYSQL_ADMIN="/home/mysql/mysql_ins/bin/mysqladmin -uUser -pPasswd -P3306 -h127.0.0.1"

  12. mkdir -p "log"
  13. EXT_INFO_LOG="log/status.log"
  14. PROC_INFO_LOG="log/processlist.log"
  15. > ${EXT_INFO_LOG}
  16. > ${PROC_INFO_LOG}

  17. function usage(){
  18.   echo "Usage : sh ${PROGRAM} [-e|-p]"
  19.   echo " -e print info from mysqladmin extended_status."
  20.   echo " -p print info from mysqladmin processlist."
  21. }

  22. ##! @TODO : 打印查询计数、链接计数、Innodb行锁计数
  23. function prt_ext_info(){
  24.   ${MYSQL_ADMIN} ext -i 1 |
  25.     awk 'BEGIN{printf "%-7s %-7s %-7s %-7s %-7s %-1s %-9s %-7s %-1s %-10s %-10s %-8s %-8s %-11s\n",\
  26.         "Commits","Selects","Inserts","Updates","Queries","|","Connected","Running","|",\
  27.         "Curr_Waits","Total_Time","Avg_Time","Max_Time","Total_Waits";
  28.         printf "%-43s %-21s %-20s\n"," ","[ Threads ]","[ Innodb_Row_Lock (time unit : ms) ]";
  29.         is_first_line=1;
  30.         qp = 0;
  31.      }
  32.      /Com_commit / { commits = $4 - last_commits; last_commits = $4 }
  33.      /Com_select / { selects = $4 - last_selects; last_selects = $4 }
  34.      /Com_insert / { inserts = $4 - last_inserts; last_inserts = $4 }
  35.      /Com_update / { updates = $4 - last_updates; last_updates = $4 }
  36.      /Queries/ { queries = $4 - last_queries; last_queries = $4; }
  37.      /Innodb_row_lock_current_waits/ { row_lock_curr_waits = $4; }
  38.      /Innodb_row_lock_time / { row_lock_total_time = $4 - last_total_time; last_total_time = $4; }
  39.      /Innodb_row_lock_time_avg/ { row_lock_avg_time = $4; }
  40.      /Innodb_row_lock_time_max/ { row_lock_avg_max = $4; }
  41.      /Innodb_row_lock_waits/ { row_lock_total_waits = $4 - last_total_waits;last_total_waits = $4; }
  42.      /Threads_connected/{ thr_conn = $4; }
  43.              /Threads_running/{
  44.         /* 因为打印的是取样时间内的变化值,由于第一行数据是总数,跳过 */
  45.         if ( is_first_line )
  46.             is_first_line = 0;
  47.      else
  48.             printf "%-7d %-7d %-7d %-7d %-10d %-9d %-10d %-10d %-10d %-8d %-8d %-11d\n",\
  49.             commits,selects,inserts,updates,queries,thr_conn,$4,\
  50.             row_lock_curr_waits,row_lock_total_time,row_lock_avg_time,row_lock_avg_max,row_lock_total_waits;
  51.      }
  52.      {fflush();}'
  53. }

  54. ##! @TODO : 打印SQL执行状态
  55. function prt_processlist()
  56. {
  57.   ${MYSQL_ADMIN} processlist -i 1 |
  58.     awk 'BEGIN{FS = "|"}
  59.     { if ($0 ~ /Id.*State/){printf "\n" };
  60.      /* 只打印有状态的链接,另外去掉了向从服务器同步binlog的链接 */
  61.      if ($8 ~ /[a-zA-Z]/ && $8 !~ /binlog to slave/){
  62.         split($9,arr_info," ");
  63.         split($8,arr_state," ");
  64.         printf "%-4s %-10s %-10s %-7s %-10s %-10s %-10s\n",\
  65.         $2,arr_state[1],arr_state[2],arr_info[1],arr_info[2],arr_info[3],arr_info[4];
  66.      }
  67.      fflush();
  68.     }'
  69. }

  70. #
  71. if [[ "$1" = "-e" ]]
  72. then
  73.     prt_ext_info | tee -a ${EXT_INFO_LOG}
  74. fi

  75. if [[ "$1" = "-p" ]]
  76. then
  77.     prt_processlist | tee -a ${PROC_INFO_LOG}
  78. fi

  79. if [[ "$1" = "-h" ]] || [[ "$1" = "-?" ]]
  80. then
  81.     usage
  82. fi
用法及输出解析:

上图打印的是从 mysqladmin extended-status 获取的一些关键信息,取样的时间为1s。
一开始的5列是事务和SQL请求相关的统计,分别是过去1s 完成的事务、Select、Insert、Update数目以及总的请求数。

接下来两列是链接相关统计,对应于 mysqladmin extended_status 中的 Threads_Connected、Threads_Running。
Threads_Connected :The number of currently open connections.
Threads_Running:The number of threads that are not sleeping.

最后5列是Innodb锁相关的统计,分别为 当前等待锁的事务数,过去1s锁定的总时长,平均等待时间,最长一次等待的时间,过去1s发生等待的总数。
Total_Time 和Total_waits 两列 mysqladmin extended-status  原来的统计是系统启动到现在的总数据,脚本变更为了过去1s的统计数据,更有意义一些


上图打印的是从 mysqladmin processlist 获取的查询请求执行状态信息,过滤了无状态的线程和主从同步线程。


阅读(2271) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~