创建监控用户(权限usage):
mysql> grant usage on *.* to zabbix@127.0.0.1 identified by 'zabbix@123'; mysql> flush privileges;
监控脚本:
# cat /etc/zabbix/alertscripts/mysql_status.sh
#!/bin/bash
#Desc:zabbix 监控 MySQL 状态
#Date:2021-3-1
#主机
HOST="127.0.0.1"
#用户
USER="zabbix"
#密码
PASSWORD="zabbix@123"
#端口
PORT="22809"
#MySQL连接
ConNECTION="/usr/local/mysql/bin/mysqladmin -h ${HOST} -u ${USER} -P ${PORT} -p${PASSWORD}"
if [ $# -ne "1" ];then
echo "arg error!"
fi
case $1 in
Uptime)
result=`${CONNECTION} status 2>/dev/null |awk '{print $2}'`
echo $result
;;
Questions)
result=`${CONNECTION} status 2>/dev/null |awk '{print $6}'`
echo $result
;;
Com_update)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Com_update" |awk '{print $4}'`
echo $result
;;
Slow_queries)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Slow_queries" |awk '{print $4}'`
echo $result
;;
Com_select)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Com_select" |awk '{print $4}'`
echo $result
;;
Com_rollback)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Com_rollback" |awk '{print $4}'`
echo $result
;;
Com_insert)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Com_insert" |awk '{print $4}'`
echo $result
;;
Com_delete)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Com_delete" |awk '{print $4}'`
echo $result
;;
Com_commit)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Com_commit" |awk '{print $4}'`
echo $result
;;
Bytes_sent)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Bytes_sent" |awk '{print $4}'`
echo $result
;;
Bytes_received)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Bytes_received" |awk '{print $4}'`
echo $result
;;
Com_begin)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Com_begin" |awk '{print $4}'`
echo $result
;;*)
echo "Usage:$0(Uptime|Questions|Com_update|Slow_queries|Com_select|Com_rollback|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin)"
;;
esac
zabbix配置
#mysql监控 UserParameter=mysql.status[*],/etc/zabbix/alertscripts/mysql_status.sh $1 #获取MySQL运行状态(与脚本无关,可以自信忽略) UserParameter=mysql.ping,/usr/local/mysql/bin/mysqladmin -u zabbix -h"127.0.0.1" -P22809 -p"zabbix@123" ping 2>/dev/null | grep -c alive
zabbix模板:
# zbx_export_templates.xml5.0 2021-11-11T08:35:01Z Templates/Databases Template DB MySQL custom Template DB MySQL custom Templates/Databases MySQL MySQL status mysql.ping 1w It requires user parameter mysql.ping, which is defined in userparameter_mysql.conf. 0 - MySQL server is down 1 - MySQL server is up MySQL Service state {last(0)}=0 MySQL is down WARNING MySQL bytes received per second mysql.status[Bytes_received] 1w FLOAT Bps The number of bytes received from all clients. It requires user parameter mysql.status[*], which is defined in userparameter_mysql.conf. MySQL CHANGE_PER_SECOND MySQL bytes sent per second mysql.status[Bytes_sent] 1w FLOAT Bps The number of bytes sent to all clients. It requires user parameter mysql.status[*], which is defined in userparameter_mysql.conf. MySQL CHANGE_PER_SECOND MySQL begin operations per second mysql.status[Com_begin] 1w FLOAT qps It requires user parameter mysql.status[*], which is defined in userparameter_mysql.conf. MySQL CHANGE_PER_SECOND MySQL commit operations per second mysql.status[Com_commit] 1w FLOAT qps It requires user parameter mysql.status[*], which is defined in userparameter_mysql.conf. MySQL CHANGE_PER_SECOND MySQL delete operations per second mysql.status[Com_delete] 1w FLOAT qps It requires user parameter mysql.status[*], which is defined in userparameter_mysql.conf. MySQL CHANGE_PER_SECOND MySQL insert operations per second mysql.status[Com_insert] 1w FLOAT qps It requires user parameter mysql.status[*], which is defined in userparameter_mysql.conf. MySQL CHANGE_PER_SECOND MySQL rollback operations per second mysql.status[Com_rollback] 1w FLOAT qps It requires user parameter mysql.status[*], which is defined in userparameter_mysql.conf. MySQL CHANGE_PER_SECOND MySQL select operations per second mysql.status[Com_select] 1w FLOAT qps It requires user parameter mysql.status[*], which is defined in userparameter_mysql.conf. MySQL CHANGE_PER_SECOND MySQL update operations per second mysql.status[Com_update] 1w FLOAT qps It requires user parameter mysql.status[*], which is defined in userparameter_mysql.conf. MySQL CHANGE_PER_SECOND MySQL queries per second mysql.status[Questions] 1w FLOAT qps It requires user parameter mysql.status[*], which is defined in userparameter_mysql.conf. MySQL CHANGE_PER_SECOND MySQL slow queries mysql.status[Slow_queries] 1w It requires user parameter mysql.status[*], which is defined in userparameter_mysql.conf. MySQL MySQL uptime mysql.status[Uptime] 1w uptime It requires user parameter mysql.status[*], which is defined in userparameter_mysql.conf. MySQL MySQL version mysql.version 1h 1w 0 CHAR It requires user parameter mysql.version, which is defined in userparameter_mysql.conf. MySQL MySQL performance 2 1 0 0 MySQL operations Template DB MySQL 500 200 0 0 1 1 0 1 0 0 0 3 0 0 MySQL bandwidth Template DB MySQL 500 270 1 0 1 1 0 1 0 0 0 3 Service state 0 Down 1 Up



