Nagios 利用check_mysql 插件监控MySQL

作为一名运维人员,对于mysql我们并不陌生。我们如何能得知mysql运行状态呢?不要忘记我们有监控之神 nagios,我们可以通过 nagios 来监控mysql的运行状态。那如何来实现nagios 监控 mysql 运行状态呢?

建立nagios专用数据库

# mysql -u root -p
mysql> create database nagios;

建立nagios专用用户

mysql> grant select on nagios.* to nagios@'%' identified by 'nagios';
mysql> flush privileges;

查询用户

mysql> select user,host from mysql.user;

验证命令

# /usr/lib64/nagios/plugins/check_mysql -u nagios -p nagios -d nagios
Uptime: 502405 Threads: 2 Questions: 1417 Slow queries: 0 Opens: 540 Flush tables: 1 Open tables: 41 Queries per second avg: 0.002

在command.cfg里增加插件命令

# 'check_mysql' command definition
define command {
command_name check_mysql
command_line $USER1$/check_mysql -H $HOSTADDRESS$ -P $ARG1$ -u $ARG2$ -p $ARG3$ -d $ARG4$
}

在localhost.cfg里定义服务

define service{
use local-service ; Name of service template to use
host_name localhost
service_description MySQL
check_command check_mysql!3306!nagios!nagios!nagios
}

监控远程mysql状态

# vim /etc/nagios/nrpe.cfg

添加如下一行:

command[check_mysql]=/usr/lib64/nagios/plugins/check_mysql -P 3306 -u nagios -p nagios -d nagios

在远程主机服务配置文件里面添加如下

define service{
use local-service ; Name of service template to use
host_name mysql
service_description MySQL
check_command check_nrpe!check_mysql
}