Zabbix监控MySQL配置

#zabbix #监控 #mysql

一、监控MySQL

1、检测MySQL信息脚本,我的放在/etc/zabbix/zabbix_agentd.d/目录下,并命令为userparameter_mysql.sh

vi userparameter_mysql.sh

#!/bin/bash
# License:     GPL

# 用户名
MYSQL_USER='zabbix'

# 密码
MYSQL_PWD='Zabbix'

# 主机地址/IP
MYSQL_HOST='1.1.1.1'

# 端口
MYSQL_PORT='3306'

# 数据连接
MYSQL_CONN="/usr/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT}"
MYSQL_CMD="/usr/bin/mysql -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT}"	#监控MYSQL主从同步状态


# 参数是否正确
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 
				;;
	Slave_status)
		result=`${MYSQL_CMD} -e "show slave status\G" | grep -e Slave_SQL_Running -e Slave_IO_Running | grep Yes | wc -l`	#如果从库show slvae status结果中Yes状态少于2,表示同步有问题
				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|Slave_status)" 
		;; 
	esac

userparameter_mysql.conf配置文件 vi userparameter_mysql.conf

UserParameter=mysql.version,mysql -V
# 获取mysql性能指标,这个是上面定义好的脚本
UserParameter=mysql.status[*],/etc/zabbix/zabbix_agentd.d/userparameter_mysql.sh $1 2>/dev/null
# 获取mysql运行状态
UserParameter=mysql.ping,mysqladmin -uzabbix -pZabbix@123 -P3306 -h  172.16.73.247  ping 2>/dev/null | grep -c alive

模版文件:将以下代码保存为 .xml文件,导入zabbix模版即可

最后更新于