zabbix監控mysql多執行個體自發現監控

來源:互聯網
上載者:User

標籤:zabbix監控多執行個體mysql

系統營運工程師   李超

1、定義每個執行個體的連接埠

cat mysql_port 33063307330833093310 331133123313

 

2、給所有執行個體建立一個zabbix使用者只允許本地訪問,生產環境具體許可權各位看官自己看著給吧O(∩_∩)O

GRANT ALL PRIVILEGES ON *.* TO [email protected]'127.0.0.1' IDENTIFIED BY 'zabbix';

 

 

3、建立自動探索MySQL連接埠指令碼

cat /data/PRG/zabbix/scripts/discovery_mysql.sh

#!/bin/bash# -------------------------------------------------------------------------------# Script_name: discovery_mysql.sh # Revision:    1.0# Date:        2017/03/09# Author:      kbsonlong# Email:       [email protected]# Website:     www.along.party# -------------------------------------------------------------------------------res=`cat /data/PRG/zabbix/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 doneprintf '\t ]\n' printf '}\n'

4、建立MySQL檢測指令碼

# cat /data/PRG/zabbix/scripts/check_Multimysql.sh #!/bin/bash# -------------------------------------------------------------------------------# FileName:    check_Multimysql.sh# Revision:    1.0# Date:        2017/03/09# Author:      kbsonlong# Email:       [email protected]# Website:     www.along.party# License:     GPL# ------------------------------------------------------------------------------- # 使用者名稱MYSQL_USER='zabbix' # 密碼MYSQL_PWD='zabbix' # 主機地址/IPMYSQL_HOST='127.0.0.1' # 連接埠MYSQL_PORT=$2 # 資料連線MYSQL_CONN="/usr/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT}"##help函數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_begin]  port"}# 參數是否正確if [ $# -lt "2" ];then     echo "參數缺失!"    help     exit 2fi  # 擷取資料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

 

5、測試指令碼是否正常

# bash discovery_mysql.sh {        "data":[         {                        "{#MYSQLPORT}":"3306"},         {                        "{#MYSQLPORT}":"3307"},         {                        "{#MYSQLPORT}":"3308"},         {                        "{#MYSQLPORT}":"3309"},         {                        "{#MYSQLPORT}":"3310"},         {                        "{#MYSQLPORT}":"3311"},         {                        "{#MYSQLPORT}":"3312"},         {                        "{#MYSQLPORT}":"3313"}         ]}# bash check_Multimysql.sh 參數缺失!Usage:check_Multimysql.sh  [ping|Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin]  port# bash check_Multimysql.sh ping 33061# bash check_Multimysql.sh Uptime 330618180446

 

6、zabbix agent設定檔添加自訂指令碼,並重啟zabbix agent

vim /data/PRG/zabbix/etc/zabbix_agentd.confUserParameter=mysql_discovery[*],/bin/bash /data/PRG/zabbix/scripts/discovery_mysql.sh#####$1==command   $2===portUserParameter=mysql.status[*],/data/PRG/zabbix/scripts/check_mysql.sh $1 $2 UserParameter=mysql.ping[*],/data/PRG/zabbix/scripts/check_mysql.sh ping $1

 

/etc/init.d/zabbix_agentd restart

 

7、使用zabbix_get檢查定義的key是否生效

# /usr/local/zabbix/bin/zabbix_get -s 192.168.62.200 -k mysql.ping[3307]   1# /usr/local/zabbix/bin/zabbix_get -s 192.168.62.200 -k mysql.status[Uptime,3307]18180813# /usr/local/zabbix/bin/zabbix_get -s 192.168.62.200 -k mysql.status[Uptime,3306]18180842# /usr/local/zabbix/bin/zabbix_get -s 192.168.62.200 -k mysql.status[Uptime,3310]18180847# /usr/local/zabbix/bin/zabbix_get -s 192.168.62.200 -k mysql.status[Bytes_sent,3310]58803854473

 

8、匯入模板Template Multi Mysql.xml


 

9、被監控主機關聯模板




zabbix監控mysql多執行個體自發現監控

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.