巧用Zabbix自定義監控Mysql性能狀態



環境是Centos6.5 x86_64系統mysql

Zabbix版本:3.0.4sql

Mysql Server 版本:5.6.29 二進制安裝數據庫


zabbix_agentd.conf的配置文件以下:bash

[root@iZuf67iz9o3i4wixgtwybnZ alertscripts]# cat  /usr/local/zabbix/etc/zabbix_agentd.conf | grep -v '^#' | grep -v '^$'
LogFile=/tmp/zabbix_agentd.log
EnableRemoteCommands=1
Server=10.18.3.193
ListenPort=10050
ServerActive=10.18.3.193
Hostname=10.18.3.191
AllowRoot=1
Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/*.conf
UnsafeUserParameters=1
#下面兩行是自定義的key
UserParameter=mysql.slave,/usr/local/zabbix/share/zabbix/alertscripts/check_mysql_slave.sh | grep -c 'ok'
UserParameter=mysql.status[*],/usr/local/zabbix/share/zabbix/alertscripts/mysql_server_status.sh $1


查看mysql受權:ide

wKiom1i3or6yXIfoAAA3WVHaxd8949.png-wh_50


mysql受權須要密碼才能訪問,可是使用密碼mysql會提示明文密碼,就會有下面的提示:spa

[root@iZuf67iz9o3i4wixgtwybnZ alertscripts]# sh check_mysql_slave.sh 
Warning: Using a password on the command line interface can be insecure.
ok -slave is running


解決辦法以下:server

[root@iZuf67iz9o3i4wixgtwybnZ alertscripts]# cat /etc/my.cnf | tail -4
[client]
user = root
host = localhost
password = 1qaz@WSX


再次運行腳本:blog

[root@iZuf67iz9o3i4wixgtwybnZ alertscripts]# sh check_mysql_slave.sh 
ok -slave is running


mysql主從監控腳本,腳本中去掉用戶,密碼直接使用命令。ip

[root@iZuf67iz9o3i4wixgtwybnZ alertscripts]# cat check_mysql_slave.sh 
#!/bin/bash
declare -a slave_is
slave_is=($(/data/mysql/bin/mysql -e "show slave status\G"|grep -E "Slave_IO_Running|Slave_SQL_Running:"|awk '{print $2}'))
if [ "${slave_is[0]}" = "Yes" -a "${slave_is[1]}" = "Yes" ];then
    echo "ok -slave is running"
    exit 0
else
    echo "down -slave is not running"
    exit 2
fi



mysql_server_status腳本,這塊上面my.cnf填寫之後,使用mysqladmin也就不提示了:get

[root@iZuf67iz9o3i4wixgtwybnZ alertscripts]# cat mysql_server_status.sh 
#!/bin/bash
source /etc/profile
mysqladmin=`which mysqladmin`
case $1 in 
    Uptime) 
        result=`${mysqladmin} status|cut -f2 -d":"|cut -f1 -d"T"` 
        echo $result 
        ;; 
    Com_update) 
        result=`${mysqladmin} extended-status |grep -w "Com_update"|cut -d"|" -f3` 
        echo $result 
        ;; 
    Slow_queries) 
        result=`${mysqladmin} status |cut -f5 -d":"|cut -f1 -d"O"` 
        echo $result 
        ;; 
    Com_select) 
        result=`${mysqladmin} extended-status |grep -w "Com_select"|cut -d"|" -f3` 
        echo $result 
                ;; 
    Com_rollback) 
        result=`${mysqladmin} extended-status |grep -w "Com_rollback"|cut -d"|" -f3` 
                echo $result 
                ;; 
    Questions) 
        result=`${mysqladmin} status|cut -f4 -d":"|cut -f1 -d"S"` 
                echo $result 
                ;; 
    Com_insert) 
        result=`${mysqladmin} extended-status |grep -w "Com_insert"|cut -d"|" -f3` 
                echo $result 
                ;; 
    Com_delete) 
        result=`${mysqladmin} extended-status |grep -w "Com_delete"|cut -d"|" -f3` 
                echo $result 
                ;; 
    Com_commit) 
        result=`${mysqladmin} extended-status |grep -w "Com_commit"|cut -d"|" -f3` 
                echo $result 
                ;; 
    Bytes_sent) 
        result=`${mysqladmin} extended-status |grep -w "Bytes_sent" |cut -d"|" -f3` 
                echo $result 
                ;; 
    Bytes_received) 
        result=`${mysqladmin} extended-status |grep -w "Bytes_received" |cut -d"|" -f3` 
                echo $result 
                ;; 
    Com_begin) 
        result=`${mysqladmin} extended-status |grep -w "Com_begin"|cut -d"|" -f3` 
                echo $result 
                ;; 
                        
        *) 
        echo "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin)" 
        ;; 
esac



在zabbix_server獲取key值,或是查看zabbix_server.log文件均可以排錯。

[root@iZuf67iz9o3i4wixgtwybmZ bin]# ./zabbix_get -s 10.18.3.191 -k mysql.status[Uptime]
ZBX_NOTSUPPORTED: Unsupported item key.
[root@iZuf67iz9o3i4wixgtwybmZ bin]# ./zabbix_get -s 10.18.3.191 -k mysql.status[Uptime]
/usr/local/zabbix/share/zabbix/alertscripts/mysql_server_status.sh: line 4: mysqladmin: command not found
[root@iZuf67iz9o3i4wixgtwybmZ bin]# ./zabbix_get -s 10.18.3.191 -k mysql.status[Uptime]
414242


zabbix_agent.log能夠看到已經獲取到自定義的key:

wKiom1i3ojuQLV_2AAEobxWRPDI852.png-wh_50


添加自定義模板,模板名稱隨意給個和mysql相關的:

wKioL1i3o-7ivVBFAABlolKDSbw675.png-wh_50


建立應用集:

wKioL1i3pDnjLB2JAAA9LYgzWFE576.png-wh_50


添加監控項,一個key一個key添加,把腳本的key都添加進去。

wKioL1i3pIrS6cCUAAB3jZDOFlw111.png-wh_50


以下所示:

wKiom1i3pFexNKMDAAGE4D0rHXA177.png-wh_50


mysql 狀態這塊只作了圖形查看,並無作觸發器,只作了主從的觸發器,須要的能夠本身添加:

wKiom1i3pPrBu-umAADKdeUVUl4065.png-wh_50


添加完成後能夠添加一臺mysql數據庫查看下最新數據,是否成功了。

wKiom1i3pUGRlJQLAAEEu7d-8H8906.png-wh_50


查看圖形監控數據:

wKiom1i3pXTy4qA1AAEtLjIBgME208.png-wh_50



參考文章:http://www.ttlsa.com/zabbix/zabbix-monitor-mysql/


模板請到附件下載:

相關文章
相關標籤/搜索