概述
zabbix自帶的mysql插件來監控mysql數據庫,可是太過簡陋了,爲了更詳細的監控,業界的同窗們都使用Percona Monitoring Plugins 監控 MySQL的方式。php
安裝php腳本運行環境
percona監控zabbix的腳本使用php寫的,因此須要準備好php運行環境,這裏直接用yum安裝就能夠知足要求了:html
yum install -y php php-mysql
安裝官方percona插件
官網:https://www.percona.com/doc/percona-monitoring-plugins/1.1/index.htmlmysql
1,安裝percona的yum源sql
yum install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm
2,安裝percona監控插件數據庫
yum install percona-zabbix-templates
3,查看percona-zabbix-templates 安裝後的腳本位置和模板位置vim
[root@mysql03 home]# rpm -ql percona-zabbix-templates-1.1.8-1.noarch /var/lib/zabbix/percona /var/lib/zabbix/percona/scripts /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php /var/lib/zabbix/percona/templates /var/lib/zabbix/percona/templates/userparameter_percona_mysql.conf /var/lib/zabbix/percona/templates/zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.8.xml
注:安裝後會生成2個目錄,/var/lib/zabbix/percona/scripts是監控腳本目錄,/var/lib/zabbix/percona/templates是監控mysql的xml模版目錄,而後把/var/lib/zabbix/percona/templates/目錄下的模版文件zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.6_1.xml複製出來,在zabbix的管理界面import導入進去,可是報以下錯誤:服務器
解決辦法:app
將zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.8.xml導入zabbix2.4中再導出。以後將新的導出xml導入到3.2中問題解決。socket
4,拷貝userparameter_percona_mysql.conf文件到被監控的mysql服務器上測試
cp /var/lib/zabbix/percona/templates/userparameter_percona_mysql.conf /etc/zabbix/zabbix_agentd.d/
5,修改客戶端的zabbix_agentd.conf文件, 把userparameter_percona_mysql.conf 所在的目錄Include增長進去
Include=/etc/zabbix/zabbix_agentd.d/*.conf
6,修改監控腳本
# 修改用戶名,密碼,端口,sock文件路徑 vim /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php $mysql_user = 'zabbix'; $mysql_pass = 'Ab.12345'; $mysql_port = 3306; $mysql_socket = "/tmp/mysql.sock";
注:若mysql沒有使用默端口3006,則還須要修改以下腳本文件:
# 1,這裏要先建好這個文件,不然會報rm權限錯誤 vim /tmp/localhost-mysql_cacti_stats.txt:3317 chown -R zabbix.zabbix /tmp/$HOST-mysql_cacti_stats.txt:3317 #2,修改端口腳本 vim /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh CACHEFILE="/tmp/$HOST-mysql_cacti_stats.txt " 換成: CACHEFILE="/tmp/$HOST-mysql_zabbix_stats.txt:3317" TIMEFLM=`stat -c %Y /tmp/$HOST-mysql_cacti_stats.txt` 換成: TIMEFLM=`stat -c %Y /tmp/$HOST-mysql_zabbix_stats.txt:3317` #3,修改mysql執行命令路徑 #RES=`HOME=~zabbix mysql -e 'SHOW SLAVE STATUS\G' | egrep '(Slave_IO_Running|Slave_SQL_Running):' | awk -F: '{print $2}' | tr '\n' ','` 換成: RES=`/usr/local/mysql/bin/mysql -e 'SHOW SLAVE STATUS\G' | egrep '(Slave_IO_Running|Slave_SQL_Running):' | awk -F: '{print $2}' | tr '\n' ','`
登錄mysql建立監控要用的帳號
mysql> GRANT USAGE,SELECT,PROCESS,SUPER,REPLICATION CLIENT ON *.* TO 'zabbix'@'localhost' IDENTIFIED BY "Ab.12345"; Query OK, 0 rows affected (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
測試監控項
agent端測試
[root@mysql01 home]# /usr/bin/php -q /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php --host localhost --items gg gg:20
[root@mysql01 scripts]# ./get_mysql_stats_wrapper.sh gm 1 [root@mysql01 scripts]# ./get_mysql_stats_wrapper.sh gw 1819813
zabbix-server端測試監控項
[root@SMP tmp]# zabbix_get -s 192.230.4.11 -k 'MySQL.Handler-commit' rm: 沒法刪除"/tmp/localhost-mysql_cacti_stats.txt": 不容許的操做 18041875
注:權限問題解決方法
登陸zabbix-agent服務器,執行:
[root@mysql01 tmp]# cd /tmp [root@mysql01 tmp]# chown -R zabbix.zabbix localhost-mysql_cacti_stats.txt [root@mysql01 tmp]# /etc/init.d/zabbix-agent restart
再次測試:
[root@SMP tmp]# zabbix_get -s 192.230.4.11 -k 'MySQL.Handler-commit' 18041956
主機關聯模板
查看數據:
mysql從庫監控slave配置
先在mysql slave的agent裏面須要check下看是否生效:
[root@mysql01 scripts]# ./get_mysql_stats_wrapper.sh running-slave ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) 0
須要配置下訪問mysql的一些基本參數:
在my.cnf配置用戶名密碼、sock路徑
[mysql] no-auto-rehash user=zabbix password=Ab.12345 socket=/var/lib/mysql/mysql.sock
再次測試:
[root@mysql03 scripts]# sh /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh running-slave 1
結束!