分散式監控系統Zabbix-3.0.3-完整安裝記錄(2)-新增mysql監控

散盡浮華發表於2016-06-26

 

Zabbix3.0 Server以後就自帶了MySQL外掛來監控mysql資料庫的模板,只需要配置好agent客戶端,然後在web端給主機增加模板就行了。
以下是公司線上的zabbix3.0環境下新增mysql監控操作記錄:

Zabbix_agent客戶端操作
1)首先在客戶端的mysql裡新增許可權,即本機使用zabbix賬號連線本地的mysql
mysql> grant all on *.* to zabbix@'localhost' identified by "123456”;
mysql> flush privileges;
2)在zabbix_agent服務目錄下建立.my.cnf 連線檔案
線上的zabbix安裝目錄是/usr/local/zabbix
[root@ff_db_master1 ~]# cd /usr/local/zabbix/etc/
[root@ff_db_master1 etc]# cat .my.cnf
[client]
user=zabbix
password=123456

--------------------------------------------------------------------------------------------------------------
注意:
如果在資料庫grant授權時,針對的是localhost,這個.my.cnf裡面就不用加host引數了【如上配置】
但如果grant授權時針對的是本機的ip(如192.168.1.25),那麼在.my.cnf檔案裡就要加上host引數進行指定了
即在.my.cnf檔案就要加上:

[client]
host=192.168.1.25
socket=/Data/app/mysql5.6.25/var/mysql.sock 【這一行可以不用加上,預設會找到的】
user=zabbix
password=123456
--------------------------------------------------------------------------------------------------------------

3)配置MySQL的key檔案
這個可以從zabbix3.0安裝時的解壓包裡拷貝過來
(線上的是從zabbix_server服務端安裝時解壓目錄/usr/local/src/zabbix3.0.3/conf/zabbix_agentd/userparameter_mysql.conf  拷貝到zabbix_agent客戶端上的/usr/local/zabbix/etc/zabbix_agentd.conf.d/ 目錄下的)
[root@ff_db_master1 zabbix_agentd.conf.d]# pwd
/usr/local/zabbix/etc/zabbix_agentd.conf.d
[root@ff_db_master1 zabbix_agentd.conf.d]# ls userparameter_mysql.conf 
userparameter_mysql.conf
然後檢視 userparameter_mysql.conf 檔案,看到類似 HOME=/var/lib/zabbix 的路徑設定,把路徑全都替換為 /usr/local/zabbix/etc/,也就是上面的.my.cnf檔案所在的目錄路徑。
另外,注意userparameter_mysql.conf 檔案裡的mysql命令路徑(提前做好mysql的系統環境變數,以防mysql命令不被系統識別)

如下:

[root@ff_db_master1 zabbix_agentd.conf.d]# cat 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 -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 -N'

UserParameter=mysql.ping,HOME=/usr/local/zabbix/etc/ mysqladmin ping | grep -c alive
UserParameter=mysql.version,mysql -V

4)按照上面修改完之後檢查一下,然後重啟zabbix_agent
[root@ff_db_master1 ~]# pkill -9 zabbix_agent
[root@ff_db_master1 ~]# /usr/local/zabbix/sbin/zabbix_agentd 

接著在zabbix_server服務端進行命令列測試[下面的192.168.1.25是客戶端的ip]

[root@Zabbix-server ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.1.25 -p 10050 -k "mysql.status[Uptime]"
12085434
[root@Zabbix-server ~]#

如果出現類似這一串key的數字,就說明配置ok,服務端能監控到客戶端的mysql資料了!
成功啦,之後在監控介面增加主機對應的MySQL模板就ok了。 (上面命令中的-k後面新增的是鍵值,這個在zabbix介面裡對應模組裡可以找到。比如在某主機的"主動發現"規則裡就能看到鍵值)

5)登入zabbix監控介面,在“配置”-“模板”裡可以看到自帶的mysql監控模板

在相應主機監控配置裡新增mysql的監控模版(zabbix自帶的)即可。

 


下面是mysql監控項的效果圖:

【1】監控mysql的頻寬:在zabbix前端可以實時檢視mysql傳送接收的位元組數。其中bytes received表示從所有客戶端接收到的位元組數,bytes sent表示傳送給所有客戶端的位元組數。


【2】監控mysql的操作狀態:在zabbix前端可以實時檢視各種SQL語句每秒鐘的操作次數。

 

相關文章