zabbix监控mysql
2017-07-12 本文已影响74人
WFF_FFW
环境介绍
操作系统:Centos 6.* ( 双核4G、硬盘至少20G)
zabbix_agentd版本:3.0.3
zabbix_server版本:3.2.6
mysql版本:5.6.29
zabbix server : 192.168.1.127
zabbix agentd :192.168.1.101
Agentd端配置zabbix监控mysql
1):创建监控mysql的zabbix用户
grant select on *.* to zabbix@"localhost" identified by "123456";
flush privileges;
2):添加zabbix用户于mysql配置文件中
[client]
user = zabbix
password = 123456
注:如果配置文件中加入了密码和用户,则当你输入mysql是它会以zabbix用户得方式进到数据库里,所以,最好不要给zabbix得权限很大,一般查询就可以。
3):添加zabbix监控mysql键值
UserParameter=mysql.version,/usr/local/mysql/bin/mysql -V
UserParameter=mysql.status[*],/etc/zabbix/chk_mysql.sh $1
UserParameter=mysql.ping,/usr/local/mysql/bin/mysqladmin -S /usr/local/mysql/mysqld.sock ping | grep -c alive
注:mysql数据库的相关路径的修改
4):添加mysql监控脚本
#!/bin/bash
# -------------------------------------------------------------------------------
# FileName: check_mysql.sh
# Revision: 1.0
# Date: 2015/06/09
# Author: DengYun
# Email: dengyun@ttlsa.com
# Website: www.ttlsa.com
# Description:
# Notes: ~
# -------------------------------------------------------------------------------
# Copyright: 2015 (c) DengYun
# License: GPL
# 数据连接
MYSQL_CONN="/usr/local/mysql/bin/mysqladmin"
# 参数是否正确
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
5):重启zabbix_agentd服务
/etc/init.d/zabbix-agent restart
6):zabbix_server 端获取zabbix_agentd端数据
/usr/local/zabbix/bin/zabbix_get -s192.168.1.101 -p10050 -k mysql.status[Uptime]
获取mysql数据
1:没有给脚本的执行权限
chmod +x chk_mysql.sh
2:修改mysql数据库配置文件并重启
[mysqladmin]
socket=/usr/local/mysql/mysqld.sock
character_set_server = utf8
/etc/init.d/mysqld restart
3:正常获取到数据,最低获取到的数据是0.如果数据是空,表示某些配置有问题.
Web端配置
mysql的模板直接用zabbix自带的Template App MySQL模板
注意:必须操作7和8否则不生效
agentd添加mysql模板
zabbix获取正常mysql.png
注:
1:上述5)操作失败,报如下错:
mysql.png
解决方法:
rm -rf /etc/zabbix/zabbix_agentd.d/*
/etc/init.d/zabbix_agentd restart
即可