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

  • Debian 12 (Bookworm)
  • Debian 11 (Bullseye)
  • Debian 10 (Buster)
  • Debian 9 (Stretch)

本页内容

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

Icinga 2 是一个开源监控系统,用于检查网络资源的可用性、在出现中断时通知用户,并生成用于报告的性能数据。你可以使用 Icinga2 监控网络服务(SMTP、POP3、HTTP、NNTP、ping)、主机资源(CPU 使用率、磁盘使用率)以及网络组件(交换机、路由器、温度和湿度传感器)。它可以与 Nagios 插件集成。

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

准备工作

  • 两台运行 Debian 12 的机器。其中一台作为 Master 服务器,另一台作为被监控的客户端。
  • 在两台服务器上拥有 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 debian-archive-keyring ufw unzip -y 其中部分软件包可能已经在你的系统上安装。

两台运行 Debian 12 的机器。其中一台作为 Master 服务器,另一台作为被监控的客户端。

在两台服务器上拥有 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 debian-archive-keyring ufw unzip -y

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

第 1 步 - 在 Master 服务器上配置防火墙

第一步是配置防火墙。Debian 默认自带 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 步 - 安装 MariaDB 服务器

Debian 12 自带最新版本的 MariaDB。你可以用一条命令安装它。

$ sudo apt install mariadb-server

检查 MySQL 的版本。

$ mysql --version
mysql  Ver 15.1 Distrib 10.11.4-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper

运行 MariaDB 安全安装脚本。

$ sudo mariadb-secure-installation

系统会要求输入 root 密码。直接按 Enter,因为我们尚未为其设置密码。

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

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

Enter current password for root (enter for none):

接下来,系统会询问是否切换到 Unix socket 认证方式。unix_socket 插件允许你使用操作系统凭据连接到 MariaDB 服务器。由于你已经有一个受保护的 root 账户,请输入 n 继续。

OK, successfully used password, moving on...

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

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] n

接下来,系统会询问是否要更改 root 密码。在 Debian 12 上,root 密码与自动化系统维护紧密相关,因此不应改动。输入 n 继续。

 ... skipping.

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] n

接下来,系统会提出几个问题以增强 MariaDB 安全性。输入 Y 以删除匿名用户、禁止远程 root 登录、删除测试数据库并重新加载权限表。

 ... skipping.

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!

你可以在命令行中输入 sudo mysql 或 sudo mariadb 来进入 MariaDB shell。

第 3 步 - 配置 MariaDB

登录到 MariaDB shell。在提示时输入你的 root 密码。

$ sudo mysql

创建 Icinga 数据库。

MariaDB [(none)]> CREATE DATABASE icinga2;

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

MariaDB [(none)]> CREATE USER 'icinga2'@'localhost' IDENTIFIED BY 'Your_password2';

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

MariaDB [(none)]> GRANT ALL PRIVILEGES ON icinga2.* TO 'icinga2'@'localhost';

由于我们不会修改 root 用户,你应该创建另一个使用密码认证的 SQL 用户来执行管理任务。请为其设置一个强密码。

MariaDB> GRANT ALL ON *.* TO 'navjot'@'localhost' IDENTIFIED BY 'Yourpassword32!' WITH GRANT OPTION;

刷新用户权限。

MariaDB [(none)]> FLUSH PRIVILEGES;

退出 shell。

MariaDB [(none)]> exit

第 4 步 - 在 Master 服务器上安装 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/debian 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/debian icinga-`lsb_release -cs` main" | sudo tee -a /etc/apt/sources.list.d/$(lsb_release -cs)-icinga.list

更新系统仓库列表。

$ sudo apt update

安装 Icinga2、Icingacli 以及监控插件。

$ sudo apt install icinga2 monitoring-plugins -y

第 5 步 - 在 Master 服务器上安装 IDO MySQL 驱动

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

$ sudo apt install -y icinga2-ido-mysql

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

在 Debian 12 上安装 Icinga 2 监控软件

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

在 Debian 12 上安装 Icinga 2 监控软件

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

在 Debian 12 上安装 Icinga 2 监控软件

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

在 Debian 12 上安装 Icinga 2 监控软件

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

$ sudo 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
Enabling feature ido-mysql. Make sure to restart Icinga 2 for these changes to take effect.

重启 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; preset: enabled)
    Drop-In: /etc/systemd/system/icinga2.service.d
             ??limits.conf
     Active: active (running) since Mon 2024-01-08 07:35:29 UTC; 4s ago
    Process: 15404 ExecStartPre=/usr/lib/icinga2/prepare-dirs /etc/default/icinga2 (code=exited, status=0/SUCCESS)
   Main PID: 15411 (icinga2)
     Status: "Startup finished."
      Tasks: 14
     Memory: 13.6M
        CPU: 858ms
     CGroup: /system.slice/icinga2.service
             ??15411 /usr/lib/x86_64-linux-gnu/icinga2/sbin/icinga2 --no-stack-rlimit daemon --close-stdio -e /var/log/icinga2/error.log
             ??15433 /usr/lib/x86_64-linux-gnu/icinga2/sbin/icinga2 --no-stack-rlimit daemon --close-stdio -e /var/log/icinga2/error.log
             ??15438 /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 设置一个强密码。

/** 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

Debian 12 自带较旧版本的 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 mainline 版本的仓库。

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

更新系统仓库。

$ sudo apt update

安装 Nginx。

$ sudo apt install nginx

验证安装。在 Debian 系统上,以下命令只有在配合 sudo 时才能正常运行。

$ sudo nginx -v
nginx version: nginx/1.25.3

启动 Nginx 服务器。

$ sudo systemctl start nginx

检查服务状态。

$ sudo systemctl status nginx
? nginx.service - nginx - high performance web server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; preset: enabled)
     Active: active (running) since Mon 2024-01-08 07:43:24 UTC; 4s ago
       Docs: https://nginx.org/en/docs/
    Process: 16330 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
   Main PID: 16331 (nginx)
      Tasks: 3 (limit: 2299)
     Memory: 2.9M
        CPU: 16ms
     CGroup: /system.slice/nginx.service
             ??16331 "nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf"
             ??16332 "nginx: worker process"
             ??16333 "nginx: worker process"

Jan 08 07:43:24 icinga systemd[1]: Starting nginx.service - nginx - high performance web server...
Jan 08 07:43:24 icinga systemd[1]: Started nginx.service - nginx - high performance web server.

第 8 步 - 安装 SSL

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

Debian 12 默认未安装 Snapd。安装 Snapd 软件包。

$ sudo apt install 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

验证 Certbot 是否正常工作。

$ certbot --version
certbot 2.8.0

运行以下命令生成 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 2024-01-08 09:47:46 UTC 1h 56min left Sun 2024-01-07 09:47:46 UTC 22h ago systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service
Mon 2024-01-08 13:35:00 UTC 5h 43min left -                           -       snap.certbot.renew.timer     snap.certbot.renew.service
Tue 2024-01-09 00:00:00 UTC 16h left      Mon 2024-01-08 00:00:01 UTC 7h ago  dpkg-db-backup.timer         dpkg-db-backup.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.2/fpm/pool.d/www.conf。

$ sudo nano /etc/php/8.2/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.2-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.2-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 之前,我们需要安装它及其命令行工具。

$ sudo apt install icingaweb2 icingacli

将 Nginx 用户添加到 icingaweb2 组。

$ sudo usermod -aG icingaweb2 nginx

将 Icingaweb 目录的权限设置为 icingaweb2 组。

$ sudo icingacli setup config directory --group icingaweb2
Successfully created configuration directory /etc/icingaweb2

使用 Icinga Web 时,你需要使用令牌进行认证。使用以下命令生成令牌。

$ sudo icingacli setup token create
The newly generated setup token is: 56951f01f9f77a68

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

$ sudo icingacli setup token show
The current setup token is: 56951f01f9f77a68

下一步是创建数据库和数据库用户。登录到 MariaDB shell。

$ sudo mysql

创建 Icinga Web 数据库。

MariaDB [(none)]> CREATE DATABASE icingaweb2;

为 Icinga Web 创建 SQL 用户账户。

MariaDB [(none)]> CREATE USER 'icingaweb2'@'localhost' IDENTIFIED BY 'Your_password3';

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

MariaDB [(none)]> GRANT ALL PRIVILEGES ON icingaweb2.* TO 'icingaweb2'@'localhost';

刷新用户权限。

MariaDB [(none)]> FLUSH PRIVILEGES;

退出 shell。

MariaDB [(none)]> exit

重启 Nginx 和 PHP-FPM 以应用权限更改。

$ sudo systemctl restart nginx php8.2-fpm

第 11 步 - 配置 IcingaWeb

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

在 Debian 12 上安装 Icinga 2 监控软件

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

在 Debian 12 上安装 Icinga 2 监控软件

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

在 Debian 12 上安装 Icinga 2 监控软件

点击 Next 进入下一页选择认证类型。

在 Debian 12 上安装 Icinga 2 监控软件

认证类型默认为 Database。点击 Next 继续。在下一页中,系统会要求你填写数据库凭据。

在 Debian 12 上安装 Icinga 2 监控软件

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

在 Debian 12 上安装 Icinga 2 监控软件

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

在 Debian 12 上安装 Icinga 2 监控软件

输入新管理员账户的凭据,点击 Next 继续。接下来会显示 Application Configuration 页面。

在 Debian 12 上安装 Icinga 2 监控软件

Enable strict content security policy 选项默认未勾选。勾选它,并保持其他所有默认值不变。点击 Next 继续。系统会要求你在最后一页查看配置。

在 Debian 12 上安装 Icinga 2 监控软件

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

在 Debian 12 上安装 Icinga 2 监控软件

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

在 Debian 12 上安装 Icinga 2 监控软件

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

在 Debian 12 上安装 Icinga 2 监控软件

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

在 Debian 12 上安装 Icinga 2 监控软件

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

在 Debian 12 上安装 Icinga 2 监控软件

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

在 Debian 12 上安装 Icinga 2 监控软件

成功完成后,点击 Login to Icinga Web 2 按钮打开登录页面(https://icinga.example.com)。

在 Debian 12 上安装 Icinga 2 监控软件

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

在 Debian 12 上安装 Icinga 2 监控软件

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

在 Debian 12 上安装 Icinga 2 监控软件

第 12 步 - 初始化 Master 服务器

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

$ sudo icinga2 node wizard

系统会询问是否为 agent 配置。输入 n 以设置 master 节点。

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 选择即可;否则输入域名后按 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

接下来,系统会询问是否要添加额外的全局 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'
c81f2a3b86534f34160ed8b776906e5452d8d09c

记下该 ticket 以备后用。

第 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/debian 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/debian 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; preset: enabled)
    Drop-In: /etc/systemd/system/icinga2.service.d
             ??limits.conf
     Active: active (running) since Mon 2024-01-08 12:52:53 UTC; 35s ago
   Main PID: 19530 (icinga2)
     Status: "Startup finished."
      Tasks: 12
     Memory: 13.4M
        CPU: 216ms
     CGroup: /system.slice/icinga2.service
             ??19530 /usr/lib/x86_64-linux-gnu/icinga2/sbin/icinga2 --no-stack-rlimit daemon --close-stdio -e /var/log/icinga2/error.log
             ??19573 /usr/lib/x86_64-linux-gnu/icinga2/sbin/icinga2 --no-stack-rlimit daemon --close-stdio -e /var/log/icinga2/error.log
             ??19578 /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

接下来,输入 master 服务器的 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 以拒绝添加更多 master endpoints。

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

接下来,系统会显示 master 服务器的证书信息。按 Y 确认信息并继续。

Parent certificate information:

 Version:             3
 Subject:             CN = icinga.example.com
 Issuer:              CN = Icinga CA
 Valid From:          Jan  8 07:36:55 2024 GMT
 Valid Until:         Feb  8 07:36:55 2025 GMT
 Serial:              3a:e5:5e:e6:d5:5e:cc:1d:89:be:18:0b:10:cb:7d:54:8f:82:b1:5e

 Signature Algorithm: sha256WithRSAEncryption
 Subject Alt Names:   icinga.example.com
 Fingerprint:         DB 62 0D 2D AF 73 02 F2 86 92 5E A8 50 CD 0F 4F F2 D6 9E 86 AE F6 F9 E4 D7 F2 F2 60 78 1B 92 E5

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'): c81f2a3b86534f34160ed8b776906e5452d8d09c

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

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

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

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

按 Enter 接受默认的本地 zone 名称(即客户端域名)。输入 master 域名作为 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 跳过添加额外的全局 zones。

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

按 Y 跳过禁用 /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 步 - 在 Master 服务器上创建 Zones 配置

重新登录服务器,创建一个新目录作为默认 zone。

$ 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

你会看到类似的输出。

[2024-01-08 13:01:26 +0000] information/cli: Icinga application loader (version: r2.14.1-1)
[2024-01-08 13:01:26 +0000] information/cli: Loading configuration file(s).
[2024-01-08 13:01:26 +0000] information/ConfigItem: Committing config item(s).
[2024-01-08 13:01:26 +0000] information/ApiListener: My API identity: icinga.example.com
[2024-01-08 13:01:26 +0000] information/ConfigItem: Instantiated 1 IcingaApplication.
[2024-01-08 13:01:26 +0000] information/ConfigItem: Instantiated 1 Host.
[2024-01-08 13:01:26 +0000] information/ConfigItem: Instantiated 1 FileLogger.
[2024-01-08 13:01:26 +0000] information/ConfigItem: Instantiated 1 IdoMysqlConnection.
[2024-01-08 13:01:26 +0000] information/ConfigItem: Instantiated 4 Zones.
[2024-01-08 13:01:26 +0000] information/ConfigItem: Instantiated 1 CheckerComponent.
[2024-01-08 13:01:26 +0000] information/ConfigItem: Instantiated 2 Endpoints.
[2024-01-08 13:01:26 +0000] information/ConfigItem: Instantiated 2 ApiUsers.
[2024-01-08 13:01:26 +0000] information/ConfigItem: Instantiated 1 ApiListener.
[2024-01-08 13:01:26 +0000] information/ConfigItem: Instantiated 1 NotificationComponent.
[2024-01-08 13:01:26 +0000] information/ConfigItem: Instantiated 246 CheckCommands.
[2024-01-08 13:01:26 +0000] information/ConfigItem: Instantiated 4 Services.
[2024-01-08 13:01:26 +0000] information/ScriptGlobal: Dumping variables to file '/var/cache/icinga2/icinga2.vars'
[2024-01-08 13:01:27 +0000] information/cli: Finished validating the configuration file(s).

重启 Icinga 服务以应用配置更改。

$ sudo systemctl restart icinga2

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

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

在 Debian 12 上安装 Icinga 2 监控软件

客户端状态显示为 UP 可能需要一些时间。点击 Client 查看其更多详情。选择 Overview >> Services,你会看到关于客户端的以下状态。

在 Debian 12 上安装 Icinga 2 监控软件

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

总结

至此,我们在 Debian 12 服务器上安装 Icinga 监控软件并将其配置为监控同一操作系统的客户端机器的教程就结束了。如果你有任何问题,请在下方评论中留言。