网站搜索

如何在 Ubuntu 22.04 LTS 上安装 Icinga 2 监控软件


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

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

先决条件

    两台运行 Ubuntu 22.04 的机器。其中一台将充当主服务器,另一台将充当监控客户端。

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

    主服务器 icinga.example.com 和客户端节点 client.example.com 的完全限定域名 (FQDN)。

    确保一切都已更新。

    $ 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(简单防火墙)。

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

$ sudo ufw status

您应该得到以下输出。

Status: inactive

允许 SSH 端口,以便防火墙在启用它时不会中断当前连接。

$ sudo ufw allow OpenSSH

允许 Icinga2 客户端连接到服务器所需的端口 5665。

$ 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!';

退出外壳。

mysql> exit

运行 MySQL 安全安装脚本。

$ sudo mysql_secure_installation

首先,系统会要求您输入 root 密码。输入它。接下来,系统将要求您安装验证密码组件。它检查 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;

退出外壳。

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 实用程序创建数据库。选择是继续。

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

系统将要求您再次确认密码。

接下来,系统会要求您启用 ido-mysql 功能。选择是继续。

您可以在 /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

安装证书机器人。

$ 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  -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

对该过程进行一次演练,以检查 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;

退出外壳。

mysql> exit

第 11 步 - 设置 IcingaWeb

在浏览器中打开 URL https://icinga.example.com,您将看到以下屏幕。

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

在下一个屏幕上,选择要安装的模块,然后单击下一步继续。默认情况下会为您选择监控模块。在下一页上,您将看到要求以及是否已满足这些要求。确保所有要求都标记为绿色。

单击下一步进入下一页选择身份验证类型。

身份验证类型默认设置为数据库。单击下一步继续。系统将要求您在下一页上填写数据库凭据。

填写在步骤 10 中创建的数据库凭据。单击验证配置按钮验证凭据。验证后,点击下一步继续。接下来,系统将要求您命名身份验证后端。

保留默认值并单击下一步继续。在下一页上,系统将要求您创建管理员帐户。

输入新管理员帐户的凭据,然后单击下一步继续。接下来,您将看到应用程序配置页面。

保留应用程序和日志记录配置的所有默认值,然后单击下一步继续。系统将要求您查看最后一页上的配置。

您可以返回更改任何设置。如果您满意,请单击下一步继续。

单击下一步继续进行监控模块的配置。接下来,系统将要求您提供 Icinga 数据库凭据。

填写第 3 步中的数据库凭据,然后单击验证配置以验证连接。验证后,点击下一步继续。接下来,系统会要求您填写 API 详细信息。

填写第 6 步中创建的 API 凭据,然后单击验证配置以验证连接。单击下一步继续。接下来,系统将要求您选择受保护的自定义变量来监控安全性。

保留默认值并单击下一步继续。接下来,系统将要求您检查监控配置。如果您愿意,可以返回并更改它。

如果您满意,请单击完成完成安装。

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

输入您的管理员帐户详细信息,然后单击登录按钮打开 Icinga Web 仪表板。

访问概述>>服务页面检查主服务器的状态,类似于以下内容。

步骤 12 - 初始化主服务器

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

$ sudo icinga2 node wizard

如果是代理设置,系统会提示您。输入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

接下来,系统将要求您提供通用名称或域名。按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.

接下来,输入主区域名称并按 Enter 继续。在我们的例子中,它与服务器域名相同。

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

接下来,系统将询问您是否要添加任何其他全局区域。按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 区域配置。

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

最后但并非最不重要的一点是,运行以下命令为客户端服务器创建票证。使用客户端的域名作为参数。

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

记下门票以供稍后使用。

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

登录客户端服务器并安装 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 节点向导以初始化客户端服务器上的代理。

$ sudo icinga2 node wizard

如果是代理设置,系统会提示您。输入 Y 以设置代理。

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

接下来,系统将要求您指定通用名称。保留默认值并按 Enter 继续。

Starting the Agent/Satellite setup routine...

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

接下来,将父端点指定为 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

接下来,输入上一步中生成的请求票证。

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键接受默认本地区域名称,即客户端域名。输入主域名作为父区域名称以继续。

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 - 在主服务器上创建区域配置

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

$ 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 来保存文件。

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

$ 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 仪表板以验证客户端计算机信息。从左侧菜单中选择概述>>主机,您将看到以下屏幕。

客户端状态可能需要一段时间才会显示为UP。单击客户端可查看有关它的更多详细信息。选择概览>>服务,您将看到有关客户的以下状态。

这确认客户端正在正确地将统计数据发送到 Icinga 主服务器。

结论

关于在 Ubuntu 22.04 服务器上安装 Icinga 监控软件并将其配置为监控运行相同操作系统的客户端计算机的教程到此结束。如果您有任何疑问,请在下面的评论中发表。

相关文章: