一、Zabbix web页面操作
1.1 为主机增加mysql template
1.2 查看主机监控项状态
点击主机 - 应用集 - 监控项
查看信息,如果有报错需要解决
如:
ERROR 1045 (28000): Access denied for user 'zabbix'@'localhost' (using password: NO)
1.3 查看是否获取到数据
检测中 - 最新数据
二、Zabbix agent配置
2.1 创建检测脚本
打开zabbix agent配置目录 /etc/zabbix
cd /etc/zabbix
chk_mysql.sh
vim chk_mysql.sh
#!/bin/bash
# -------------------------------------------------------------------------------
# FileName: check_mysql.sh
# Revision: 1.0
# Date: 2016/09/29
# Author: lisiyu
# Email:
# Website:
# Description:
# Notes: ~
# -------------------------------------------------------------------------------
# Copyright: 2015 (c) DengYun
# License: GPL
# 用户名
MYSQL_USER='zabbix'
# 密码
MYSQL_PWD='zabbix'
# 主机地址/IP
MYSQL_HOST='127.0.0.1'
# 端口
MYSQL_PORT='3306'
# 数据连接
MYSQL_CONN="/usr/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT}"
# 参数是否正确
if [ $# -ne "1" ];then
echo "arg error!"
fi
# 获取数据
case $1 in
Uptime)
result=`${MYSQL_CONN} status|cut -f2 -d":"|cut -f1 -d"T"`
echo $result
;;
Com_update)
result=`${MYSQL_CONN} extended-status |grep -w "Com_update"|cut -d"|" -f3`
echo $result
;;
Slow_queries)
result=`${MYSQL_CONN} status |cut -f5 -d":"|cut -f1 -d"O"`
echo $result
;;
Com_select)
result=`${MYSQL_CONN} extended-status |grep -w "Com_select"|cut -d"|" -f3`
echo $result
;;
Com_rollback)
result=`${MYSQL_CONN} extended-status |grep -w "Com_rollback"|cut -d"|" -f3`
echo $result
;;
Questions)
result=`${MYSQL_CONN} status|cut -f4 -d":"|cut -f1 -d"S"`
echo $result
;;
Com_insert)
result=`${MYSQL_CONN} extended-status |grep -w "Com_insert"|cut -d"|" -f3`
echo $result
;;
Com_delete)
result=`${MYSQL_CONN} extended-status |grep -w "Com_delete"|cut -d"|" -f3`
echo $result
;;
Com_commit)
result=`${MYSQL_CONN} extended-status |grep -w "Com_commit"|cut -d"|" -f3`
echo $result
;;
Bytes_sent)
result=`${MYSQL_CONN} extended-status |grep -w "Bytes_sent" |cut -d"|" -f3`
echo $result
;;
Bytes_received)
result=`${MYSQL_CONN} extended-status |grep -w "Bytes_received" |cut -d"|" -f3`
echo $result
;;
Com_begin)
result=`${MYSQL_CONN} extended-status |grep -w "Com_begin"|cut -d"|" -f3`
echo $result
;;
*)
echo "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin)"
;;
esac
注意:脚本的zabbix用户运行权限
2.2 修改mysql检测配置
将原先的注释了,添加新的配置
vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
#UserParameter=mysql.status[*],echo "show global status where Variable_name='$1';" | HOME=/var/lib/zabbix mysql -N | awk '{print $$2}'
UserParameter=mysql.status[*],/etc/zabbix/chk_mysql.sh $1
#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.ping,mysqladmin -uzabbix -pzabbix -P3306 -h127.0.0.1 ping | grep -c alive
UserParameter=mysql.version,mysql -V
2.3 关闭SELinux和firewall
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
systemctl stop firewalld
systemctl disable firewalld
2.4 重启agent
systemctl stop zabbix-agent
systemctl start zabbix-agent
2.5 如果还有问题,创建mysql连接文件
vim /etc/zabbix/zabbix_agentd.d/.my.cnf
[client]
user = zabbix
password = zabbix
三、zabbix测试mysql
注意,要在zabbix server上运行zabbix_get,在agent上会报错:
zabbix_get [24860]: Check access restrictions in Zabbix agent configuration
因为zabbix server是编译安装,所以我这里目录在/usr/local/zabbix
cd /usr/local/zabbix/bin
./zabbix_get -s 192.168.16.214 -p10050 -k "system.cpu.load[all,avg15]";
./zabbix_get -s 192.168.16.214 -p10050 -k mysql.status[Uptime];
参考
zabbix监控mysql性能(106)
Zabbix_get小工具的使用
Zabbix官网 Recipes for monitoring
zabbix3.0 监控mysql服务免用户名密码登录的问题故障处理详细过程