Home > Database > Mysql Tutorial > body text

Zabbix监控mysql Master Slave主从同步(复制)状态,附脚本

WBOY
Release: 2016-06-07 16:35:08
Original
1350 people have browsed it

使用Zabbix监控服务器的时候,用到了监控Mysql的主从状态,有关Mysql主从配置请看这篇文章:[备忘]mysql Master Slave主从同步(复制)配置及常见问题。 我们知道,Mysql主从复制同步(复制)启动后,要满足以下两个条件才算是成功,即: Slave_IO_Running:

使用Zabbix监控服务器的时候,用到了监控Mysql的主从状态,有关Mysql主从配置请看这篇文章:[备忘]mysql Master Slave主从同步(复制)配置及常见问题。

我们知道,Mysql主从复制同步(复制)启动后,要满足以下两个条件才算是成功,即:

Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Copy after login

脚本一

新建一个脚本文件,mysqlms.sh

#!/bin/bash
/usr/local/mysql/bin/mysql -uzabbix -e 'show slave status\G' |grep -E "Slave_IO_Running|Slave_SQL_Running"|awk '{print $2}'|grep -c Yes
Copy after login

将其放置到某目录(需要有权限),我这里放到/etc/zabbix/mysqlms.sh,在zabbix_agentd.conf中下方加入以下一条语句:

UserParameter=mysql.slavestatus,/etc/zabbix/mysqlms.sh
Copy after login

重启zabbix-agent:

sudo service zabbix-agent restart
Copy after login

截下来在Zabbix-Server所在服务器执行以下语句,测试是否成功:

zabbix_get -s 192.168.1.106 -k  mysql.slavestatus
Copy after login

其中192.168.1.106为刚刚脚本所在服务器,mysql.slavestatus为Key值。若返回2,则表示主从服务正常。
然后将该监控项在web平台测加入items,设置好报警通知即可。
(如何加入ietms请查看这篇文章最下方:Zabbix服务器监控系统部署之自定义监控项的添加及配置(二))

脚本二

新建脚本mysqlms2.py

#!/usr/bin/python 
#coding:utf-8 
import MySQLdb 
import sys 
class check_mysql_repl(): 
    def __init__(self): 
        self.dbhost = 'localhost' 
        self.dbuser = 'root' 
        self.dbpass = 'wisp888' 
        self.dbport = 3306 
        self.sock = "/data/db_misc/mysql_3306.sock" 
        self.conn = MySQLdb.connect(unix_socket=self.sock) #根据实际情况连接
        self.cursor = self.conn.cursor(cursorclass = MySQLdb.cursors.DictCursor) 
        self.sql = 'show slave status' 
        self.cursor.execute(self.sql) 
        self.data = self.cursor.fetchall() 
        self.io = self.data[0]['Slave_IO_Running'] 
        self.sql = self.data[0]['Slave_SQL_Running'] 
        self.conn.close() 
    def get_io_status(self): 
        if self.io == 'Yes': 
            return 1 
        else: 
            return 0 
    def get_sql_status(self): 
        if self.io == 'Yes': 
            return 1 
        else: 
            return 0                
if __name__ == "__main__": 
    if len(sys.argv) != 2: 
        print "Usage: %s [io|sql]" % sys.argv[0] 
        sys.exit(1) 
    mysql = check_mysql_repl() 
    if sys.argv[1] == "io": 
        print mysql.get_io_status() 
    elif sys.argv[1] == "sql": 
        print mysql.get_sql_status()
Copy after login

(该脚本来自网络)
打开编辑文件

vi /etc/zabbix/zabbix_agentd.conf
Copy after login

下方加入以下内容:

UserParameter=mysql.repl_io,/etc/zabbix/mysqlms2.py io 
UserParameter=mysql.repl_sql,/etc/zabbix/mysqlms2.py sql
Copy after login

其他设置同脚本一。

完。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!