使用Zabbix官方模板監控MySQL

來源:互聯網
上載者:User

標籤:zabbix   mysql   監控   

 關於Zabbix和MySQL安裝就不在說明,請自行參考相關文檔


複製官方MySQL監控的userparameter到安裝目錄,我這裡是採用的3.4.1源碼安裝,安裝目錄在/usr/local/zabbix目錄,如果你是rpm包安裝請自行尋找檔案和目錄

cp zabbix-3.4.1/conf/zabbix_agentd/userparameter_mysql.conf /usr/local/zabbix/etc/zabbix_agentd.conf.d/

使用root使用者登入到MySQL並授權一個專門監控使用的使用者,當然也可以直接使用root,但不安全推薦使用普通使用者,並且指定需要的許可權即可

#登入資料庫mysql -uroot -p#下面是需要在資料庫裡面執行的命令,為了安裝請不要指定all許可權,也不要指定所有庫,並且需要有一個安全的密碼grant all on *.* to "username"@"localhost" identified by "USER_PASSWORD";

編輯/usr/local/zabbix/etc/zabbix_agentd.conf.d/userparameter_mysql.conf

[[email protected] ~]# cat /usr/local/zabbix/etc/zabbix_agentd.conf.d/userparameter_mysql.conf# For all the following commands HOME should be set to the directory that has .my.cnf file with password information.# Flexible parameter to grab global variables. On the frontend side, use keys like mysql.status[Com_insert].# Key syntax is mysql.status[variable].UserParameter=mysql.status[*],echo "show global status where Variable_name=‘$1‘;" | HOME=/var/lib/zabbix mysql -N | awk ‘{print $$2}‘# Flexible parameter to determine database or table size. On the frontend side, use keys like mysql.size[zabbix,history,data].# Key syntax is mysql.size[<database>,<table>,<type>].# Database may be a database name or "all". Default is "all".# Table may be a table name or "all". Default is "all".# Type may be "data", "index", "free" or "both". Both is a sum of data and index. Default is "both".# Database is mandatory if a table is specified. Type may be specified always.# Returns value in bytes.# ‘sum‘ on data_length or index_length alone needed when we are getting this information for whole database instead of a single tableUserParameter=mysql.size[*],bash -c ‘echo "select sum($(case "$3" in both|"") echo "data_length+index_length";; data|index) echo "$3_length";; free) echo "data_free";; esac)) from information_schema.tables$([[ "$1" = "all" || ! "$1" ]] || echo " where table_schema=\"$1\"")$([[ "$2" = "all" || ! "$2" ]] || echo "and table_name=\"$2\"");" | HOME=/var/lib/zabbix mysql -N‘UserParameter=mysql.ping,HOME=/var/lib/zabbix mysqladmin ping | grep -c aliveUserParameter=mysql.version,mysql -V

需要更改的地方:

  1. 如果你的zabbix不是yum安裝,那麼需要更改HOME後面的變數為zabbix設定檔所在目錄,如:/usr/local/zabbix/etc

  2. 如果你的MySQL不是yum安裝,那麼需要添加MySQL的可執行檔到環境變數,或者更改為絕對路徑

  3. 更改預設MySQL訪問的使用者名稱

  4. 也可以添加自訂Key

更改後的內容如下:(根據自己需求)

[[email protected] ~]# cat /usr/local/zabbix/etc/zabbix_agentd.conf.d/userparameter_mysql.conf # For all the following commands HOME should be set to the directory that has .my.cnf file with password information.# Flexible parameter to grab global variables. On the frontend side, use keys like mysql.status[Com_insert].# Key syntax is mysql.status[variable].UserParameter=mysql.status[*],echo "show global status where Variable_name=‘$1‘;" | HOME=/usr/local/zabbix/etc mysql -uzabbix -N | awk ‘{print $$2}‘# Flexible parameter to determine database or table size. On the frontend side, use keys like mysql.size[zabbix,history,data].# Key syntax is mysql.size[<database>,<table>,<type>].# Database may be a database name or "all". Default is "all".# Table may be a table name or "all". Default is "all".# Type may be "data", "index", "free" or "both". Both is a sum of data and index. Default is "both".# Database is mandatory if a table is specified. Type may be specified always.# Returns value in bytes.# ‘sum‘ on data_length or index_length alone needed when we are getting this information for whole database instead of a single tableUserParameter=mysql.size[*],bash -c ‘echo "select sum($(case "$3" in both|"") echo "data_length+index_length";; data|index) echo "$3_length";; free) echo "data_free";; esac)) from information_schema.tables$([[ "$1" = "all" || ! "$1" ]] || echo " where table_schema=\"$1\"")$([[ "$2" = "all" || ! "$2" ]] || echo "and table_name=\"$2\"");" | HOME=/usr/local/zabbix/etc mysql -uzabbix -N‘UserParameter=mysql.ping,HOME=/usr/local/zabbix/etc mysqladmin -uzabbix ping | grep -c aliveUserParameter=mysql.version,mysql -V#下面是自己新增的KeyUserParameter=mysql.variables[*],HOME=/usr/local/zabbix/etc mysql -uzabbix -e "show variables like ‘$1‘;" | tail -n 1 | awk ‘{print $$2}‘

在設定檔指定的HOME目錄建立指定的.my.cnf設定檔,主要定義串連MySQL的資訊

[[email protected] ~]# vim /usr/local/zabbix/etc/.my.cnf [mysql]host=localhostuser=zabbixpassword=slf8gadffdasdPRWport=3387[mysqladmin]host=localhostuser=zabbixpassword=slf8gadffdasdPRWport=3387

修改zabbix_agentd設定檔,最檔案最後增加一行,如果是yum安裝,請根據需求更改

Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/

重啟zabbix agent

/etc/init.d/zabbix_agentd restart

在zabbix監控平台對應的主機添加內建的MySQL監控模板,最一可以就可看監控資料,如果沒有資料請根據報錯自行解決,大致會碰到以下一些問題

  1. userparameter設定檔修改錯誤,比如:指定的使用者不對,路徑不對

  2. 授權資料庫使用者權限不對,無法擷取資料

  3. .my.cnf設定檔錯誤或者路徑錯誤

  4. zabbix_agentd設定檔沒有包含userparameter_mysql.conf設定檔


監控如下:

650) this.width=650;" src="https://s1.51cto.com/oss/201710/22/fa3e60ad9e28afe4cc9cd0007680b559.png-wh_500x0-wm_3-wmp_4-s_3293767820.png" title="5K33O`A8_TPVNC]E)Z6$3CT.png " alt="fa3e60ad9e28afe4cc9cd0007680b559.png-wh_" />


最後我們新增一個自訂的監控,因為我們有一個自訂Key,如所示,我們的Key為mysql.variables[variables_name],需要傳一個參數,這個參數就相當於show variables like "variables_name";,用於擷取指定variables的值,比如:connect_timeout

650) this.width=650;" src="https://s5.51cto.com/oss/201710/22/6953e057e82a5fbbcf80861d083f0fad.png-wh_500x0-wm_3-wmp_4-s_428671416.png" title="IJ0$[X25_OS@I51CW)A1621.png" alt="6953e057e82a5fbbcf80861d083f0fad.png-wh_" />

注意更改間隔,時間間隔根據自己需求更改


最後我們可以根據上一個自訂監控,寫一個關於show status;的監控,都沒有問題,或者更複雜的一個業務監控,關於zabbix自訂監控還需要讀者查閱更多相關文檔

本文出自 “藍色_風暴” 部落格,請務必保留此出處http://270142877.blog.51cto.com/12869137/1975010

使用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.