时间:2021-07-01 10:21:17 帮助过:4人阅读
2、配置zabbix_agent
vim /etc/zabbix/zabbix_agentd.conf
UserParameter=mysql.version,mysql -V
UserParameter=mysql.status[*],/usr/local/zabbix/scripts/chk_mysql.sh $1
UserParameter=mysql.ping,mysqladmin -uzabbix -p123456 -P3306 -h127.0.0.1 ping | grep -c alive
3、数据库添加账号:
mysql -uroot -p
GRANT PROCESS,SUPER,REPLICATION CLIENT ON *.* TO zabbix@‘127.0.0.1‘ IDENTIFIED BY ‘123456‘;
4、写check脚本
mkdir -p /usr/local/zabbix/scripts
vim /usr/local/zabbix/scripts/chk_mysql
#!/bin/sh
MYSQL_SOCK="/usr/local/mysql/mysql.sock"
MYSQL_USER=‘zabbix‘
MYSQL_PWD=‘123456‘
MYSQL_HOST=‘127.0.0.1‘
MYSQL_PORT=‘3306‘
ARGS=1
if [ $# -ne "$ARGS" ];then
echo "Please input one arguement:"
fi
case $1 in
Uptime)
result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK status|cut -f2 -d":"|cut -f1 -d"T"`
echo $result
;;
Com_update)
result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Com_update"|cut -d"|" -f3`
echo $result
;;
Slow_queries)
result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK status |cut -f5 -d":"|cut -f1 -d"O"`
echo $result
;;
Com_select)
result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Com_select"|cut -d"|" -f3`
echo $result
;;
Com_rollback)
result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Com_rollback"|cut -d"|" -f3`
echo $result
;;
Questions)
result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK status|cut -f4 -d":"|cut -f1 -d"S"`
echo $result
;;
Com_insert)
result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Com_insert"|cut -d"|" -f3`
echo $result
;;
Com_delete)
result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Com_delete"|cut -d"|" -f3`
echo $result
;;
Com_commit)
result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Com_commit"|cut -d"|" -f3`
echo $result
;;
Bytes_sent)
result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Bytes_sent" |cut -d"|" -f3`
echo $result
;;
Bytes_received)
result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Bytes_received" |cut -d"|" -f3`
echo $result
;;
Com_begin)
result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Com_begin"|cut -d"|" -f3`
echo $result
;;
*)
echo "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions)"
;;
esac
别忘了脚本授权:
chmod u+x /usr/local/zabbix/chk_mysql.sh
chown -R zabbix.zabbix /usr/local/zabbix/chk_mysql.sh
5、脚本准备完后,重启agent
5、验证
在zabbix-server服务器上测试是否可以获取到mysql的status信息:
zabbix_get -s 127.0.0.1 -p10050 -k mysql.status[Com_update]
有结果就是ok的!
PS:zabbix_get命令:
zabbix_get -s 10.8.95.74 -p10050 -k "system.cpu.load[all,avg15]";
本文出自 “10620401” 博客,请务必保留此出处http://10630401.blog.51cto.com/10620401/1978809
zabbix监控mysql
标签:zabbix 监控 mysql