Nagios 监控mysql服务器详细实现过程

1、NRPE介绍

NRPE是Nagios的一个功能扩展,它可在远程Linux/Unix主机上执行插件程序。通过在远程服务器上安装NRPE插件及Nagios插件程序来向Nagios监控平台提供该服务器的本地情况,如CPU负载,内存使用,磁盘使用等。这里将Nagios监控端称为Nagios服务器端,而将远程被监控的主机称为Nagios客户端。

Nagios监控远程主机的方法有多种,其方式包括SNMP,NRPE,SSH,NCSA等。这里介绍其通过NRPE监控远程Linux主机的方式。

NRPE(Nagios Remote Plugin Executor)是用于在远端服务器上运行监测命令的守护进程,它用于让Nagios监控端基于安装的方式触发远端主机上的检测命令,并将检测结果返回给监控端。而其执行的开销远低于基于SSH的检测方式,而且检测过程不需要远程主机上的系统账号信息,其安全性也高于SSH的检测方式。

 

2、NRPE的工作原理

NRPE有两部分组成

check_nrpe插件:位于监控主机上
 

nrpe daemon:运行在远程主机上,通常是被监控端agent

注意:nrpe daemon需要Nagios-plugins插件的支持,否则daemon不能做任何监控

当Nagios需要监控某个远程Linux主机的服务或者资源情况时:

首先:Nagios会运行check_nrpe这个插件,告诉它要检查什么;

其次:check_nrpe插件会连接到远程的NRPE daemon,所用的方式是SSL;

然后:NRPE daemon 会运行相应的Nagios插件来执行检查;

最后:NRPE daemon 将检查的结果返回给check_nrpe 插件,插件将其递交给nagios做处理。

 

<版权所有,文章允许转载,但必须以链接方式注明源地址,否则追究法律责任!> 
原博客地址:    http://blog.csdn.net/mchdba/article/details/46666229
原作者:黄杉 (mchdba)

 

3、被监控端安装Nagios-plugins插件和NRPE

去mysql客户端安装nrpe客户端服务

3.1、添加nagios用户

[root@localhost ~]# useradd -s/sbin/nologin nagios 

 

3.2,安装nagios插件

[root@localhost ~]# yum -yinstall gcc gcc-c++ make openssl openssl-devel

3.3,安装nrpe

tar -xvf nrpe-2.15.tar.gz

cd nrpe-2.15

./configure--with-nrpe-user=nagios --with-nrpe-group=nagios --with-nagios-user=nagios--with-nagios-group=nagios --enable-command-args --enable-ssl  

make all

make install-plugin

make install-daemon  

make install-daemon-config

 

3.4,去检查nrpe配置

grep -v '^#' /usr/local/nagios/etc/nrpe.cfg |sed '/^$/d'

[root@localhost ~]# grep -v '^#' /usr/local/nagios/etc/nrpe.cfg|sed '/^$/d'

log_facility=daemon

pid_file=/var/run/nrpe.pid

server_port=5666

nrpe_user=nagios

nrpe_group=nagios

allowed_hosts=127.0.0.1,192.168.121.211

dont_blame_nrpe=0

allow_bash_command_substitution=0

debug=0

command_timeout=60

connection_timeout=300

command[check_users]=/usr/local/nagios/libexec/check_users-w 8 -c 12

command[check_load]=/usr/local/nagios/libexec/check_load -w15,10,5 -c 30,25,20

command[check_sda1]=/usr/local/nagios/libexec/check_disk -w20% -c 10% -p /dev/sda3

command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs-w 5 -c 10 -s Z

command[check_total_procs]=/usr/local/nagios/libexec/check_procs-w 750 -c 800

command[check-host-alive]=/usr/local/nagios/libexec/check_ping-H 10.254.3.72 -w 3000.0,80% -c 5000.0,100% -p 5

command[check_mysql_status]=/usr/local/nagios/libexec/check_mysql-unagios -P3306 -s /usr/local/mysql/mysql.sock -Hlocalhost--password='nagiosq@0625' -d test -w 60 -c 100

[root@localhost ~]#

 

3.5,启动nrpe

/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe

 

 

4,制作启动脚本:

[root@localhost bin]# cat /etc/init.d/nrped

 

#chkconfig: 2345 80 90

#description:auto_run

 

NRPE=/usr/local/nagios/bin/nrpe 

NRPECONF=/usr/local/nagios/etc/nrpe.cfg  

   

case "$1" in

   start)  

       echo -n "Starting NRPE daemon..."

       $NRPE -c $NRPECONF -d  

       echo " done."

       ;;  

   stop)  

       echo -n "Stopping NRPE daemon..."

       pkill -u nagios nrpe  

       echo " done."

   ;;  

   restart)  

       $0 stop  

       sleep 2  

       $0 start  

       ;;  

   *)  

       echo "Usage: $0 start|stop|restart"

       ;;  

   esac 

exit 0  

[root@localhost bin]#

 

 

5,设置启动

chmod +x /etc/init.d/nrped  

chkconfig --add nrped 

chkconfig nrped on

 

[root@localhost bin]# ps -eaf|grep nrpe

nagios  30440     1  0 23:48 ?        00:00:00 /usr/local/nagios/bin/nrpe -c/usr/local/nagios/etc/nrpe.cfg -d

root    30442  3292  0 23:48 pts/0    00:00:00 grep nrpe

[root@localhost bin]#

[root@localhost bin]# netstat -tnlp |grepnrpe

tcp       0      0 0.0.0.0:5666                0.0.0.0:*                   LISTEN      30440/nrpe         

tcp       0      0 :::5666                     :::*                        LISTEN      30440/nrpe         

[root@localhost bin]#

 

 

6,监控端安装nrpe

tar xf nrpe-2.15.tar.gz

cd nrpe-2.15  

./configure --with-nrpe-user=nagios --with-nrpe-group=nagios --with-nagios-user=nagios --with-nagios-group=nagios  --enable-command-args --enable-ssl

make all

make install-plugin 

 

#安装完成后,会在Nagios安装目录的libexec下生成check_nrpe的插件,如下所示:

[root@test_db2 nagios]#  ll /usr/local/nagios/libexec/check_nrpe

-rwxrwxr-x 1 nagios nagios 76777 Jun 2523:53 /usr/local/nagios/libexec/check_nrpe

[root@test_db2 nagios]#

 

检测报错:

[root@test_db2 libexec]# ./check_nrpe -H192.168.121.210

CHECK_NRPE: Error - Could not complete SSLhandshake.

[root@test_db2 libexec]#

 

去客户端在allowed_hosts中添加监控端IP地址

[root@localhost ~]# vim/usr/local/nagios/etc/nrpe.cfg

allowed_hosts=127.0.0.1,192.168.121.211

 

 

在mysql服务器上,报错:

[root@localhost ~]#/usr/local/nagios/libexec/check_mysql -unagios -P3306 -S -s/usr/local/mysql/mysql.sock -Hlocalhost --password='nagiosq@0512' -d test -w 60-c 100

/usr/local/nagios/libexec/check_mysql:error while loading shared libraries: libmysqlclient.so.18: cannot open sharedobject file: No such file or directory

[root@localhost ~]#

 

[root@localhost ~]# find / -namelibmysqlclient.so.18

/usr/local/mysql/lib/libmysqlclient.so.18

/root/mysql/mysql-5.6.12/libmysql/libmysqlclient.so.18

/root/mysql-5.6.12/libmysql/libmysqlclient.so.18

[root@localhost ~]# ln -s/usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18

[root@localhost ~]#

 

添加mysql账号:

mysql> GRANT PROCESS, SUPER, REPLICATIONCLIENT ON *.* TO 'nagios'@'localhost' IDENTIFIED BY 'nagiosq@0625';

Query OK, 0 rows affected (0.05 sec)

 

mysql>

 

客户端自己检测:

[root@localhost ~]#/usr/local/nagios/libexec/check_mysql -unagios -P3306  -s /usr/local/mysql/mysql.sock -Hlocalhost--password='nagiosq@0625' -d test -w 60 -c 100

Uptime: 1823238  Threads: 6 Questions: 684495  Slow queries:0  Opens: 124  Flush tables: 1  Open tables: 116  Queries per second avg:0.375|Connections=3116c;;; Open_files=19;;; Open_tables=116;;; Qcache_free_memory=61538880;;;Qcache_hits=176271c;;; Qcache_inserts=41370c;;; Qcache_lowmem_prunes=0c;;;Qcache_not_cached=331835c;;; Qcache_queries_in_cache=3373;;; Queries=684496c;;;Questions=681384c;;; Table_locks_waited=0c;;; Threads_connected=5;;;Threads_running=2;;; Uptime=1823238c;;;

[root@localhost ~]#

 

7,完善配置文件

7.1在command.cfg里面添加监控command命令

将servers.cfg添加进nagios.cfg里面,将command.cfg里面的check_host_alive以及check_mysql_status补充好,然后重启nagios

# add by timman on 20150512

define command{

       command_name    check_mysql_status

       command_line    $USER1$/check_mysql_status-w $ARG1$ -c $ARG2$

       }

 

# 'check_nrpe' command definition,add bytimman on 20140508

define command{

       command_name    check_nrpe

       command_line    $USER1$/check_nrpe-H $HOSTADDRESS$ -c $ARG1$

       }

 

define command{

       command_name    check_host_alive

       command_line    $USER1$/check_ping-H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 5

       }

 

7.2在servers.cfg里面添加check_mysql_status、 check_host_alive等服务监控项。

[root@test_db2 etc]# more servers.cfg

#  servicedefinition

 

define service{

         host_name               cactitest

       service_description     check_load

       check_command          check_nrpe!check_load

       max_check_attempts      5

       normal_check_interval   3

       retry_check_interval    2

       check_period            24x7

       notification_interval   10

       notification_period     24x7

       notification_options    w,u,c,r

       contact_groups          dba

       }

 

define service{

       host_name               cactitest

       service_description    check_host_alive

       check_command          check_host_alive

       max_check_attempts      5

       normal_check_interval   3

       retry_check_interval    2

       check_period            24x7

       notification_interval   10

       notification_period     24x7

       notification_options    w,u,c,r

       contact_groups          dba

       }

 

define service{

       host_name               cactitest

       service_description     Check Disksda1

       check_command          check_nrpe!check_sda1

       max_check_attempts      5

       normal_check_interval   3

       retry_check_interval    2

       check_period            24x7

       notification_interval   10

       notification_period     24x7

       notification_options    w,u,c,r

       contact_groups          dba

       }

 

define service{

       host_name               cactitest

       service_description     TotalProcesses

       check_command           check_nrpe!check_total_procs

       max_check_attempts      5

       normal_check_interval   3

       retry_check_interval    2

       check_period            24x7

       notification_interval   10

       notification_period     24x7

       notification_options    w,u,c,r

       contact_groups          dba

       }

 

define service{

       host_name               cactitest

       service_description     CurrentUsers

       check_command          check_nrpe!check_users

       max_check_attempts      5

       normal_check_interval   3

       retry_check_interval    2

       check_period            24x7

       notification_interval   10

       notification_period     24x7

       notification_options    w,u,c,r

       contact_groups          dba

       }

 

 

define service{

       host_name               cactitest

       service_description     CheckZombie Procs

       check_command          check_nrpe!check_zombie_procs

       max_check_attempts      5

       normal_check_interval   3

       retry_check_interval    2

       check_period            24x7

       notification_interval   10

       notification_period     24x7

       notification_options    w,u,c,r

       contact_groups          dba

       }

 

 

define service{

       host_name               cactitest

       service_description     CheckMysql Status

       check_command          check_nrpe!check_mysql_status

       max_check_attempts      5

       normal_check_interval   3

       retry_check_interval    2

       check_period            24x7

       notification_interval   10

       notification_period     24x7

       notification_options    w,u,c,r

       contact_groups          dba

       }

 

 

 

7.3 在nagios.cfg加载servers.cfg等配置文件

在nagios.cfg里面配置

cfg_file=/usr/local/nagios/etc/servers.cfg

 

同时添加新的主机或者服务配置cfg文件,都需要在nagios.cfg里面配置一下,然后重启nagios才能生效,如下所示:

[root@test_db2 nagios]# grep -v '^#'/usr/local/nagios/etc/nagios.cfg |sed '/^$/d' |grep cfg_file

cfg_file=/usr/local/nagios/etc/objects/commands.cfg

cfg_file=/usr/local/nagios/etc/objects/contacts.cfg

cfg_file=/usr/local/nagios/etc/contactgroups.cfg

cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg

cfg_file=/usr/local/nagios/etc/objects/templates.cfg

cfg_file=/usr/local/nagios/etc/hosts.cfg

cfg_file=/usr/local/nagios/etc/servers.cfg

cfg_file=/usr/local/nagios/etc/hostgroups.cfg

cfg_file=/usr/local/nagios/etc/objects/localhost.cfg

[root@test_db2 nagios]#

 

 

8,之后在页面就可以看到监控效果了

 

 Nagios 监控mysql服务器详细实现过程

 

 

参考文章:http://os.51cto.com/art/201409/452605.htm

 

上一篇:linux-为什么用Nagios调用ps时,ps在Perl脚本中仅返回一行输出?


下一篇:Nagios通过check_http监控一台web应用服务器上多个tomcat服务