標籤:nagios
監控MySQL有兩種方法,一種是通過官方的check_mysql外掛程式,另一種是通過第三方的check_mysql_health的外掛程式。check_mysql_health的功能更為強大一些,這裡我兩種都簡單的說下。
第一種方法:使用nagios-plugins 官方內建的chech_mysql外掛程式
監控mysql可能在nagios伺服器上沒有官方的mysql的檢測的外掛程式。
需要先yum install mysql-devel
然後在重新編譯安裝一次nagios-plugins外掛程式
查看現在已經有了
[[email protected] ~]# ll /usr/local/nagios/libexec/check_mysql-rwxr-xr-x 1 nagios nagios 190768 10月 8 14:02 /usr/local/nagios/libexec/check_mysql
登陸進命令列模式
mysql -uroot -p
在被監控的遠程伺服器上(mysql伺服器上)要先建立一個可以遠程登陸mysql伺服器的帳號
GRANT ALL PRIVILEGES ON *.* TO ‘nagios‘@‘192.168.163.129‘ IDENTIFIED BY ‘nagios‘;
查看使用者已經有了nagios使用者
select user,host from mysql.user;
在監控主機上添加命令,需要修改commands.cfg
vim /usr/local/nagios/etc/objects/commands.cfg#check mysqldefine command{command_name check_mysqlcommand_line $USER1$/check_mysql -H $HOSTADDRESS$ -P $ARG1$ -u $ARG2$ -p $ARG3$}
在監控主機上添加監控的服務
define service{use generic-service,srv-pnphost_name mysql-master-1service_description MYSQLcheck_command check_mysql!3306!nagios!nagiosnotifications_enabled 1}
測試下nagios的配置是否有錯誤
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
沒問題重啟nagios服務
/etc/init.d/nagios restart
第二種方法:check_mysql_health外掛程式比起官方的check_mysql外掛程式功能更為強大,check_mysql_health不但能監控MySQL是否正常運行,還能監控MySQL主從、MySQL串連數情況、MySQL慢查詢等多種監控指標。
check_mysql_health官方介紹首頁:http://labs.consol.de/nagios/check_mysql_health/
安裝check_mysql_health外掛程式
wget http://labs.consol.de/download/shinken-nagios-plugins/check_mysql_health-2.1.8.2.tar.gztar zxvf check_mysql_health-2.1.8.2.tar.gzcd check_mysql_health-2.1.8.2./configuremakemake install
check_mysql_health外掛程式基本用法
/usr/local/nagios/libexec/check_mysql_health --hostname 192.168.163.130 --port 3306 --username nagios --password nagios --mode slow-queries
各參數的含義,尤其–mode參數有很多監控的指標可以參考,具體查看官網可以知。
--hostname 定義被監控主機的IP或機器名--port 定義被監控主機上MySQL的運行連接埠--username 定義被監控主機上MySQL的使用者名稱--password 定義被監控主機上MySQL的密碼--mode 定義被監控主機上MySQL的監控指標
和官方外掛程式類似,首先要建立一個可以用於遠程登陸的mysql帳號
GRANT ALL PRIVILEGES ON *.* TO ‘nagios‘@‘192.168.163.130‘ IDENTIFIED BY ‘nagios‘;
在監控主機上添加可以讓命令以便讓監控主機識別這個外掛程式。
vi /usr/local/nagios/etc/objects/commands.cfgdefine command{ command_name check_mysql_health command_line $USER1$/check_mysql_health --hostname $ARG1$ --port $ARG2$ --username $ARG3$ --password $ARG4$ --mode $ARG5$ }
在監控主機上添加要監控的服務
監控MySQL連線時間define service{use generic-service ; Name of service template to usehost_name mysql-master-1service_description check_mysql_connection_timecheck_command check_mysql_health!192.168.163.130!3306!nagios!nagios!connection-timenotifications_enabled 1};監控MySQL串連數define service{use generic-service ; Name of service template to usehost_name mysql-master-1service_description check_mysql_threads_connectedcheck_command check_mysql_health!192.168.163.130!3306!nagios!nagios!threads-connectednotifications_enabled 1};監控MySQL慢查詢情況define service{use generic-service ; Name of service template to usehost_name mysql-master-1service_description check_mysql_slow_queriescheck_command check_mysql_health!192.168.163.130!3306!nagios!nagios!slow-queriesnotifications_enabled 1};監控MySQL鎖表情況define service{use generic-service ; Name of service template to usehost_name mysql-master-1service_description check_mysql_table_lock_contentioncheck_command check_mysql_health!192.168.163.130!3306!nagios!nagios!table-lock-contentionnotifications_enabled 1}
如果web面板可能提示錯誤:
CRITICAL – statefilesdir /var/tmp/check_mysql_health does not exist or is not writable
可能和許可權關係或者/var/temp下沒有這個check_mysql_health
查看下check_mysql_health的許可權所屬。
[[email protected] ~]# ll /usr/local/nagios/libexec/check_mysql_health -rwxr-xr-x 1 root root 122024 10月 9 15:57 /usr/local/nagios/libexec/check_mysql_health
外掛程式的許可權應該所屬nagios使用者和組,因此修改之。
chown nagios:nagios /usr/local/nagios/libexec/check_mysql_health
軟連結一個
ln -s /usr/local/nagios/libexec/check_mysql_health /var/tmp/check_mysql_health
本文出自 “從心開始” 部落格,請務必保留此出處http://hao360.blog.51cto.com/5820068/1587163
nagios監控被監控主機上的應用服務mysql資料庫