zabbix4.4 监控mysql

时间:2021-07-03 00:27:31

#template_db_mysql.conf created by Zabbix for "Template DB MySQL" and Zabbix 4.2
#For OS Linux: You need create .my.cnf in zabbix-agent home directory (/var/lib/zabbix by default
#For OS Windows: You need add PATH to mysql and mysqladmin and create my.cnf in %WINDIR%my.cnf,C:my.cnf,BASEDIRmy.cnf https://dev.mysql.com/doc/refman/5.7/en/option-files.html
#The file must have three strings:
#[client]
#user=zbx_monitor
#password=<password>
#
UserParameter=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"
UserParameter=mysql.status[*],/server/scripts/mysql.sh $1

这是官方给的文件  放上去感觉没太大用处  除了让默认mysql.status启用 也没有数据流量。

1、在/etc/zabbix/下创建.my.cnf文件:

[mysql]
host=localhost     
user=zabbix
password=zabbix
socket=/var/lib/mysql/mysql.sock
[mysqladmin]
host=localhost
user=zabbix
password=zabbix
socket=/var/lib/mysql/mysql.sock
mysql.sock位置查找方法:
[[email protected] ~]# find / -name mysql.sock
/var/lib/mysql/mysql.sock

2、查看userparameter_mysql.conf文件位置,将其复制到/etc/zabbix/zabbix_agentd.d目录

[[email protected] zabbix_agentd.d]# find / -name userparameter_mysql.conf
/usr/share/doc/zabbix-agent-4.4.5/userparameter_mysql.conf

[[email protected] zabbix_agentd.d]# cp /usr/share/doc/zabbix-agent-4.4.5/userparameter_mysql.conf /etc/zabbix/zabbix_agentd.d/

 修改userparameter_mysql.conf文件内容为:

[[email protected] zabbix_agentd.d]# cat userparameter_mysql.conf 
#UserParameter=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"


UserParameter=mysql.status[*],echo "show global status where Variable_name=‘$1‘;" | HOME=/etc/zabbix /usr/bin/mysql -N | awk ‘{print $$2}‘
UserParameter=mysql.size[*],bash -c ‘echo "select sum($(case "$3" in both|"") echo "data_length index_length";; data|index) echo "$3_length";; free) echo "data_free";; esac)) from information_schema.tables$([[ "$1" = "all" || ! "$1" ]] || echo " where table_schema="$1"")$([[ "$2" = "all" || ! "$2" ]] || echo "and table_name="$2"");" | HOME=/usr/local/zabbix/etc mysql -N‘
UserParameter=mysql.ping,HOME=/etc/zabbix /usr/bin/mysqladmin ping
# | grep -c alive
UserParameter=mysql.version,/usr/bin/mysql -V
UserParameter=mysql.vars[*],echo "show variables where Variable_name=‘$1‘;" | HOME=/etc/zabbix /usr/bin/mysql -N | awk ‘{print $$2}‘

3、重新启动相关服务:

systemctl restart mariadb

systemctl restart zabbix-agemt

4、在web端添加监控项

 

键值为:UserParameter=mysql.ping

zabbix4.4 监控mysql

 

 

zabbix4.4 监控mysql

 

 5、模板创建完成以后,应用于主机,并查看该主机最新数据对应模板 数据信息是否正确。

zabbix4.4 监控mysql