看了网上很多mysql主从监控的,大部分都是shell的,就算是python的,也是在python下跑shell语句。我写了一个python的监控脚本,用到了mysqldb这个包。脚本如下:

[root@SSAVL2734 libexec]# cat check_mysql_formal.py
#!/usr/bin/python
import MySQLdb
import sys
import MySQLdb.cursors
#coding:utf-8 if len(sys.argv) != 5:
print len(sys.argv)
print "Usage: %s [host port user passwd]" % sys.argv[0]
sys.exit(1)
else:
host2 = sys.argv[1]
port2 = sys.argv[2]
user2 = sys.argv[3]
passwd2 = sys.argv[4]
conn=MySQLdb.connect(host=host2,port=3306,user=user2,passwd=passwd2,cursorclass=MySQLdb.cursors.DictCursor)
cur= conn.cursor()
cur.execute(r'show slave status')
data=cur.fetchall()
print data
io=data[0]['Slave_IO_Running']
sql=data[0]['Slave_SQL_Running']
Seconds_Behind_Master=data[0]['Seconds_Behind_Master']
#print data[0]
#print Seconds_Behind_Master
#print type(Seconds_Behind_Master)
#data= dict(zip(keys,data[0]))
if sql == 'No' or io == 'No':
print 'mysql replication is critical,please contact MYSQL DBA immediately'
sys.exit(2)
else:
print 'replication is OK'
sys.exit(0)

我看很多人都是用mysql的root跑show slave status这句话,测试是没问题,在生产环境运行还是有很大风险。这里我们新建一个监控账户,赋予能够查看复制情况的权限:
mysql> grant replication client on *.* to 'nagios'@'%' identified by 'nagios';

mysql> flush privileges;

验证是否生效:

[root@SSAVL2521 libexec]# mysql -h 10.90.**.* -unagios -pnagios -e "show slave status\G"

host port user passwd分别对应服务器名 端口号 账户 密码。

执行结果如下:

[root@SSAVL2734 libexec]# python check_mysql_formal2.py 10.90.**.* 3306 nagios nagios

replication is OK

最新文章

  1. 高性能JavaScript--数据存储(简要学习笔记二)
  2. OC多态
  3. Asp.net有关访问页面权限的限制和错误页面配置
  4. windows地址转发
  5. jq三级全选全不选
  6. 第二百五十三、四、五天 how can I 坚持
  7. 以正确的方式开源 Python 项目
  8. 母亲的牛奶(milk)
  9. vue 组件开发
  10. 电脑移动后WIFI连接失败解决方法
  11. JS table内容转成二维数组,支持colspan和rowspan
  12. Redis内存数据库快速入门
  13. Kotlin入门(4)声明与操作数组
  14. Rinkeby中测试币的申请
  15. 使用/dev/uinput的简要介绍(含demo程序)【转】
  16. JSP—cookie
  17. 2-51单片机ESP8266学习-AT指令(开发板51单片机自动冷启动下载原理)
  18. Docker(二)
  19. python爬虫 发送定时气象预报
  20. tomcat启动报错:java.lang.IllegalArgumentException: Document base D:\apache-tomcat-6.0.45\webapps\activiti-explorer does not exist or is not a readable directory

热门文章

  1. SQL 网文链接
  2. Curator Recipes(Cache&Counter)
  3. FragmentTransation中的remove和detach有什么区别?
  4. 记一次Debian下PHP环境的搭建(nginx+mariadb+PHP)!
  5. VirtualBox 主机与虚拟机互通
  6. 【6】锋利的 jQuery 笔记
  7. 词典 (noi OpenJudge)
  8. [MFC美化] SkinSharp使用详解2-SkinH.h函数介绍
  9. The Clocks
  10. MyBatis学习-映射文件标签篇(select、resultMap)