使用zabbix监控mariadb性能状态

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
相关文章
相关标签/搜索