Zabbix调用Python3脚本监控MySQL主从同步状态
笺注:这是在 MySQL5.6主从/主主同步 的基础上进行的。
从服务器的数据库管理员root@localhost无密码验证登录MySQL:
查看当前登录的数据库用户:
mysql> Select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
mysql>
查看root@localhost的权限:
mysql> show grants for root@localhost;
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql>
注释:此时,root@localhost是没有密码的
显示所有的库:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| data1 |
| data2 |
| mysql |
| performance_schema |
+--------------------+
5 rows in set (0.07 sec)
注释:库mysql是默认就有的。
监控原理:从服务器的状态中,以下两项(Slave_IO_Running、Slave_SQL_Running)的返回值都必须为Yes
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.168.130
Master_User: happy
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000001
Read_Master_Log_Pos: 454
Relay_Log_File: mysqld-relay-bin.000005
Relay_Log_Pos: 236
Relay_Master_Log_File: binlog.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 454
Relay_Log_Space: 570
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 5bb1908b-7fcc-11e8-9ee0-000c292b173a
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
mysql>
注释:Seconds_Behind_Master是衡量Master与Slave之间主从同步延迟情况的一个重要参数,返回值为0时,表示没有延迟。
######
在从服务器上安装第三方库(PyMySQL),可参考:Python3脚本管理Linux下的MySQL
查看PyMySQL的版本:
[root@mysql-2 ~]# pip3 freeze
PyMySQL==0.10.1
[root@mysql-2 ~]#
注释:PyMySQL可以管理MySQL,也可以管理MariaDB
######
在从服务器上创建Python3脚本:(监控Slave_IO_Running、Slave_SQL_Running)
[root@mysql_2 ~]# cat /usr/local/Replication_status.py
#coding=utf-8
import pymysql
def db_connect():
db=pymysql.connect("localhost", "root", "", "mysql",3306,charset="utf8") #打开数据库连接;本地连接(localhost)、用户名、密码、其中一个库(mysql)的名称、端口号、字符集。
cursor=db.cursor()
cursor.execute("show slave status") #执行SQL语句
Results=cursor.fetchone()
#print (Results)
#print (Results[10])
#print (Results[11])
a = Results[10]
b = Results[11]
if a=='Yes' and b=='Yes':
print(1)
else:
print(0)
db.close()
def main():
try:
db_connect()
except Exception as e:
print(e)
if __name__ == "__main__":
main()
设置脚本权限:
[root@mysql_2 ~]# chmod a+x /usr/local/Replication_status.py
[root@mysql_2 ~]#
测试:
在从服务器上关闭同步:
mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)
mysql>
在从服务器上本地测试Python3脚本:
[root@mysql_2 ~]# python3 /usr/local/Replication_status.py
0
[root@mysql_2 ~]#
在从服务器上开启同步:
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql>
在从服务器上本地测试Python3脚本:
[root@mysql_2 ~]# python3 /usr/local/Replication_status.py
1
[root@mysql_2 ~]#
###############
被监控主机的Agent的安装可参考 Zabbix使用Agent监控CentOS6/Redhat6
修改被监控主机的Agent配置文件:(只监控从服务器)
[root@mysql_2 ~]# vi /etc/zabbix/zabbix_agentd.conf
# UnsafeUserParameters=0
修改为:(启用该功能)
UnsafeUserParameters=1
插入:
UserParameter=Replication_status,/usr/bin/python3 /usr/local/Replication_status.py
如下图:
保存好配置文件后,重启zabbix-agent:
[root@mysql_2 ~]# service zabbix-agent restart
Shutting down Zabbix agent: [确定]
Starting Zabbix agent: [确定]
[root@mysql_2 ~]#
######
在Zabbix服务器测试,验证能否获取被监控主机(192.168.168.131)的键值:
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.168.131 -p10050 -k "Replication_status"
1
[root@centos8 ~]#
##################Zabbix添加被监控主机:(只监控从服务器)
配置》主机》创建主机: (主机名称无需与被监控主机的真实主机名一致;群组可以随便选;IP地址那里要输入被监控主机的IP地址;端口为10050)
注释:
使用agent代理程序的接口。
使用 TCP 10050 端口。
主机添加成功:(主机可以不用模板的)
给“主从同步状态”创建监控项:
自定义名称: MySQL Replication_status
键值: Replication_status
信息类型: 数字(无正负)
备注:其他地方保持默认,点击底下的“添加”按键。
给监控项“MySQL Replication_status”创建触发器:(返回值等于0就告警)
自定义名称: MySQL Replication_status is down
严重性: 严重
插入表达式:(监控项: MySQL_Slave_2: MySQL Replication_status )
自动生成的表达式:(触发器的表达式要用到监控项中的键值)
{MySQL_Slave_2:Replication_status.last()}=0
本页拖下去:
给监控项“MySQL Replication_status”创建图形:
监控项:
查看图形“MySQL Replication_status image”:
监测》图形:
注释:图形中的“最新”值为 1
测试:
在从服务器上关闭同步:
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
mysql>
在从服务器上本地测试Python3脚本:
[root@mysql_2 ~]# python3 /usr/local/Replication_status.py
0
[root@mysql_2 ~]#
在Zabbix服务器上远程测试:
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.168.131 -k "Replication_status"
0
[root@centos8 ~]#
监控项“MySQL Replication_status”的返回值等于0时,仪表板会如下图显示:(显示对应触发器的名称)
MySQL Replication_status is down
图形“MySQL Replication_status image”会自动发生改变:
注释:图形中的数据线是从右往左延伸的。
注释:图形中的“最新”值变为 0
在从服务器上开启同步:
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql>
图形“MySQL Replication_status image”也会自动发生改变:
注释:图形中的“最新”值变为 1
仪表板的告警自动消失:
###############
###############
在从服务器上创建Python3脚本:(监控Seconds_Behind_Master)
[root@mysql_2 ~]# cat /usr/local/Seconds_Behind_Master.py
#coding=utf-8
import pymysql
def db_connect():
db = pymysql.connect("localhost", "root", "", "mysql",3306,charset="utf8")
cursor = db.cursor()
SQL_1 = "show slave status" #SQL语句
cursor.execute(SQL_1) #执行SQL语句
Result_1 = cursor.fetchone()
print(Result_1[32])
#print(type(Result_1[32])) #<class 'int'>
db.close()
def main():
try:
db_connect()
except Exception as e:
print(e)
if __name__ == "__main__":
main()
设置脚本权限:
[root@mysql_2 ~]# chmod a+x /usr/local/Seconds_Behind_Master.py
[root@mysql_2 ~]#
运行脚本的结果:
[root@mysql-2 ~]# python3 /usr/local/Seconds_Behind_Master.py
0
[root@mysql-2 ~]#
###############
修改被监控主机的Agent配置文件:(只监控从服务器)
[root@mysql_2 ~]# vi /etc/zabbix/zabbix_agentd.conf
在UnsafeUserParameters=1下面插入自定义的键值:
UserParameter=My_Seconds_Behind_Master,/usr/bin/python3 /usr/local/Seconds_Behind_Master.py
保存好配置文件后,重启zabbix-agent:
[root@mysql_2 ~]# service zabbix-agent restart
Shutting down Zabbix agent: [确定]
Starting Zabbix agent: [确定]
[root@mysql_2 ~]#
######
在Zabbix服务器测试,验证能否获取被监控主机(192.168.168.131)的键值:
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.168.131 -k My_Seconds_Behind_Master
0
[root@centos8 ~]#
######
给键值“Seconds_Behind_Master”创建监控项:
自定义名称: Seconds Behind Master 192.168.168.130
键值: My_Seconds_Behind_Master
信息类型: 数字(无正负)
单位: !秒
备注:其他地方保持默认,点击底下的“添加”按键。
给监控项“Seconds Behind Master 192.168.168.130”创建图形:
监控项:
查看图形“Seconds Behind Master 192.168.168.130 image”:
监测》图形:
给监控项“Seconds Behind Master 192.168.168.130”创建触发器:
自定义名称: Replication lag is too high (over 30m for 5m)
严重性: 警告
表达式: {MySQL_Slave_2:My_Seconds_Behind_Master.min(5m)}>30m
注释:触发器的表达式要用到监控项中的键值。
本页拖下去:
再次查看图形“Seconds Behind Master 192.168.168.130 image”:
注释:
replication lag
即,复制滞后
Replication lag is too high (over 30m for 5m)
即,连续5分钟,Seconds_Behind_Master的时长超过1800秒
相关文章:
Zabbix使用模板监控MySQL主从同步状态
Zabbix调用Python3脚本监控Linux下的MySQL
Zabbix调用Python3脚本监控Linux进程/内存使用率/分区使用率
#################################
#################################
亲,学习研究也要劳逸结合哦,来我微店逛逛,买点东西好好犒劳犒劳自己和家人吧^_^^_^
苏泊尔多功能电热锅韩式电火锅8-10人家用电炒锅不粘锅电锅电烤锅
苏泊尔电火锅多功能家用电热锅不沾锅一体电煮锅宿舍4-6人
|