Centos7安装部署Nagios

打印 上一主题 下一主题

主题 857|帖子 857|积分 2571

简介

Nagios 是一款自动化运维工具,可以协助运维人员监控服务器的运行状况,而且拥有报警功能。
  1. 1、Nagios 核心及插件将安装在/usr/local/nagios
  2. 2、Nagios 核心会配置好监控当前服务器的一些基本信息
复制代码
仅供参考,记录一下centOS7下nagios配置步调,完备的步调以及原理请参照原文,原文非常详细!
想要实现的效果是,把一台linux配置为监控主机,这台监控主机监控本机的一些服务,另外还监控一台linux机器和一台windows机器的服务。
nagios的安装:
安装底子支持套件:
 [root@nagios-a ~]# yum install gcc glibc glibc-common gd gd-devel xinetd openssl-devel -y
创建系统用户
创建nagios用户并设置暗码
  1. /usr/sbin/useradd -m nagios
  2. passwd nagios
复制代码
创建nagcmd用户组,将nagios和apache(web服务器的运行用户)添加至该组
/usr/sbin/groupadd nagcmd
/usr/sbin/usermod -a -G nagcmd nagios
/usr/sbin/usermod -a -G nagcmd apache
下载Nagios安装包
  1. wget -O nagioscore.tar.gz https://github.com/NagiosEnterprises/nagioscore/archive/nagios-4.4.14.tar.gz  #下载nagios核心4.4.14
复制代码
下载的路径在/root下
 nagios焦点的编译与安装 解压源码包
  1. tar xzf nagioscore.tar.gz   #解压源码包 <br>cd  nagioscore-nagios-4.4.14/ #解压完成后,切换到nagios目录下
复制代码
编译配置信息
./configure --with-command-group=nagcmd
编译
make all
安装nagios,并初始化脚本及底子配置文件
make install
make install-init
make install-config
make install-commandmode
配置web服务器
创建apache的配置文件,配置文件目次在/etc/httpd/conf.d/文件下
  1. make install-webconf
复制代码
为nagios的web界面创建nagiosadmin用户,并设置暗码
  1. htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin        #这个账户是登录nagios网页中的用户,密码设置为123456
复制代码
把apache加入到开机启动项,并重启apache
  1. systemctl enable httpd.service                        #把httpd加入到开机启动项中
  2. systemctl restart httpd.service                        #重启httpd服务
复制代码
编译并安装nagios插件包
Nagios 实际的监控脚本都是通过插件实现的,本文中的插件包是官方提供插件包(约莫50个用于监控的插件)
wget https://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz #下载nagios插件plugins-2.2.1
解压插件包
tar -zxvf nagios-plugins-2.2.1.tar.gz
cd nagios-plugins-2.2.1
编译并安装
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make all
make install
ls /usr/local/nagios/libexec/|wc -l
#查看插件的个数
启动nagios焦点
将nagios焦点加入到自动启动列表
  1. chkconfig --add nagios
  2. chkconfig nagios on
复制代码
检查nagios底子配置文件是否有问题
  1. /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
复制代码
重启nagios服务
  1. systemctl restart nagios<br><br>开启防火墙端口
复制代码
  1. firewall-cmd --zone=public --add-port=80/tcp<br>firewall-cmd --zone=public --add-port=80/tcp --permanent
复制代码
打开浏览器验证
  1. http://192.168.10.1/nagios
  2. 用户名:nagiosadmin
  3. 密码:123456
复制代码
[code][/code]插件装好了,下面修改一下配置文件,设置监控的主机,首先设置监控本机:
在/usr/local/nagios/etc/object/下,有一个localhost.cfg文件,这个文件本来就是存在的,是不需要修改的。
define host {
use linux-server ; Name of host template to use
                                                    ; This host definition will inherit all variables that are defined
                                                    ; in (or inherited by) the linux-server host template definition.
    host_name               localhost
    alias                   1.121
    address                 127.0.0.1
}
这样,本机就修改完了,接下来,进行监控windows的配置:
在/usr/local/nagios/etc/object/下,有一个windows.cfg文件,这个文件也是本来就存在的,修改这个文件,使nagios可以监控windows主机,修改这个文件也非常简单,只需要把windows主机的ip地址放在address对应位置就可以了:
define host {
use windows-server ; Inherit default values from a template
    host_name               winserver               ; The name we're giving to this host
    alias                   My Windows Server       ; A longer name associated with the host
    address                 192.168.59.137 ; IP address of the host
}
固然,alias可以改成任意的名字。
重启nagios,可以发现localhost的服务被正常监控,windows主机也可以被ping通,但是windows的服务是监控不到的,这是怎么回事呢,是因为windows没有装nagios插件呀,这样nagios就无法监控到windows的服务了,下面为windows安装nagios插件:
windows下的nagios监控插件名叫NSClient++,访问官网 http://www.nsclient.org,下载安装包,这里我下载的是NSCP-0.5.2.35-x64 .msi,下载完成以后,双击进行安装。


 
这里我选的是comlete:
安装完成后,记得在服务中把NSClient++服务启动起来:
 

 然后看到有些服务已经起来了,但是有些服务看起来并不能正常监控,下面修改一下windows插件的配置文件,默认安装的位置是在C:\Program Files\NSClient++下,nsclient.ini这个文件就是配置文件了
# If you want to fill this file with all available options run the following command:
#   nscp settings --generate --add-defaults --load-all
# If you want to activate a module and bring in all its options use:
#   nscp settings --activate-module  --add-defaults
# For details run: nscp settings --help

; in flight - TODO
[/settings/default]
; Undocumented key
allowed hosts = 192.200.1.121

; in flight - TODO
[/settings/NRPE/server]
; Undocumented key
verify mode = none
; Undocumented key
insecure = true

; in flight - TODO
[/modules]
; Undocumented key
CheckExternalScripts = enabled
; Undocumented key
CheckHelpers = enabled
; Undocumented key
CheckNSCP = enabled
; Undocumented key
CheckDisk = enabled
; Undocumented key
WEBServer = enabled
; Undocumented key
CheckSystem = enabled
; Undocumented key
NSClientServer = enabled
; Undocumented key
CheckEventLog = enabled
; Undocumented key
NSCAClient = enabled
; Undocumented key
NRPEServer = enabled
把 diable改为enabled,这样服务就能被监控了,改完之跋文得去把服务里把NSClient++服务重启一下,然后就能看到监控正常了呢
固然,如果有些服务你不想让他监控,那就去配置文件修改一下监控的内容,好比不想监控一些windows服务,那么就在:windows.cfg文件里把相应的服务注释掉:
 
#define service {
#
#    use                     generic-service
#    host_name               winserver
#    service_description     W3SVC
#    check_command           check_nt!SERVICESTATE!-d SHOWALL -l W3SVC
#}
 
# Create a service for monitoring the Explorer.exe process
# Change the host_name to match the name of the host you defined above
#define service {
#    use                     generic-service
#    host_name               winserver
#    service_description     Explorer
#    check_command           check_nt!PROCSTATE!-d SHOWALL -l Explorer.exe
#}
同理,linux的本机localhost.cfg文件,注释掉相应的服务:
 
#define service {
#
#    use                     local-service           ; Name of service template to use
#    host_name               localhost
#   service_description     HTTP
#    check_command           check_http
#    notifications_enabled   0
#}
之后重启nagios服务,可以看到注释掉的服务都不见了:
 至此,已经配置好了nagios对本机和windows主机的监控,下面进行配置nagios监控其他linux机器:
监控其他linux机器,一般来说需要手动添加两个文件,在安装nagios目次下的 /nagios/etc/object/下,分别是hosts.cfg文件和services.cfg文件,其中hosts.cfg文件主要指定被监控主机的干系情况,包括ip地址以及主机名等;而services.cfg则是定义需要监控的服务等,下面给出简单的配置示例:
hosts.cfg
define host{
        use                     linux-server
        host_name               1.131
        address                 192.168.59.138
        }
define host{
        use                     linux-server
        host_name               2.76
        address                 192.168.59.139
        }
#####################################################################
define hostgroup{
        hostgroup_name          test-servers
        alias                   test servers
        members                 1.131,2.76
        }
在hosts.cfg文件中简单定义了两台需要监控的linux主机以及一个主机组,use项是引用了local-service服务的属性值,这个local-service在template.cfg文件中有定义,定义如下:
define host {
name linux-server ; The name of this host template
    use                             generic-host            ; This template inherits other values from the generic-host template
    check_period                    24x7                    ; By default, Linux hosts are checked round the clock
    check_interval                  5                       ; Actively check the host every 5 minutes
    retry_interval                  1                       ; Schedule host check retries at 1 minute intervals
    max_check_attempts              10                      ; Check each Linux host 10 times (max)
    check_command                   check-host-alive        ; Default command to check Linux hosts
    notification_period             workhours               ; Linux admins hate to be woken up, so we only notify during the day
                                                            ; Note that the notification_period variable is being overridden from
                                                            ; the value that is inherited from the generic-host template!
    notification_interval           120                     ; Resend notifications every 2 hours
    notification_options            d,u,r                   ; Only send notifications for specific host states
    contact_groups                  admins                  ; Notifications get sent to the admins by default
    register                        0                       ; DON'T REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
}
在services.cfg文件中简单定义了一个服务,用于测试监控主机是否能够正常被监控到:
 
define service{
        use                     local-service
        host_name               1.131,2.76
        service_description     check-host-alive
        check_command           check-host-alive
     }
如今两个配置文件写好了,然后需要在nagios.cfg(nagios.cfg文件在nagios目次下的etc下)文件中添加对这两个文件的引用,只需要添加以下两行:
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
cfg_file=/usr/local/nagios/etc/objects/services.cfg
至此,对远端linux主机的简单监控就完成了,重启nagios以后可以看到两台linux主机已经up:
 如果想要监控远端主机的多个服务,固然还是要装linux插件的,以远端主机1.131为例,安装nagios插件和nrpe,依然是去nagios官网下载,先增加nagios用户,之后获取nagios插件包和nrpe包,解压后安装:
useradd nagios
wget https://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz
wget https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-3.2.1/nrpe-3.2.1.tar.gz
tar -zxvf nagios-plugins-2.2.1.tar.gz
tar -zxvf nrpe-3.2.1.tar.gz
cd nagios-plugins-2.2.1/
./configure --prefix=/usr/local/nagios
make
make all
make install
chown nagios.nagios /usr/local/nagios
chown -R nagios.nagios /usr/local/nagios/libexec
cd nrpe-3.2.1/
./configure --prefix=/user/local/nagios
make all
make install
然后,修改一下配置文件,允许监控主机的监控,需要修改的是/usr/local/nagios/etc目次下的nrpe.cfg文件:
allowed_hosts=127.0.0.1,192.168.59.136
这里,只需要在allowed_hosts后加上监控主机的ip就可以了,然后开启nrpe服务,确认端口开启以后去监控主机上测试一下:
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[root@nagios-b etc]# netstat -tunlp | grep nrpe
tcp        0      0 0.0.0.0:5666            0.0.0.0:*               LISTEN      12545/nrpe         
tcp6       0      0 :::5666                 :::*                    LISTEN      12545/nrpe     
可以看到,端口已经在被监听了,下面去监控主机上测试一下:
[root@nagios-a ~]# /usr/local/nagios/libexec/check_nrpe -H 192.168.59.138
NRPE v3.2.1
正常显示了NRPE的版本
之后可以配置被监控主机的其他服务了,修改监控主机的/usr/local/nagios/etc/objects下的services.cfg,好比修改成这样:
define service{
        use                     local-service
        host_name               1.131
        service_description     check-host-alive
        check_command           check-host-alive
     }
define service {
    use                     local-service
    host_name               1.131
    service_description     PING
    check_command           check_ping!100.0,20%!500.0,60%
}
 
# Define a service to check the disk space of the root partition
# on the local machine.  Warning if < 20% free, critical if
# < 10% free space on partition.
define service {
    use                     local-service
    host_name               1.131
    service_description     Root Partition
    check_command           check_local_disk!20%!10%!/
}
 
# Define a service to check the number of currently logged in
# users on the local machine.  Warning if > 20 users, critical
# if > 50 users.
define service {
    use                     local-service
    host_name               1.131
    service_description     Current Users
    check_command           check_local_users!20!50
}
 
# Define a service to check the number of currently running procs
# on the local machine.  Warning if > 250 processes, critical if
# > 400 processes.
define service {
    use                     local-service
    host_name               1.131
    service_description     Total Processes
    check_command           check_local_procs!250!400!RSZDT
}
 
# Define a service to check the load on the local machine.
define service {
    use                     local-service
    host_name               1.131
    service_description     Current Load
    check_command           check_local_load!5.0,4.0,3.0!10.0,6.0,4.0
}
 
# Define a service to check the swap usage the local machine.
# Critical if less than 10% of swap is free, warning if less than 20% is free
define service {
    use                     local-service
    host_name               1.131
    service_description     Swap Usage
    check_command           check_local_swap!20%!10%
}
 
# Define a service to check SSH on the local machine.
# Disable notifications for this service by default, as not all users may have SSH enabled.
define service {
    use                     local-service
    host_name               1.131
    service_description     SSH
    check_command           check_ssh
    notifications_enabled   0
}
之后检查一下配置文件有没有错误:
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Nagios Core 4.4.3
Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 2019-01-15
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 20 services.
        Checked 4 hosts.
        Checked 3 host groups.
        Checked 0 service groups.
        Checked 1 contacts.
        Checked 1 contact groups.
        Checked 24 commands.
        Checked 5 time periods.
        Checked 0 host escalations.
        Checked 0 service escalations.
Checking for circular paths...
        Checked 4 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就好了,
 可以看到这些服务都可以正常监控了呢!
 到这里配置已经完成了,还可以设置一个邮件报警的功能,这个功能的配置是在contacts.cfg这个配置文件中配置的,内容如下:
define contact {

contact_name xiaobai ; Short name of user
    use                     generic-contact         ; Inherit default values from generic-contact template (defined above)
    alias                   Nagios Admin            ; Full name of user
    email                   *****@***.com           ;

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

农妇山泉一亩田

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表