1. 程式人生 > >Zabbix監控Mysql資料庫效能

Zabbix監控Mysql資料庫效能

監控mysql(預設監控模板不能用,再agentd.conf開啟自定義key,自己編寫指令碼)

在zabbix_agentd.conf尾部新增(注意相關檔案路徑,以及指令碼許可權等問題)

vim zabbix_agentd.conf

UserParameter=mysql.version,mysql -V

UserParameter=mysql.ping,mysqladmin -uroot -p123456 -S /tmp/mysql.sock ping | grep -c alive

UserParameter=mysql.status[*],/tmp/checkmysql.sh $1 $2

web上新增Item模板模板,選擇預設就好

wKiom1cfJx2BfYujAABM5GAE4mM135.png

編寫監控mysql指令碼

#!/bin/sh

MYSQL_SOCK="/tmp/mysql.sock"

MYSQL_PWD=123456

ARGS=1

if [ $# -ne "$ARGS" ];then

 echo "Please input one arguement:"

fi

case $1 in

    Uptime)

        result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK status 2> /dev/null | cut -f2 -d":" | cut -f1 -d"T"`

            echo $result

            ;;

        Com_update)

            result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status 2> /dev/null | grep -w "Com_update" | cut -d"|" -f3`

            echo $result

            ;;

        Slow_queries)

        result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK status 2> /dev/null |cut -f5 -d":"|cut -f1 -d"O"`

                echo $result

                ;;

    Com_select)

        result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status 2> /dev/null |grep -w "Com_select"|cut -d"|" -f3`

                echo $result

                ;;

    Com_rollback)

        result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status 2> /dev/null |grep -w "Com_rollback"|cut -d"|" -f3`

                echo $result

                ;;

    Questions)

        result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK status 2> /dev/null |cut -f4 -d":"|cut -f1 -d"S"`

                echo $result

                ;;

    Com_insert)

        result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status 2> /dev/null |grep -w "Com_insert"|cut -d"|" -f3`

                echo $result

                ;;

    Com_delete)

        result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status 2> /dev/null |grep -w "Com_delete"|cut -d"|" -f3`

                echo $result

                ;;

    Com_commit)

        result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status 2> /dev/null |grep -w "Com_commit" |cut -d"|" -f3`

                echo $result

               ;;

    Bytes_sent)

        result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status 2> /dev/null |grep -w "Bytes_sent"|cut -d"|" -f3`

                echo $result

                ;;

    Bytes_received)

        result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status 2> /dev/null |grep -w "Bytes_received"|cut -d"|" -f3`

                echo $result

                ;;

    Com_begin)

        result=`mysqladmin -uroot -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

###################################################

wKiom1cfJ4GST8z-AAHdaxi05eM147.png

##############################################################

監控mysql主從複製

vim zabbix_agentd.conf

在尾部新增

UserParameter=mysql.replication,/tmp/mysqlzc.sh $1 $2

我自建立的item叫做mysqlzc

wKiom1cfKBWjdlcLAAD-psshHrg313.png

建立key的時候,key名稱的要與在agent配置檔案裡面新增的一致

wKiom1cfKBXTKamAAABAgZRV7Po029.png

主從指令碼只需要一條命令就可以了(注意路徑以及許可權)

Vim /tmp/mysqlzc.sh

#!/bin/bash

mysql -uroot -p123456 -e 'show slave status\G' |grep -E "Slave_IO_Running|Slave_SQL_Running"|awk '{print $2}'|grep -c Yes

本文轉載於http://manyushuai.blog.51cto.com/8818545/1767934