zabbix自定义监控(主从、主从延迟)

1.mysql主从监控

主库授权

mysql> grant  replication slave  on *.* to 'csl'@'192.168.153.137' identified by '123' ;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit

//从库上测试
[root@localhost ~]# mysql -ucsl -p123 -h192.168.153.138
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit
Bye

修改配置文件

//修改主库配置文件
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve

server-id = 10  //添加此行
log-bin = mysql_bin   //添加此行

//重启服务
[root@localhost ~]# systemctl restart mysqld.service

//修改从库配置文件
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve

server-id = 20
relay-log = my_relay

//重启服务
[root@localhost ~]# systemctl restart mysqld.service

开启主从服务

//在主库上查看主库信息
mysql>  show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql_bin.000003 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)


//在从库上配置主库信息
mysql> change master to
    -> master_host='192.168.153.138' ,
    -> master_user='csl' ,
    -> master_password='123' ,
    -> master_log_file=' mysql_bin.000003' ,
    -> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> start slave ;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.153.138
                  Master_User: csl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000003
          Read_Master_Log_Pos: 154
               Relay_Log_File: my_relay.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql_bin.000003
             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




检查主从

//主库创建数据库
mysql> create database csl;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| csl                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)


//从库查看
mysql> 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| csl                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> 

在zabbix agent端编写脚本

[root@localhost ~]# mkdir /scripts
[root@localhost ~]# cd /scripts/
[root@localhost scripts]# touch check_replication.sh
[root@localhost scripts]# chmod +x check_replication.sh
[root@localhost scripts]# vim check_replication.sh
#!/bin/bash
count=$(mysql --defaults-file=/scripts/.password  -e 'show slave  status\G'  | grep '_Running:' |grep -c 'Yes' )
if [ $count -ne 2  ];then
        echo  '1'
else
        echo  '0'
fi
[root@localhost ~]# vim /scripts/.password
[client]
user=zabbix
password=123


[root@localhost scripts]# ./check_replication.sh 
0

//授权登录的用户
[root@localhost scripts]# mysql -uroot -p123

mysql> grant select on *.* to 'zabbix'@'localhost' identified by  '123';
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> grant SUPER, REPLICATION CLIENT  on *.* to 'zabbix'@'localhost' identified by '123';
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)


修改agent的配置文件

[root@localhost ~]# cd /usr/local/etc/
[root@localhost etc]# vim zabbix_agentd.conf
# Mandatory: no
# Range: 0-1
# Default:
UnsafeUserParameters=1     //取消注释,并改为1

UserParameter=check_replication,/scripts/check_replication.sh    //在文件末尾添加此行

//重启
[root@localhost ~]# zabbix_agentd


//在server端检查key是否可用
[root@localhost ~]# zabbix_get  -s 192.168.153.137 -k check_replication 
0


创建监控项
在这里插入图片描述
添加触发器
在这里插入图片描述
在这里插入图片描述

触发报警

//从库触发报警
[root@localhost ~]# mysql -uroot -p123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 53
Server version: 5.7.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

mysql> 



//zabbix server端 查看键值
[root@localhost ~]# zabbix_get  -s 192.168.153.137 -k check_replication 
1

在这里插入图片描述
查看邮件
在这里插入图片描述

2.mysql主从延迟

agent端写脚本

//写主从延迟脚本
[root@localhost scripts]# vim check_replication_delay.sh
#!/bin/bash
  
delay_count=$(mysql --defaults-file=/scripts/.password  -e 'show slave status\G'| grep 'Behind' | awk '{print $2}')
if [ "$delay_count" != NULL ];then
        echo $delay_count
else
        echo 0
fi
[root@localhost scripts]# chmod +x check_replication_delay.sh
[root@localhost scripts]# ./check_replication_delay.sh 
0

agent端修改配置文件

//
[root@localhost scripts]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_replication_delay,/scripts/check_replication_delay.sh   //添加此行


[root@localhost scripts]# pkill zabbix
[root@localhost scripts]# zabbix_agentd 

服务端检查键值

[root@localhost ~]# zabbix_get  -s 192.168.153.137 -k check_replication_delay
0

创建监控项
在这里插入图片描述
添加触发器
在这里插入图片描述
效果
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Zabbix中监视MySQL主从状态,需要执行以下步骤: 1. 确保MySQL主从复制已正确配置并正在运行。可以通过在主服务器上运行SHOW MASTER STATUS; 和在从服务器上运行SHOW SLAVE STATUS; 来检查复制状态。 2. 在MySQL主服务器上创建一个具有适当权限的MySQL用户以供Zabbix使用。可以使用以下命令创建用户: CREATE USER 'zabbix'@'localhost' IDENTIFIED BY 'password'; GRANT REPLICATION CLIENT ON *.* TO 'zabbix'@'localhost'; 3. 在Zabbix服务器上安装MySQL监视器模板。该模板包含用于监视MySQL服务器的预定义项和触发器。 4. 在Zabbix服务器上创建一个MySQL主服务器主机,将其与MySQL监视器模板关联,并配置主机的连接参数。这些参数应包括MySQL主服务器的IP地址、端口和上一步中创建的MySQL用户的凭据。 5. 在Zabbix服务器上创建一个MySQL从服务器主机,将其与MySQL监视器模板关联,并配置主机的连接参数。这些参数应包括MySQL从服务器的IP地址、端口和上一步中创建的MySQL用户的凭据。 6. 等待一段时间,以便Zabbix收集有关MySQL主从复制状态的数据。可以通过查看Zabbix监视器模板中的图形和报告来检查这些数据。 7. 如果需要,可以根据需要创建自定义Zabbix触发器,以便在MySQL主从复制状态出现问题时接收警报。 请注意,这只是一个基本的概述。实际的实施可能因环境和要求的不同而有所不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值