之前写过一篇文章Zabbix监控mysql,https://blog.csdn.net/sj349781478/article/details/79205280
今天所要说的也是Zabbix监控mysql,只不过是多实例的mysql
1、监控的内容,是通过show status过滤出来的,MySQL运行状态详解
2、MySQL性能指标,包括tps / qps / 线程状态 / 流量状态 等等 , 这里使用qps为例。
3、配置:定义每个实例的端口
#vi mysql_port
3306
3307
3308
4、给所有实例创建一个zabbix用户只允许本地访问,生产环境具体权限各位看官自己看着给吧O(∩_∩)O
mysql>GRANT ALL PRIVILEGES ON *.* TO 'zabbix'@'localhost' IDENTIFIED BY 'zabbix';
5、创建自动发现MySQL端口脚本
#vi discovery_mysql.sh
#!/bin/bash
res=`cat /usr/local/zabbix-agent/scripts/mysql_port|grep -v "^#"`
port=($res)
printf '{\n'
printf '\t"data":[\n'
for key in ${!port[@]}
do
if [[ "${#port[@]}" -gt 1 && "${key}" -ne "$((${#port[@]}-1))" ]];then
printf '\t {\n'
printf "\t\t\t\"{#MYSQLPORT}\":\"${port[${key}]}\"},\n"
else [[ "${key}" -eq "((${#port[@]}-1))" ]]
printf '\t {\n'
printf "\t\t\t\"{#MYSQLPORT}\":\"${port[${key}]}\"}\n"
fi
done
printf '\t ]\n'
printf '}\n'
6、创建MySQL检测脚本
# vi check_Multimysql.sh
#!/bin/bash
MYSQL_USER='zabbix'
MYSQL_PWD='zabbix'
MYSQL_HOST='localhost'
MYSQL_PORT=$2
MYSQL_CONN="/usr/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} --socket=/data/mysql_${MYSQL_PORT}/data/mysql_${MYSQL_PORT}.sock --port=${MYSQL_PORT} "
help() {
echo "Usage:$0 [ping|Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_be
gin] port"
}
echo "....!"
help
exit 2
fi
case $1 in
ping)
result=`${MYSQL_CONN} ping | grep -c alive`
echo $result
;;
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
;;
*)
help
;;
esac
7、测试脚本是否正常(客户端)
8、zabbix agent配置文件添加自定义脚本,并重启zabbix agent(客户端)
UserParameter=mysql_discovery[*],/bin/bash /usr/local/zabbix-agent/scripts/discovery_mysql.sh
#####$1==command $2===port
UserParameter=mysql.status[*],/usr/local/zabbix-agent/scripts/check_Multimysql.sh $1 $2
UserParameter=mysql.ping[*],/usr/local/zabbix-agent/scripts/check_Multimysql.sh ping $1
/etc/init.d/zabbix_agentd restart
9、使用zabbix_get检查定义的key是否生效(服务端)
10、下面是图形化的配置
1)首先创建一个模板(Template Linux MySQL Master Discovery),然后在模板中创建一个自动发现规则(Linux MySQL Discovery)
2)在这个自动发现规则内创建多个item,我这里就给一个样例。
MySQL bytes received per second on {#MYSQLPORT} mysql.status[Bytes_received,{#MYSQLPORT}]
这个item包括Name定义、Key怎么定义、Type选项,Application定义,以及保存时间;最后一个比较重要的就是Store value,常用的有两种,一种是取出来的值是什么就存储是什么,第二种就是取每秒钟的差值(本次取值减去上次取值的差除以60,得到每秒钟的速率),这是因为我们从MySQL状态变量取来的值有些是累积值,利用zabbix这个功能就可以得到比如每秒钟的QPS/TPS等。也不是说每个值都需要这么取每秒速率,比如MySQL连接相关的状态变量就需要当前值。
参考文章
https://blog.csdn.net/qq942477618/article/details/60960865
http://blog.51cto.com/jinlong/2051703
http://www.ywnds.com/?p=7203
https://www.cnblogs.com/skyflask/articles/7623638.html