1、在监控端 zabbix-agent 创建 mysql 配置文件
cd /etc/zabbix/
vi .my.cnf
# 添加如下内容
[client]
user=root
host=localhost
password=Abc@123456
2、测试
# mysql能否连上,返回1,正确
HOME=/etc/zabbix/ mysqladmin ping | grep -c alive
3、修改 MySQL 模版配置文件
vi /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
红色的地方是要修改的地方 (本例是YUM安装的Zabbix,源码安装的自行调整)
# 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=/etc/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=/etc/zabbix mysql -N'UserParameter=mysql.ping,HOME=/etc/zabbix mysqladmin ping | grep -c alive
UserParameter=mysql.version,mysql -V
Zabbix Server与Agent之间监控数据的采集主要是通过Zabbix Server主动向Agent询问某个Key的值,Agent会根据Key去调用相应的函数去获取这个值并返回给Server端。Agent端本并没有内置MySQL的监控功能(但是Server端提供了相应的Template配置),所以我们需要使用Zabbix的User Parameters功能,为MySQL添加监控脚本。
Agent工作原理
Agent 安装在被监控主机上,定期主动的监控本机的资源和应用,然后将数据进行处理发送给ZabbixServer. Agent工作方式又分为Passive Check 和 Active Check。
Passive Check:Zabbix Server 发起数据索取请求,Agent响应对应的数据.
Active Check:Agent首先从Zabbix Server 检索监控项列表,然后定期将对应的数据主动的发送到.Zabbix ServerZabbix Agent 本身预定义了一些监控类型,而对于没有预定义的需要管理员自行定义.因此,Zabbix提供了”UserParameter”参数,以方便用户根据自己的需求自定义想要获取的数据.
UserParameter 语法
UserParameter=<key>,<command>
用户自定义一个key,用命令来获取用户想要监控的数据,也就是key的值;定好UserParameter参数后,在为主机或者模板配置监控项的时候,在”key”中输入上面自定义的key的名字就可以了。假如我要获取Mysql Server的版本,我可以这样定义UserParameter,打开 Zabbix Agent安装路径下的 /etc/zabbix_agentd.conf 配置文件,翻页到最后页面,键入如下行
# mysql.version是key
# mysql -V是命令
UserParameter=mysql.version,mysql -V
这里我们自定义的key名就是 mysql.version
,命令 mysql -V
用来获取Mysql 版本号,其实就是key对应的值.
UserParameter参数实现的原理通俗来讲,就是我们先要熟悉Mysql命令,通过Mysql的命令获取想要的数据,然后赋值给自定义的key,最后通过Zabbix Server获取这个值通过图像等方式展示出来.
Zabbix Sever 端接获取数据
zabbix_get -s 192.168.0.104 -k mysql.version