阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Linux上Nagios安装配置初入门

152次阅读
没有评论

共计 9642 个字符,预计需要花费 25 分钟才能阅读完成。

一、监控端安装 nagios
1、Nagios 简介
Nagios 通常由一个主程序 (Nagios)、一个插件程序(Nagios-plugins) 和四个可选的 ADDON(NRPE、NSCA、NSClient++ 和 NDOUtils)组成。Nagios 的监控工作都是通过插件实现的,因此,Nagios 和 Nagios-plugins 是服务器端工作所必须的组件。而四个 ADDON 中,NRPE 用来在监控的远程 Linux/Unix 主机上执行脚本插件以实现对这些主机资源的监控;NSCA 用来让被监控的远程 Linux/Unix 主机主动将监控信息发送给 Nagios 服务器 (这在冗余监控模式中特别要用到);NSClient++ 是用来监控 Windows 主机时安装在 Windows 主机上的组件;而 NDOUtils 则用来将 Nagios 的配置信息和各 event 产生的数据存入数据库,以实现这些数据的快速检索和处理。这四个 ADDON(附件) 中,NRPE 和 NSClient++ 工作于客户端,NDOUtils 工作于服务器端,而 NSCA 则需要同时安装在服务器端和客户端。

目前,Nagios 只能安装在 Linux 系统主机上,其编译需要用到 gcc。同时,如果打算使用 web 界面的管理工具的话,还需要有 apache 服务器和 GD 图形库的支持。

2、安装前的准备工作
(1)解决安装 Nagios 的依赖关系
Nagios 基本组件的运行依赖于 httpd、gcc 和 gd。可以通过以下命令来检查 nagios 所依赖的 rpm 包是否已经完全安装:

# yum -y install httpd gcc glibc glibc-common gd gd-devel php php-mysql mysql mysql-devel mysql-server

说明:如果通过编译源代码的方式安装,后面许多相关文件的路径需要按照源代码安装时的配置逐一修改。此外,得按需启动必要的服务,如 httpd 等。

(2)添加 nagios 运行所需要的用户和组

# groupadd  nagcmd
# useradd -G nagcmd nagios
# passwd nagios

把 daemon 加入到 nagcmd 组,以便于在通过 web Interface 操作 nagios 时能够具有足够的权限:

# usermod -a -G nagcmd daemon

3、编译安装 nagios

# wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-3.5.1.tar.gz#_ga=1.194172141.1791413989.1469521324
# tar xvf nagios-3.5.1.tar.gz -C /usr/local/src/
# cd /usr/local/src/nagios/
# ./configure --prefix=/usr/local/nagios --sysconfdir=/etc/nagios --with-command-group=nagcmd --enable-event-broker 
# make all
# make install
# make install-init
# make install-commandmode
# make install-config

在 httpd 的配置文件目录 (conf.d) 中创建 Nagios 的 Web 程序配置文件:

# make install-webconf

创建一个登录 nagios web 程序的用户,在以后通过 web 登录 nagios 认证时所用:

# htpasswd -c /etc/nagios/htpasswd.users nagiosadmin

以上过程配置结束以后需要重新启动 httpd:

# service httpd restart

4、编译、安装 nagios-plugins
nagios 的所有监控工作都是通过插件完成的,因此,在启动 nagios 之前还需要为其安装官方提供的插件。

# wget http://www.nagios-plugins.org/download/nagios-plugins-2.1.1.tar.gz#_ga=1.1710961.1791413989.1469521324
# tar xvf nagios-plugins-2.1.1.tar.gz -C /usr/local/src/
# cd /usr/local/src/nagios-plugins-2.1.1
# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
# make && make install

5、配置并启动 Nagios
(1)把 nagios 添加为系统服务并将之加入到自动启动服务队列

# chkconfig --add nagios
# chkconfig nagios on

(2)检查其主配置文件的语法是否正确

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

(3)如果上面的语法检查没有问题,接下来就可以正式启动 nagios 服务了:

# service nagios start

(4)配置 selinux
如果系统开启了 selinux 服务,则默认为拒绝 nagios web cgi 程序的运行。可以通过下面的命令来检查系统是否开启了 selinux:

# getenforce

如果上面命令的结果显示开启了 selinux 服务,可通过下面的命令暂时性的将其关闭:

# setenforce 0

通过编辑 /etc/sysconfig/selinux 文件完全关闭 selinux,将其中的 selinux 后面的值“force”修改为“disable”即可。
当然,也可以通过以下方式将 nagios 的 CGI 程序运行于 SELinux/targeted 模式而不用关闭 selinux:

# chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin
# chcon -R -t httpd_sys_content_t /usr/local/nagios/share

(5)通过 web 界面查看 nagios:http://your_nagios_IP/nagios
通过身份验证后即可查看当前默认监控本机的状况
Linux 上 Nagios 安装配置初入门
二、基于 NRPE 监控远程 Linux 主机
1、NRPE 简介
Nagios 监控远程主机的方法有多种,其方式包括 SNMP、NRPE、SSH 和 NCSA 等。这里介绍其通过 NRPE 监控远程 Linux 主机的方式。

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

2、安装配置被监控端
(1)添加 nagios 用户

# useradd -s /sbin/nologin nagios

(2)NRPE 依赖于 nagios-plugins,因此,需要先安装之

# tar xvf nagios-plugins-2.1.1.tar.gz -C /usr/local/src/
# cd /usr/local/src/nagios-plugins-2.1.1/
# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
# make all
# make install

(3)安装 NRPE

# tar xvf nrpe-3.0.tar.gz -C /usr/local/src/
# cd /usr/local/src/nrpe-3.0
# ./configure --prefix=/usr/local/nrpe \
   --sysconfdir=/etc/nrpe \
   --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-config

(4)配置 NRPE

# vim /etc/nrpe/nrpe.cfg
log_facility=daemon
pid_file=/var/run/nrpe.pid
server_address=172.16.100.11
server_port=5666
nrpe_user=nagios
nrpe_group=nagios
allowed_hosts=172.16.100.1
command_timeout=60
connection_timeout=300
debug=0

上述配置指令可以做到见名知义,因此,配置过程中根据实际需要进行修改即可。其中,需要特定说明的是 allowed_hosts 指令用于定义本机所允许的监控端的 IP 地址。

(5)启动 NRPE

# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg –d

为了便于 NRPE 服务的启动,可以将如下内容定义为 /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

也可在 /etc/xinetd.d 目录中创建 nrpe 文件,使其成为一个基于非独立守护进程的服务,通过重启 xinetd 来实现启动 NRPE 进程,文件内容如下

service nrpe
{flags = REUSE
    socket_type = stream
    wait = no
    user = nagios
    group = nagios
    server = /usr/local/nagios/bin/nrpe
    server_args = -c /etc/nagios/nrpe.cfg -i
    log_on_failure += USERID
    disable = no
}

(6)配置允许远程主机监控的对象

在被监控端,可以通过 NRPE 监控的服务或资源需要通过 nrpe.conf 文件使用命令进行定义,定义命令的语法格式为:

command[<command_name>]=<command_to_execute>

比如:

  • command[check_rootdisk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /
  • command[check_swap]=/usr/local/nagios/libexec/check_disk -w 40% -c 20%
  • command[check_sensors]=/usr/local/nagios/libexec/check_sensors
  • command[check_users]=/usr/local/nagios/libexec/check_users -w 10 -c 20
  • command[check_load]=/usr/local/nagios/libexec/check_load -w 10,8,5 -c 20,18,15
  • command[check_zombies]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
  • command[check_all_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200

3、配置监控端
(1)安装 NRPE

# tar xvf nrpe-3.0.tar.gz -C /usr/local/src/
# cd /usr/local/src/nrpe-3.0
# ./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
# /usr/local/nagios/libexec/check_nrpe -H client_IP   ## 测试是否能与被监控端通信
NRPE vnrpe-3.0

(2)定义如何监控远程主机及服务:

通过 NRPE 监控远程 Linux 主机要使用 chech_nrpe 插件进行,其语法格式如下:

check_nrpe -H <host> [-n][-u][-p <port>][-t <timeout>][-c <command>][-a <arglist...>]

定义监控远程 Linux 主机资源的通用性命令:

# vim /etc/nagios/objects/commands.cfg
define command {
       command_name   check_nrpe
       command_line   $USER1$/check_nrpe –H "$HOSTADDRESS$" -c $ARG1$
 }

定义远程 Linux 主机的资源配置文件,可根据实际情况定义

# cat /etc/nagios/objects/linux.cfg  |grep "^\s*[^#\t].*$"
define host{use        linux-server ; Inherit default values from a template
    host_name   webserver   ; The name we're giving to this host
    alias       My Web Server   ; A longer name associated with the host
    address     192.168.1.72    ; IP address of the host
    }
define hostgroup{hostgroup_name  web-servers ; The name of the hostgroup
    alias           Web Servers ; Long name of the group
    }
define service{
    use                 generic-service
    host_name           webserver
    service_description CHECK USERS
    check_command       check_nrpe!check_users
    }
define service{
    use                 generic-service
    host_name           webserver
    service_description Load
    check_command       check_nrpe!check_load
    }
define service{
    use                 generic-service
    host_name           webserver
    service_description Disk
    check_command       check_nrpe!check_xvda1
    }
define service{
    use                     generic-service
    host_name               webserver
    service_description     Total Processes 
    check_command           check_nrpe!check_total_procs
    }
define service{
    use                  generic-service
    host_name            webserver
    service_description  Zombie Processes
    check_command        check_nrpe!check_zombie_procs
    }

而后编辑 nagios 配置文件,使用刚才定义的 linux.cfg,检查语法正确后重启 nagios 服务

# vim /etc/nagios/nagios.cfg 
cfg_file=/etc/nagios/objects/linux.cfg
# /usr/local/nagios/bin/nagios -v /etc/nagios/nagios.cfg 
# service nagios start

Linux 上 Nagios 安装配置初入门
三、基于 NSClinet++ 监控 Windows 主机
windows 主机安装 NSClinet++ 完成后,默认已经启动服务,12489 是 check_nt 与 NSClinet++ 通信的端口,5666 为 nrpe 监听的端口
Linux 上 Nagios 安装配置初入门
1、基于 check_nt
Windows 端要启用的模块,修改配置后要重启服务

[modules]
CheckSystem.dll
CheckDisk.dll
FileLogger.dll
NSClientListener.dll

[settings]
allowed_hosts = 

在 nagios 端使用如下命令测试测试

# /usr/local/nagios/libexec/check_nt -h   ## 查看帮助信息
check_nt -H <client ip> -p <port> -v <command> ...
# check_nt -H 192.168.1.250 -p 12489 -v CPULOAD -w 80 -c 90 -l 5,80,90 

定义监控远程 Windows 主机资源的通用性命令:

# vim /etc/nagios/objects/commands.cfg
define command{
    command_name    check_nt
    command_line    $USER1$/check_nt -H $HOSTADDRESS$ -p 12489 -s PASSWORD -v $ARG1$ $ARG2$
    }

根据实际情况定义远程 Windows 主机的资源配置文件

# cat /etc/nagios/objects/windows.cfg |grep "^\s*[^#\t].*$"
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.1.2 ; IP address of the host
    }
define hostgroup{hostgroup_name  windows-servers ; The name of the hostgroup
    alias           Windows Servers ; Long name of the group
    }
define service{
    use                 generic-service
    host_name           winserver
    service_description NSClient++ Version
    check_command       check_nt!CLIENTVERSION
    }
define service{
    use                 generic-service
    host_name           winserver
    service_description Uptime
    check_command       check_nt!UPTIME
    }
define service{
    use                 generic-service
    host_name           winserver
    service_description CPU Load
    check_command       check_nt!CPULOAD!-l 5,80,90
    }
define service{
    use                 generic-service
    host_name           winserver
    service_description Memory Usage
    check_command       check_nt!MEMUSE!-w 80 -c 90
    }
define service{
    use                 generic-service
    host_name           winserver
    service_description C:\ Drive Space
    check_command       check_nt!USEDDISKSPACE!-l c -w 80 -c 90
    }
define service{
    use                 generic-service
    host_name           winserver
    service_description W3SVC
    check_command       check_nt!SERVICESTATE!-d SHOWALL -l W3SVC
    }
define service{
    use                 generic-service
    host_name           winserver
    service_description Explorer
    check_command       check_nt!PROCSTATE!-d SHOWALL -l Explorer.exe
    }

而后编辑 nagios 配置文件,使用刚才定义的 windows.cfg,检查语法正确后重启 nagios 服务

# vim /etc/nagios/nagios.cfg 
cfg_file=/etc/nagios/objects/windows.cfg
# /usr/local/nagios/bin/nagios -v /etc/nagios/nagios.cfg 
# service nagios start

————————————– 分割线 ————————————–

在 Ubuntu 下配置 Mrtg 监控 Nginx 和服务器系统资源 http://www.linuxidc.com/Linux/2013-08/88417.htm

使用 snmp+Mrtg 监控 Linux 系统 http://www.linuxidc.com/Linux/2012-11/73561.htm

Mrtg 服务器搭建(监控网络流量)http://www.linuxidc.com/Linux/2012-07/64315.htm

网络监控器 Nagios 全攻略 http://www.linuxidc.com/Linux/2013-07/87067.htm

Nagios 搭建与配置详解 http://www.linuxidc.com/Linux/2013-05/84848.htm

Nginx 环境下构建 Nagios 监控平台 http://www.linuxidc.com/Linux/2011-07/38112.htm

在 RHEL5.3 上配置基本的 Nagios 系统(使用 Nagios-3.1.2) http://www.linuxidc.com/Linux/2011-07/38129.htm

CentOS 5.5+Nginx+Nagios 监控端和被控端安装配置指南 http://www.linuxidc.com/Linux/2011-09/44018.htm

Ubuntu 13.10 Server 安装 Nagios Core 网络监控运用 http://www.linuxidc.com/Linux/2013-11/93047.htm

————————————– 分割线 ————————————–

Nagios 的详细介绍:请点这里
Nagios 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-08/133870.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计9642字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中