監控MySQL有兩種方法,一種是經過官方的check_mysql插件,另外一種是經過第三方的check_mysql_health的插件。check_mysql_health的功能更爲強大一些,這裏我兩種都簡單的說下。mysql
監控mysql可能在nagios服務器上沒有官方的mysql的檢測的插件。ios
須要先 yum install mysql-devel
而後在從新編譯安裝一次nagios-plugins插件web
查看如今已經有了sql
[root@c65mini ~]# ll /usr/local/nagios/libexec/check_mysql -rwxr-xr-x 1 nagios nagios 190768 10月 8 14:02 /usr/local/nagios/libexec/check_mysql
登錄進命令行模式vim
mysql -uroot -p
在被監控的遠程服務器上(mysql服務器上)要先建立一個能夠遠程登錄mysql服務器的帳號服務器
GRANT ALL PRIVILEGES ON *.* TO 'nagios'@'192.168.163.129' IDENTIFIED BY 'nagios';
查看用戶已經有了nagios用戶ide
select user,host from mysql.user;
在監控主機上添加命令,須要修改commands.cfg測試
vim /usr/local/nagios/etc/objects/commands.cfg #check mysql define command{ command_name check_mysql command_line $USER1$/check_mysql -H $HOSTADDRESS$ -P $ARG1$ -u $ARG2$ -p $ARG3$ }
在監控主機上添加監控的服務spa
define service{ use generic-service,srv-pnp host_name mysql-master-1 service_description MYSQL check_command check_mysql!3306!nagios!nagios notifications_enabled 1 }
測試下nagios的配置是否有錯誤插件
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
沒問題重啓nagios服務
/etc/init.d/nagios restart
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.gz tar zxvf check_mysql_health-2.1.8.2.tar.gz cd check_mysql_health-2.1.8.2 ./configure make make 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.cfg define 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 use host_name mysql-master-1 service_description check_mysql_connection_time check_command check_mysql_health!192.168.163.130!3306!nagios!nagios!connection-time notifications_enabled 1 } ;監控MySQL鏈接數 define service{ use generic-service ; Name of service template to use host_name mysql-master-1 service_description check_mysql_threads_connected check_command check_mysql_health!192.168.163.130!3306!nagios!nagios!threads-connected notifications_enabled 1 } ;監控MySQL慢查詢狀況 define service{ use generic-service ; Name of service template to use host_name mysql-master-1 service_description check_mysql_slow_queries check_command check_mysql_health!192.168.163.130!3306!nagios!nagios!slow-queries notifications_enabled 1 } ;監控MySQL鎖表狀況 define service{ use generic-service ; Name of service template to use host_name mysql-master-1 service_description check_mysql_table_lock_contention check_command check_mysql_health!192.168.163.130!3306!nagios!nagios!table-lock-contention notifications_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的權限所屬。
[root@c65mini ~]# 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