Press "Enter" to skip to content

利用Zabbix自带模板监控Mysql

安装环境:Redhat 6,Zabbix 3.2.1(LNMP环境)

一、vi /home/app/zabbix/scripts/checkmysqlperformance.sh
#!/bin/sh
MYSQL_SOCK="/home/app/mysql/tmp/mysql.sock"
MYSQL_USER='root'
MYSQL_PWD='test1234'
MYSQL_HOST='1.2.3.4'
MYSQL_PORT='3306'
ARGS=1
if [ $# -ne "$ARGS" ];then
echo "Please input onearguement:"
fi
case $1 in
Uptime)
result=`/home/app/mysql/bin/mysqladmin -u$MYSQL_USER -p${MYSQL_PWD} -S $MYSQL_SOCK status 2>/dev/null |cut -f2 -d":"|cut -f1-d"T"`
echo $result
;;
Com_update)
result=`/home/app/mysql/bin/mysqladmin -u$MYSQL_USER -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status 2>/dev/null |grep -w "Com_update"|cut -d"|" -f3`
echo $result
;;
Slow_queries)
result=`/home/app/mysql/bin/mysqladmin -u$MYSQL_USER -p${MYSQL_PWD}-S $MYSQL_SOCK status 2>/dev/null |cut -f5 -d":"|cut -f1 -d"O"`
echo $result
;;
Com_select)
result=`/home/app/mysql/bin/mysqladmin -u$MYSQL_USER -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status 2>/dev/null |grep -w "Com_select"|cut -d"|" -f3`
echo $result
;;
Com_rollback)
result=`/home/app/mysql/bin/mysqladmin -u$MYSQL_USER -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status 2>/dev/null |grep -w "Com_rollback"|cut -d"|" -f3`
echo $result
;;
Questions)
result=`/home/app/mysql/bin/mysqladmin -u$MYSQL_USER -p${MYSQL_PWD}-S $MYSQL_SOCK status 2>/dev/null |cut -f4 -d":"|cut -f1 -d"S"`
echo $result
;;
Com_insert)
result=`/home/app/mysql/bin/mysqladmin -u$MYSQL_USER -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status 2>/dev/null |grep -w "Com_insert"|cut -d"|" -f3`
echo $result
;;
Com_delete)
result=`/home/app/mysql/bin/mysqladmin -u$MYSQL_USER -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status 2>/dev/null |grep -w "Com_delete"|cut -d"|" -f3`
echo $result
;;
Com_commit)
result=`/home/app/mysql/bin/mysqladmin -u$MYSQL_USER -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status 2>/dev/null |grep -w "Com_commit"|cut -d"|" -f3`
echo $result
;;
Bytes_sent)
result=`/home/app/mysql/bin/mysqladmin -u$MYSQL_USER -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status 2>/dev/null |grep -w "Bytes_sent"|cut -d"|" -f3`

echo $result
;;
Bytes_received)
result=`/home/app/mysql/bin/mysqladmin -u$MYSQL_USER -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status 2>/dev/null |grep -w "Bytes_received" |cut -d"|" -f3`
echo $result
;;
Com_begin)
result=`/home/app/mysql/bin/mysqladmin -u$MYSQL_USER -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status 2>/dev/null |grep -w "Com_begin"|cut -d"|" -f3`
echo $result
;;

*)
echo"Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions)"
;;
esac

chown -R zabbix:zabbix /home/app/zabbix

chmod 755 /home/app/zabbix/scripts/checkmysqlperformance.sh

二、在/home/app/zabbix/etc/zabbix_agentd.conf里面添加UserParameter,格式如下,对于Zabbix来说,脚本其实就是一个插件。

UserParameter=mysql.version,mysql -V
UserParameter=mysql.ping,mysqladmin -uroot -pxxxxxx -S /home/app/mysql/tmp/mysql.sock ping | grep -c alive
UserParameter=mysql.status[*],/home/app/zabbix/scripts/checkmysqlperformance.sh $1

三、重启agentd服务器,然后添加模板Template App MySQL。

四、查看数据。

Be First to Comment

发表评论

电子邮件地址不会被公开。 必填项已用*标注