热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

MYSQL数据库状态检查脚本(Python版)_MySQL

MYSQL数据库状态检查脚本(Python版)
python

bitsCN.com #原shell版View Code
  1 #!/bin/bash   2   3 # Script Name: mysql_status_check.sh   4 # Description: check mysql servers status   5 # Author: Xinggang Wang - OpsEye.com   6 # Create Date: 2012/3/30   7   8 #获取MySQL所在服务器IP/端口/用户名/密码   9 read -p "Host=" HOST  10 read -p "Port=" PORT  11 read -p "User=" USER  12 read -sp "Password=" PASSWORD  13 echo  14  15 #默认为127.0.0.1/3306/root  16 if [ "${HOST}" = "" ]  17 then  18 HOST='127.0.0.1'  19 fi  20  21 if [ "${PORT}" = "" ]  22 then  23 PORT='3306'  24 fi  25  26 if [ "${USER}" = "" ]  27 then  28 USER='root'  29 fi  30  31 #注意密码为空的时候的格式  32 mysql_list="  33 $HOST:$PORT:$USER:$PASSWORD  34 "  35 #计算函数,提高脚本效率  36 compute(){  37 formula="$1"  38 awk 'BEGIN{printf("%.2f",'$formula')}' 2>/dev/null &&  39 echo $value || echo NULL  40 }  41  42 for mysql in $mysql_list  43 {  44 host=${mysql%%:*}  45 port=$(echo $mysql|awk -F: '{print $2}')  46 user=$(echo $mysql|awk -F: '{print $3}')  47 passwd=${mysql##*:}  48  49 [ -z "$passwd" ] && mysql="mysql -h$host -P$port -u$user" ||  50 mysql="mysql -h$host -P$port -u$user -p$passwd"  51  52 unset Uptime  53 # 把show global status的值赋给相应的参数名称(这里相当于大量的变量赋值操作)  54 eval $( $mysql -e "show global status" | awk '{print $1"=/x27"$2"/047"}')  55 [ X = X"$Uptime" ] && continue  56  57 # Mysql VER  58 VER=`$mysql -e"status;"|grep 'Server version'|awk '{print $3}'`  59  60 # Uptime  61 UPTIME=`compute "$Uptime/3600/24"`  62  63 # Threads_connected  64 threads_cOnnected=`compute "$Threads_connected"`  65  66 # QPS Questions/Uptime  67 qps=`compute "$Questions/$Uptime"`  68  69 # TPS (Com_commit + Com_rollback)/Uptime  70 tps=`compute "($Com_commit+$Com_rollback)/$Uptime"`  71  72 # Reads Com_select + Qcache_hits  73 reads=`compute "$Com_select+$Qcache_hits"`  74  75 # Writes Com_insert + Com_update + Com_delete + Com_replace  76 writes=`compute "$Com_insert+$Com_update+$Com_delete+$Com_replace"`  77  78 # Read/Writes Ratio reads/writes*100%  79 rwratio=`compute "$reads/$writes*100"`%  80  81 # MyISAM Key_buffer_read_hits (1 - Key_reads/Key_read_requests) * 100  82 key_buffer_read_hits=`compute "(1-$Key_reads/$Key_read_requests)*100"`%  83  84 # MyISAM Key_buffer_write_hits (1 - Key_writes/Key_write_requests) * 100  85 key_buffer_write_hits=`compute "(1-$Key_writes/$Key_write_requests)*100"`%  86  87 # Query_cache_hits (Qcache_hits / (Qcache_hits + Qcache_inserts)) * 100%  88 query_cache_hits=`compute "$Qcache_hits/($Qcache_hits+$Qcache_inserts)*100"`%  89  90 # Innodb_buffer_read_hits (1 - Innodb_buffer_pool_reads/Innodb_buffer_pool_read_requests) * 100  91 innodb_buffer_read_hits=`compute "(1-$Innodb_buffer_pool_reads/$Innodb_buffer_pool_read_requests)*100"`%  92  93 # Thread_cache_hits (1 - Threads_created / Connections) * 100%  94 thread_cache_hits=`compute "(1-$Threads_created/$Connections)*100"`%  95  96 # Slow_queries_per_second Slow_queries / Uptime * 60  97 slow_queries_per_secOnd=`compute "$Slow_queries/$Uptime"`  98  99 # Select_full_join_per_second Select_full_join / Uptime * 60 100 select_full_join_per_secOnd=`compute "$Select_full_join/$Uptime*60"` 101 102 # select_full_join_in_all_select (Select_full_join / Com_select) * 100 103 select_full_join_in_all_select=`compute "($Select_full_join/$Com_select)*100"`% 104 105 # MyISAM Lock Contention (Table_locks_waited / Table_locks_immediate) * 100 106 myisam_lock_cOntention=`compute "($Table_locks_waited/$Table_locks_immediate)*100"`% 107 108 # Temp_tables_to_disk (Created_tmp_disk_tables / Created_tmp_tables) * 100 109 temp_tables_to_disk_ratio=`compute "($Created_tmp_disk_tables/$Created_tmp_tables)*100"`% 110 111 # print formated MySQL status report 112 title="******************** MySQL--${HOST}--${PORT} ***********************" 113 width=$((`echo "$title"|wc -c`-1)) 114 115 echo "$title" 116 117 export IFS=':' 118 while read name value ;do 119 printf "%36s :/t%10s/n" $name $value 120 done <99%):$key_buffer_read_hits 130 MyISAM Key buffer write hits:$key_buffer_write_hits 131 Query cache hits:$query_cache_hits 132 InnoDB buffer read hits(>95%):$innodb_buffer_read_hits 133 Thread cache hits(>90%):$thread_cache_hits 134 Slow queries per second:$slow_queries_per_second 135 Select full join per second:$select_full_join_per_second 136 Select full join in all select:$select_full_join_in_all_select 137 MyiSAM lock contention(<1%):$myisam_lock_contention 138 Temp tables to disk ratio:$temp_tables_to_disk_ratio 139 EOF 140 141 unset IFS 142 143 for i in `seq $width`;{ echo -n "*";};echo 144 } 145 146 exit 0
#Python版View Code
  1 #!/usr/bin/env python  2   3 #-*- coding: utf-8 -*-  4   5 # Script Name: mysql_status_check.py  6   7 # Description: check mysql servers status  8   9 # Author: Bruce.Zuo  10  11 # Create Date: 2012/06/05 12  13 import os,sys 14  15 import MySQLdb 16  17 import getpass 18  19  20  21 host=raw_input("host:") 22  23 user=raw_input("user:") 24  25 password=getpass.getpass() 26  27  28  29 try: 30  31    cOnn= MySQLdb.connect(host = host, user=user ,passwd = password, db = 'test') 32  33 except MySQLdb.ERROR,e: 34  35    print "Error %d:%s"%(e.args[0],e.args[1]) 36  37    exit(1) 38  39 cursor=conn.cursor() 40  41  42  43 cursor.execute('show global status;') 44  45 result_set=cursor.fetchall() 46  47 cursor.close() 48  49 conn.close() 50  51  52  53 def get_value(key_name): 54  55         for rows in result_set: 56  57                 if rows[0]==key_name: 58  59                         return float(rows[1]) 60  61  62  63 print ('MySQL-'+host+'-3306').center(60,'*') 64  65 print 'Uptime:'.rjust(40),get_value('Uptime') 66  67 print 'Threads_connected:'.rjust(40),get_value('Threads_connected') 68  69 print 'QPS:'.rjust(40),round(get_value('Questions') / get_value('Uptime'),2) 70  71 print 'TPS:'.rjust(40),round(get_value('Com_commit')+get_value('Com_rollback') / get_value('Uptime'),2) 72  73 reads=get_value('Com_select')+ get_value('Qcache_hits') 74  75 writes=get_value('Com_insert')+get_value('Com_update')+get_value('Com_delete')+get_value('Com_replace') 76  77 print 'Reads:'.rjust(40),get_value('Com_select')+ get_value('Qcache_hits') 78  79 print 'Writes:'.rjust(40),get_value('Com_insert')+get_value('Com_update')+get_value('Com_delete')+get_value('Com_replace') 80  81 print 'Read/Writes Ratio:'.rjust(40),round(reads / writes,2),'%' 82  83 print 'MyISAM Key buffer read hits(>99%):'.rjust(40),round(1-get_value('Key_reads') / (get_value('Key_read_requests')*100),2),'%' 84  85 print 'MyISAM Key buffer write hits:'.rjust(40),round(1-get_value('Key_writes') / (get_value('Key_write_requests')*100),2),'%' 86  87 print 'Query cache hits:'.rjust(40),round(get_value('Qcache_hits') / (get_value('Qcache_hits')+get_value('Qcache_inserts'))*100,2),'%' 88  89 print 'InnoDB buffer read hits(>95%):'.rjust(40),round(1-get_value('Innodb_buffer_pool_reads') / (get_value('Innodb_buffer_pool_read_requests')*100),2),'%' 90  91 print 'Thread cache hits(>90%):'.rjust(40),round(1-get_value('Threads_created') / (get_value('Connections')*100),2),'%' 92  93 print 'Slow queries per second:'.rjust(40),round(get_value('Slow_queries') / get_value('Uptime'),2) 94  95 print 'Select full join per second:'.rjust(40),round(get_value('Select_full_join') / get_value('Uptime'),2) 96  97 print 'Select full join in all select:'.rjust(40),round(get_value('Select_full_join') / (get_value('Com_select')*100),2),'%' 98  99 print 'MyiSAM lock contention(<1%):'.rjust(40),round(get_value('Table_locks_waited') / (get_value('Table_locks_immediate')*100),2),'%'100 101 print 'Temp tables to disk ratio:'.rjust(40),round(get_value('Created_tmp_disk_tables') / (get_value('Created_tmp_tables')*100),2),'%'102 103 print '*'*60

#根据这个方法,可以添加更多的状态项。

#效果图

bitsCN.com
推荐阅读
  • 搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的详细步骤
    本文详细介绍了搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的步骤,包括环境说明、相关软件下载的地址以及所需的插件下载地址。 ... [详细]
  • 如何实现织梦DedeCms全站伪静态
    本文介绍了如何通过修改织梦DedeCms源代码来实现全站伪静态,以提高管理和SEO效果。全站伪静态可以避免重复URL的问题,同时通过使用mod_rewrite伪静态模块和.htaccess正则表达式,可以更好地适应搜索引擎的需求。文章还提到了一些相关的技术和工具,如Ubuntu、qt编程、tomcat端口、爬虫、php request根目录等。 ... [详细]
  • 安装mysqlclient失败解决办法
    本文介绍了在MAC系统中,使用django使用mysql数据库报错的解决办法。通过源码安装mysqlclient或将mysql_config添加到系统环境变量中,可以解决安装mysqlclient失败的问题。同时,还介绍了查看mysql安装路径和使配置文件生效的方法。 ... [详细]
  • 本文介绍了Python高级网络编程及TCP/IP协议簇的OSI七层模型。首先简单介绍了七层模型的各层及其封装解封装过程。然后讨论了程序开发中涉及到的网络通信内容,主要包括TCP协议、UDP协议和IPV4协议。最后还介绍了socket编程、聊天socket实现、远程执行命令、上传文件、socketserver及其源码分析等相关内容。 ... [详细]
  • Java实战之电影在线观看系统的实现
    本文介绍了Java实战之电影在线观看系统的实现过程。首先对项目进行了简述,然后展示了系统的效果图。接着介绍了系统的核心代码,包括后台用户管理控制器、电影管理控制器和前台电影控制器。最后对项目的环境配置和使用的技术进行了说明,包括JSP、Spring、SpringMVC、MyBatis、html、css、JavaScript、JQuery、Ajax、layui和maven等。 ... [详细]
  • PHP设置MySQL字符集的方法及使用mysqli_set_charset函数
    本文介绍了PHP设置MySQL字符集的方法,详细介绍了使用mysqli_set_charset函数来规定与数据库服务器进行数据传送时要使用的字符集。通过示例代码演示了如何设置默认客户端字符集。 ... [详细]
  • Centos7.6安装Gitlab教程及注意事项
    本文介绍了在Centos7.6系统下安装Gitlab的详细教程,并提供了一些注意事项。教程包括查看系统版本、安装必要的软件包、配置防火墙等步骤。同时,还强调了使用阿里云服务器时的特殊配置需求,以及建议至少4GB的可用RAM来运行GitLab。 ... [详细]
  • 本文介绍了在Hibernate配置lazy=false时无法加载数据的问题,通过采用OpenSessionInView模式和修改数据库服务器版本解决了该问题。详细描述了问题的出现和解决过程,包括运行环境和数据库的配置信息。 ... [详细]
  • Python语法上的区别及注意事项
    本文介绍了Python2x和Python3x在语法上的区别,包括print语句的变化、除法运算结果的不同、raw_input函数的替代、class写法的变化等。同时还介绍了Python脚本的解释程序的指定方法,以及在不同版本的Python中如何执行脚本。对于想要学习Python的人来说,本文提供了一些注意事项和技巧。 ... [详细]
  • 本文介绍了Hyperledger Fabric外部链码构建与运行的相关知识,包括在Hyperledger Fabric 2.0版本之前链码构建和运行的困难性,外部构建模式的实现原理以及外部构建和运行API的使用方法。通过本文的介绍,读者可以了解到如何利用外部构建和运行的方式来实现链码的构建和运行,并且不再受限于特定的语言和部署环境。 ... [详细]
  • 本文介绍了高校天文共享平台的开发过程中的思考和规划。该平台旨在为高校学生提供天象预报、科普知识、观测活动、图片分享等功能。文章分析了项目的技术栈选择、网站前端布局、业务流程、数据库结构等方面,并总结了项目存在的问题,如前后端未分离、代码混乱等。作者表示希望通过记录和规划,能够理清思路,进一步完善该平台。 ... [详细]
  • 本文介绍了RPC框架Thrift的安装环境变量配置与第一个实例,讲解了RPC的概念以及如何解决跨语言、c++客户端、web服务端、远程调用等需求。Thrift开发方便上手快,性能和稳定性也不错,适合初学者学习和使用。 ... [详细]
  • 本文介绍了计算机网络的定义和通信流程,包括客户端编译文件、二进制转换、三层路由设备等。同时,还介绍了计算机网络中常用的关键词,如MAC地址和IP地址。 ... [详细]
  • 本文介绍了在Mac上配置环境变量,实现Python3的命令行调用的步骤。首先通过官网下载或使用brew安装Python3,并找到安装路径。然后将该路径添加到环境变量中,可以通过编辑.bash_profile文件或执行source命令来实现。配置完成后,即可在命令行中直接调用Python3。 ... [详细]
  • 树莓派语音控制的配置方法和步骤
    本文介绍了在树莓派上实现语音控制的配置方法和步骤。首先感谢博主Eoman的帮助,文章参考了他的内容。树莓派的配置需要通过sudo raspi-config进行,然后使用Eoman的控制方法,即安装wiringPi库并编写控制引脚的脚本。具体的安装步骤和脚本编写方法在文章中详细介绍。 ... [详细]
author-avatar
sadsafsasgdg
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有