nagios 配置详解

之前已经详细介绍了nagios + pnp4nagios 安装笔记,这是我在 CentOS6.6 虚拟机上逐条命令重复验证过的,希望能给大家一点帮助。但是,成功的安装只是 nagios 监控道路上的第一步,要想 nagios 发挥应有的作用,还得从配置入手。

客户端配置

nrpe.cfg

nagios 在进行配置时,要先配置客户端(也就是被监控端),然后才能在服务端(也就是监控端)进行配置,否则服务端更新重载时将会出现客户端未定义,或者客户端脚本无法找到的错误。

客户端配置,主要是 nrpe.cfg 的配置,在《nagios + pnp4nagios 安装笔记》这篇文章中,安装客户端时,我们已经修改过一项配置,就是添加信任IP,现在我们来看看具体的监控配置。打开 nrpe.cfg ,找到如下配置:

vi /usr/local/nagios/etc/nrpe.cfg

########nrpe.cfg########
# The following examples use hardcoded command arguments...

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_hda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda1
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
########nrpe.cfg########

这些,就是我们定义的客户端监控命令以及阈值,服务端会根据 command 里面的命令,去执行相关脚本,然后根据脚本返回的结果以及 command 定义的阈值来判断是否正常。我们可以直接执行脚本查看返回值是否正常,比如:

/usr/local/nagios/libexec/check_users -w 5 -c 10

########脚本返回值########
USERS OK - 1 users currently logged in |users=1;5;10;0
########脚本返回值########

关于定义监控命令的脚本使用方法,可以使用 --help 来查看帮助信息:

/usr/local/nagios/libexec/check_users --help

########帮助信息########
...
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.
 -w, --warning=INTEGER
    Set WARNING status if more than INTEGER users are logged in
 -c, --critical=INTEGER
    Set CRITICAL status if more than INTEGER users are logged in
...
########帮助信息########

关于客户端的配置就说到这,很简单,在 nagios 插件和 nrpe 正确安装后,都会默认存在一些常用的监控项目,只要参考一下进行配置就行。需要注意的也就两点,一是脚本文件是否存在,二是执行返回结果格式是否正确。

服务端配置

nagios.cfg

说到 nagios 服务端(监控端)的配置,咱先来说说 nagios.cfg 这个文件,这个文件是整个 nagios 的核心配置文件,在里面可以对 nagios 各种功能进行控制,在《nagios + pnp4nagios 安装笔记》中,安装 pnp4nagios 时,我们曾修改过 nagios.cfg ,开启数据写入的功能。按理说如果没有特殊的需求,这个文件是不用改的,直接修改 nagios 提供的模板就行。但是咱们先来看看 localhost.cfg 文件:

vi /usr/local/nagios/etc/objects/localhost.cfg

我们可以从中找到 define host (主机定义), define hostgroup (主机组定义)和 define service (监控服务定义)。如果说有1000台主机分100个主机组,每个主机组有10个服务需要监控,那文件的内容肯定得很长,如果有某台主机或某个服务需要修改,就显得很不容易。那可不可以把这个文件拆分呢?比如拆分成这样易管理的形式:

./hosts/1-10hosts.cfg
./hosts/11-20hosts.cfg
./hosts/21-30hosts.cfg
...
./host_groups/first_group.cfg
./host_groups/second_group.cfg
./host_groups/third_group.cfg
...
./service_groups/service_1.cfg
./service_groups/service_2.cfg
./service_group/service_3.cfg
...

答案是可以的。我们再次来看看 nagios.cfg 配置,找到以下内容:

vi /usr/local/nagios/etc/nagios.cfg

########nagios.cfg########
# OBJECT CONFIGURATION FILE(S)
# These are the object configuration files in which you define hosts,
# host groups, contacts, contact groups, services, etc.
# You can split your object definitions across several config files
# if you wish (as shown below), or keep them all in a single config file.

# You can specify individual object config files as shown below:
cfg_file=/usr/local/nagios/etc/objects/commands.cfg
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg
cfg_file=/usr/local/nagios/etc/objects/templates.cfg
...
########nagios.cfg########

由此可以看到,配置的生效与否,不是根据文件名,而是根据 nagios.cfg 里面是否引用来判断。也就是说,文件是可以拆分的,但是拆分后,必须在 nagios.cfg 加以引用,才能生效。 nagios 对于主机,主机组和监控服务的定义,也不是通过文件名,而是通过 define 的内容来判断。知道了这些,我们就能了解,所谓的配置文件名以及存放的路径,没有固定的含义,只是为了区分各种 define 而存在,也能由用户自行创建和划分。

知道了 nagios.cfg 和配置文件名以及 define 的关系后,下面我们就不按配置的文件名来进行分析了,直接按 define 的内容来分析,这样可以突破文件名的限制,以便更深入地了解 nagios 的运行机制。

define command

要想监控主机,当然少不了监控命令, define command 就是用来定义监控命令的,一般存在于 commands.cfg 文件内:

vi /usr/local/nagios/etc/objects/commands.cfg

########commands.cfg#######
...
#'check_nrpe'  command definition
define command{
        command_name  check_nrpe
        command_line  $USER1$/check_nrpe -H $HOSTADDRESS$ -c  $ARG1$
        }
...
########commands.cfg#######

就拿我们在《nagios + pnp4nagios 安装笔记》中添加的 nrpe 命令来说, define command 主要包含两个内容,一个是 command_name (命令名称),一个是 command_line (执行内容),这些都是可自定义的。比如,我可以写个输出指定内容的执行脚本 check_money.sh ,放在 ~ 目录,当 nagios 使用到这条命令时便执行脚本,可以这么定义:

vi /usr/local/nagios/etc/objects/commands.cfg

########commands.cfg#######
...
#'check_nrpe'  command definition
define command{
        command_name  check_money
        command_line  ~/check_money.sh
        }
...
########commands.cfg#######

我们也可以看到, nagios 默认安装时,便给我们定义了很多常用的命令,比如 check_pingcheck_tcp 之类的,我们也可以根据自己的需求更改其命令的定义。至于那些 $HOSTADDRESS$ 的变量引用,下次再统一整理,知道 nagios 有很多数据是可以从变量直接引用的这回事就行了。

define timeperiod

有些服务,有时候我们需要 7X24 小时的紧急故障通知;但是有些无关紧要的服务,我们不需要 7X24 小时,只需要工作日或者工作时间通知就行了。这就用到了 define timeperiod (监控时间段定义),这个定义一般存在于 timeperiods.cfg 内而且很少变动,官方也给我们定义了一些常用的时间段,如有特殊情况,参照官方提供的模板进行修改就好:

vi /usr/local/nagios/etc/objects/timeperiods.cfg

########timeperiods.cfg########
...
define timeperiod{
        timeperiod_name 24x7 ;定义监控时间段名称
        alias           24 Hours A Day, 7 Days A Week ;定义详细说明
        sunday          00:00-24:00 ;监控时间配置
        monday          00:00-24:00
        tuesday         00:00-24:00
        wednesday       00:00-24:00
        thursday        00:00-24:00
        friday          00:00-24:00
        saturday        00:00-24:00
        }
...
########timeperiods.cfg########

register

这个不是 define 的定义,为何要在此特别说明呢?因为这个选项对我们以后 host 和 service 甚至 contact 的定义都很有用,因为这个选项决定了它里面配置的东西,是否成为模板。咱们来看看模板文件 templates.cfg 的内容:

vi /usr/local/nagios/etc/objects/templates.cfg

########templates.cfg########
...
# Generic host definition template - This is NOT a real host, just a template!

define host{
        name                            generic-host    ; 主机名称
        notifications_enabled           1               ; 启用通知
        event_handler_enabled           1               ; 启用主机事件管理
        flap_detection_enabled          1               ; 启用状态抖动监测
        failure_prediction_enabled      1               ; 启用故障预测
        process_perf_data               1               ; 启用性能数据记录
        retain_status_information       1               ; 保留状态信息直到程序重新启动
        retain_nonstatus_information    1               ; 不保留状态信息直到程序重新启动
        notification_period             24x7            ; 发送通知时间段
        register                        0               ; 不进行注册:0为不注册,即作为模板使用
        }

# Linux host definition template - This is NOT a real host, just a template!

define host{
        name                            linux-server    ; 主机名称
        use                             generic-host    ; 使用generic-host模板的配置
        check_period                    24x7            ; 检查时间段
        check_interval                  5               ; 检查间隔时间
        retry_interval                  1               ; 重试检查间隔时间,
        max_check_attempts              10              ; 告警前最大检查数
        check_command                   check-host-alive ; 主机检查命令,在commands.cfg定义
        notification_period             workhours       ; 发送通知时间段,如果在generic-host模板里也有此定义,则按当前值为准
        notification_interval           120             ; 主机故障后再次通知间隔数,0为只通知一次
        notification_options            d,u,r           ; 主机需要通知的状态
        contact_groups                  admins          ; 状态通知的用户组
        register                        0               ; 不进行注册:0为不注册,即作为模板使用
        }
...
########templates.cfg########

从以上内容可以看到,当 register0 时,配置不生效,但可作为模板,其中 use 后面,跟的就是使用的模板。从中也可以看到,模板也可以进行嵌套,自由度很大,只要在配置后加 register 0 ,它就可以作为模板来使用。当配置的信息跟模板冲突时,以当前配置为准。从 templates.cfg 中我们可以看到后面我们将要说的3个 define 的内容: define contact 定义联系人方式; define host 定义主机; define service 定义服务监控。

define contact 和 define contactgroup

这个是定义联系人和用户组,一般存在于 contacts.cfg 文件,用来配置联系人的各种信息。

vi /usr/local/nagios/etc/objects/contacts.cfg

########contacts.cfg########
...
define contact{
        contact_name                    nagiosadmin             ; 联系人姓名
        use                             generic-contact         ; 使用generic-contact模板
        alias                           Nagios Admin            ; 联系人详细备注
        email                           nagios@localhost        ; email地址
        }
...
define contactgroup{
        contactgroup_name       admins
        alias                   Nagios Administrators
        members                 nagiosadmin        ; 这里填写contact_name,用英文逗号分隔
        }
...
########contacts.cfg########

define host 和 define hostgroup

定义主机和主机组,默认存在于 localhost.cfg ,对主机的监控进行配置。之前有说过,如果主机和主机组较多,可自行拆分以便管理。

vi /usr/local/nagios/etc/objects/localhost.cfg 

########localhost.cfg########
...
# Define a host for the local machine

define host{
        use                     linux-server,host-pnp            ; 使用linux-server模板和host-php模板
        host_name               localhost                        ; 主机名
        alias                   localhost                        ; 主机详细备注
        address                 127.0.0.1                        ; 主机IP
        }
...
# Define an optional hostgroup for Linux machines

define hostgroup{
        hostgroup_name  linux-servers ; 主机组名称
        alias           Linux Servers ; 主机组详细备注
        members         localhost     ; 主机组包含的主机
        }
...
########localhost.cfg########

define service 和 define servicegroup

服务监控和服务组定义,默认存在于 localhost.cfg (服务组在默认情况下未配置),为了方便管理可自行拆分。

vi /usr/local/nagios/etc/objects/localhost.cfg 

########localhost.cfg#######
...
# Define a service to "ping" the local machine

define service{
        use                             local-service,srv-pnp         ; 使用的模板
        host_name                       localhost                     ; 监控的主机
        service_description             Users                         ; 监控的项目名
        check_command                   check_nrpe!check_users        ; 监控命令
        }
...
define servicegroup{
        servicegroup_name       check_users_group                     ; 监控组的名字
        alias                   check users group                     ; 监控组详细备注
        members                 localhost,Users                       ; 监控的具体项目,格式为:主机名,项目名
        }
########localhost.cfg#######

这个 check_command 值得说一下,后面跟的 check_nrpe 是我们在 commands.cfg 里面定义的, ! 后面跟的是定义时使用的 $ARG1$ 参数,所以根据我们安装时对 check_nrpe 的定义, check_nrpe!check_users 的意思是,利用 nrpe 到目标主机查找 check_users 定义,并根据定义的命令执行,也就是我们在客户端配置 nrpe.cfg 里面所说的 command[check_users] 定义,如果找不到,服务端就会报错。所以我们建议先对客户端进行配置,再在服务端配置相应的监控。

检查配置

综上所述,我们基本完成了对 nagios 的监控的基本配置,但是大家也看到,需要配置的地方很多,选项也很杂。有时候昏头昏脑地就配置完了,无法保证百分之百正确。其实 nagios 给了我们一个对配置进行自查,并就相应的错误进行提示的方法:

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

########返回结果########
...
Total Warnings: 0
Total Errors:   0
...
########返回结果########

如果配置正确,会返回无警告和无错误的结果,如果配置有问题,则会给出详细的错误信息,以便修改。

重载 nagios

nagios 给了我们一个能使配置生效而无需重启 nagios 的平稳过度方式,那就是重载:

sudo service nagios reload

########返回结果########
Running configuration check...done.
Reloading nagios configuration...done
########返回结果########

当然了,你也可以使用 restart 来重启。

OK,对于 nagios + pnp4nagios 的安装篇以及配置篇终于完结了,have fun!

消息盒子

# 暂无消息 #

只显示最新10条未读和已读信息