监控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