1. 程式人生 > >使用Zabbix官方模板監控MySQL

使用Zabbix官方模板監控MySQL

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

[root@db ~]# 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 table
UserParameter=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 alive
UserParameter=mysql.version,mysql -V

需要更改的地方:

  1. 如果你的zabbix不是yum安裝,那麽需要更改HOME後面的變量為zabbix配置文件所在目錄,如:/usr/local/zabbix/etc

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

  3. 更改默認MySQL訪問的用戶名

  4. 也可以添加自定義Key

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

[root@db ~]# 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 table
UserParameter=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 alive

UserParameter=mysql.version,mysql -V

#下面是自己新增的Key
UserParameter=mysql.variables[*],HOME=/usr/local/zabbix/etc mysql -uzabbix -e "show variables like ‘$1‘;" | tail -n 1 | awk ‘{print $$2}‘

在配置文件指定的HOME目錄創建指定的.my.cnf配置文件,主要定義連接MySQL的信息

[root@db ~]# vim /usr/local/zabbix/etc/.my.cnf 

[mysql]
host=localhost
user=zabbix
password=slf8gadffdasdPRW
port=3387

[mysqladmin]
host=localhost
user=zabbix
password=slf8gadffdasdPRW
port=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配置文件


監控效果圖如下:

技術分享


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

技術分享

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


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

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

使用Zabbix官方模板監控MySQL