監控MySQL有兩種方法,一種是通過官方的check_mysql插件,另一種是通過第三方的check_mysql_health的插件。check_mysql_health的功能更爲強大一些,這裏我兩種都簡單的說下。
第一種方法:使用nagios-plugins 官方自帶的chech_mysql插件
監控mysql可能在nagios服務器上沒有官方的mysql的檢測的插件。
需要先 yum install mysql-devel
然後在重新編譯安裝一次nagios-plugins插件
查看現在已經有了
[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
登陸進命令行模式
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 mysql define command{ command_name check_mysql command_line $USER1$/check_mysql -H $HOSTADDRESS$ -P $ARG1$ -u $ARG2$ -p $ARG3$ }
在監控主機上添加監控的服務
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插件比起官方的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.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