zabbix 监控docker中的mysql
1、修改my.cnf文件
添加以下内容
[mysql]
host=127.0.0.1
user=root
password=root
[mysqladmin]
host=127.0.0.1
user=root
password=root
2、修改后重启mysql
#docker restart mysql
3、测试
[root@test3 conf]# echo "show variables where Variable_name='max_connections';" | docker exec -i mysql bash -c "mysql -N" | awk '{print $2}'
151
[root@test3 conf]# echo "show global status where Variable_name='Threads_connected';" | docker exec -i mysql bash -c "mysql -N" | awk '{print $2}'
2
3、添加mysql.conf文件(名字无所谓主要是.conf)
进入zabbix-agent的conf目录,自己根据实际的配置路径找
#cd /usr/local/zabbix_agent/conf/zabbix_agentd/
#vi mysql.conf
#mysql是容器名
#最大连接数
UserParameter=mysql_max_connections,echo "show variables where Variable_name='max_connections';" | docker exec -i mysql bash -c "mysql -N" | awk '{print $2}'
UserParameter=mysql_current_connections,echo "show global status where Variable_name='Threads_connected';" | docker exec -i mysql bash -c "mysql -N" | awk '{print $2}'
# 缓冲池 单位G
UserParameter=mysql_buffer_pool_size,echo "show variables where Variable_name='innodb_buffer_pool_size';" | docker exec -i mysql 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 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 bash -c "mysql -N" | awk '{print $$2}'
# 实例状态 1 为连接中,0为未连接
UserParameter=mysql_ping,HOME=/usr/local/zabbix_agent docker exec -i mysql bash -c "mysqladmin ping" | grep -c alive
#mysql版本号
UserParameter=mysql_version,docker exec -i mysql bash -c "mysql -V" | awk '{print $3}'
#当前数据库数量
UserParameter=mysql_databases_number,echo "show databases" | docker exec -i mysql bash -c "mysql -N" | wc -l
#当前用户数(除了mysql系统用户)
UserParameter=mysql_user_number,echo "select host,user from mysql.user where user not like 'mysql.%';" | docker exec -i mysql bash -c "mysql -N" | wc -l
#查看整个库的空间大小 单位G
UserParameter=mysql_databases_space,echo "select concat(round(sum(DATA_LENGTH/1024/1024/1024),5)) as data from information_schema.TABLES" | docker exec -i mysql bash -c "mysql -N"
#当前连接实例数,一共有多少个客户端连接mysql实例
UserParameter=mysql_Threads_connected,echo " show status like 'Threads%'; " | docker exec -i mysql bash -c "mysql -N" | grep Threads_connected | awk '{print $2}'
4、重启zabbix-agent服务
5、web管理添加监控
6、验证
把模板添加都一个主机上,在最新数据查看