网站搜索

如何在 RHEL、Rocky 和 AlmaLinux 中安装 Nagios


Nagios 是一款出色的开源监控工具,它提供了更全面的监控环境,可以始终关注您的所有机器/网络,无论您是在数据中心还是只是在你的小实验室。

借助 Nagios,您可以在一个窗口上远程监控远程主机及其服务。如果您的服务器出现问题,它会显示警告指示,这最终有助于我们在问题发生之前检测到它们。它帮助我们减少停机时间业务损失

近日,Nagios于2022年10月4日发布了最新版本Nagios Core 4.4.9以及最新稳定版Nagios插件2.3.3

本文旨在指导您简单地了解如何在 RHEL 和基于 RHEL 的发行版(例如 CentOS Stream)上从源代码(tarball)安装和配置最新版本的 Nagios CoreRocky LinuxAlmaLinuxFedora

30 分钟内,您将监控您的本地计算机,无需高级安装过程,只需基本安装即可在当今大多数 Linux 服务器上100%运行。

请注意:此处显示的安装说明是基于RHEL 9.0 Linux 发行版编写的。

在 Linux 中安装 Nagios 核心和 Nagios 插件

如果您正确遵循这些说明,您最终将得到以下信息。

  • Nagios 及其插件将安装在 /usr/local/nagios 目录下。
  • Nagios 将配置为监视本地计算机的一些服务(磁盘使用情况CPU 负载当前用户总进程< 等)
  • Nagios Web 界面将在 http://localhost/nagios 上提供

第 1 步:安装 Apache 和 PHP 软件包

我们需要安装ApachePHP、和一些库,例如gccglibcglibc-common 、GD 库及其开发库,然后再使用源安装 Nagios。为此,我们可以使用 yum 软件包安装程序。

yum install -y httpd httpd-tools php gcc glibc glibc-common gd gd-devel make net-snmp

第2步:创建Nagios用户和组

使用 useradd 命令和 nagcmd 组帐户创建新的 nagios 用户并设置密码

[root@tecmint]# useradd nagios
[root@tecmint]# groupadd nagcmd

接下来,使用 usermod 命令将 nagios 用户和 apache 用户添加到 nagcmd 组。

[root@tecmint]# usermod -G nagcmd nagios
[root@tecmint]# usermod -G nagcmd apache

第 3 步:下载 Nagios 核心和 Nagios 插件

为您的 Nagios 安装及其所有未来的下载创建一个目录。

[root@tecmint]# mkdir /root/nagios
[root@tecmint]# cd /root/nagios

现在使用 wget 命令下载最新的 Nagios Core 和 Nagios Plugins 软件包。

wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.4.9.tar.gz
wget https://nagios-plugins.org/download/nagios-plugins-2.3.3.tar.gz

步骤 4:提取 Nagios 核心和 Nagios 插件

我们需要使用 tar 命令提取下载的包,如下所示。

tar -xf nagios-4.4.9.tar.gz
tar -xf nagios-plugins-2.3.3.tar.gz

当您提取这些 tarball 时,该目录中将出现两个新文件夹。

ls -l

total 13804
drwxrwxr-x 21 root root     4096 Nov 17 02:22 nagios-4.4.9
-rw-r--r--  1 root root 11339450 Nov 17 03:08 nagios-4.4.9.tar.gz
drwxr-xr-x 15 root root     4096 Mar 11  2020 nagios-plugins-2.3.3
-rw-r--r--  1 root root  2782610 Mar 11  2020 nagios-plugins-2.3.3.tar.gz

步骤 5:安装和配置 Nagios Core

现在,首先,我们将配置Nagios Core,为此,我们需要转到Nagios目录并运行配置文件,如果一切顺利,它将显示输出最后作为样本输出。

cd nagios-4.4.9/
./configure --with-command-group=nagcmd

Creating sample config files in sample-config/ ...


*** Configuration summary for nagios 4.4.9 2022-11-16 ***:

 General Options:
 -------------------------
        Nagios executable:  nagios
        Nagios user/group:  nagios,nagios
       Command user/group:  nagios,nagcmd
             Event Broker:  yes
        Install ${prefix}:  /usr/local/nagios
    Install ${includedir}:  /usr/local/nagios/include/nagios
                Lock file:  /run/nagios.lock
   Check the result directory:  /usr/local/nagios/var/spool/checkresults
           Init directory:  /lib/systemd/system
  Apache conf.d directory:  /etc/httpd/conf.d
             Mail program:  /usr/bin/mail
                  Host OS:  linux-gnu
          IOBroker Method:  epoll

 Web Interface Options:
 ------------------------
                 HTML URL:  http://localhost/nagios/
                  CGI URL:  http://localhost/nagios/cgi-bin/
 Traceroute (used by WAP):  


Review the options above for accuracy.  If they look okay,
type 'make all' to compile the main program and CGIs.

配置完成后,我们需要使用make allmake install命令编译安装所有二进制文件,它将在您的计算机中安装所有需要的库,我们可以继续下一步。

make all
make install
*** Main program, CGIs and HTML files installed ***

You can continue with installing Nagios as follows (type 'make'
without any arguments for a list of all possible options):

  make install-init
     - This installs the init script in /lib/systemd/system

  make install-commandmode
     - This installs and configures permissions on the
       directory for holding the external command file

  make install-config
     - This installs sample config files in /usr/local/nagios/etc

make[1]: Leaving directory '/root/nagios/nagios-4.4.9'

以下命令将为 Nagios 安装init 脚本

make install-init

为了让 Nagios 从命令行工作,我们需要安装命令模式

make install-commandmode

接下来,安装示例 Nagios 文件,请运行以下命令。

make install-config
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/etc
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/etc/objects
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/nagios.cfg /usr/local/nagios/etc/nagios.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/cgi.cfg /usr/local/nagios/etc/cgi.cfg
/usr/bin/install -c -b -m 660 -o nagios -g nagios sample-config/resource.cfg /usr/local/nagios/etc/resource.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/templates.cfg /usr/local/nagios/etc/objects/templates.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/commands.cfg /usr/local/nagios/etc/objects/commands.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/contacts.cfg /usr/local/nagios/etc/objects/contacts.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/timeperiods.cfg /usr/local/nagios/etc/objects/timeperiods.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/localhost.cfg /usr/local/nagios/etc/objects/localhost.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/windows.cfg /usr/local/nagios/etc/objects/windows.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/printer.cfg /usr/local/nagios/etc/objects/printer.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/switch.cfg /usr/local/nagios/etc/objects/switch.cfg

*** Config files installed ***

Remember, these are *SAMPLE* config files.  You'll need to read
the documentation for more information on how to actually define
services, hosts, etc. to fit your particular needs.

第 6 步:自定义 Nagios 配置

使用您选择的编辑器打开“contacts.cfg”文件,并将与 nagiosadmin 联系人定义关联的电子邮件地址设置为接收电子邮件警报。

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

Just one contact defined by default - the Nagios admin (that's you)
This contact definition inherits a lot of default values from the
'generic-contact' template which is defined elsewhere.

define contact {

    contact_name            nagiosadmin             ; Short name of user
    use                     generic-contact         ; Inherit default values from generic-contact template (defined above)
    alias                   Nagios Admin            ; Full name of user
    email                   [email  ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
}

步骤 7:安装并配置 Nagios 的 Web 界面

我们完成了后端的所有配置,现在我们将使用以下命令配置 Nagios 的 Web 界面。以下命令将为 Nagios 配置 Web 界面,并创建一个 Web 管理员用户“nagiosadmin”。

make install-webconf

在此步骤中,我们将为“nagiosadmin”创建密码。执行此命令后,请提供密码两次并记住,因为当您登录 Nagios Web 界面时将使用此密码。

htpasswd -s -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
New password:
Re-type new password:
Adding password for user nagiosadmin

重新启动 Apache 以使新设置生效。

systemctl restart httpd

第8步:编译并安装Nagios插件

去那里并按照下面的指示配置和安装它。

cd /root/nagios
cd nagios-plugins-2.3.3
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install

步骤 9:验证 Nagios 配置文件

现在我们已经完成了 Nagios 配置,是时候验证它了,请插入以下命令。如果一切顺利,它将显示类似于以下输出。

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Nagios Core 4.4.9
Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 2022-11-16
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 8 services.
	Checked 1 hosts.
	Checked 1 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 1 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

步骤10:将Nagios服务添加到系统启动

为了使 Nagios 在重新启动后工作,我们需要在系统启动时使用 systemctl 命令启用 nagioshttpd

systemctl enable nagios
systemctl enable httpd

接下来,重新启动Nagios以使新设置生效。

systemctl restart nagios

步骤 11:在防火墙中允许 Nagios Web

要访问 Nagios Web 界面,您需要通过运行以下命令打开 Apache 端口:

firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

第12步:登录Nagios Web界面

您的 Nagios 已准备好工作,请在浏览器中使用“http://Your-server-IP-address/nagios”或“http://FQDN/nagios”打开它Strong>”并提供用户名“nagiosadmin”和密码

Nagios 网络仪表板

主机视图

Nagios 概述

服务视图

流程视图

恭喜!您已成功安装并配置 Nagios 及其插件。您刚刚开始监控之旅。

将 Nagios 3.x 升级到 Nagios Core 4.x

如果您已经运行旧版本的 Nagios,则可以随时升级。为此,您只需下载最新的 nagios 源 tar 存档并按如下所示进行配置。

systemctl stop nagios
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.4.9.tar.gz
tar -zxvf nagios-4.4.9.tar.gz
cd nagios-4.4.9
./configure
make all
make install
systemctl start nagios
结论

现在就这样,在我即将发表的文章中,我将向您展示如何添加 LinuxWindows打印机开关、<设备到 Nagios 监控服务器。

如果您在安装时遇到任何问题,请通过评论与我们联系。在此之前,请继续关注并连接到Tecmint,并且不要忘记点赞分享我们以传播。

另请阅读:

  • 如何将Linux主机添加到Nagios监控服务器
  • 如何将Windows主机添加到Nagios监控服务器