Zabbix 监控MySQL主从状态(Python脚本)
1、在被监控机(Mysql Slave端)放置检查脚本:
/usr/local/zabbix/bin/check_mysql_repl.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):
- ifself.io == 'Yes':
- return1
- else:
- return0
- def get_sql_status(self):
- ifself.io == 'Yes':
- return1
- else:
- return0
- 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()
2、配置zabbix_agent.conf,添加Key和监控脚本:
vi /usr/local/zabbix/etc/zabbix_agentd.conf,文件最后添加以下两行:
UserParameter=mysql.repl_io,/usr/local/zabbix/bin/check_mysql_repl.py io UserParameter=mysql.repl_sql,/usr/local/zabbix/bin/check_mysql_repl.py sql
UserParameter是固定的
mysql.repl_io 是自定的Key,下面添加监控项的时候用到
/usr/local/zabbix/bin/check_mysql_repl.py io 这一段就是上面的脚本及参数
重启zabbix_agentd
3、添加监控项:
4、添加触发器:
此触发器的作用是检测到脚本的返回值不是1就会报警。
就这样一个简单的监控主从配置就完成!~~
相关推荐
迷途风景 2020-07-28
wyqwilliam 2020-06-13
JamesRayMurphy 2020-05-31
usepython 2020-05-28
gululululu 2020-05-28
爱文学更爱编程 2020-05-26
llltaotao 2020-05-19
ericxieforever 2020-05-07
zhongzhiwei 2020-05-08
zjyhll 2020-05-06
bcbeer 2020-04-16
景泽元的编程 2020-04-16
jhshanyu00 2020-04-10
gululululu 2020-03-20
URML 2020-02-22
Yasin 2020-02-13
waiwaiLILI 2020-02-03
wintershii 2020-02-01