当前位置:   article > 正文

Zabbix自定义监控(主从延迟)_zabbix delay

zabbix delay

1、自定义MySQL主从监控和声音报警

1.1 部署MySQL主从

数据库主服务器

  1. [root@master ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
  2. [root@sql-master ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
  3. [root@master ~]# yum -y install mariadb mariadb-server
  4. [root@master ~]# systemctl start mariadb.service
  5. [root@master ~]# systemctl enable mariadb.service
  6. [root@master ~]# mysql_secure_installation
  7. [root@master ~]# systemctl stop firewalld.service
  8. [root@master ~]# systemctl disable firewalld.service
  9. [root@master ~]# sed -i s/SELINUX=enforing/SELINUX=disabled/g /etc/selinux/config
  10. [root@master ~]# setenforce 0

数据库从服务器

  1. [root@slave ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
  2. [root@slave ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
  3. [root@slave ~]# yum -y install mariadb mariadb-server
  4. [root@slave ~]# systemctl start mariadb.service
  5. [root@slave ~]# systemctl enable mariadb.service
  6. [root@slave ~]# mysql_secure_installation
  7. [root@slave ~]# systemctl stop firewalld.service
  8. [root@slave ~]# systemctl disable firewalld.service
  9. [root@slave ~]# sed -i s/SELINUX=enforing/SELINUX=disabled/g /etc/selinux/config
  10. [root@slave ~]# setenforce 0

修改配置文件

数据库主服务器

  1. [root@master ~]# vim /etc/my.cnf.d/mariadb-server.cnf
  2. // 添加内容
  3. [mysqld]
  4. log_bin=mysql-bin //开启二进制日志
  5. server_id=1 //服务id号,不可从复,值为0时则表示拒绝服务器连接
  6. [root@master ~]# systemctl restart mariadb.service

数据库从服务器:

  1. [root@slave ~]# vim /etc/my.cnf.d/mariadb-server.cnf
  2. // 添加内容
  3. [mysqld]
  4. server_id=2 //服务id号,不可重复,为0则拒绝从服务器连接
  5. relay_log=myrelay //开启中继日志
  6. [root@slave ~]# systemctl restart mariadb.service

创建用户并授权,让从服务可以登陆主服务器
数据库主服务器

  1. [root@master ~]# mysql -uroot -p123456
  2. // 创建用户
  3. MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'yibie'@'%' IDENTIFIED BY 'yibie123!';
  4. Query OK, 0 rows affected (0.000 sec)
  5. // 刷新权限
  6. MariaDB [(none)]> FLUSH PRIVILEGES;
  7. Query OK, 0 rows affected (0.001 sec)
  8. MariaDB [(none)]> show master status;
  9. +------------------+----------+--------------+------------------+
  10. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
  11. +------------------+----------+--------------+------------------+
  12. | mysql-bin.000001 | 639 | | |
  13. +------------------+----------+--------------+------------------+
  14. 1 row in set (0.000 sec)

登陆到从服务器的数据库,在数据库中配置主服务器的信息

  1. [root@slave ~]# mysql -uroot -p123456
  2. MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.23.117',MASTER_PORT=3306,MASTER_USER='yibie',MASTER_PASSWORD='yibie123';
  3. Query OK, 0 rows affected (0.005 sec)
  4. MariaDB [(none)]> START SLAVE;
  5. Query OK, 0 rows affected (0.000 sec)
  6. MariaDB [(none)]> SHOW SLAVE STATUS \G
  7. *************************** 1. row ***************************
  8. Slave_IO_State: Waiting for master to send event
  9. Master_Host: 192.168.23.117
  10. Master_User: yibie
  11. Master_Port: 3306
  12. Connect_Retry: 60
  13. Master_Log_File: mysql-bin.000001
  14. Read_Master_Log_Pos: 759
  15. Relay_Log_File: mariadb-relay-bin.000002
  16. Relay_Log_Pos: 1058
  17. Relay_Master_Log_File: mysql-bin.000001
  18. Slave_IO_Running: Yes
  19. Slave_SQL_Running: Yes

从服务器安装zabbix

下载zabbix软件包、解压、创建zabbix用户、安装依赖包、编译安装

  1. [root@slave ~]# cd /usr/src/
  2. [root@slave src]# wget https://cdn.zabbix.com/zabbix/sources/stable/5.4/zabbix-5.4.4.tar.gz
  3. [root@slave src]# ll
  4. total 23700
  5. drwxr-xr-x. 2 root root 6 Aug 12 2018 debug
  6. drwxr-xr-x. 2 root root 6 Aug 12 2018 kernels
  7. -rw-r--r--. 1 root root 24266079 Aug 30 16:23 zabbix-5.4.4.tar.gz
  8. [root@slave src]# tar xf zabbix-5.4.4.tar.gz
  9. [root@slave src]# useradd -r -M -s /sbin/nologin zabbix
  10. [root@slave src]# yum -y install net-snmp-devel libevent-devel make gcc gcc-c++
  11. // 编译安装
  12. [root@slave src]# cd zabbix-5.4.4/
  13. [root@slave zabbix-5.4.4]# ./configure --enable-agent
  14. [root@slave zabbix-5.4.4]# make install
  15. // 更改zabbix配置文件
  16. [root@slave ~]# vim /etc/zabbix_agentd.conf
  17. Server=192.168.23.116
  18. ServerActive=192.168.23.116
  19. Hostname=192.168.23.118

1.2 编写脚本文件

指定key和脚本位置

  1. //修改配置文件,让key可以执行脚本识别MySQL状态
  2. [root@slave scripts]# vim /etc/zabbix_agentd.conf
  3. ...
  4. UnsafeUserParameters=1
  5. ...
  6. //在文件末尾添加
  7. UserParameter=check_replication,/scripts/check_replication.sh
  8. // 杀死进程重新启动
  9. [root@slave scripts]# pkill zabbix_agentd
  10. [root@slave scripts]# zabbix_agentd
  11. [root@slave ~]# mysql -uroot -p123456
  12. //创建一个zabbix的用户,并设置权限只读和密码
  13. MariaDB [(none)]> grant select on *.* to 'zabbix'@'localhost' identified by 'zabbix';
  14. Query OK, 0 rows affected (0.001 sec)
  15. MariaDB [(none)]> grant SUPER, REPLICATION CLIENT on *.* to 'zabbix'@'localhost' identified by 'zabbix';
  16. Query OK, 0 rows affected (0.001 sec)
  17. //刷新权限
  18. MariaDB [(none)]> flush privileges;
  19. Query OK, 0 rows affected (0.000 sec)
  1. [root@slave ~]# mkdir /scripts
  2. [root@slave ~]# cd /scripts
  3. [root@slave scripts]# vim check_replication.sh
  4. #!/bin/bash
  5. count=$(mysql -uzabbix -pzabbix -e 'show slave status\G' | grep '_Running' | grep -c 'Yes')
  6. if [ $count -ne 2 ];then
  7. echo '1'
  8. else
  9. echo '0'
  10. fi
  11. [root@slave scripts]# chmod +x check_replication.sh
  12. // 测试脚本
  13. [root@slave scripts]# ./check_replication.sh
  14. 0
  15. // 在到zabbix服务端测试
  16. [root@localhost ~]# zabbix_get -s 192.168.23.118 -k check_replication
  17. 0

1.3 web界面配置监控和触发器

添加监控主机

监控主从2

添加监控项

监控主从3

添加触发器

监控主从4

手动触发报警

  1. // 从服务器上关闭主从复制
  2. [root@slave ~]# mysql -uroot -p123456
  3. MariaDB [(none)]> stop slave;
  4. Query OK, 0 rows affected (0.001 sec)
  5. MariaDB [(none)]> SHOW SLAVE STATUS \G
  6. *************************** 1. row ***************************
  7. Slave_IO_State:
  8. Master_Host: 192.168.23.117
  9. Master_User: yibie
  10. Master_Port: 3306
  11. Connect_Retry: 60
  12. Master_Log_File: mysql-bin.000001
  13. Read_Master_Log_Pos: 759
  14. Relay_Log_File: mariadb-relay-bin.000002
  15. Relay_Log_Pos: 1058
  16. Relay_Master_Log_File: mysql-bin.000001
  17. Slave_IO_Running: No
  18. Slave_SQL_Running: No

监控主从5

声音报警
这里不同的报警等级声音也是不一样的 可以根据自己的喜好来更改声音和报警次数

声音报警

2、自定义MySQL主从延迟

编写脚本

  1. [root@slave scripts]# vim check_replication_delay.sh
  2. #!/bin/bash
  3. delay=$( mysql -uzabbix -pzabbix -e 'show slave status\G' | grep 'Behind'| awk '{print $2}' )
  4. if [ $delay != NULL ];then
  5. echo '$delay'
  6. else
  7. echo '0'
  8. fi
  9. [root@slave scripts]# chmod +x check_replication_delay.sh
  10. [root@slave scripts]# ./check_replication_delay.sh
  11. $delay

修改配置文件

  1. [root@slave scripts]# vim /etc/zabbix_agentd.conf
  2. UserParameter=check_replication_delay,/scripts/check_replication_delay.sh
  3. [root@slave scripts]# pkill zabbix_agentd
  4. [root@slave scripts]# zabbix_agentd
  5. [root@localhost ~]# zabbix_get -s 192.168.23.118 -k check_replication_delay
  6. $delay

web界面配置监控项和触发器

监控主从6

监控主从7

3、用户和组权限

这里所显示的用户都是来自zabbix数据库中的表,如果你想添加新的用户有两种方法一种是直接在web界面添加新的用户,或者到数据库中插入数据,然后就可以在web界面中看到所添加的用户

zabbix用户权限1

添加用户到管理员组

zabbix用户权限2

zabbix用户权限3

zabbix用户权限4

 此时可以去数据库中查看是否添加用户成功

  1. [root@localhost ~]# mysql -uroot -pabc123
  2. mysql> show databases;
  3. +--------------------+
  4. | Database |
  5. +--------------------+
  6. | information_schema |
  7. | mysql |
  8. | performance_schema |
  9. | sys |
  10. | zabbix |
  11. +--------------------+
  12. 5 rows in set (0.00 sec)
  13. mysql> use zabbix;
  14. Reading table information for completion of table and column names
  15. You can turn off this feature to get a quicker startup with -A
  16. Database changed
  17. mysql> show tables;
  18. +----------------------------+
  19. | Tables_in_zabbix |
  20. +----------------------------+
  21. | users |
  22. +----------------------------+
  23. 166 rows in set (0.00 sec)
  24. mysql> select * from users\G;
  25. *************************** 3. row ***************************
  26. userid: 3
  27. username: yibie
  28. name:
  29. surname:
  30. passwd: $2y$10$IM1y4KP2RBzvWMekXYq.7.oT7KAQ/D6qWQsJxZwGr3Z0F9CC0Qip2
  31. url:
  32. autologin: 0
  33. autologout: 0
  34. lang: default
  35. refresh: 30s
  36. theme: default
  37. attempt_failed: 0
  38. attempt_ip:
  39. attempt_clock: 0
  40. rows_per_page: 50
  41. timezone: default
  42. roleid: 2
  43. 3 rows in set (0.00 sec)

修改用户密码
如果你已经登陆到了web界面,却忘记了用户密码可以直接点击用户名到里面修改密码。

zabbix用户权限5

而如果忘记密码而登陆不上zabbix web页面时,可以到zabbix服务器系统上进行更改密码
zabbix用户的密码是使用MD5加密方式,所有只需要生成一个MD5加密的密码 替换即可

  1. //生成一个新的MD5密码,密码是yibie 加密后是下面的字符串
  2. //进入到数据库中更新密码
  3. [root@localhost ~]# echo -n yibie |openssl md5
  4. (stdin)= 4a9c940e9f4128afeafefc6b75ab68a6
  5. [root@localhost ~]# mysql -uroot -pabc123
  6. mysql> use zabbix;
  7. Reading table information for completion of table and column names
  8. You can turn off this feature to get a quicker startup with -A
  9. Database changed
  10. mysql> update users set passwd='4a9c940e9f4128afeafefc6b75ab68a6' where userid = '3';
  11. Query OK, 1 row affected (0.00 sec)
  12. Rows matched: 1 Changed: 1 Warnings: 0
  13. mysql> FLUSH PRIVILEGES;
  14. Query OK, 0 rows affected (0.01 sec)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/960745
推荐阅读
相关标签
  

闽ICP备14008679号