网站搜索

如何在CentOS 8上安装Icinga 2监控工具


在此页

  1. 先决条件
  2. 设置存储库
  3. 安装和配置 LAMP 堆栈
  4. 安装和配置 Icinga2
  5. 安装和配置 Icingaweb2
  6. 通过 Web 界面配置 Icinga Web 2
  7. 验证 Icinga 2 和 Icinga Web 2 安装
  8. 结论

监控是服务器基础设施和数据中心最重要的组成部分之一。它将帮助您检测服务器上将发生的问题和问题,以便您可以更快地解决这些问题。有一些可用的开源监控工具,其中最受欢迎的工具之一是 Icinga 2。

Icinga 是流行的 Nagios 监控工具的一个分支,现在已经用 C++ 从头开始重写。作为 Nagios 项目的分支,Icinga 附带了额外的工具和功能,包括 icinga director、icinga 模块和 icinga 插件。此外,它还支持分布式监控,允许您从一个仪表板管理多个 Icinga 实例。

在本教程中,我们将逐步向您展示如何在 CentOS 8 服务器上使用 Apache2 和 MariaDB 安装和配置 Icinga2。

先决条件

  • 一台 CentOS 8 服务器。确保其更新到最新版本。
  • 至少 2GB RAM 和 2 个内核。如果你有更多,它会更好。
  • 具有 Sudo 权限的用户。

设置存储库

首先需要在centos 8系统中添加epel、remi、icinga仓库。然后您必须安装 dnf \config-manager\ 工具并使用该工具启用 \PowerTools\ 存储库。

1. 通过运行以下命令为centos 8 服务器添加epel 和remi 存储库。

sudo dnf install -y epel-release
sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-8.rpm

2.现在运行下面的命令下载并添加centos 8系统的Icinga仓库。

sudo dnf install -y https://packages.icinga.com/epel/icinga-rpm-release-8-latest.noarch.rpm

3. 之后,执行以下命令安装 dnf config-manager 工具并启用 PowerTools 存储库。

sudo dnf install -y 'dnf-command(config-manager)'
sudo dnf config-manager --set-enabled powertools

4. 所有过程完成后,使用以下命令验证系统上所有已启用的存储库。

sudo dnf repolist

您将看到类似于下面的输出,并确保 epel、remi、icinga-* 和 powertools 存储库在列表中。

repo id                                                                                              repo name
appstream                                                                                            CentOS Linux 8 - AppStream
baseos                                                                                               CentOS Linux 8 - BaseOS
epel                                                                                                 Extra Packages for Enterprise Linux 8 - x86_64
epel-modular                                                                                         Extra Packages for Enterprise Linux Modular 8 - x86_64
extras                                                                                               CentOS Linux 8 - Extras
icinga-stable-release                                                                                ICINGA (stable release for epel)
powertools                                                                                           CentOS Linux 8 - PowerTools
remi-modular                                                                                         Remi's Modular repository for Enterprise Linux 8 - x86_64
remi-safe                                                                                            Safe Remi's RPM repository for Enterprise Linux 8 - x86_64

安装和配置 LAMP 堆栈

Icinga 监控工具有两个主要组件,处理所有监控作业的“icinga2”和基于 PHP 的强大、干净、快速的 Web 应用程序“icingaweb2”,允许您从管理 Icinga 堆栈网络浏览器。现在您将在 centos 8 系统上安装和配置基本的 LAMP Stack。

1. 使用以下命令为 PHP 7.4 启用 remi 模块。

sudo dnf module enable -y php:remi-7.4

接下来,执行以下命令来安装基本的 LAMP Stack 包。

sudo dnf install -y httpd mariadb-server mariadb php php-cli php-devel php-pear make php-mysqlnd php-pecl-imagick php-pecl-imagick-devel

现在启动 apache(centos 上的 httpd)和 mariadb 服务,并使它们在系统启动时启动。

sudo systemctl enable --now httpd
sudo systemctl enable --now mariadb

您将看到类似如下的输出,apache 和 mariadb 服务已启动并启用。

# Apache started and enabled
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

# MariaDB started and enabled
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.

2. 之后,运行以下命令设置 mariadb root 密码,出于安全原因,您将删除默认数据库 test 和匿名用户,同时禁用 root 用户的远程登录。

sudo mysql_secure_installation

下面是您将获得的类似输出,并确保为 mariadb root 用户键入一个新的强密码并键入 Y 以确认所有操作。

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

To log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): PRESS ENTER
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] Y
New password: TYPE STRONG PASSWORD
Re-enter new password: TYPE STRONG PASSWORD
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

安装和配置 Icinga2

现在您将安装和配置 Icinga 本身的主要组件。此阶段将涵盖 icinga2 包的安装、创建新数据库和用户、导入数据库模式以及设置 Icinga REST API。

1.执行以下命令安装Icinga监控工具包。

sudo dnf install -y icinga2 nagios-plugins-all

之后,启动并启用 icinga2 服务,然后验证 Icinga 服务状态。

sudo systemctl enable --now icinga2
sudo systemctl status icinga2

如果您看到类似如下的输出,则 Icinga 服务已启动并正在运行。

# Icinga2 started and enabled
Created symlink /etc/systemd/system/multi-user.target.wants/icinga2.service → /usr/lib/systemd/system/icinga2.service.

# Icinga2 status running
? icinga2.service - Icinga host/service/network monitoring system
   Loaded: loaded (/usr/lib/systemd/system/icinga2.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2021-07-18 02:38:19 UTC; 6s ago
  Process: 31290 ExecStartPre=/usr/lib/icinga2/prepare-dirs /etc/sysconfig/icinga2 (code=exited, status=0/SUCCESS)
 Main PID: 31298 (icinga2)
    Tasks: 13 (limit: 4964)
   Memory: 20.9M
   CGroup: /system.slice/icinga2.service
           ??31298 /usr/lib64/icinga2/sbin/icinga2 --no-stack-rlimit daemon --close-stdio -e /var/log/icinga2/error.log
           ??31319 /usr/lib64/icinga2/sbin/icinga2 --no-stack-rlimit daemon --close-stdio -e /var/log/icinga2/error.log
           ??31324 /usr/lib64/icinga2/sbin/icinga2 --no-stack-rlimit daemon --close-stdio -e /var/log/icinga2/error.log

2.接下来,如果你在centos 8系统上启动并运行了SELinux,那么你需要安装icinga2-selinux包。但如果 SELinux 被禁用,请跳过这部分。

执行以下命令检查 SELinux 状态。

sudo sestatus

如果您得到类似如下的输出,那么您的 SELinux 已启用。否则,您将禁用输出,这意味着您可以跳过 SELinux 部分。

SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   permissive
Mode from config file:          permissive
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Memory protection checking:     actual (secure)
Max kernel policy version:      32

现在使用以下命令安装 icinga2-selinux 包。

sudo dnf install -y icinga2-selinux

之后,通过重新启动 icinga2 服务来应用新配置,并验证 Icinga 是否已启动并在 SELinux 上下文中运行。

sudo systemctl restart icinga2
ps -eZ | grep icinga2

以下是您将获得的类似输出。

system_u:system_r:icinga2_t:s0    33479 ?        00:00:00 icinga2
system_u:system_r:icinga2_t:s0    33499 ?        00:00:00 icinga2
system_u:system_r:icinga2_t:s0    33504 ?        00:00:00 icinga2

3. 接下来,使用以下命令为 icinga2 安装 IDO MySQL 后端。

sudo dnf install -y icinga2-ido-mysql

现在执行下面的命令登录到 MySQL shell 并输入你的 root 密码。

sudo mysql -u root -p

使用下面的 mysql 查询为 icinga2 服务创建一个新的数据库和用户。

MariaDB [(none)]> CREATE DATABASE icinga;
MariaDB [(none)]> GRANT SELECT, INSERT, UPDATE, DELETE, DROP, CREATE VIEW, INDEX, EXECUTE ON icinga.* TO 'icinga'@'localhost' IDENTIFIED BY 'icingapassdb';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> quit

现在运行下面的 mysql 命令将 icinga2-ido-mysql 数据库模式导入 icinga 数据库,并输入您的 mariadb root 密码的密码。

sudo mysql -u root -p icinga < /usr/share/icinga2-ido-mysql/schema/mysql.sql

之后,启用 icinga2-ido-mysql 功能并重新启动 icinga2 服务以使用以下命令应用新配置。

sudo icinga2 feature enable ido-mysql
sudo systemctl restart icinga2

您将看到与下图类似的输出。

4. 接下来,您需要生成 icinga2 REST API 配置和证书。 icingaweb2 需要 REST API 发送与监控作业相关的操作,例如重新安排检查、查询对象详细信息等。

运行下面的 icinga2 命令以生成新的 API 用户、配置和 TLS 证书。

sudo icinga2 api setup

你会看到类似下面的输出,新用户 root 和密码在 /etc/icinga2/conf.d/api-users.conf 配置文件中可用,TLS 证书在 /var/lib/icinga2 中可用/证书/目录。

information/cli: Generating new CA.
information/base: Writing private key to '/var/lib/icinga2/ca//ca.key'.
information/base: Writing X509 certificate to '/var/lib/icinga2/ca//ca.crt'.
information/cli: Generating new CSR in '/var/lib/icinga2/certs//centos8.csr'.
information/base: Writing private key to '/var/lib/icinga2/certs//centos8.key'.
information/base: Writing certificate signing request to '/var/lib/icinga2/certs//centos8.csr'.
information/cli: Signing CSR with CA and writing certificate to '/var/lib/icinga2/certs//centos8.crt'.
information/pki: Writing certificate to file '/var/lib/icinga2/certs//centos8.crt'.
information/cli: Copying CA certificate to '/var/lib/icinga2/certs//ca.crt'.
information/cli: Adding new ApiUser 'root' in '/etc/icinga2/conf.d/api-users.conf'.
information/cli: Reading '/etc/icinga2/icinga2.conf'.
information/cli: Enabling the 'api' feature.
Enabling feature api. Make sure to restart Icinga 2 for these changes to take effect.
information/cli: Updating 'NodeName' constant in '/etc/icinga2/constants.conf'.
information/cli: Created backup file '/etc/icinga2/constants.conf.orig'.
information/cli: Updating 'ZoneName' constant in '/etc/icinga2/constants.conf'.
information/cli: Backup file '/etc/icinga2/constants.conf.orig' already exists. Skipping backup.
Done.

Now restart your Icinga 2 daemon to finish the installation!

或者,要手动添加新的 API 用户,请使用 nano 编辑器命令编辑配置 /etc/icinga2/conf.d/api-users.conf,如下所示。

sudo nano /etc/icinga2/conf.d/api-users.conf

粘贴以下示例配置,并确保根据需要定义 API 用户、密码和权限。

object ApiUser "icingaweb2" {
  password = "Wijsn8Z9eRs5E25d"
  permissions = [ "status/query", "actions/*", "objects/modify/*", "objects/query/*" ]
}

最后,通过使用以下命令重新启动 icinga2 服务来应用新配置。

sudo systemctl restart icinga2

安装和配置 Icingaweb2

在此阶段,您将安装和配置 icingaweb2 Web 应用程序,包括设置数据库和生成安装令牌。

1.执行以下命令安装icingaweb2包。

sudo dnf install -y icingaweb2 icingacli

安装期间,将生成 icingaweb2 的新 apache 配置。使用以下命令验证新配置。

cat /etc/httpd/conf.d/icingaweb2.conf

以下是您将看到的配置。

Alias /icingaweb2 "/usr/share/icingaweb2/public"

# Remove comments if you want to use PHP FPM and your Apache version is older than 2.4
#<IfVersion < 2.4>
#    # Forward PHP requests to FPM
#    SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
#    <LocationMatch "^{urlPath}/(.*\.php)$">
#        ProxyPassMatch "fcgi://127.0.0.1:9000/{documentRoot}/$1"
#    </LocationMatch>
#</IfVersion>

<Directory "/usr/share/icingaweb2/public">
    Options SymLinksIfOwnerMatch
    AllowOverride None
.......

要应用新配置,请使用以下命令重新启动 apache/httpd 服务。

sudo systemctl restart httpd

2.接下来,键入以下命令登录到mysql shell。

sudo mysql -u root -p

执行下面的一些 mysql 查询,为 icingaweb2 创建一个新的数据库和用户。

MariaDB [(none)]> CREATE DATABASE icingaweb2;
MariaDB [(none)]> GRANT ALL ON icingaweb2.* TO IDENTIFIED BY 'icingaweb2passdb';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> quit

3. 之后,使用以下命令为下一阶段的安装生成 icingaweb2 设置令牌。

sudo icingacli setup token create
sudo icingacli setup token show

您将显示为以下输出。确保将 icinga2 安装令牌复制到您的笔记中。

# Generated setup token
The newly generated setup token is: 41e33db4cf0c5617

# Show generated token
The current setup token is: 41e33db4cf0c5617

通过 Web 界面配置 Icinga Web 2

现在,打开您的 Web 浏览器和 URL http://SERVER-IP/icingaweb2/setup,您将看到 icinga2 安装页面。

1. 将 icingaweb2 设置标记粘贴到框上,然后单击下一步。

2. 在这里,将 Icinga 模块保留为默认值,然后单击下一步。

3. 现在确保所有 icingaweb2 系统要求都具有“绿色”颜色,然后单击下一步按钮。

4. 将后端身份验证保留为默认数据库,然后单击下一步。

5. 为 icingaweb2 键入数据库名称、用户名和密码,然后再次单击下一步。

6. 将身份验证后端名称保留为默认的 icingaweb2,然后单击下一步。

7. 现在键入新的用户和密码管理,然后单击下一步。

8. 将应用程序和日志配置保留为默认值,然后单击下一步。

9. 仔细检查所有配置,然后单击下一步继续。

10.现在点击下一步配置icinga2监控模块。

11. 为 icinga2-ido-mysql 键入数据库名称、用户名和密码,然后单击“验证配置”按钮。

如果您遇到类似 There is currently no icinga instance writing to the IDO 的错误。确保 icinga 实例已配置并且能够写入 IDO,返回到您的终端并手动编辑配置。

运行以下命令来编辑 IDO 配置。

sudo nano /etc/icinga2/features-available/ido-mysql.conf

如下所示复制并粘贴 ido mysql 配置。

object IdoMysqlConnection "ido-mysql" {
user = "icinga", password = "icingapassdb", host = "localhost", database = "icinga" }

现在按 Ctrl+x,然后按 y,然后按 enter 保存文件。

接下来,使用以下命令重新启动 Icinga2 服务。

sudo systemctl restart icinga2

现在回到 Web 浏览器并启用选项 Skip Validation,然后再次单击 Next。

12. 现在选择传输类型本地命令文件并单击下一步。

13. 将监控安全配置保留为默认值并单击下一步。

14. 仔细检查您的 Icinga 安装配置并单击完成继续。

15. 安装成功后,您将得到如下图所示的输出。然后单击登录到 Icinga Web 2 按钮。

验证 Icinga 2 和 Icinga Web 2 安装

1. 输入您的 Icinga 管理员用户和密码,然后单击登录按钮。

2. 现在你会得到一个类似如下的页面。

正如您在仪表板上看到的,icinga2 监控工具自动检测到一些服务。颜色为绿色的服务表示健康,颜色为黄色的服务表示警告需要检查。错误服务将显示为红色。

结论

教程完成后,您已经了解了使用 LAMP Stack 在最新的 CentOS 8 服务器上安装和配置 icinga2 监控工具。 icinga2的安装需要你专注于每个阶段,最重要的是你知道自己在做什么,而不是盲目地执行命令。

相关文章: