文章目录
- 一、部署Zabbix 5.0环境
- 1.1、下载Zabbix 5.0镜像
- 1.2、创建镜像
- 1.3、浏览器访问
- 二、监控Linux主机
- 三、监控MySQL实例
- 四、总结
一、部署Zabbix 5.0环境
1.1、下载Zabbix 5.0镜像
[root@docker36 ~]# docker pull registry.cn-hangzhou.aliyuncs.com/lhrbest/lhrzabbix5:1.01.0: Pulling from lhrbest/lhrzabbix5 c1e1cb5fc6d2: Already exists f36cbc8bf71f: Pull complete Digest: sha256:5e22edb3347a27a731bd48742c982e34dcbcd47b63db2e9b64549ad69488475eStatus: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/lhrbest/lhrzabbix5:1.0registry.cn-hangzhou.aliyuncs.com/lhrbest/lhrzabbix5:1.0
1.2、创建镜像
[root@docker36 ~]# docker run -d --name lhrzabbix5 -h lhrzabbix5 \> -p 225:22 -p 23306:3306 -p 280:80 -p 20051:10051 \> -v /sys/fs/cgroup:/sys/fs/cgroup \> --privileged=true \> lhrbest/lhrzabbix5:1.0 /usr/sbin/init Unable to find image 'lhrbest/lhrzabbix5:1.0' locally1.0: Pulling from lhrbest/lhrzabbix5 Digest: sha256:5e22edb3347a27a731bd48742c982e34dcbcd47b63db2e9b64549ad69488475eStatus: Downloaded newer image for lhrbest/lhrzabbix5:1.07fa309da58043680aa0fa14b0692b35249ad5557d5d4cf0d0897a29afad13256[root@docker36 ~]# docker exec -it lhrzabbix5 bash[root@lhrzabbix5 /]#
1.3、浏览器访问
访问Zabbix:http://192.168.66.36:280/zabbix
默认用户名密码:Admin/zabbix
二、监控Linux主机
监控Linux主机可以通过snmp(Simple Network Management Protocol)简单网络管理协议,也可以通过zabbix-agent监控。本文使用snmp来配置。
yum install -y net-snmp net-snmp-utils systemctl start snmpd systemctl status snmpd systemctl enable snmpd
配置文件:vi /etc/snmp/snmpd.conf,添加如下内容:
view systemview included .1
启动snmpd:
[root@lhrzabbix5 ~]# systemctl status snmpd● snmpd.service - Simple Network Management Protocol (SNMP) Daemon. Loaded: loaded (/usr/lib/systemd/system/snmpd.service; enabled; vendor preset: disabled) Active: active (running) since Wed 2021-01-13 16:37:24 CST; 39s ago Main PID: 81937 (snmpd) CGroup: /system.slice/snmpd.service └─81937 /usr/sbin/snmpd -LS0-6d -f Jan 13 16:37:24 docker36 systemd[1]: Starting Simple Network Management Protocol (SNMP) Daemon....Jan 13 16:37:24 docker36 snmpd[81937]: NET-SNMP version 5.7.2Jan 13 16:37:24 docker36 systemd[1]: Started Simple Network Management Protocol (SNMP) Daemon..[root@docker36 ~]# netstat -anp | grep 161udp 0 0 0.0.0.0:161 0.0.0.0:* 81937/snmpd
在zabbix server上测试:
nc -zvu localhost 161 snmpwalk -v 2c -c public localhost | wc -l
结果:
[root@lhrzabbix5 /]# nc -zvu localhost 161Ncat: Version 7.50 ( https://nmap.org/ncat )Ncat: Connected to 127.0.0.1:161.Ncat: UDP packet sent successfully Ncat: 1 bytes sent, 0 bytes received in 2.02 seconds.[root@lhrzabbix5 /]# snmpwalk -v 2c -c public localhost | wc -l7086
此时需要在server端配置:
配置——主机——创建主机——移除interface的客户端——添加SNMP客户端
——填写主机名称、可见名称、群组、IP地址,如图所示:
点击添加,然后等待1分钟,等待图标变成绿色即正常,如图所示:
监控结果:
三、监控MySQL实例
被监控端Mysql主机安装Zabbix-agent
rpm -ivh http://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpmsed -i "s/gpgcheck=1/gpgcheck=0/g" /etc/yum.repos.d/zabbix.repo yum -y install zabbix-agent
被监控端修改zabbix-agent配置文件
vim /etc/zabbix/zabbix_agentd.conf Server=localhost Hostname=mysql-localsystemctl restart zabbix-agent systemctl enable zabbix-agent ss -antulp | grep :10050日志: /var/log/zabbix/zabbix_agentd.log
被监控端修改MySQL配置文件:
mkdir -p /var/lib/zabbix cat > /var/lib/zabbix/.my.cnf <<"EOF"[mysql]host=localhostuser=root password=lhr port=3306socket=/var/lib/mysql/mysql.sock[mysqladmin]host=localhostuser=root password=lhr port=3306socket=/var/lib/mysql/mysql.sock EOF
编辑监控模板,若没有该文件,可以手动新增:
[root@docker35 yum.repos.d]# more /etc/zabbix/zabbix_agentd.d/userparameter_mysql.confUserParameter=mysql.ping[*], mysqladmin -h"$1" -P"$2" ping UserParameter=mysql.get_status_variables[*], mysql -h"$1" -P"$2" -sNX -e "show global status"UserParameter=mysql.version[*], mysqladmin -s -h"$1" -P"$2" version UserParameter=mysql.db.discovery[*], mysql -h"$1" -P"$2" -sN -e "show databases"UserParameter=mysql.dbsize[*], mysql -h"$1" -P"$2" -sN -e "SELECT SUM(DATA_LENGTH + INDEX_LENGTH) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='$3'"UserParameter=mysql.replication.discovery[*], mysql -h"$1" -P"$2" -sNX -e "show slave status"UserParameter=mysql.slave_status[*], mysql -h"$1" -P"$2" -sNX -e "show slave status"
重启zabbix-agent:
systemctl restart zabbix-agent
注意:这里的主机名称需要和配置文件中的Hostname=mysql-local保持一致,否则会报错。
等待一会即可看到数据:
四、总结
相比Prometheus来说,感觉zabbix监控MySQL或其它数据库不如Prometheus方便。但是,zabbix和Prometheus都是目前使用率最广泛的2款监控工具。