0x01 前言
zabbix內置Mysql的監控模版,由於mariadb和Mysql二者的相關性,因此這個模版也能用在mariadb services上。mysql
0x02 Mysql
首先要在mariadb新建一個帳戶,這個帳戶不須要有任何權限。這個帳戶只是用來登入mariadb獲取服務狀態。sql
GRANT USAGE ON *.* TO 'zabbix'@'localhost' IDENTIFIED BY 'passwd'; FLUSH PRIVILEGES;
請將如下內容根據實際狀況進行替換:vim
- user name >>mariadb帳戶名
- server ip >>mariadb服務器IP
- passwd >>mariadb密碼
0x03 Zabbix Agent
完成mariadb的用戶添加後,還要在mariadb服務器安裝Zabbix Agent。經過如下文件能夠得知還須要新建一個文件,並在這個文件內填入mariadb的信息:bash
/etc/zabbix/zabbix_agentd.d/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
而後新建如下文件並進行修改,要注意的是 /var/lib/zabbix 這個路徑可能並不存在,須要手動新建:服務器
#新建文件夾 mkdir -p /var/lib/zabbix #修改文件 vim /var/lib/zabbix/.my.cnf #填入內容 [mysql] user=zabbix password=zabbix host=127.0.0.1 [mysqladmin] user=zabbix password=zabbix host=127.0.0.1
完成後須要經過如下命令手動重啓mariadb services和zabbix agent services:frontend
#重啓mariadb systemctl restart mariadb.service #重啓zabbix agent systemctl restart zabbix-agent.service