ZABBIX4.x监控容器部署的MySQL
根据zabbix4.x自带的mysql模版配置进行配置的
创建帐号
创建数据库帐号
CREATE USER 'zbx_monitor'@'%' IDENTIFIED BY 'passwd';
GRANT USAGE,REPLICATION CLIENT,PROCESS,SHOW DATABASES,SHOW VIEW ON *.* TO 'zbx_monitor'@'%';
FLUSH PRIVILEGES;
配置mysql服务免密
因为mysql是使用了pxc,映射了一个配置文件,其他mysql 可以修改最近到此文件my.cnf
[mysql]
host=127.0.0.1
user=zbx_monitor
password=passwd
[mysqladmin]
host=127.0.0.1
user=zbx_monitor
password=passwd
重启mysql容器服务。
编写zabbix-agentd.conf配置文件(已存在可不操作)
vim /etc/zabbix/zabbix_agentd.conf
# 查看是否引入
Include=/etc/zabbix/zabbix_agentd.d/*.conf
编写配置文件:(可把默认的MySQL配置文件删除)
vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
# 连接数
UserParameter=mysql.max_connections,echo "show variables where Variable_name='max_connections';" | docker exec -i mysql-node88 bash -c "mysql -N" | awk '{print $2}'
UserParameter=mysql.current_connections,echo "show global status where Variable_name='Threads_connected';" | docker exec -i mysql-node88 bash -c "mysql -N" | awk '{print $2}'
# 缓冲池
UserParameter=mysql.buffer_pool_size,echo "show variables where Variable_name='innodb_buffer_pool_size';" | docker exec -i mysql-node88 bash -c "mysql -N" |awk '{printf "%.2f",$2/1024/1024/1024}'
UserParameter=mysql.buffer_pool_usage_percent,echo "show global status where Variable_name='Innodb_buffer_pool_pages_free' or Variable_name='Innodb_buffer_pool_pages_total';" | docker exec -i mysql-node88 bash -c "mysql -N" | awk '{a[NR]=$2}END{printf "%.1f",100-((a[1]/a[2])*100)}'
# 增删改查
UserParameter=mysql.status[*],echo "show global status where Variable_name='$1';" |docker exec -i mysql-node88 bash -c "mysql -N" | awk '{print $$2}'
# 实例状态
#UserParameter=mysql.ping,docker exec -i mysql-node88 bash -c "mysqladmin ping" | grep -c alive
UserParameter=mysql.ping,docker ps | grep mysql |wc -l
UserParameter=mysql.version,docker exec -i mysql-node88 bash -c "mysql -V"
注:需要改动的位置
mysql-node88 是容器名称,根据自己实际情况修改,非容器安装mysql,把
docker exec -i mysql-node88 bash -c
去掉即可,重新启动zabbix-agent服务systemctl restart zabbix-agent
zabbix-server端验证,可不可以获取到数据
[chenyh@zabbix_proxy_01 ~]$ zabbix_get -s 10.196.69.88 -p 10050 -k 'mysql.ping'
1
[chenyh@zabbix_proxy_01 ~]$ zabbix_get -s 10.196.69.90 -p 10050 -k 'mysql.ping'
1
报错
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.39/containers/mysql20210111/json: dial unix /var/run/docker.sock: connect: permission denied
报错大致意思就是,没有权限
解决方法:
1.给/var/run/docker.sock
添加权限,不太建议。
chmod 663 /var/run/docker.sock
2.把zabbix用户加入docker组,推荐
usermod -aG docker zabbix
参考
1.https://developer.aliyun.com/article/1148932
2.https://blog.csdn.net/Hu_wen/article/details/112009193