一、Zabbix监控mysql主从
1、部署mysql主从,使用mariadb进行操作
192.168.244.142 master.example.com master
192.168.244.143 slave.example.com slave
2、将server、agent1、master、slave主机的/etc/hosts文件全部设置为
192.168.244.139 server.example.com server
192.168.244.141 agent1.example.com agent1
192.168.244.142 master.example.com master
192.168.244.143 slave.example.com slave
3、master和slave为centos8的操作系统,将centos8的安装源下载下来,然后两台主机都安装mariadb mariadb-server
[root@slave yum.repos.d]# dnf -y install mariadb mariadb-server
[root@master yum.repos.d]# dnf -y install mariadb mariadb-server
systemctl restart mariadb;systemctl enable mariadb
4、两台主机都初始化mysql数据库
mysql_secure_installation
5、修改数据库配置文件,然后两台主机都重启mariadb服务
[root@master ~]# cat /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[mysqld]
log_bin=mysql-bin
server_id=20
[root@master ~]# systemctl restart mariadb.service
[root@slave ~]# cat /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[mysqld]
log_bin=mysql-bin
server_id=30
[root@slave ~]# systemctl restart mariadb.service
6、进入数据库配置主从
master:
[root@master ~]# mysql -uroot -p1
MariaDB [(none)]> grant replication slave on *.* to 'user'@'slave' identified by '1';
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by "redhat";
Query OK, 0 rows affected (0.000 sec)
slave:
MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by "1";
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> change master to master_host='master',master_user='user',master_password='1';
Query OK, 0 rows affected (0.003 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.004 sec)
MariaDB [(none)]> show slave status\G;
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes
7、在slave主机中安装zabbix-agent软件包,将slave添加到zabbix web监控平台中
[root@slave ~]# cat /etc/yum.repos.d/zabbix.repo
[aliyun]
name=aliyun
baseurl=https://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/8/x86_64/
enable=1
gpgcheck=0
[qinghua]
name=Zabbix Official Repository - $basearch
#baseurl=http://repo.zabbix.com/zabbix/3.4/rhel/7/$basearch/
baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.4/rhel/8/$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE5
[root@slave ~]# dnf -y install zabbix-agent
8、修改/etc/zabbix/zabbix_agentd.conf,重启服务
vim /etc/zabbix/zabbix_agentd.conf
Server=192.168.244.139
ServerActive=192.168.244.139
Hostname=slave
[root@slave ~]# systemctl restart zabbix-agent.service
[root@slave ~]# systemctl enable zabbix-agent.service
Created symlink /etc/systemd/system/multi-user.target.wants/zabbix-agent.service → /usr/lib/systemd/system/zabbix-agent.service.
9、进入zabbix web监控平台,添加主机
10、在slave主机上配置脚本
[root@slave ~]# vim /etc/zabbix/script/mysql_slave_status.sh
#!/bin/bash
USER="root"
PASSWD="1"
NAME=$1
function IO {
if [ $Slave_IO_Running == "Connecting" ];then
echo 0
else
echo 1
fi
}
bin/bash
USER="root"
iiPASSWD="1"
#!/bin/bash
#!/bin/bash
USER="root"
PASSWD="1"
NAME=$1
function IO {
Slave_IO_Running=`mysql -u $USER -p$1 -e "show slave status\G;" 2> /dev/null |grep Slave_IO_Running |awk '{print $2}'`
if [ $Slave_IO_Running == "Connecting" ];then
echo 0
else
echo 1
fi
}
function SQL {
Slave_SQL_Running=`mysql -u $USER -p$1 -e "show slave status\G;" 2> /dev/null |grep Slave_SQL_Running: |awk '{print $2}'`
if [ $Slave_SQL_Running == "Yes" ];then
echo 0
else
echo 1
fi
[root@slave script]# chmod +x mysql_slave_status.sh
[root@slave script]# chown -R zabbix.zabbix /etc/zabbix/script
11、编写一个自配置文件,里面指定上面编写的脚本的路径,然后重启服务
[root@slave ~]# cd /etc/zabbix/zabbix_agentd.d/
[root@slave zabbix_agentd.d]# vim userparameter_mysql_slave.conf
[root@slave zabbix_agentd.d]# cat userparameter_mysql_slave.conf
UserParameter=mysql.slave[*],/etc/zabbix/script/mysql_slave_status.sh $1
[root@slave zabbix_agentd.d]# chown -R zabbix.zabbix /etc/zabbix/zabbix_agentd.d/userparameter_mysql_slave.conf
[root@slave zabbix_agentd.d]# systemctl restart zabbix-agent.service
12、去zabbix server验证状态,使用zabbix_get命令验证,需要先下载zabbix-get软件包
[root@server ~]# zabbix_get -s 192.168.244.143 -k mysql.slave[sql]
0
[root@server ~]# zabbix_get -s 192.168.244.143 -k mysql.slave[io]
0
13、在zabbix web平台配置
新建监控项
新建触发器 创建图形
14、测试验证,将mysql主从关闭,查看zabbix告警信息,验证邮箱是否能接收到邮件
二、zabbix监控主从延迟
1、配置库脚本
[root@slave ~]# cd /etc/zabbix/script/
[root@slave script]# vim mysql_delay.sh
[root@slave script]# cat mysql_delay.sh
#Behind:落后主库多少秒,存在秒数则出现主库复制之间的延迟
#只要当延迟数据为NULL,以及0-200是正常的,否则其他数字输入1表示错误
#!/bin/bash
delay=$(mysql -uroot -p1 -e 'show slave status\G' 2> /dev/null | grep 'Seconds_Behind_Master' | awk '{print $2}')
if [ $delay == "NULL" ];then
echo 0
elif [ $delay -ge 0 ] && [ $delay -le 200 ];then
echo 0
else
echo $delay
fi
[root@slave script]# chown -R zabbix.zabbix mysql_delay.sh
[root@slave script]# chmod +x mysql_delay.sh
2、配置agentd文件,并重启服务
[root@slave ~]# vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql_slave.conf
[root@slave ~]# cat /etc/zabbix/zabbix_agentd.d/userparameter_mysql_slave.conf
UserParameter=mysql.slave[*],/etc/zabbix/script/mysql_slave_status.sh $1
UserParameter=check_mysql_delay,/bin/bash /etc/zabbix/script/mysql_delay.sh
[root@slave ~]# systemctl restart zabbix-agent.service
[root@slave ~]# cd /etc/zabbix/script/
[root@slave script]# ./mysql_delay.sh
0
3、zabbix server主机进行脚本测试
[root@server ~]# zabbix_get -s 192.168.244.143 -k check_mysql_delay
0
4、在zabbix web平台操作
添加监控项
添加触发器
5、测试报警信息是否发送到邮箱
三、创建主机组
1、创建主机–以master主机为例
Master:
[root@master yum.repos.d]# scp root@192.168.244.139:/etc/yum.repos.d/zabbix.repo .
root@192.168.244.139's password:
zabbix.repo 100% 384 27.3KB/s 00:00
[root@master yum.repos.d]# ls
CentOS-Base.repo epel-testing-modular.repo
epel-modular.repo epel-testing.repo
epel.repo zabbix.repo
[root@master ~]# dnf -y install zabbix-agent
[root@master ~]# vim /etc/zabbix/zabbix_agentd.conf
Server=192.168.244.139
ServerActive=192.168.244.139
Hostname=master
[root@master ~]# systemctl restart zabbix-agent.service
[root@master ~]# systemctl enable zabbix-agent.service
Created symlink /etc/systemd/system/multi-user.target.wants/zabbix-agent.service → /usr/lib/systemd/system/zabbix-agent.service.
2、在zabbix web平台创建主机组
3、创建主机
四、创建用户组
五、创建用户
|