在本页

  • 先决条件
  • 设置软件仓库
  • 安装和配置 LAMP Stack
  • 安装和配置 Icinga2
  • 安装和配置 Icingaweb2
  • 通过 Web 界面配置 Icinga Web 2
  • 验证 Icinga 2 和 Icinga Web 2 的安装
  • 结论

监控是服务器基础设施和数据中心最重要的组件之一。它将帮助你检测服务器上出现的问题和故障,以便你更快地解决这些问题。目前有一些开源监控工具可用,其中最流行的之一是 ' Icinga 2 '。

Icinga 是作为流行的 Nagios 监控工具的一个分支而创建的,现在已用 C++ 从头重写。作为 Nagios 项目的一个分支,Icinga 附带了额外的工具和特性,包括 icinga director、icinga modules 和 icinga plugins。此外,它还支持分布式监控,使你可以从一个仪表板管理多个 Icinga 实例。

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

先决条件

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

设置软件仓库

首先,你需要将 epel、remi 和 icinga 软件仓库添加到 centos 8 系统。然后你必须安装 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 Stack

Icinga 监控工具有两个主要组件:" icinga2 " 处理所有监控任务," icingaweb2 " 是一个基于 PHP 的功能强大、简洁且快速的 Web 应用程序,使你能够从 Web 浏览器管理你的 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 处于 ' enabled ' 状态。否则,你将得到 ' disabled ' 的输出,这意味着你可以跳过 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

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

如何在 CentOS 8 上安装 Icinga 2 监控工具的完整教程

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/certs/ 目录中。

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 icingaweb2@localhost 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 设置令牌粘贴到框中,然后点击 Next 。

如何在 CentOS 8 上安装 Icinga 2 监控工具的完整教程

2. 此处,将 Icinga 模块保持默认,然后点击 Next 。

如何在 CentOS 8 上安装 Icinga 2 监控工具的完整教程

3. 现在确保所有 icingaweb2 系统要求都显示 ` green ` 颜色,然后点击 Next 按钮。

如何在 CentOS 8 上安装 Icinga 2 监控工具的完整教程

4. 将后端认证保持默认 ' Database ',然后点击 Next 。

如何在 CentOS 8 上安装 Icinga 2 监控工具的完整教程

5. 输入 icingaweb2 的数据库名称、用户名和密码,然后再次点击 Next 。

如何在 CentOS 8 上安装 Icinga 2 监控工具的完整教程

6. 将认证后端名称保持默认 ' icingaweb2 ',然后点击 Next 。

如何在 CentOS 8 上安装 Icinga 2 监控工具的完整教程

7. 现在输入新的管理员用户和密码,然后点击 Next 。

如何在 CentOS 8 上安装 Icinga 2 监控工具的完整教程

8. 将应用程序和日志配置保持默认,然后点击 Next 。

如何在 CentOS 8 上安装 Icinga 2 监控工具的完整教程

9. 仔细核对所有配置,然后点击 Next 继续。

如何在 CentOS 8 上安装 Icinga 2 监控工具的完整教程

10. 现在点击 Next 来配置 icinga2 监控模块。

如何在 CentOS 8 上安装 Icinga 2 监控工具的完整教程

11. 输入 icinga2-ido-mysql 的数据库名称、用户名和密码,然后点击 Validate Configuration 按钮。

如果出现类似 ' 当前没有 icinga 实例写入 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 。

如何在 CentOS 8 上安装 Icinga 2 监控工具的完整教程

12. 现在选择传输类型 ' Local Command File ',然后点击 Next 。

如何在 CentOS 8 上安装 Icinga 2 监控工具的完整教程

13. 将监控安全配置保持默认,然后点击 Next 。

如何在 CentOS 8 上安装 Icinga 2 监控工具的完整教程

14. 仔细核对你的 Icinga 安装配置,然后点击 Finish 继续。

如何在 CentOS 8 上安装 Icinga 2 监控工具的完整教程

15. 安装成功后,你将得到如下图所示的输出。然后点击 Login to Icinga Web 2 按钮。

如何在 CentOS 8 上安装 Icinga 2 监控工具的完整教程

验证 Icinga 2 和 Icinga Web 2 的安装

1. 输入你的 Icinga 管理员用户和密码,然后点击 Login 按钮。

如何在 CentOS 8 上安装 Icinga 2 监控工具的完整教程

2. 现在你将看到类似下面的页面。

如何在 CentOS 8 上安装 Icinga 2 监控工具的完整教程

正如你在仪表板上看到的,有一些服务被 icinga2 监控工具自动检测到。绿色的服务表示 health ,黄色的服务表示需要检查的 warning 。错误服务将显示为红色。

结论

随着本教程的完成,你已经学习了在带有 LAMP Stack 的最新 CentOS 8 服务器上安装和配置 icinga2 监控工具。icinga2 的安装要求你专注于每个阶段,最重要的是你知道自己在做什么,而不是盲目执行命令。