Zabbix 3.2自帶監控mysql數據庫的模板,只是沒有監控主從狀態的監控項,咱們能夠將自帶的模板克隆一份存爲Template App MySQL_slave,而後配置agent客戶端,隨後在web端給主機增長監控項和觸發器。
1)首先在zabbix客戶端的mysql裏添加帳號,即只是本機使用zabbix帳號鏈接本地的mysql
mysql> grant all privileges on *.* to zabbix@'localhost' identified by "Passw0rd」;
mysql> flush privileges;
2)在zabbix_agent目錄/etc/zabbix/zabbix_agentd.d下建立.my.cnf 鏈接文件
vi .my.cnf
[mysql]
host=localhost
user=zabbix
password=Passw0rd
socket=/var/lib/mysql/mysql.sock
[mysqladmin]
host=localhost
user=zabbix
password=Passw0rd
socket=/var/lib/mysql/mysql.sock
3,修改自帶監控插件的配置文件
cd /etc/zabbix/zabbix_agentd.d/
cat userparameter_mysql.conf
# For all the following commands HOME should be set to the directory that has .my.cnf file with password information.
# Flexible parameter to grab global variables. On the frontend side, use keys like mysql.status[Com_insert].
# Key syntax is mysql.status[variable].
UserParameter=mysql.status[*],echo "show global status where Variable_name='$1';" | HOME=/var/lib/zabbix mysql -N | awk '{print $$2}'
# Flexible parameter to determine database or table size. On the frontend side, use keys like mysql.size[zabbix,history,data].
# Key syntax is mysql.size[<database>,<table>,<type>].
# Database may be a database name or "all". Default is "all".
# Table may be a table name or "all". Default is "all".
# Type may be "data", "index", "free" or "both". Both is a sum of data and index. Default is "both".
# Database is mandatory if a table is specified. Type may be specified always.
# Returns value in bytes.
# 'sum' on data_length or index_length alone needed when we are getting this information for whole database instead of a single table
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=/var/lib/zabbix mysql -N'
UserParameter=mysql.ping,HOME=/var/lib/zabbix mysqladmin ping | grep -c alive
UserParameter=mysql.version,mysql -V
#添加如下監控項:
#slave狀態是否正常
UserParameter=slavestatus,echo "show slave status\G" | HOME=/var/lib/zabbix mysql -N | grep -c "Yes"
#seconds behind master
UserParameter=sbehindmaster,echo "show slave status" | HOME=/var/lib/zabbix mysql -N | awk '{print $25}'
#當前線程數
UserParameter=threads,HOME=/var/lib/zabbix mysqladmin status | cut -f3 -d":" | cut -f1 -d"Q" | tr -d ' '
就像第一句英文說的將全部HOME=/var/lib/zabbix改成/etc/zabbix/zabbix_agentd.d
4,重啓zabbix-agent,在zabbix web端 檢測中--最新數據--選擇對應主機,若看到最新數據即成功了。
這樣就避免了
UserParameter=mysql.threads,mysqladmin -uroot -ppassword status | cut -f3 -d":" | cut -f1 -d"Q" | tr -d ' '
這種監控項直接讀取數據時提示:
Warning: Using a password on the command line interface can be insecure
而取不到值。mysql