Nagios之MySQL单机与主从监控

本章节所有的服务器系统均为CentOS release 6.4,其他信息如下:

  • 监控端IP:172.16.56.131,主机名:monitors,nagios-core、nagios-plugins和NRPE已经安装配置完毕;
  • 被监控端MySQL主库IP:192.183.3.145,主机名:kk,MySQL版本为5.6.32,已经安装并配置好了nagios-plugins和NRPE;
  • 被监控端MySQL备库IP:172.16.56.129,主机名:nn,MySQL版本为5.6.30。

1.Nagios之MySQL单机监控

关于MySQL监控的插件可以参详:https://exchange.nagios.org/directory/Plugins/Databases/MySQL。本文介绍监控MySQL的两种方法:一种是通过Nagios官方的check_mysql插件(还有第三方类似的check_mysqld插件,据介绍拥有check_mysql及其他功能),另一种第三方的check_mysql_health插件。

1.1.check_mysql

参考https://github.com/nagios-plugins/nagios-plugins上的REQUIREMENTS说明(https://github.com/nagios-plugins/nagios-plugins/blob/master/REQUIREMENTS),check_mysql、check_mysql_query插件使用要求MySQL库可用,可以从http://dev.mysql.com/downloads/下载对应lib库:libmysql、libmysqlclient;如果监控系统有Redhat源可以yum安装mysql-devel和mysql-libs,另外必须在PATH中指定mysql_config或编译时用--with-mysql=DIR指定DIR/bin/mysql_config。

因为在这之前,nagios监控端已经安装了nagios-plugins,此时libexec目录并无check_mysql:

[root@monitors ~]#ll /usr/local/nagios/libexec/check_mysql*
ls: 无法访问/usr/local/nagios/libexec/check_mysql*: 没有那个文件或目录

下面重装nagios-plugins试试,关于它的包含的插件官方介绍参详http://nagios-plugins.org/doc/man/index.html。

1.监控端重装nagios-plugins

[root@monitors tmp]# cd /home/nagios/
[root@monitors nagios]# yum -y install mysql-devel mysql-libs
[root@monitors nagios]# wget http://nagios-plugins.org/download/nagios-plugins-2.1.2.tar.gz
[root@monitors nagios]# tar zxvf nagios-plugins-2.1.2.tar.gz
[root@monitors nagios]# cd nagios-plugins-2.1.2
[root@monitors nagios-plugins-2.1.2]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
            --with-apt-get-command: 
              --with-ping6-command: /bin/ping6 -n -U -w %d -c %d %s
               --with-ping-command: /bin/ping -n -U -w %d -c %d %s
                       --with-ipv6: yes
                      --with-mysql: /usr/bin/mysql_config
                    --with-openssl: yes
                     --with-gnutls: no
               --enable-extra-opts: yes
                       --with-perl: /usr/bin/perl
             --enable-perl-modules: no
                     --with-cgiurl: /nagios/cgi-bin
               --with-trusted-path: /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
                   --enable-libtap: no
[root@monitors nagios-plugins-2.1.2]# make && make install
make[1]: Leaving directory `/home/nagios/nagios-plugins-2.1.2/po'
make[1]: Entering directory `/home/nagios/nagios-plugins-2.1.2'
make[2]: Entering directory `/home/nagios/nagios-plugins-2.1.2'
make[2]: Nothing to be done for `install-exec-am'.
make[2]: Nothing to be done for `install-data-am'.
make[2]: Leaving directory `/home/nagios/nagios-plugins-2.1.2'
make[1]: Leaving directory `/home/nagios/nagios-plugins-2.1.2'
[root@monitors nagios-plugins-2.1.2]# ll /usr/local/nagios/libexec/check_mysql*
-rwxr-xr-x 1 nagios nagios 193142 2月  22 22:23 /usr/local/nagios/libexec/check_mysql
-rwxr-xr-x 1 nagios nagios 179988 2月  22 22:23 /usr/local/nagios/libexec/check_mysql_query
1)查看重装效果

[root@monitors nagios-plugins-2.1.2]# /usr/local/nagios/libexec/check_mysql -H 192.183.3.145 -unagios -dnntest -p123456
Uptime: 274340  Threads: 2  Questions: 4005  Slow queries: 0  Opens: 70  Flush tables: 1  Open tables: 63  Queries per second avg: 0.014|Connections=914c;;; Open_files=19;;; Open_tables=63;;; Qcache_free_memory=1031352;;; Qcache_hits=0c;;; Qcache_inserts=0c;;; Qcache_lowmem_prunes=0c;;; Qcache_not_cached=3c;;; Qcache_queries_in_cache=0;;; Queries=4006c;;; Questions=4004c;;; Table_locks_waited=0c;;; Threads_connected=2;;; Threads_running=2;;; Uptime=274340c;;;

2)监控配置

先看一下check_mysql命令参数的含义,都比较简单,就不详述了:

[root@monitors objects]# /usr/local/nagios/libexec/check_mysql -h
check_mysql v2.1.2 (nagios-plugins 2.1.2)
Copyright (c) 1999-2014 Nagios Plugin Development Team
<devel@nagios-plugins.org>
This program tests connections to a MySQL server
Usage:
check_mysql [-d database] [-H host] [-P port] [-s socket]
[-u user] [-p password] [-S] [-l] [-a cert] [-k key]
[-C ca-cert] [-D ca-dir] [-L ciphers] [-f optfile] [-g group]
Options:
-h, --help
Print detailed help screen
-V, --version
Print version information
--extra-opts=[section][@file]
Read options from an ini file. See
https://www.nagios-plugins.org/doc/extra-opts.html
for usage and examples.
-H, --hostname=ADDRESS
Host name, IP Address, or unix socket (must be an absolute path)
-P, --port=INTEGER
Port number (default: 3306)
-n, --ignore-auth
Ignore authentication failure and check for mysql connectivity only
-s, --socket=STRING
Use the specified socket (has no effect if -H is used)
-d, --database=STRING
Check database with indicated name
-f, --file=STRING
Read from the specified client options file
-g, --group=STRING
Use a client options group
-u, --username=STRING
Connect using the indicated username
-p, --password=STRING
Use the indicated password to authenticate the connection
==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==
Your clear-text password could be visible as a process table entry
-S, --check-slave
Check if the slave thread is running properly.
-w, --warning
Exit with WARNING status if slave server is more than INTEGER seconds
behind master
-c, --critical
Exit with CRITICAL status if slave server is more then INTEGER seconds
behind master
-l, --ssl
Use ssl encryptation
-C, --ca-cert=STRING
Path to CA signing the cert
-a, --cert=STRING
Path to SSL certificate
-k, --key=STRING
Path to private SSL key
-D, --ca-dir=STRING
Path to CA directory
-L, --ciphers=STRING
List of valid SSL ciphers
There are no required arguments. By default, the local database is checked
using the default unix socket. You can force TCP on localhost by using an
IP address or FQDN ('localhost' will use the socket as well).
Notes:
You must specify -p with an empty string to force an empty password,
overriding any my.cnf settings.
Send email to help@nagios-plugins.org if you have questions regarding use of this software. To submit patches or suggest improvements, send email to devel@nagios-plugins.org

这里就简单配置一个命令模板:

[root@monitors objects]# vi commands.cfg 
#check_mysql command definition
define command{
        command_name check_mysql
        command_line $USER1$/check_mysql -H $HOSTADDRESS$ -unagios -p123456
}
[root@monitors objects]# vi mysqlservices.cfg 
# check_mysql service definition
define service{
use local-service
host_name kk
service_description check_mysql
check_command check_mysql
}
3 )检查配置并重启nagios

[root@monitors objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Nagios Core 4.2.0
Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 08-01-2016
License: GPL
Website: https://www.nagios.org
Reading configuration data...
   Read main config file okay...
Warning: Duplicate definition found for service 'Swap Usage' on host 'kk' (config file '/usr/local/nagios/etc/objects/publinuxserver.cfg', starting on line 75)
Warning: Duplicate definition found for service 'Current Users' on host 'kk' (config file '/usr/local/nagios/etc/objects/publinuxserver.cfg', starting on line 38)
   Read object config files okay...
Running pre-flight check on configuration data...
Checking objects...
	Checked 22 services.
	Checked 2 hosts.
	Checked 2 host groups.
	Checked 0 service groups.
	Checked 2 contacts.
	Checked 1 contact groups.
	Checked 27 commands.
	Checked 5 time periods.
	Checked 0 host escalations.
	Checked 0 service escalations.
Checking for circular paths...
	Checked 2 hosts
	Checked 0 service dependencies
	Checked 0 host dependencies
	Checked 5 timeperiods
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...
Total Warnings: 0
Total Errors:   0
Things look okay - No serious problems were detected during the pre-flight check
重启nagios:
[root@monitors objects]# service nagios restart
Running configuration check...
Stopping nagios: done.
Starting nagios: done.

打开nagios的web界面,check_mysql监控项在其中:


1.2.check_mysql_health

1.2.1.check_mysql_health简介

相较于check_mysql,check_mysql_health的功能更为强大一些,不但能监控MySQL是否正常运行,还能监控MySQL连接数情况、MySQL慢查询等多种监控指标。

check_mysql_health是一个用于检查MySQL数据库的各种参数的插件,监控项包括登录时间,索引使用,缓冲池命中率,查询缓存命中率,慢查询,磁盘上的临时表,表缓存命中率,连接的线程等等,可参详https://labs.consol.de/nagios/check_mysql_health/index.html。

1.命令行参数

  • --Hostname:数据库服务器地址
  • --Username:数据库用户
  • --Password:数据库用户密码
  • --database:连接的数据库名,默认值:information_schema 
  • --Mode:监控项
  • --Name:目前仅限于sql
  • --Name2:如果使用--mode=sql,输出结果中将有SQL语句与sql运行结果;
  • --Warning:触发warning警告的阈值;
  • --critical:触发critical警告的阈值;
  • --environment:传递环境参数到脚本中;
  • --method:连接数据库的方式
  • --units <%|KB|MB|GB>:用于--mode=sql的输出美化

2.监测项

Keyword

Description

Range

connection-time

连接到数据库服务器的时间

0..n Seconds (1, 5)

uptime

MySQL服务运行的时间

0..n Seconds (10:, 5: Minutes)

threads-connected

数据库服务器当前打开的连接数

1..n (10, 20)

threadcache-hitrate

缓存的线程命中率

0%..100% (90:, 80:)

threads-created

每秒创建的线程数

 

threads-running

当前运行的线程数

 

threads-cached

当前缓存的线程数

 

connects-aborted

每秒连接失败的连接请求

 

clients-aborted

每秒客户端导致连接失败的连接数量

 

q[uery]cache-hitrate

查询缓存的命中率

0%..100% (90:, 80:)

q[uery]cache-lowmem-prunes

从查询缓存中清除出去的查询语句量

n/sec (1, 10)

[myisam-]keycache-hitrate

Myisam引擎对象索引的缓存命中率

0%..100% (99:, 95:)

[innodb-]bufferpool-hitrate

InnoDB引擎对象的缓存池命中率

0%..100% (99:, 95:)

[innodb-]bufferpool-wait-free

InnoDB缓冲池可用的待清理页

0..n/sec (1, 10)

[innodb-]log-waits

因log buffer过小导致的InnoDB日志等待比率

0..n/sec (1, 10)

tablecache-hitrate

表缓存命中率

0%..100% (99:, 95:)

table-lock-contention

表锁争用比率

0%..100% (1, 2)

index-usage

索引利用率(与全表扫描相反)

0%..100% (90:, 80:)

table-fragmentation

需要分析的表对象

 

tmp-disk-tables

在磁盘上创建临时表的比率

0%..100% (25, 50)

slow-queries

每秒触发的慢查询数量

0..n/sec (0.1, 1)

long-running-procs

运行时间超过1分钟的会话数总和

0..n (10, 20)

slave-lag

从库与主库之间的延迟

0..n Seconds

slave-io-running

检查从库DB的IO线程是否在运行

 

slave-sql-running

检查从库DB的SQL线程是否在运行

 

sql

根据执行的SQL进行输出,与参数--name和--name2联合使用。

0..n

open-files

已打开文件数/可打开文件数量总和

0%..100% (80, 95)

encode

读取标准输入(STDIN)并输出编码字符串

 

cluster-ndb-running

检查所有群集节点是否正在运行

 

其中:Query-Cache=Qcache_hits/(Qcache_hits+Com_select).

3.连接字符串

使用--username和--password连接到数据库,加上--hostname、--socket、--port参数可以更为精确的指定数据库服务器。

如果在环境变量中提供相应的连接参数,可以忽略--hostname,--username和--password以及--socket和--port。从版本3.x开始,可以通过自己的属性(自定义对象变量)扩展Nagios中的服务定义,这些将在环境检查命令执行时呈现,这些环境变量包括:

  • NAGIOS__SERVICEMYSQL_HOST(_mysql_host)
  • NAGIOS__SERVICEMYSQL_USER(_mysql_user)
  • NAGIOS__SERVICEMYSQL_PASS(_mysql_pass)
  • NAGIOS__SERVICEMYSQL_PORT(_mysql_port)
  • NAGIOS__SERVICEMYSQL_SOCK(_mysql_sock)

4.安装

编译安装选项:

  • --prefix=BASEDIRECTORY,指定应存储check_mysql_health的目录,(默认值:/usr/local/nagios);
  • --with-nagios-user=SOMEUSER,此用户将是check_mysql_health文件的所有者(默认值:nagios);
  • --with-nagios-group=SOMEGROUP,check_mysql_health插件的组,(默认值:nagios);
  • --with-perl=PATHTOPERL,指定要使用的perl解释器的路径,(默认值:PATH中的perl)。

1.2.2.check_mysql_health安装与配置

通过check_mysql、check_mysql_health监控mysql,都需要在被监控的远程服务器上(mysql服务器上)要先创建一个可以远程登陆mysql服务器的账号。

1.数据库创建nagios用户

[root@kk /]#mysql -uroot -S /var/lib/mysql/mysql.sock -P 3306 -p
mysql> use mysql;
mysql> grant select on *.* to nagios@'%' identified by '123456';
或者:grant usage on *.* to 'nagios'@'nagiosserver' identified by 'nagiospassword'
mysql> select User,Password,Host from user;
mysql> select User,Password,Host from mysql.user;
mysql> flush privileges;

2.安装check_mysql_health

check_mysql_health官方介绍主页:http://labs.consol.de/nagios/check_mysql_health/,最新版本的check_mysql_health-2.2.2,下载地址:https://labs.consol.de/assets/downloads/nagios/check_mysql_health-2.2.2。

[root@monitors ~]# cd /home/nagios/
[root@monitors nagios]# wget https://labs.consol.de/assets/downloads/nagios/check_mysql_health-2.2.2.tar.gz
[root@monitors nagios]# tar zxvf check_mysql_health-2.2.2.tar.gz 
[root@monitors nagios]# cd check_mysql_health-2.2.2
[root@monitors check_mysql_health-2.2.2]# ./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios --with-perl --with-statefiles-dir=/tmp

[root@monitors check_mysql_health-2.2.2]#  make && make install
[root@monitors check_mysql_health-2.2.2]#cd /usr/local/nagios/libexec/
[root@monitors libexec]# ./check_mysql_health 

因为check_mysql_health是用perl写的,将check_mysql_health的第一行由原来的#!yes -w修改为#!/usr/bin/perl -w。

[root@monitors libexec]# vim check_mysql_health


[root@monitors libexec]# ./check_mysql_health -h


解决方法:

[root@monitors libexec]# yum -y install perl-Time-HiRes

再次执行:



[root@monitors libexec]# ./check_mysql_health --hostname 192.183.3.145 --port 3306 -username nagios --password 123456 --mode connection-time

出现Can't locate DBI.pm的错误时:是由于没有安装perl DBD-MySQL驱动,可以直接用yum安装yum install perl-DBI perl-DBD-MySQL -y,也可以利用源码安装perl DBD-MySQL驱动,http://www.cpan.org/modules/by-module/DBD/找到最新的版本,现在最新的为DBD-mysql-4.036.tar.gz

[root@monitors libexec]# yum install perl-DBI perl-DBD-MySQL -y

再次执行:

[root@monitors libexec]# ./check_mysql_health --hostname 192.183.3.145 --port 3306 -username nagios --password 123456 --mode connection-time --warning 0.001 --critical 0.0011


3.配置监控项

将check_mysql_health的监控项定义到nagios监控系统组,以便自动输出监控信息和状态报告。

1)配置nagios命令行文件

[root@monitors nagios]# vi etc/objects/commands.cfg
# check_mysql_health command definition
define command{
command_name check_mysql_health
command_line $USER1$/check_mysql_health --hostname $ARG1$ --port $ARG2$ --username $ARG3$ --password $ARG4$ --mode $ARG5$
}
2)配置监控服务项

[root@monitors nagios]# vi etc/objects/mysqlservices.cfg
define service{
use local-service
host_name kk
service_description connection-time
check_command check_mysql_health!192.183.3.145!3306!nagios!123456!connection-time
}
define service{
use local-service
host_name kk
service_description uptime
check_command check_mysql_health!192.183.3.145!3306!nagios!123456!uptime
}

2)添加服务文件到nagios配置文件

[root@monitors etc]# vi nagios.cfg 
#mysql-service
cfg_file=/usr/local/nagios/etc/objects/mysqlservices.cfg

3)重启nagios服务

[root@monitors etc]# service nagios restart
Running configuration check...
Stopping nagios: done.
Starting nagios: done.
[root@monitors etc]# service httpd restart
Stopping httpd:                                      [  OK  ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName                                           [  OK  ]

打开nagios管理界面,可见新配置的connection-time、uptime监控项




2.MySQL主从监控

对于MySQL主从的情况,我们想了解的情况主要有3个:second_behind_master、slave_io_running、slave_sql_running,实现该监控功能有2个方面,一是MySQL自带的check_mysql插件,另外就是第三方编写的plugins,比如check_mysql_slave、check_mysql_replication和check_replication_slave等(参详https://exchange.nagios.org/directory/Plugins/Databases/MySQL),原理都是通过执行“show slave stutas\G”来获取备库的状态,本文就简单介绍通过check_mysql、check_mysql_health来实现主从监控。

2.1.check_mysql监控MySQL主从

2.1.1.MySQL备库安装配置nagios-plugins和NRPE

1.安装前准备

[root@nn ~]#useradd nagios
[root@nn ~]# yum -y install mysql-devel mysql-libs

显示fail,因为yum源提供的事5.1版本的,与从库5.6的存在“conflicts with”问题,所以手动下载对应版本的mysql-devel、mysql-libs安装。

注意:5.6版本libmysql,libmysqlclient包含在MySQL-shared、MySQL-shared-compat中,安装这2个即可。

[root@nn ~]#cd /home/mysql/MySQL-5.6.30-1.linux_glibc2.5.x86_64.rpm-bundle/
[root@nn MySQL-5.6.30-1.linux_glibc2.5.x86_64.rpm-bundle]#rpm -ivh MySQL-devel-5.6.30-1.linux_glibc2.5.x86_64.rpm
warning: MySQL-devel-5.6.30-1.linux_glibc2.5.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
   1:MySQL-devel            ########################################### [100%]
[root@nn MySQL-5.6.30-1.linux_glibc2.5.x86_64.rpm-bundle]#rpm -ivh MySQL-shared-5.6.30-1.linux_glibc2.5.x86_64.rpm
warning: MySQL-shared-5.6.30-1.linux_glibc2.5.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
   1:MySQL-shared           ########################################### [100%]
[root@nn MySQL-5.6.30-1.linux_glibc2.5.x86_64.rpm-bundle]#rpm -ivh MySQL-shared-compat-5.6.30-1.linux_glibc2.5.x86_64.rpm 
warning: MySQL-shared-compat-5.6.30-1.linux_glibc2.5.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
   1:MySQL-shared-compat    ########################################### [100%]

2.下载并安装nagios-plugins:


[root@nn ~]#cd /home/nagios/
[root@nn nagios]#wget http://nagios-plugins.org/download/nagios-plugins-2.1.2.tar.gz
--2017-02-24 10:09:18--  http://nagios-plugins.org/download/nagios-plugins-2.1.2.tar.gz
正在解析主机 nagios-plugins.org... 72.14.186.43
正在连接 nagios-plugins.org|72.14.186.43|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:2695301 (2.6M) [application/x-gzip]
正在保存至: “nagios-plugins-2.1.2.tar.gz”
100%[==========================================================>] 2,695,301   1.05M/s   in 2.4s    
2017-02-24 10:09:21 (1.05 MB/s) - 已保存 “nagios-plugins-2.1.2.tar.gz” [2695301/2695301])
[root@nn nagios]#tar -xzf nagios-plugins-2.1.2.tar.gz 
[root@nn nagios]#cd nagios-plugins-2.1.2
[root@nn nagios-plugins-2.1.2]#./configure  --with-nagios-user=nagios --with-nagios-group=nagios
            --with-apt-get-command: 
              --with-ping6-command: /bin/ping6 -n -U -w %d -c %d %s
               --with-ping-command: /bin/ping -n -U -w %d -c %d %s
                       --with-ipv6: yes
                      --with-mysql: /usr/bin/mysql_config
                    --with-openssl: yes
                     --with-gnutls: no
               --enable-extra-opts: yes
                       --with-perl: /usr/bin/perl
             --enable-perl-modules: no
                     --with-cgiurl: /nagios/cgi-bin
               --with-trusted-path: /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
                   --enable-libtap: no
[root@nn nagios-plugins-2.1.2]#make&make install
make[1]: Leaving directory `/home/nagios/nagios-plugins-2.1.2/po'
make[1]: Entering directory `/home/nagios/nagios-plugins-2.1.2'
make[2]: Entering directory `/home/nagios/nagios-plugins-2.1.2'
make[2]: Nothing to be done for `install-exec-am'.
make[2]: Nothing to be done for `install-data-am'.
make[2]: Leaving directory `/home/nagios/nagios-plugins-2.1.2'
make[1]: Leaving directory `/home/nagios/nagios-plugins-2.1.2'
[1]+  Exit 2                  make
[root@nn nagios-plugins-2.1.2]#chown -R nagios:nagios /usr/local/nagios
[root@nn nagios-plugins-2.1.2]#ls /usr/local/nagios/libexec/check_mysql*
/usr/local/nagios/libexec/check_mysql  /usr/local/nagios/libexec/check_mysql_query

3.下载安装NRPE

[root@nn nagios-plugins-2.1.2]#cd /home/nagios/
[root@nn nagios]#tar zxf nrpe-3.0.1.tar.gz
[root@nn nagios]#cd nrpe-3.0.1
[root@nn nrpe-3.0.1]#yum -y install openssl openssl-devel
[root@nn nrpe-3.0.1]#./configure --with-nagios-user=nagios --with-nagios-group=nagios
*** Configuration summary for nrpe 3.0.1 09-08-2016 ***:
 General Options:
 -------------------------
 NRPE port:    5666
 NRPE user:    nagios
 NRPE group:   nagios
 Nagios user:  nagios
 Nagios group: nagios
Review the options above for accuracy.  If they look okay,
type 'make all' to compile the NRPE daemon and client
or type 'make' to get a list of make options.
[root@nn nrpe-3.0.1]#make all
cd ./src/; make
make[1]: Entering directory `/home/nagios/nrpe-3.0.1/src'
gcc -g -O2 -I/usr/include/krb5   -DHAVE_CONFIG_H -I ../include -I ./../include -o nrpe ./nrpe.c ./utils.c ./acl.c -lssl -lcrypto   -lnsl   
gcc -g -O2 -I/usr/include/krb5   -DHAVE_CONFIG_H -I ../include -I ./../include -o check_nrpe ./check_nrpe.c ./utils.c -lssl -lcrypto   -lnsl  
make[1]: Leaving directory `/home/nagios/nrpe-3.0.1/src'
*** Compile finished ***
You can now continue with the installation or upgrade process.
Read the PDF documentation (NRPE.pdf) for information on the next
steps you should take to complete the installation or upgrade.
[root@nn nrpe-3.0.1]#make install-plugin
cd ./src/; make install-plugin
make[1]: Entering directory `/home/nagios/nrpe-3.0.1/src'
/usr/bin/install -c -m 755 -d /usr/local/nagios/bin
/usr/bin/install -c -m 755 ../uninstall /usr/local/nagios/bin/nrpe-uninstall
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/libexec
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/libexec
/usr/bin/install -c -m 775 -o nagios -g nagios check_nrpe /usr/local/nagios/libexec
make[1]: Leaving directory `/home/nagios/nrpe-3.0.1/src'
[root@nn nrpe-3.0.1]#make install-daemon
cd ./src/; make install-daemon
make[1]: Entering directory `/home/nagios/nrpe-3.0.1/src'
/usr/bin/install -c -m 755 -d /usr/local/nagios/bin
/usr/bin/install -c -m 755 ../uninstall /usr/local/nagios/bin/nrpe-uninstall
/usr/bin/install -c -m 755 nrpe /usr/local/nagios/bin
/usr/bin/install -c -m 755 -o nagios -g nagios -d /usr/local/nagios/var
make[1]: Leaving directory `/home/nagios/nrpe-3.0.1/src'
[root@nn nrpe-3.0.1]#make install-config
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/etc
/usr/bin/install -c -m 644 -o nagios -g nagios sample-config/nrpe.cfg /usr/local/nagios/etc

4.配置nrpe

[root@nn nrpe-3.0.1]# vim /usr/local/nagios/etc/nrpe.cfg

修改allowed_hosts=192.183.3.145,172.16.56.131,172.16.56.129,允许Nagios服务器端访问;

在命令行测试如下的监测命令,这里根据自己的监测需求对命令进行修改:

/usr/local/nagios/libexec/check_nrpe -H localhost -c check_users
/usr/local/nagios/libexec/check_nrpe -H localhost -c check_load
/usr/local/nagios/libexec/check_nrpe -H localhost -c check_sda1
/usr/local/nagios/libexec/check_nrpe -H localhost -c check_total_procs
/usr/local/nagios/libexec/check_nrpe -H localhost -c check_zombie_procs

查看配置结果:

[root@nn nrpe-3.0.1]#grep -v '^#' /usr/local/nagios/etc/nrpe.cfg |sed '/^$/d'
log_facility=daemon
debug=0
pid_file=/usr/local/nagios/var/nrpe.pid
server_port=5666
nrpe_user=nagios
nrpe_group=nagios
allowed_hosts=192.183.3.145,172.16.56.131,172.16.56.129
dont_blame_nrpe=0
allow_bash_command_substitution=0
command_timeout=60
connection_timeout=300
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_sda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda1
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 150 -c 200

5.启动nrpe

添加端口:

[root@nn nrpe-3.0.1]#vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 5666 -j ACCEPT
[root@nn nrpe-3.0.1]#/etc/init.d/iptables restart
[root@nn nrpe-3.0.1]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[root@nn nrpe-3.0.1]#netstat -tulpn | grep nrpe 
tcp        0      0 0.0.0.0:5666                0.0.0.0:*                   LISTEN      59428/nrpe          
tcp        0      0 :::5666                     :::*                        LISTEN      59428/nrpe 

为nrpe编写启动脚本,使得nrpe以standard alone方式运行:

[root@nn nrpe-3.0.1]#vi /etc/init.d/nrped
#!/bin/bash
# chkconfig: 2345 88 12   
# description: NRPE DAEMON   
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@nn nrpe-3.0.1]#chmod +x /etc/init.d/nrped 
[root@nn nrpe-3.0.1]#chkconfig --add nrped  
[root@nn nrpe-3.0.1]#chkconfig nrped on
[root@nn nrpe-3.0.1]#service nrped start 
Starting NRPE daemon... done.

6.测试nrpe

[root@nn nrpe-3.0.1]#/usr/local/nagios/libexec/check_nrpe -H 172.16.56.129 -c check_users
USERS OK - 2 users currently logged in |users=2;5;10;0

7.Nagios监控端添加MySQL备库监控

[root@monitors ~]# vim /usr/local/nagios/etc/objects/hosts.cfg 
define host{
          host_name nn
          alias slave-server
          use linux-server
          address 172.16.56.129
          max_check_attempts 5
          check_period 24x7
          check_interval 5
          retry_interval 1
          max_check_attempts 10
          check_command check-host-alive
          notification_period 24x7
          notification_interval 30
          notification_options d,r
          contact_groups admins
         }
define hostgroup{
        hostgroup_name  remote-linux-servers ; The name of the hostgroup
        alias           remoteLinux Servers ; Long name of the group
        members         kk,nn   ; Comma separated list of hosts that belong to this group
        }

将prilinuxserver.cfg、mysqlservices.cfg中的“host_name kk”全部改为“hostgroup_name  remote-linux-servers”,并重启nagios服务。


[root@monitors ~]# vim /usr/local/nagios/etc/objects/prilinuxserver.cfg 
[root@monitors ~]# vim /usr/local/nagios/etc/objects/mysqlservices.cfg 
[root@monitors objects]# service nagios configtest
Nagios Core 4.2.0
Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 08-01-2016
License: GPL
Website: https://www.nagios.org
Reading configuration data...
   Read main config file okay...
   Read object config files okay...
Running pre-flight check on configuration data...
Checking objects...
	Checked 26 services.
	Checked 3 hosts.
	Checked 2 host groups.
	Checked 0 service groups.
	Checked 2 contacts.
	Checked 1 contact groups.
	Checked 27 commands.
	Checked 5 time periods.
	Checked 0 host escalations.
	Checked 0 service escalations.
Checking for circular paths...
	Checked 3 hosts
	Checked 0 service dependencies
	Checked 0 host dependencies
	Checked 5 timeperiods
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...
Total Warnings: 0
Total Errors:   0
Things look okay - No serious problems were detected during the pre-flight check
Object precache file created:
/usr/local/nagios/var/objects.precache
[root@monitors objects]#  service nagios restart
Running configuration check...
Stopping nagios: done.
Starting nagios: done.
2.1.2.check_mysql之MySQL主从监控配置

1.nrpe与check_mysql结合的方式监控MySQL主从

主库创建监控用户:

mysql> grant Replication client on *.* to 'nagios'@'%' identified by '123456';
mysql> flush privileges;
Query OK, 0 rows affected (0.19 sec)
[root@nn libexec]#mysql -unagios -p123456 -e "show slave status\G"
Warning: Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.183.3.145
                  Master_User: backup
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000007
          Read_Master_Log_Pos: 1267
               Relay_Log_File: nn-relay-bin.000003
                Relay_Log_Pos: 564
        Relay_Master_Log_File: master-bin.000007
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1267
              Relay_Log_Space: 1543
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 129
                  Master_UUID: 5a22d499-20b4-11e6-a81e-d43d7e101ba1
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0

MySQL备库:

[root@nn ~]#/usr/local/nagios/libexec/check_mysql -H172.16.56.129 -unagios -p123456 -S -w 20 -c 60
Uptime: 17435  Threads: 3  Questions: 48  Slow queries: 0  Opens: 71  Flush tables: 1  Open tables: 64  Queries per second avg: 0.002 Slave IO: Yes Slave SQL: Yes Seconds Behind Master: 0|Connections=16c;;; Open_files=22;;; Open_tables=64;;; Qcache_free_memory=1031352;;; Qcache_hits=0c;;; Qcache_inserts=0c;;; Qcache_lowmem_prunes=0c;;; Qcache_not_cached=4c;;; Qcache_queries_in_cache=0;;; Queries=49c;;; Questions=36c;;; Table_locks_waited=0c;;; Threads_connected=1;;; Threads_running=1;;; Uptime=17435c;;; 'seconds behind master'=0.000000s;20.000000;60.000000;
[root@nn ~]#service nrped stop
[root@nn ~]#vim /usr/local/nagios/etc/nrpe.cfg 
command[check_mysql_slave]=/usr/local/nagios/libexec/check_mysql -H172.16.56.129 -unagios -p123456 -S -w 20 -c 60
[root@nn ~]#service nrped start
Starting NRPE daemon... done.

Nagios监控端:

[root@monitors objects]# vi commands.cfg 
#check_mysql_slave command definition
define command{
        command_name check_mysql_slave
        command_line $USER1$/check_mysql -H $HOSTADDRESS$ -unagios -p123456 -S
}
[root@monitors objects]# vi prilinuxserver.cfg 
#  monitoring the mysql-slave-status on the remote host
define service{
use local-service
host_name nn
service_description slavestatus
check_command check_nrpe!check_mysql_slave
contact_groups admins
}
[root@monitors objects]# service nagios configtest
[root@monitors objects]#  service nrped restart
打开nagios web端可见新配置的监控项:

2.check_mysql方式监控MySQL主从
1)配置command.cfg
[root@monitors libexec]# vi /usr/local/nagios/etc/objects/commands.cfg 
#check_mysql_slave command definition
define command{
        command_name check_mysql_slave
        command_line $USER1$/check_mysql -H $HOSTADDRESS$ -unagios -p123456 -S
}
2)配置service文件
[root@monitors libexec]# vi /usr/local/nagios/etc/objects/mysqlservices.cfg 
# check_mysql_slave service definition
define service{
use local-service
host_name nn
service_description slave_status
check_command check_mysql_slave
}
重启nagios即可看到配置结果:


2.2 check_mysql_health监控MySQL主从

check_mysql_health监控MySQL主从非常简单,只需要新增服务的监控项即可:
[root@monitors libexec]# vi /usr/local/nagios/etc/objects/mysqlservices.cfg 
define service{
use local-service
hostgroup_name  remote-linux-servers
service_description slave-io-running
check_command check_mysql_health!172.16.56.129!3306!nagios!123456!slave-io-running
}
define service{
use local-service
hostgroup_name  remote-linux-servers
service_description slave-sql-running
check_command check_mysql_health!172.16.56.129!3306!nagios!123456!slave-sql-running
}
define service{
use local-service
hostgroup_name  remote-linux-servers
service_description slave-lag
check_command check_mysql_health!172.16.56.129!3306!nagios!123456!slave-lag
}
重启nagios服务:
[root@monitors libexec]# /etc/init.d/nagios restart
Running configuration check...
Stopping nagios: done.
Starting nagios: done.
刷新nagios监控页面:

至此,MySQL的主从监控安装配置完毕,不足之处,欢迎批评指正!
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值