Zabbix 監控Mysql資料庫及主從資料庫

來源:互聯網
上載者:User

標籤:zabbix

Zabbix監控mysql主從資料庫在腳步出現使用者名稱和密碼是會出現如下報錯“Warning: Using a password on the command line interface can be insecure”,報錯原因是mysql 5.6版本增加了密碼安全性原則,之前版本可以使用的命令列裡加上密碼就會強制報錯,所以使用zabbix監控mysql的時候,就會由於收到zabbix用戶端日誌報錯資訊。結合了網友的解決方案,現將整理出來供大家參考。



一,zabbix被監控端的設定:

1,首先配置mysql資料庫,配置mysql的--login-pathde 安全登入:

 設定--login-path:

[[email protected] conf]# mysql_config_editor set --login-path=local --host=localhost --user=zabbix -p

Enter password: 

[[email protected] conf]# mysql_config_editor print --all

[local]

user = zabbix

password = *****

host = localhost

命令解釋:

--login-path是設定訪問的名字,我設定的local;

--host是指定允許訪問的host地址,這個地址是你grant的時候配置的;

--user是使用者名稱,也是grant時候配置的;

-p是指定密碼,同樣是grant配置

2,進入mysql,修改zabbix帳號的許可權和密碼:

 

mysql> create user ‘zabbix‘ identified by  ‘zabbix‘

    -> ;

Query OK, 0 rows affected (0.00 sec)

 

mysql> select user,host from mysql.user;

+------------+--------------+

| user       | host         |

+------------+--------------+

| databak    | %            |

| ppt        | %            |

| root       | %            |

| slave_user | %            |

| zabbix     | %            |

| slave_user | 192.168.1.49 |

+------------+--------------+

6 rows in set (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON `zabbix`.* TO ‘zabbix‘@‘%‘;

Query OK, 0 rows affected (0.00 sec)

 

mysql> commit;

 

3,測試mysql login-path=local

[[email protected] conf]# mysql --login-path=local

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 542

Server version: 5.6.25-log Source distribution

 

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

 

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

 

mysql> quit

 

以上就配置好了安全訪問模式。

 

二,在用戶端裡,需要把監控的內容json化展示,然後服務端可以通過Regex過濾出結果,腳步的內容如下,然後放到/usr/local/zabbix/bin 檔案夾裡。

#!/bin/bash#Fucation:mysql low-level discovery#Script_name mysql_low_discovery.shmysql() {            port=($(sudo /bin/netstat -tpln | awk -F "[ :]+" ‘/[m]ysql/ {print $4}‘))            printf ‘{\n‘            printf ‘\t"data":[\n‘               for key in ${!port[@]}                   do                       if [[ "${#port[@]}" -gt 1 && "${key}" -ne "$((${#port[@]}-1))" ]];then              socket=`ps aux|grep ${port[${key}]}|grep -v grep|awk -F ‘=‘ ‘{print $10}‘|cut -d ‘ ‘ -f 1`                          printf ‘\t {\n‘                          printf "\t\t\t\"{#MYSQLPORT}\":\"${port[${key}]}\"},\n"                     else [[ "${key}" -eq "((${#port[@]}-1))" ]]              socket=`ps aux|grep ${port[${key}]}|grep -v grep|awk -F ‘=‘ ‘{print $10}‘|cut -d ‘ ‘ -f 1`                          printf ‘\t {\n‘                          printf "\t\t\t\"{#MYSQLPORT}\":\"${port[${key}]}\"}\n"                       fi               done                          printf ‘\t ]\n‘                          printf ‘}\n‘}$1


結果如:

650) this.width=650;" src="http://s2.51cto.com/wyfs02/M01/75/DC/wKioL1ZEA_bj5sUEAAAzatpIZ30009.png" title="1.png" alt="wKioL1ZEA_bj5sUEAAAzatpIZ30009.png" />


2,給指令碼low_level_discovery賦可執行許可權

[[email protected] conf]# chmod +x /usr/local/zabbix/bin/low_level_discovery

[[email protected] conf]# ll /usr/local/zabbix/bin/low_level_discovery

-rwxr-xr-x 1 root root 1055 Nov 11 15:45 /usr/local/zabbix/bin/low_level_discovery

 

3,允許zabbix使用者無密碼運行mysql,netstat,/usr/local/mysql/bin是mysql程式地址,可根據情況自由修改。

[[email protected] bin]# echo "zabbix ALL=(root) NOPASSWD:/usr/local/mysql/bin/mysql,/bin/netstat" >> /etc/sudoers

所示:

650) this.width=650;" src="http://s4.51cto.com/wyfs02/M00/75/DC/wKioL1ZEBJKw2ur_AADfbJxIIWc562.png" style="float:none;" title="1.png" alt="wKioL1ZEBJKw2ur_AADfbJxIIWc562.png" />


4,禁用 requiretty不關閉的話會無法擷取資料,zabbix日誌還會報錯.

[[email protected] bin]# sed -i ‘s/^Defaults.*.requiretty/#Defaults    requiretty/‘ /etc/sudoers

650) this.width=650;" src="http://s5.51cto.com/wyfs02/M00/75/DE/wKiom1ZEBErCTox2AADNej1K3ZE259.png" style="float:none;" title="2.png" alt="wKiom1ZEBErCTox2AADNej1K3ZE259.png" />




5,修改zabbix_agentd.conf檔案的最後添加一下內容:

UserParameter=zabbix_low_discovery[*],/bin/bash /usr/local/zabbix/bin/low_level_discovery  $1

UserParameter=mysql_stats_5.6[*],sudo /usr/local/mysql/bin/mysql --login-path=local -P $1  -e "show global status"|grep "\<$2\>"|cut  -f2

UserParameter=mysql_stats_slave_5.6[*],sudo /usr/local/mysql/bin/mysql --login-path=local -P $1  -e "show slave status\G"|grep "\<$2\>"|awk ‘{if($NF=="Yes") {print 1} else {print 0}}‘


6,在服務端進行測試。

650) this.width=650;" src="http://s4.51cto.com/wyfs02/M01/75/DC/wKioL1ZEBcWCHqv2AAA4TKr1NsQ715.png" title="3.png" alt="wKioL1ZEBcWCHqv2AAA4TKr1NsQ715.png" />



三,zabbix服務端web設定:

1,模板匯入

把template mysql auto discovery匯入到zabbix裡,具體:配置-----模板------載入-------選擇檔案。

2,設定Regex:管理-----一般------Regex--------新的Regex-------輸入名稱和測試字串--------測試--------儲存。

650) this.width=650;" src="http://s1.51cto.com/wyfs02/M01/75/DE/wKiom1ZEBgKA91wdAAHwjtswHkU509.png" title="1.png" alt="wKiom1ZEBgKA91wdAAHwjtswHkU509.png" />


3,設定模板的更新間隔並把主機關聯到模板(如果設定太小的話,一是伺服器壓力大,另一個就是你檢測的連接埠突然宕掉了,還沒有來的急警示,主機通過jsonlai 來擷取不到這個資訊,就會認為沒有這個連接埠,模板裡就會自動關閉這個監控項的內容)

650) this.width=650;" src="http://s4.51cto.com/wyfs02/M01/75/DE/wKiom1ZEBkmTsByWAADCWIfG4SI816.png" title="2.png" alt="wKiom1ZEBkmTsByWAADCWIfG4SI816.png" />


4,最後監控的內容如下:

650) this.width=650;" src="http://s4.51cto.com/wyfs02/M02/75/DE/wKiom1ZEBtqgQIvSAAI8ZcrnT-c387.png" title="3.png" alt="wKiom1ZEBtqgQIvSAAI8ZcrnT-c387.png" />


最後非常感謝51cto的dl528888(http://dl528888.blog.51cto.com/2382721/1677545)博主,本文大部分都是參考他的文章,再次表示感謝,剛開始出現了問題,博主非常熱情的替我遠程解決,再次表示非常的非常的感謝。

本文出自 “清風明月” 部落格,請務必保留此出處http://liqingbiao.blog.51cto.com/3044896/1712080

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.