本教程适用于以下操作系统版本

  • Ubuntu 22.04(Jammy Jellyfish)
  • Ubuntu 20.04(Focal Fossa)
  • Ubuntu 18.04(Bionic Beaver)

本页内容

  • 前置条件
  • 步骤 1 - 在主服务器上配置防火墙
  • 步骤 2 - 安装 MySQL
  • 步骤 3 - 配置 MySQL
  • 步骤 4 - 在主服务器上安装 Icinga2 和监控插件
  • 步骤 5 - 在主服务器上安装 IDO MySQL 驱动
  • 步骤 6 - 配置 Icinga2 API
  • 步骤 7 - 安装 Nginx
  • 步骤 8 - 安装 SSL
  • 步骤 9 - 配置 Nginx 和 PHP 配置 PHP-FPM 配置 Nginx
  • 步骤 10 - 准备 Web 安装
  • 步骤 11 - 安装配置 IcingaWeb
  • 步骤 12 - 初始化主服务器
  • 步骤 13 - 在客户端服务器上初始化 Icinga2 Agent
  • 步骤 14 - 在主服务器上创建 Zones 配置
  • 步骤 15 - 在 Icinga 仪表盘上验证
  • 总结
  • 配置 PHP-FPM
  • 配置 Nginx

Icinga2 是一款免费的开源网络监控软件,可以检查来自网络的资源可用性,并在发生中断时通知用户。你可以使用 Icinga2 监控网络服务(SMTP、POP3、HTTP、NNTP、ping)、主机资源(CPU 使用率、磁盘使用率)以及网络组件(交换机、路由器、温度和湿度传感器)。它可以与 Nagios 插件集成。

在本教程中,你将学习如何在 Ubuntu 22.04 服务器上安装 Icinga2,以及如何将其连接到客户端节点。我们将使用 Nginx 而不是默认的 Apache 服务器来运行 Icinga2 Web。

前置条件

  • 两台运行 Ubuntu 22.04 的机器。其中一台作为主服务器,另一台作为被监控的客户端。
  • 在两台服务器上均拥有 sudo 权限的非 root 用户。
  • 主服务器的完整域名(FQDN)icinga.example.com,以及客户端节点 client.example.com。
  • 确保所有软件包都已更新。$ sudo apt update $ sudo apt upgrade
  • 系统需要安装少量软件包。$ sudo apt install wget curl nano software-properties-common dirmngr apt-transport-https gnupg2 ca-certificates lsb-release ubuntu-keyring unzip -y 其中部分软件包可能已经安装在你的系统上。

两台运行 Ubuntu 22.04 的机器。其中一台作为主服务器,另一台作为被监控的客户端。

在两台服务器上均拥有 sudo 权限的非 root 用户。

主服务器的完整域名(FQDN)icinga.example.com,以及客户端节点 client.example.com。

确保所有软件包都已更新。

$ sudo apt update
$ sudo apt upgrade

系统需要安装少量软件包。

$ sudo apt install wget curl nano software-properties-common dirmngr apt-transport-https gnupg2 ca-certificates lsb-release ubuntu-keyring unzip -y

其中部分软件包可能已经安装在你的系统上。

步骤 1 - 在主服务器上配置防火墙

第一步是配置防火墙。Ubuntu 默认自带 ufw(Uncomplicated Firewall)。

检查防火墙是否正在运行。

$ sudo ufw status

你应该会看到如下输出。

Status: inactive

放行 SSH 端口,以免启用防火墙时中断当前的连接。

$ sudo ufw allow OpenSSH

放行端口 5665,这是 Icinga2 客户端连接服务器所必需的。

$ sudo ufw allow 5665

同时放行 HTTP 和 HTTPS 端口。

$ sudo ufw allow http
$ sudo ufw allow https

启用防火墙

$ sudo ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup

再次检查防火墙状态。

$ sudo ufw status

你应该会看到类似的输出。

Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
80/tcp                     ALLOW       Anywhere
443                        ALLOW       Anywhere
5665                       ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
80/tcp (v6)                ALLOW       Anywhere (v6)
443 (v6)                   ALLOW       Anywhere (v6)
5665 (v6)                  ALLOW       Anywhere (v6)

步骤 2 - 安装 MySQL

Ubuntu 22.04 自带最新版本的 MySQL。你可以使用一条命令安装它。

$ sudo apt install mysql-server

检查 MySQL 的版本。

$ mysql --version
mysql  Ver 8.0.32-0ubuntu0.22.04.2 for Linux on x86_64 ((Ubuntu))

对于 MySQL 8.0.28 及以上版本,此步骤是必需的。进入 MySQL Shell。

$ sudo mysql

运行以下命令为 root 用户设置密码。请确保密码包含数字、大写字母、小写字母和特殊字符的混合。

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourPassword12!';

退出 Shell。

mysql> exit

运行 MySQL 安全安装脚本。

$ sudo mysql_secure_installation

首先,系统会要求输入 root 密码,请输入。接下来,系统会询问是否安装 Validate Password Component(密码验证组件),它用于检查 MySQL 中使用的密码强度。按 Y 安装。接着,系统会要求设置密码验证策略的级别。选择 2,因为它是最强级别。

Securing the MySQL server deployment.

Enter password for user root:

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: Y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Using existing password for root.

Estimated strength of the password: 100

接下来,输入 N 拒绝更改 root 密码。同时,输入 Y 以删除匿名用户、禁止 root 远程登录、删除测试数据库并重新加载权限表。

Change the password for root ? ((Press y|Y for Yes, any other key for No) : N

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : Y
Success.

By default, MySQL 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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : Y
Success.

All done!

步骤 3 - 配置 MySQL

登录 MySQL Shell。在提示时输入你的 root 密码。

$ sudo mysql -u root -p

创建 Icinga 数据库。

mysql> CREATE DATABASE icinga2;

为 Icinga2 创建 SQL 用户账户。不要更改数据库名和用户名,因为它们已经使用默认值设置好了。如果你想更改它们,则需要在第 5 步安装 MySQL 驱动时执行一些额外步骤。输入密码后,你可能会收到一个错误,然后会被要求重新配置,在那里你可以指定自定义的数据库名和用户。

mysql> CREATE USER 'icinga2'@'localhost' IDENTIFIED BY 'Your_password2';

将该数据库的所有权限授予该用户。

mysql> GRANT ALL PRIVILEGES ON icinga2.* TO 'icinga2'@'localhost';

刷新用户权限。

mysql> FLUSH PRIVILEGES;

退出 Shell。

mysql> exit

步骤 4 - 在主服务器上安装 Icinga2 和监控插件

我们将使用 Icinga2 官方仓库进行安装。下载并导入 Icinga2 GPG 密钥。

$ wget -O - https://packages.icinga.com/icinga.key | sudo gpg --dearmor -o /usr/share/keyrings/icinga-archive-keyring.gpg

运行以下命令,创建并将 Icinga2 仓库信息添加到 APT 源列表。

$ echo "deb [signed-by=/usr/share/keyrings/icinga-archive-keyring.gpg] https://packages.icinga.com/ubuntu icinga-`lsb_release -cs` main" | sudo tee /etc/apt/sources.list.d/$(lsb_release -cs)-icinga.list
$ echo "deb-src [signed-by=/usr/share/keyrings/icinga-archive-keyring.gpg] http://packages.icinga.com/ubuntu icinga-`lsb_release -cs` main" | sudo tee -a /etc/apt/sources.list.d/$(lsb_release -cs)-icinga.list

更新系统仓库列表。

$ sudo apt update

安装 Icinga2 和监控插件。

$ sudo apt install icinga2 monitoring-plugins -y

步骤 5 - 在主服务器上安装 IDO MySQL 驱动

Icinga2 要正常工作需要一个数据库。为此,我们需要安装 IDO MySQL 驱动并配置数据库连接。运行以下命令安装 MySQL 驱动。

$ sudo apt install -y icinga2-ido-mysql

系统会提示你使用 dbconfig-common 工具来配置该驱动并创建数据库。选择 Yes 继续。

在 Ubuntu 22.04 LTS 系统上安装并配置 Icinga 2 监控软件教程

接下来,系统会要求输入 icinga2 数据库的 MySQL 密码。输入在第 3 步中配置的密码以继续。

在 Ubuntu 22.04 LTS 系统上安装并配置 Icinga 2 监控软件教程

系统会再次要求确认密码。

在 Ubuntu 22.04 LTS 系统上安装并配置 Icinga 2 监控软件教程

接下来,系统会询问是否启用 ido-mysql 功能。选择 Yes 继续。

在 Ubuntu 22.04 LTS 系统上安装并配置 Icinga 2 监控软件教程

你可以在 /etc/icinga2/features-available/ido-mysql.conf 文件中查看数据库详细信息。

$ cat /etc/icinga2/features-available/ido-mysql.conf
/**
 * The db_ido_mysql library implements IDO functionality
 * for MySQL.
 */

library "db_ido_mysql"

object IdoMysqlConnection "ido-mysql" {
  user = "icinga2",
  password = "Your_password2",
  host = "localhost",
  database = "icinga2"
}

启用 ido-mysql 功能。

$ sudo icinga2 feature enable ido-mysql

重启 Icinga2 服务。

$ sudo systemctl restart icinga2

验证服务状态。

$ sudo systemctl status icinga2
? icinga2.service - Icinga host/service/network monitoring system
     Loaded: loaded (/lib/systemd/system/icinga2.service; enabled; vendor preset: enabled)
    Drop-In: /etc/systemd/system/icinga2.service.d
             ??limits.conf
     Active: active (running) since Mon 2023-05-01 08:02:36 UTC; 1s ago
    Process: 9823 ExecStartPre=/usr/lib/icinga2/prepare-dirs /etc/default/icinga2 (code=exited, status=0/SUCCESS)
   Main PID: 9828 (icinga2)
     Status: "Startup finished."
      Tasks: 10
     Memory: 17.6M
        CPU: 657ms
     CGroup: /system.slice/icinga2.service
             ??9828 /usr/lib/x86_64-linux-gnu/icinga2/sbin/icinga2 --no-stack-rlimit daemon --close-stdio -e /var/log/icinga2/error.log
             ??9846 /usr/lib/x86_64-linux-gnu/icinga2/sbin/icinga2 --no-stack-rlimit daemon --close-stdio -e /var/log/icinga2/error.log
             ??9849 /usr/lib/x86_64-linux-gnu/icinga2/sbin/icinga2 --no-stack-rlimit daemon --close-stdio -e /var/log/icinga2/error.log

步骤 6 - 配置 Icinga2 API

要通过 HTTP 管理和配置 Icinga2 监控,你需要配置 Icinga2 API。运行以下命令以启用 Icinga2 API、为 Icinga2 生成 TLS 证书并更新 Icinga2 配置。

$ sudo icinga2 api setup

你应该会看到类似的输出。

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//icinga.example.com.csr'.
information/base: Writing private key to '/var/lib/icinga2/certs//icinga.example.com.key'.
information/base: Writing certificate signing request to '/var/lib/icinga2/certs//icinga.example.com.csr'.
information/cli: Signing CSR with CA and writing certificate to '/var/lib/icinga2/certs//icinga.example.com.crt'.
information/pki: Writing certificate to file '/var/lib/icinga2/certs//icinga.example.com.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!

上述命令会创建一个 /etc/icinga2/conf.d/api-users.conf 文件,其中默认用户 root 拥有 Icinga2 API 的全部权限。我们需要创建一个具有 Icinga Web 所需最小权限的新用户。

打开 api-users.conf 文件进行编辑。

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

在文件末尾添加以下代码。

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

记下这些凭据,稍后访问网站时会用到。Icinga2 API 服务器默认监听端口 5665。重启服务以使更改生效。

$ sudo systemctl restart icinga2

下一步是安装 Icinga Web 界面。它默认针对 Apache 预先配置,但我们将使用 Nginx 服务器。因此,我们首先需要安装 Nginx 和 SSL 证书。

步骤 7 - 安装 Nginx

Ubuntu 22.04 自带较旧版本的 Nginx。要安装最新版本,你需要下载官方的 Nginx 仓库。

导入 Nginx 的签名密钥。

$ curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

添加 Nginx 稳定版的仓库。

$ echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg arch=amd64] \
http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list

更新系统仓库。

$ sudo apt update

安装 Nginx。

$ sudo apt install nginx

验证安装。

$ nginx -v
nginx version: nginx/1.24.0

启动 Nginx 服务器。

$ sudo systemctl start nginx

步骤 8 - 安装 SSL

我们需要安装 Certbot 来生成 SSL 证书。你可以使用 Ubuntu 的仓库安装 Certbot,也可以通过 Snapd 工具获取最新版本。我们将使用 Snapd 版本。

Ubuntu 22.04 默认已安装 Snapd。运行以下命令以确保你的 Snapd 版本是最新的。

$ sudo snap install core && sudo snap refresh core

安装 Certbot。

$ sudo snap install --classic certbot

使用以下命令,通过创建指向 /usr/bin 目录的符号链接,确保 Certbot 命令可以运行。

$ sudo ln -s /snap/bin/certbot /usr/bin/certbot

运行以下命令生成 SSL 证书。

$ sudo certbot certonly --nginx --agree-tos --no-eff-email --staple-ocsp --preferred-challenges http -m 
[email protected]
 -d icinga.example.com

上述命令会将证书下载到服务器上的 /etc/letsencrypt/live/icinga.example.com 目录。

生成 Diffie-Hellman 组证书。

$ sudo openssl dhparam -dsaparam -out /etc/ssl/certs/dhparam.pem 4096

检查 Certbot 续期调度服务。

$ sudo systemctl list-timers

你会发现 snap.certbot.renew.service 是计划运行的服务之一。

NEXT                        LEFT          LAST                        PASSED         UNIT                     ACTIVATES
------------------------------------------------------------------------------------------------------------------------------------
Mon 2023-05-01 13:37:57 UTC 3h 45min left Mon 2023-05-01 07:20:42 UTC 2h 31min ago   ua-timer.timer           ua-timer.service
Mon 2023-05-01 14:39:29 UTC 4h 47min left Sat 2023-02-04 16:04:18 UTC 2 months ago   motd-news.timer          motd-news.service
Mon 2023-05-01 15:53:00 UTC 6h left       n/a                         n/a            snap.certbot.renew.timer snap.certbot.renew.service

对流程进行一次演练(dry run),以检查 SSL 续期是否正常工作。

$ sudo certbot renew --dry-run

如果没有看到错误,说明一切就绪。你的证书将自动续期。

步骤 9 - 配置 Nginx 和 PHP

由于 Icinga 是针对 Apache 配置的,PHP-FPM 软件包默认未安装。如果你想将图表导出为 PDF,还需要 PHP Imagick 模块。运行以下命令安装 PHP-FPM 和 PHP Imagick 库。

$ sudo apt install php-fpm php-imagick

配置 PHP-FPM

打开文件 /etc/php/8.1/fpm/pool.d/www.conf。

$ sudo nano /etc/php/8.1/fpm/pool.d/www.conf

我们需要将 PHP 进程的 Unix 用户/组设置为 nginx。在文件中找到 user=www-data 和 group=www-data 两行,并将它们改为 nginx。

...
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
user = nginx
group = nginx
...

在文件中找到 listen.owner = www-data 和 listen.group = www-data 两行,并将它们改为 nginx。

; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server. Many
; BSD-derived systems allow connections regardless of permissions. The owner
; and group can be specified either by name or by their numeric IDs.
; Default Values: user and group are set as the running user
;                 mode is set to 0660
listen.owner = nginx
listen.group = nginx

按 Ctrl + X 保存文件,并在提示时输入 Y。

重启 PHP-FPM 服务。

$ sudo systemctl restart php8.1-fpm

配置 Nginx

创建并打开文件 /etc/nginx/conf.d/icinga.conf 进行编辑。

$ sudo nano /etc/nginx/conf.d/icinga.conf

在其中粘贴以下代码。

server {
    listen       443 ssl http2;
    listen       [::]:443 ssl http2;
    server_name  icinga.example.com;

    access_log  /var/log/nginx/icinga.access.log;
    error_log   /var/log/nginx/icinga.error.log;

    # SSL
    ssl_certificate      /etc/letsencrypt/live/icinga.example.com/fullchain.pem;
    ssl_certificate_key  /etc/letsencrypt/live/icinga.example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/icinga.example.com/chain.pem;
    ssl_session_timeout  5m;
    ssl_session_cache shared:MozSSL:10m;
    ssl_session_tickets off;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
    ssl_ecdh_curve X25519:prime256v1:secp384r1:secp521r1;
    ssl_stapling on;
    ssl_stapling_verify on;
    ssl_dhparam /etc/ssl/certs/dhparam.pem;
    resolver 8.8.8.8;

    location ~ ^/index\.php(.*)$ {
        # fastcgi_pass 127.0.0.1:9000;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock; # Depends On The PHP Version
        fastcgi_index index.php;
        # try_files $uri =404;
        # fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /usr/share/icingaweb2/public/index.php;
        fastcgi_param ICINGAWEB_CONFIGDIR /etc/icingaweb2;
        fastcgi_param REMOTE_USER $remote_user;
    }

    location ~ ^/(.*)? {
        alias /usr/share/icingaweb2/public;
        index index.php;
        rewrite ^/$ /dashboard;
        try_files $1 $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        return 404;
    }
}

# enforce HTTPS
server {
    listen       80;
    listen       [::]:80;
    server_name  icinga.example.com;
    return 301   https://$host$request_uri;
}

注意,Nginx 配置中使用的根目录是 /usr/share/icingaweb2/public。

完成后按 Ctrl + X 保存文件,并在提示时输入 Y。

打开文件 /etc/nginx/nginx.conf 进行编辑。

$ sudo nano /etc/nginx/nginx.conf

在 include /etc/nginx/conf.d/*.conf; 这一行之前添加以下行。

server_names_hash_bucket_size  64;

按 Ctrl + X 保存文件,并在提示时输入 Y。

验证 Nginx 配置文件语法。

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重启 Nginx 服务。

$ sudo systemctl restart nginx

步骤 10 - 准备 Web 安装

在访问 Icinga Web 之前,我们需要对其进行设置。使用 Icinga Web 时,需要使用令牌进行身份验证。使用以下命令生成令牌。

$ sudo icingacli setup token create
The newly generated setup token is: 3c4d4e155635f512

记下该令牌,因为稍后会用到。你也可以随时使用以下命令重新获取它。

$ sudo icingacli setup token show
The current setup token is: 3c4d4e155635f512

下一步是创建数据库和数据库用户。登录 MySQL Shell。

$ sudo mysql -u root -p

创建 Icinga Web 数据库。

mysql> CREATE DATABASE icingaweb2;

为 Icinga Web 创建 SQL 用户账户。

mysql> CREATE USER 'icingaweb2'@'localhost' IDENTIFIED BY 'Your_password3';

将该数据库的所有权限授予该用户。

mysql> GRANT ALL PRIVILEGES ON icingaweb2.* TO 'icingaweb2'@'localhost';

刷新用户权限。

mysql> FLUSH PRIVILEGES;

退出 Shell。

mysql> exit

步骤 11 - 安装配置 IcingaWeb

在浏览器中打开 URL https://icinga.example.com,你将看到以下界面。

在 Ubuntu 22.04 LTS 系统上安装并配置 Icinga 2 监控软件教程

输入上一步生成的令牌,然后按 Next 按钮继续。

在 Ubuntu 22.04 LTS 系统上安装并配置 Icinga 2 监控软件教程

在下一个界面中,选择你要安装的模块,然后点击 Next 继续。Monitoring 模块默认已为你选中。在接下来的页面中,会显示各项依赖要求及其是否满足。请确保所有要求均标记为绿色。

在 Ubuntu 22.04 LTS 系统上安装并配置 Icinga 2 监控软件教程

点击 Next 进入下一页以选择身份验证类型。

在 Ubuntu 22.04 LTS 系统上安装并配置 Icinga 2 监控软件教程

身份验证类型默认设置为 Database(数据库)。点击 Next 继续。在下一页中,系统会要求填写数据库凭据。

在 Ubuntu 22.04 LTS 系统上安装并配置 Icinga 2 监控软件教程

填写在第 10 步中创建的数据库凭据。点击 Validate Configuration(验证配置)按钮以验证凭据。验证通过后,点击 Next 继续。接下来,系统会要求为身份验证后端命名。

在 Ubuntu 22.04 LTS 系统上安装并配置 Icinga 2 监控软件教程

保留默认值并点击 Next 继续。在下一页中,系统会要求创建一个管理员账户。

在 Ubuntu 22.04 LTS 系统上安装并配置 Icinga 2 监控软件教程

输入新管理员账户的凭据,然后点击 Next 继续。接下来,将显示 Application Configuration(应用配置)页面。

在 Ubuntu 22.04 LTS 系统上安装并配置 Icinga 2 监控软件教程

保留应用和日志配置的所有默认值,然后点击 Next 继续。在最后一页,系统会要求你检查配置。

在 Ubuntu 22.04 LTS 系统上安装并配置 Icinga 2 监控软件教程

你可以返回修改任意设置。如果满意,点击 Next 继续。

在 Ubuntu 22.04 LTS 系统上安装并配置 Icinga 2 监控软件教程

点击 Next 继续配置监控模块。接下来,系统会要求输入 Icinga 数据库凭据。

在 Ubuntu 22.04 LTS 系统上安装并配置 Icinga 2 监控软件教程

填写第 3 步中的数据库凭据,然后点击 Validate Configuration 验证连接。验证通过后,点击 Next 继续。接下来,系统会要求填写 API 详细信息。

在 Ubuntu 22.04 LTS 系统上安装并配置 Icinga 2 监控软件教程

填写第 6 步中创建的 API 凭据,然后点击 Validate Configuration 验证连接。点击 Next 继续。接下来,系统会要求为监控安全选择受保护的自定义变量。

在 Ubuntu 22.04 LTS 系统上安装并配置 Icinga 2 监控软件教程

保留默认值并点击 Next 继续。接下来,系统会要求检查 Monitoring 配置。如果需要,你可以返回修改。

在 Ubuntu 22.04 LTS 系统上安装并配置 Icinga 2 监控软件教程

如果满意,点击 Finish 完成安装。

在 Ubuntu 22.04 LTS 系统上安装并配置 Icinga 2 监控软件教程

成功完成后,点击 Login to Icinga Web 2 按钮打开登录页面。

在 Ubuntu 22.04 LTS 系统上安装并配置 Icinga 2 监控软件教程

输入你的管理员账户信息,然后点击 Login 按钮打开 Icinga Web 仪表盘。

在 Ubuntu 22.04 LTS 系统上安装并配置 Icinga 2 监控软件教程

访问 Overview >> Services 页面,查看主服务器的状态,如下所示。

在 Ubuntu 22.04 LTS 系统上安装并配置 Icinga 2 监控软件教程

步骤 12 - 初始化主服务器

下一步是将主服务器初始化为主节点。主节点充当监控体系的主控制器。运行以下命令启动初始化过程。

$ sudo icinga2 node wizard

系统会提示是否为 agent 安装。输入 n 以设置主节点。

Welcome to the Icinga 2 Setup Wizard!

We will guide you through all required configuration details.

Please specify if this is an agent/satellite setup ('n' installs a master setup) [Y/n]: n

接下来,系统会要求输入 common name(通用名)或域名。按 Enter 选择所显示的默认值。

Please specify the common name (CN) [icinga.example.com]:
Reconfiguring Icinga...
Checking for existing certificates for common name 'icinga.example.com'...
Certificate '/var/lib/icinga2/certs//icinga.example.com.crt' for CN 'icinga.example.com' already existing. Skipping certificate generation.
Generating master configuration for Icinga 2.
'api' feature already enabled.

接下来,输入主区域(master zone)名称,然后按 Enter 继续。在本例中,它与服务器域名相同。

Master zone name [master]: icinga.example.com

接下来,系统会询问是否要添加额外的全局区域(global zones)。按 n 跳过添加,然后按 Enter 继续。

Default global zones: global-templates director-global
Do you want to specify additional global zones? [y/N]: n

在下一步中,保持 API 绑定主机和端口为默认值,然后按 Enter 继续。

Please specify the API bind host/port (optional):
Bind Host []:
Bind Port []:

接下来,按 Y 禁用 /etc/icinga2/conf.d/ 目录中的配置,因为我们稍后将使用 Icinga2 Zones 配置。

Do you want to disable the inclusion of the conf.d directory [Y/n]: Y
Disabling the inclusion of the conf.d directory...
Checking if the api-users.conf file exists...

Done.

Now restart your Icinga 2 daemon to finish the installation!

重启服务以使更改生效。

$ sudo systemctl restart icinga2

最后但同样重要的是,运行以下命令为客户端服务器创建票据(ticket)。使用客户端的域名作为参数。

$ sudo icinga2 pki ticket --cn 'client.example.com'
365cd931d3091537622a95b113b17775893a224f

记下该票据以备后用。

步骤 13 - 在客户端服务器上初始化 Icinga2 Agent

登录客户端服务器并安装 Icinga2 和监控插件。运行以下命令完成安装。

$ wget -O - https://packages.icinga.com/icinga.key | sudo gpg --dearmor -o /usr/share/keyrings/icinga-archive-keyring.gpg
$ echo "deb [signed-by=/usr/share/keyrings/icinga-archive-keyring.gpg] https://packages.icinga.com/ubuntu icinga-`lsb_release -cs` main" | sudo tee /etc/apt/sources.list.d/$(lsb_release -cs)-icinga.list
$ echo "deb-src [signed-by=/usr/share/keyrings/icinga-archive-keyring.gpg] http://packages.icinga.com/ubuntu icinga-`lsb_release -cs` main" | sudo tee -a /etc/apt/sources.list.d/$(lsb_release -cs)-icinga.list
$ sudo apt update
$ sudo apt install icinga2 monitoring-plugins -y

验证 Icinga 服务是否已启用并正在运行。

$ sudo systemctl status icinga2
? icinga2.service - Icinga host/service/network monitoring system
     Loaded: loaded (/lib/systemd/system/icinga2.service; enabled; vendor preset: enabled)
    Drop-In: /etc/systemd/system/icinga2.service.d
             ??limits.conf
     Active: active (running) since Tue 2023-05-02 07:32:07 UTC; 59min ago
   Main PID: 7602 (icinga2)
     Status: "Startup finished."
      Tasks: 8
     Memory: 13.9M
        CPU: 1.018s
     CGroup: /system.slice/icinga2.service
             ??7602 /usr/lib/x86_64-linux-gnu/icinga2/sbin/icinga2 --no-stack-rlimit daemon --close-stdio -e /var/log/icinga2/error.log
             ??7620 /usr/lib/x86_64-linux-gnu/icinga2/sbin/icinga2 --no-stack-rlimit daemon --close-stdio -e /var/log/icinga2/error.log
             ??7623 /usr/lib/x86_64-linux-gnu/icinga2/sbin/icinga2 --no-stack-rlimit daemon --close-stdio -e /var/log/icinga2/error.log

启动 Icinga Node Wizard 以在客户端服务器上初始化 agent。

$ sudo icinga2 node wizard

系统会提示是否为 agent 安装。输入 Y 以设置 agent。

Welcome to the Icinga 2 Setup Wizard!

We will guide you through all required configuration details.

Please specify if this is an agent/satellite setup ('n' installs a master setup) [Y/n]: Y

接下来,系统会要求指定 common name(通用名)。保留默认值并按 Enter 继续。

Starting the Agent/Satellite setup routine...

Please specify the common name (CN) [client.example.com]:

接下来,将父端点(parent endpoint)指定为 icinga.example.com,并输入 Y 以从客户端建立到父节点的连接。

Please specify the parent endpoint(s) (master or satellite) where this node should connect to:
Master/Satellite Common Name (CN from your master/satellite node): icinga.example.com

Do you want to establish a connection to the parent node from this node? [Y/n]: Y

接下来,输入主服务器的 IP 地址,并保持端口值为默认不变。

Please specify the master/satellite connection information:
Master/Satellite endpoint host (IP address or FQDN): 199.247.31.184
Master/Satellite endpoint port [5665]:

输入 N 以拒绝添加更多主端点。

Add more master/satellite endpoints? [y/N]: N

接下来,将显示主服务器的证书信息。按 Y 确认信息并继续。

Parent certificate information:

 Version:             3
 Subject:             CN = icinga.example.com
 Issuer:              CN = Icinga CA
 Valid From:          May  1 08:28:26 2023 GMT
 Valid Until:         Jun  1 08:28:26 2024 GMT
 Serial:              53:50:d4:1a:85:c5:70:5b:b1:f6:0e:c3:b6:e4:db:62:df:8d:5e:d9

 Signature Algorithm: sha256WithRSAEncryption
 Subject Alt Names:   icinga.example.com
 Fingerprint:         DC BB 90 68 29 E5 C4 B5 74 F5 BC 8C BF A1 8F BF D2 9D A4 E5 A9 9A 43 2C 3B 24 AE A9 CD 19 32 F4

Is this information correct? [y/N]: Y

接下来,输入在上一步中生成的请求票据(request ticket)。

Please specify the request ticket generated on your Icinga 2 master (optional).
 (Hint: # icinga2 pki ticket --cn 'client.example.com'): 365cd931d3091537622a95b113b17775893a224f

保持 API 绑定主机和端口为默认,然后按 Enter 继续。

Please specify the API bind host/port (optional):
Bind Host []:
Bind Port []:

接下来,输入两次 Y 以接受来自主节点的配置和命令。

Accept config from parent node? [y/N]: Y
Accept commands from parent node? [y/N]: Y

按 Enter 接受默认本地区域名(即客户端域名)。输入主服务器域名作为父区域(parent zone)名称以继续。

Reconfiguring Icinga...
Disabling feature notification. Make sure to restart Icinga 2 for these changes to take effect.
Enabling feature api. Make sure to restart Icinga 2 for these changes to take effect.

Local zone name [client.example.com]:
Parent zone name [master]: icinga.example.com

按 N 跳过添加额外的全局区域。

Default global zones: global-templates director-global
Do you want to specify additional global zones? [y/N]: N

按 N 跳过禁用来自 /etc/icinga2/conf.d/ 目录的配置。

Do you want to disable the inclusion of the conf.d directory [Y/n]: Y
Disabling the inclusion of the conf.d directory...

Done.

Now restart your Icinga 2 daemon to finish the installation!

重启 Icinga 服务以使配置更改生效。

$ sudo systemctl restart icinga2

步骤 14 - 在主服务器上创建 Zones 配置

重新登录服务器并创建一个新目录作为默认区域。

$ sudo mkdir -p /etc/icinga2/zones.d/icinga.example.com/

接下来,在新创建的目录中创建一个配置文件并打开进行编辑。

$ sudo nano /etc/icinga2/zones.d/icinga.example.com/client.example.com.conf

在其中粘贴以下代码。代码中的 IP 地址应与客户端的公网 IP 地址一致。

// Endpoints
object Endpoint "client.example.com" {
}
// Zones
object Zone "client.example.com" {
    endpoints = [ "client.example.com" ]
    parent = "icinga.example.com"
}
// Host Objects
object Host "client.example.com" {
    check_command = "hostalive"
    address = "95.179.138.148"
    vars.client_endpoint = name
}

完成后按 Ctrl + X 保存文件,并在提示时输入 Y。

创建并打开 services 文件进行编辑。

$ sudo nano /etc/icinga2/zones.d/icinga.example.com/services.conf

在其中粘贴以下代码。

// Ping
 apply Service "Ping" {
 check_command = "ping4"
 assign where host.address // check executed on master
 }
 // System Load
 apply Service "System Load" {
 check_command = "load"
 command_endpoint = host.vars.client_endpoint // Check executed on client01
 assign where host.vars.client_endpoint
 }
 // SSH Service
 apply Service "SSH Service" {
 check_command = "ssh"
 command_endpoint = host.vars.client_endpoint
 assign where host.vars.client_endpoint
 }
 // Icinga 2 Service
 apply Service "Icinga2 Service" {
 check_command = "icinga"
 command_endpoint = host.vars.client_endpoint
 assign where host.vars.client_endpoint
 }

运行以下命令以验证配置。

$ sudo icinga2 daemon -C

你应该会看到类似的输出。

[2023-05-02 11:21:23 +0000] information/cli: Icinga application loader (version: r2.13.7-1)
[2023-05-02 11:21:23 +0000] information/cli: Loading configuration file(s).
[2023-05-02 11:21:23 +0000] information/ConfigItem: Committing config item(s).
[2023-05-02 11:21:23 +0000] information/ApiListener: My API identity: icinga.example.com
[2023-05-02 11:21:23 +0000] information/ConfigItem: Instantiated 1 IcingaApplication.
[2023-05-02 11:21:23 +0000] information/ConfigItem: Instantiated 1 Host.
[2023-05-02 11:21:23 +0000] information/ConfigItem: Instantiated 1 FileLogger.
[2023-05-02 11:21:23 +0000] information/ConfigItem: Instantiated 1 IdoMysqlConnection.
[2023-05-02 11:21:23 +0000] information/ConfigItem: Instantiated 1 CheckerComponent.
[2023-05-02 11:21:23 +0000] information/ConfigItem: Instantiated 4 Zones.
[2023-05-02 11:21:23 +0000] information/ConfigItem: Instantiated 2 Endpoints.
[2023-05-02 11:21:23 +0000] information/ConfigItem: Instantiated 2 ApiUsers.
[2023-05-02 11:21:23 +0000] information/ConfigItem: Instantiated 1 ApiListener.
[2023-05-02 11:21:23 +0000] information/ConfigItem: Instantiated 1 NotificationComponent.
[2023-05-02 11:21:23 +0000] information/ConfigItem: Instantiated 244 CheckCommands.
[2023-05-02 11:21:23 +0000] information/ConfigItem: Instantiated 4 Services.
[2023-05-02 11:21:23 +0000] information/ScriptGlobal: Dumping variables to file '/var/cache/icinga2/icinga2.vars'
[2023-05-02 11:21:23 +0000] information/cli: Finished validating the configuration file(s).

重启 Icinga 服务以使配置更改生效。

$ sudo systemctl restart icinga2

步骤 15 - 在 Icinga 仪表盘上验证

打开 Icinga2 Web 仪表盘以验证客户端机器信息。从左侧菜单中选择 Overview >> Hosts,你将看到以下界面。

在 Ubuntu 22.04 LTS 系统上安装并配置 Icinga 2 监控软件教程

客户端状态显示 UP 可能需要一些时间。点击 Client 查看其更多详细信息。选择 Overview >> Services,你将看到以下与客户端相关的状态。

在 Ubuntu 22.04 LTS 系统上安装并配置 Icinga 2 监控软件教程

这证实了客户端正在正确地向 Icinga 主服务器发送统计数据。

总结

本教程到此结束,我们介绍了如何在 Ubuntu 22.04 服务器上安装 Icinga 监控软件,并将其配置为监控一台运行相同操作系统的客户端机器。如果你有任何问题,请在下方评论中留言。