一、配置Zabbix Agent
1、插件安裝及配置
下載下傳位址:https://www.percona.com/downloads/percona-monitoring-plugins/
安裝percona的監控mysql軟體包:
<code>rpm -ivh percona-zabbix-templates-1.1.7-1.noarch.rpm</code>
檢視rpm安裝的檔案路徑:
<code>[root@zabbix ~]</code><code># rpm -ql percona-zabbix-templates-1.1.7-1.noarch</code>
<code>/var/lib/zabbix/percona</code>
<code>/var/lib/zabbix/percona/scripts</code>
<code>/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper</code><code>.sh</code>
<code>/var/lib/zabbix/percona/scripts/ss_get_mysql_stats</code><code>.php</code>
<code>/var/lib/zabbix/percona/templates</code>
<code>/var/lib/zabbix/percona/templates/userparameter_percona_mysql</code><code>.conf</code>
<code>/var/lib/zabbix/percona/templates/zabbix_agent_template_percona_mysql_server_ht_2</code><code>.0.9-sver1.1.7.xml</code>
其中get_mysql_stats_wrapper.sh和ss_get_mysql_stats.php是mysql監控腳本,供zabbix agent調用;userparameter_percona_mysql.conf是zabbix agent的配置檔案,包含了mysql監控的自定義key;zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.7.xml是zabbix模闆檔案,需要在zabbix web端導入。
檢視zabbix agent的Include目錄:
<code>[root@zabbix ~]</code><code># grep '^Include' /etc/zabbix/zabbix_agentd.conf</code>
<code>Include=</code><code>/etc/zabbix/zabbix_agentd</code><code>.d/</code>
複制配置檔案到zabbix agent的Include目錄:
<code>cp</code> <code>/var/lib/zabbix/percona/templates/userparameter_percona_mysql</code><code>.conf </code><code>/etc/zabbix/zabbix_agentd</code><code>.d/</code>
進入percona的腳本目錄:
<code>cd</code> <code>/var/lib/zabbix/percona/scripts</code>
修改腳本裡的mysql連接配接位址:
<code>vim get_mysql_stats_wrapper.sh</code>
<code>HOST=localhost</code>
建立mysql連接配接配置檔案ss_get_mysql_stats.php.cnf,寫入使用者名和密碼:
<code>vim ss_get_mysql_stats.php.cnf</code>
<code><?php</code>
<code>$mysql_user = </code><code>'root'</code><code>;</code>
<code>$mysql_pass = </code><code>'s3cret'</code><code>;</code>
mysql主從監控的配置:
<code># 找到下面一行,可以看到mysql指令後面沒有連接配接參數,如果你的環境這樣執行沒有問題的話,可以不改</code>
<code>RES=`HOME=~zabbix mysql -e </code><code>'SHOW SLAVE STATUS\G'</code> <code>| </code><code>egrep</code> <code>'(Slave_IO_Running|Slave_SQL_Running):'</code> <code>| </code><code>awk</code> <code>-F: </code><code>'{print $2}'</code> <code>| </code><code>tr</code> <code>'\n'</code> <code>','</code><code>`</code>
<code># 在mysql指令後面加上連接配接參數:</code>
<code>RES=`HOME=~zabbix mysql -h localhost -uroot -ps3cret -e </code><code>'SHOW SLAVE STATUS\G'</code> <code>| </code><code>egrep</code> <code>'(Slave_IO_Running|Slave_SQL_Running):'</code> <code>| </code><code>awk</code> <code>-F: </code><code>'{print $2}'</code> <code>| </code><code>tr</code> <code>'\n'</code> <code>','</code><code>`</code>
<code># 如果腳本運作時出現提示“Warning: Using a password on the command line interface can be insecure.”,解決方法</code>
<code>RES=`HOME=~zabbix mysql -h localhost -uroot -ps3cret -e </code><code>'SHOW SLAVE STATUS\G'</code> <code>2>&1 | </code><code>egrep</code> <code>'(Slave_IO_Running|Slave_SQL_Running):'</code> <code>| </code><code>awk</code> <code>-F: </code><code>'{print $2}'</code> <code>| </code><code>tr</code> <code>'\n'</code> <code>','</code><code>`</code>
<a href="https://s2.51cto.com/wyfs02/M01/00/A8/wKiom1mbuxrzo0TQAABcGSy1bak364.png-wh_500x0-wm_3-wmp_4-s_731625140.png" target="_blank"></a>
2、測試腳本
本地執行腳本測試:
<code>/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper</code><code>.sh gg</code>
<code>/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper</code><code>.sh running-slave</code>
如果上面測試結果不正常,繼續下面調試,檢視錯誤原因:
<code>/usr/bin/php</code> <code>-q </code><code>/var/lib/zabbix/percona/scripts/ss_get_mysql_stats</code><code>.php --host localhost --items gg</code>
正常輸出:
<a href="https://s4.51cto.com/wyfs02/M00/00/A8/wKiom1mbu8uQb-NpAAA8Z046dYQ917.png" target="_blank"></a>
解決上面問題後,再用zabbix使用者執行腳本,檢視輸出:
<code>sudo</code> <code>-u zabbix -H </code><code>/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper</code><code>.sh gg</code>
如果遇到zabbix使用者權限問題,解決方法:
<code>rm</code> <code>-f </code><code>/tmp/localhost-mysql_cacti_stats</code><code>.txt</code>
<code>chown</code> <code>-R zabbix. </code><code>/var/lib/zabbix</code>
<a href="https://s5.51cto.com/wyfs02/M02/9F/58/wKioL1mbu82Ago3CAAAwrXIP3Ho725.png" target="_blank"></a>
所有調試正常後,重新開機zabbix agent:
<code>service zabbix-agent restart</code>
最後到zabbix server端遠端測試一下:
<code>zabbix_get -s 192.168.60.211 -k MySQL.Key-</code><code>read</code><code>-requests</code>
<a href="https://s4.51cto.com/wyfs02/M00/9F/58/wKioL1mbu9fyAfkSAAAswEbXzfY902.png" target="_blank"></a>
二、配置Zabbix Web
1、zabbix web端導入模闆檔案
模闆檔案(zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.7.xml)在插件安裝目錄/var/lib/zabbix/percona/templates下,複制出來導入到zabbix web。
導入後的模闆名“Percona MySQL Server Template”,模闆分組“Percona Templates”。
<a href="https://s2.51cto.com/wyfs02/M02/9F/58/wKioL1mbvCDCGeCsAACKzhCW6gU819.png" target="_blank"></a>
注意:官方安裝包裡的模闆是在zabbix2.0.9版本下測試的,經測試可導入到zabbix2.0和2.2版本。
zabbix3.0版本環境不能直接導入,解決方法是:找個zabbix2.2版本環境中轉一下,即從2.2版本中導出後,再導入到3.0版本。
2、zabbix web端建立主機
建立一個主機,主機連結模闆“Percona MySQL Server Template”,把主機剩餘配置完成。等待5分鐘左右後檢視mysql監控資料。
附percona網站連結:
https://www.percona.com/doc/percona-monitoring-plugins/LATEST/zabbix/index.html
本文轉自 張斌_青島 51CTO部落格,原文連結:http://blog.51cto.com/qicheng0211/1958291