Docker-compose创建Zabbix监控服务器并监控Mysql参数(Zabbix5.0)
目录环境安装Docker、Docker-compose及配置加速编写.yaml文件启动zabbix-server端浏览器访问Zabbix-web页面配置Zabbix-agent端监控Mysql的参数在server端添加监控项环境centos 7.5zabbix-server:192.168.253.110zab...
·
目录
环境
centos 7.5 zabbix-server:192.168.253.110 zabbix-agent:192.168.253.120
安装Docker、Docker-compose及配置加速
1)配置阿里的docker源
[root@zabbix-server ~]# wget -O /etc/yum.repos.d/epel.repo #自己玩,我就没在意docker是ce还是ee
http://mirrors.aliyun.com/repo/epel-7.repo
2)安装docker、docker-compose
[root@zabbix-server ~]# yum -y install docker docker-compose
[root@zabbix-server ~]# vim /etc/docker/daemon.json #配置加速
{"registry-mirrors": ["https://26ahzfln.mirror.aliyuncs.com"]}
3)启动docker
[root@zabbix-server ~]# systemctl restart docker
[root@zabbix-server ~]# setenforce 0 ###使用docker不用关闭防火墙
编写.yaml文件启动zabbix-server端
1).yaml源码
[root@zabbix-server src]# vim docker-compose.yml
version: "3"
services:
mysql-server:
image: bingozhou/mysql5.7
environment:
MYSQL_ROOT_PASSWORD: 123
MYSQL_DATABASE: zabbix
MYSQL_USER: zabbix
MYSQL_PASSWORD: 123
ports:
- "3306:3306"
zabbix-server:
image: zabbix/zabbix-server-mysql
environment:
DB_SERVER_HOST: mysql-server
MYSQL_DATABASE: zabbix
MYSQL_USER: zabbix
MYSQL_PASSWORD: 123
ports:
- "10051:10051"
depends_on:
- "mysql-server"
zabbix-web:
image: zabbix/zabbix-web-nginx-mysql
environment:
DB_SERVER_HOST: mysql-server
MYSQL_DATABASE: zabbix
MYSQL_USER: zabbix
MYSQL_PASSWORD: 123
PHP_TZ: Asia/Shanghai
ZBX_SERVER_HOST: zabbix-server
ports:
- "80:8080"
depends_on:
- "mysql-server"
- "zabbix-server"
zabbix-agent:
image: zabbix/zabbix-agent
environment:
ZBX_SERVER_HOST: zabbix-server
ports:
- "10050:10050"
depends_on:
- "zabbix-server"
2)启动服务
[root@zabbix-server src]# docker-compose up -d
Creating src_mysql-server_1 ... done
Digest: sha256:292b05bdb41c4b16dc12f882bb3be18547e6680372549dcec61ad711f80bd976
Creating src_zabbix-server_1 ... done
Creating src_mysql-server_1 ...
Creating src_zabbix-server_1 ...
Creating src_zabbix-web_1 ... done
Creating src_zabbix-agent_1 ... done
[root@zabbix-server src]# docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------------------
src_mysql-server_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp
src_zabbix-agent_1 /sbin/tini -- /usr/bin/doc ... Up 0.0.0.0:10050->10050/tcp
src_zabbix-server_1 /sbin/tini -- /usr/bin/doc ... Up 0.0.0.0:10051->10051/tcp
src_zabbix-web_1 docker-entrypoint.sh Up 0.0.0.0:80->8080/tcp, 8443/tcp
浏览器访问Zabbix-web页面
1)浏览器访问
2)zabbix界面汉化
配置Zabbix-agent端
1)配置zabbix.yum源
[root@zabbix-agent ~]# rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
[root@zabbix-agent ~]# yum -y install zabbix-agent
[root@zabbix-agent ~]# systemctl start zabbix-agent
2)修改配置文件
[root@zabbix-agent ~]# vim /etc/zabbix/zabbix_agentd.conf
Server=192.168.253.110
ServerActive=192.168.253.110
3)重启zabbix-agent
[root@zabbix-agent ~]# systemctl restart zabbix-agent
监控Mysql的参数
1)在zabbix-agent端安装mysql
[root@zabbix-agent ~]# yum -y install mariadb mariadb-server
[root@zabbix-agent ~]# systemctl start mariadb
2)写mysql.sh文件在/etc/zabbix(在agent端)
[root@zabbix-agent ~]# cd /etc/zabbix/
[root@zabbix-agent zabbix]# vim mysql.sh #脚本中是mysql的一些参数
#!/bin/bash
# 用户名
MYSQL_USER='root'
# 密码
MYSQL_PWD='123456'
# 主机地址/IP
MYSQL_HOST='localhost'
# 端口
MYSQL_PORT='3306'
# 数据连接
MYSQL_CONN="/usr/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT}"
# 参数是否正确
if [ $# -ne "1" ];then
echo "arg error!"
fi
# 获取数据
case $1 in
Uptime)
result=`${MYSQL_CONN} status|cut -f2 -d":"|cut -f1 -d"T"`
echo $result
;;
Com_update)
result=`${MYSQL_CONN} extended-status |grep -w "Com_update"|cut -d"|" -f3`
echo $result
;;
Slow_queries)
result=`${MYSQL_CONN} status |cut -f5 -d":"|cut -f1 -d"O"`
echo $result
;;
Com_select)
result=`${MYSQL_CONN} extended-status |grep -w "Com_select"|cut -d"|" -f3`
echo $result
;;
Com_rollback)
result=`${MYSQL_CONN} extended-status |grep -w "Com_rollback"|cut -d"|" -f3`
echo $result
;;
Questions)
result=`${MYSQL_CONN} status|cut -f4 -d":"|cut -f1 -d"S"`
echo $result
;;
Com_insert)
result=`${MYSQL_CONN} extended-status |grep -w "Com_insert"|cut -d"|" -f3`
echo $result
;;
Com_delete)
result=`${MYSQL_CONN} extended-status |grep -w "Com_delete"|cut -d"|" -f3`
echo $result
;;
Com_commit)
result=`${MYSQL_CONN} extended-status |grep -w "Com_commit"|cut -d"|" -f3`
echo $result
;;
Bytes_sent)
result=`${MYSQL_CONN} extended-status |grep -w "Bytes_sent" |cut -d"|" -f3`
echo $result
;;
Bytes_sent)
result=`${MYSQL_CONN} extended-status |grep -w "Bytes_sent" |cut -d"|" -f3`
echo $result
;;
Bytes_received)
result=`${MYSQL_CONN} extended-status |grep -w "Bytes_received" |cut -d"|" -f3`
echo $result
;;
Com_begin)
result=`${MYSQL_CONN} extended-status |grep -w "Com_begin"|cut -d"|" -f3`
echo $result
;;
*)
echo "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin)"
;;
esac
3)修改mysql密码并测试
[root@zabbix-agent zabbix]# mysqladmin -uroot password 123456
[root@zabbix-agent zabbix]# chmod 777 mysql.sh
[root@zabbix-agent zabbix]# ./mysql.sh Uptime 启动了多久
1094
4)添加zabbix-agent键值
[root@zabbix-agent zabbix]# vim /etc/zabbix/zabbix_agentd.conf
Hostname=192.168.253.120 本机ip地址
UserParameter=mysql.status[*],/etc/zabbix/mysql.sh $1
在server端添加监控项
1)创建主机并添加
2)创建监控项
3)创建图形
4)查看监控图表
更多推荐
已为社区贡献4条内容
所有评论(0)