如何在 Ubuntu 16.04 上使用 Icinga 监控主机和服务

介绍

Icinga 是一个开源监控系统,用于监控网络主机和服务的健康状况. 在本教程中,我们将使用 Icinga 来设置两种不同类型的监控配置. 第一种是基于您主机的外部服务的简单网络检查,例如对您的网站进行定期 HTTP 请求。

前提条件

在开始本教程之前,你应该已经完成本系列的前一个教程, How To Install Icinga and Icinga Web on Ubuntu 16.04. 这会让你在单个主机上运行Icinga核心和Icinga Web接口,我们将称之为 icinga-master节点。

您还需要一些服务器来监控。我们将使用两个Ubuntu 16.04服务器,为我们的示例安装了Apache。您可以使用上面的LAMP教程(https://andsky.com/tech/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-16-04)的Apache部分来设置这些。

步骤 1 – 设置简单的主机监控

使用Icinga监控服务器的一个简单的方法是设置定期检查其外部可用的服务,因此对于网页主机,我们会定期点击服务器的IP地址,并尝试访问网页。

让我们为 Web 服务器设置监控。 作为前提,选择一个提到的 Apache 服务器,并确保默认的 Apache 位址主页得到正确的服务。 我们将把这个服务器称为 web-1.example.com。

<$>[注] 注: Icinga 总是默认使用其所处理的任何主机的完全合格域名(FQDN)。

这些都很好用,只需保持一致,如果你没有真正的FQDN,请始终在你配置的任何Icinga地址字段中使用服务器的IP地址。

要添加一个新的主机,我们需要编辑 Icinga 的 hosts.conf 文件。

1sudo nano /etc/icinga2/conf.d/hosts.conf

这将打开一个文件,其中有一些解释性评论和一个单一的主机 block 定义. 现有的 object Host NodeName 配置块定义了 icinga-master 主机,这是我们安装了 Icinga 和 Icinga Web 的主机。

 1[label /etc/icinga2/conf.d/hosts.conf]
 2. . .
 3object Host "web-1.example.com" {
 4  import "generic-host"
 5  address = "web-1_server_ip"
 6  vars.http_vhosts["http"] = {
 7    http_uri = "/"
 8  }
 9  vars.notification["mail"] = {
10    groups = [ "icingaadmins" ]
11  }
12}

这定义了一个名为web-1.example.com的主机,从称为generic-host的模板中导入一些默认主机配置,将Icinga指向正确的IP地址,然后定义一些变量,告诉Icinga在根(/)URL上检查HTTP响应,并通过电子邮件通知icingaadmins组出现问题。

保存并关闭文件,然后重新启动 Icinga:

1sudo systemctl restart icinga2

在浏览器中重新切换到 Icinga Web 界面. 界面更新本身相当快,所以您不需要更新页面. 新主机信息应该以短的顺序填充,健康检查将从 Pending 变为 Ok 一旦 Icinga 收集了足够的信息。

这是监控主机上的外部服务的一个很好的方法,还有其他检查可用于SSH服务器,SMTP等。

接下来,我们将通过Icinga代理设置监控,以便我们可以关注更详细的系统信息。

步骤 2 – 设置基于代理的监控

Icinga 提供了主和客户端节点之间安全通信的机制,以便运行更广泛的远程健康检查,而不是只知道我们的 Web 服务器正在成功地服务页面,我们还可以监控CPU负载,流程数量,磁盘空间等。

我们将设置第二台服务器来监控它,我们将其称为web-2.example.com。我们需要在远程机器上安装Icinga软件,运行一些设置向导来连接,然后在Icinga主节点上更新一些配置文件。

<$>[注] 注: 有许多方法来构建一个Icinga安装,包括多层的 master/satellite/client节点,高可用性故障,以及多种方式来共享节点之间的配置细节。我们正在设置一个简单的两层结构,一个 master节点和多个 client节点。此外,所有配置都将在 master节点上完成,我们的健康检查命令将安排在主节点上,并推到客户端。

设置主节点

首先,我们需要设置主节点来创建客户端连接,我们通过在我们的主节点上运行节点设置 wizard 来做到这一点:

1sudo icinga2 node wizard

这将启动一个脚本,向我们提出几个问题,并为我们设置问题。下文,我们点击ENTER,以接受大多数默认信息。

 1[secondary_label Output]
 2Please specify if this is a satellite setup ('n' installs a master setup) [Y/n]: n
 3Starting the Master setup routine...
 4Please specify the common name (CN) [icinga-master.example.com]: ENTER to accept the default, or type your FQDN
 5Checking for existing certificates for common name 'icinga-master.example.com'...
 6Certificates not yet generated. Running 'api setup' now.
 7Enabling feature api. Make sure to restart Icinga 2 for these changes to take effect.
 8Please specify the API bind host/port (optional): ENTER
 9Bind Host []: ENTER
10Bind Port []: ENTER
11Done.
12
13Now restart your Icinga 2 daemon to finish the installation!

重新启动 Icinga 以完成更新配置:

1sudo systemctl restart icinga2

打开防火墙端口以允许对 Icinga 的外部连接:

1sudo ufw allow 5665

现在我们将切换到客户端节点,安装Icinga并运行相同的向导。

设置客户端节点

登录我们正在呼叫的服务器 web-2.example.com. 我们需要重新安装Icinga存储库,然后安装Icinga本身。

1[environment second]
2curl -sSL https://packages.icinga.com/icinga.key | sudo apt-key add -

打开icinga.list文件:

1[environment second]
2sudo nano /etc/apt/sources.list.d/icinga.list

点击存储库详细信息:

1[environment second]
2[label /etc/apt/sources.list.d/icinga.list]
3deb https://packages.icinga.com/ubuntu icinga-xenial main

保存并关闭文件,然后更新包缓存:

1[environment second]
2sudo apt-get update

请注意,我们不需要我们在主节点上安装的icinga2-ido-mysql包:

1[environment second]
2sudo apt-get install icinga2

现在我们在这个服务器上运行节点向导,但我们执行 satellite config 而不是 master:

1[environment second]
2sudo icinga2 node wizard
 1[environment second]
 2[secondary_label Output]
 3Please specify if this is a satellite setup ('n' installs a master setup) [Y/n]: y
 4Starting the Node setup routine...
 5Please specify the common name (CN) [web-2.example.com]: ENTER
 6Please specify the master endpoint(s) this node should connect to:
 7Master Common Name (CN from your master setup): icinga-master.example.com
 8Do you want to establish a connection to the master from this node? [Y/n]: y
 9Please fill out the master connection information:
10Master endpoint host (Your master's IP address or FQDN): icinga-master_server_ip
11Master endpoint port [5665]: ENTER
12Add more master endpoints? [y/N]: ENTER
13Please specify the master connection for CSR auto-signing (defaults to master endpoint host):
14Host [icinga-master_server_ip]: ENTER
15Port [5665]: ENTER

向导将从我们的主节点获取公共证书,并向我们显示其细节。

1[environment second]
2[secondary_label Output]
3. . .
4Is this information correct? [y/N]: y
5information/cli: Received trusted master certificate.
6
7Please specify the request ticket generated on your Icinga 2 master.
8 (Hint: # icinga2 pki ticket --cn 'web-2.example.com'):

在此时刻,返回你的服务器,并运行引导程序提示你的命令. 不要忘记在它面前的sudo:

1sudo icinga2 pki ticket --cn 'web-2.example.com'

该命令将输出一个密钥. 将其复制到您的剪辑板,然后返回客户端节点,粘贴它,并点击ENTER,以继续与引导程序。

 1[environment second]
 2[secondary_label Output]
 3. . .
 4information/cli: Requesting certificate with ticket '5f53864a504a1c42ad69faa930bffa3a12600546'.
 5
 6Please specify the API bind host/port (optional):
 7Bind Host []: ENTER
 8Bind Port []: ENTER
 9Accept config from master? [y/N]: n
10Accept commands from master? [y/N]: y
11Done.
12
13Now restart your Icinga 2 daemon to finish the installation!

现在打开您的防火墙上的Icinga端口:

1[environment second]
2sudo ufw allow 5665

然后重新启动 Icinga 以完全更新配置:

1[environment second]
2sudo systemctl restart icinga2

您可以通过netstat验证两个服务器之间的连接:

1[environment second]
2netstat | grep :5665

连接可能需要一段时间,最终netstat会输出显示正确端口上的ESTABLISHED连接的行。

1[environment second]
2[secondary_label Output]
3tcp 0 0 web-2_server_ip:58188 icinga-master_server_ip:5665 ESTABLISHED

这表明我们的服务器已连接,我们已经准备好配置客户端检查。

配置代理监控

尽管主机和客户机现在已连接,但仍有某些设置要在主机上进行,以启用监控。

一个重要的组织级别在一个Icinga安装是一个 _zone_的概念. 所有客户端节点必须创建自己的区域,并报告到一个母区,在这种情况下我们的主节点. 默认情况下,我们的主节点的区域是以其FQDN命名的。

创建区域目录:

1sudo mkdir /etc/icinga2/zones.d/icinga-master.example.com

我们将创建一个服务配置文件,这将定义我们将在任何远程客户端节点上执行的一些服务检查。

1sudo nano /etc/icinga2/zones.d/icinga-master.example.com/services.conf

粘贴下列内容,然后保存并关闭:

 1[label services.conf]
 2apply Service "load" {
 3  import "generic-service"
 4  check_command = "load"
 5  command_endpoint = host.vars.client_endpoint
 6  assign where host.vars.client_endpoint
 7}
 8
 9apply Service "procs" {
10  import "generic-service"
11  check_command = "procs"
12  command_endpoint = host.vars.client_endpoint
13  assign where host.vars.client_endpoint
14}

这定义了两个服务检查:第一个会报告CPU负载,第二个会检查服务器上的进程数量.每个服务定义的最后两行很重要。 command_endpoint告诉Icinga,这个服务检查需要发送到远程命令终点。

让我们现在创建一个这样的主机。在我们之前创建的区域目录中打开一个新的文件. 在这里,我们将该文件命名为远程主机:

1sudo nano /etc/icinga2/zones.d/icinga-master.example.com/web-2.example.com.conf

粘贴下面的配置,然后保存和关闭文件:

 1[label web-2.example.com.conf]
 2object Zone "web-2.example.com" {
 3  endpoints = [ "web-2.example.com" ]
 4  parent = "icinga-master.example.com"
 5}
 6
 7object Endpoint "web-2.example.com" {
 8  host = "web-2_server_ip"
 9}
10
11object Host "web-2.example.com" {
12  import "generic-host"
13  address = "web-2_server_ip"
14  vars.http_vhosts["http"] = {
15    http_uri = "/"
16  }
17  vars.notification["mail"] = {
18    groups = [ "icingaadmins" ]
19  }
20  vars.client_endpoint = name
21}

该文件定义了我们远程主机的区域,并将其连接到主机的区域。它还定义了主机作为终端,然后定义了主机本身,从通用主机模板中导入了一些默认规则。它还设置了一些vars来创建HTTP检查并启用电子邮件通知。 请注意,因为这个主机有vars.client_endpoint = name定义,它也将被分配给我们刚刚在services.conf中定义的服务检查。

重启 Icinga 以更新 config:

1sudo systemctl restart icinga2

返回Icinga Web接口,新主机将出现检查 Pending. 几分钟后,这些检查应该转换为 Ok. 这意味着我们的客户端节点正在成功地运行主节点的检查。

结论

在本教程中,我们设置了两种不同类型的监控 Icinga,外部服务检查和基于代理的托管检查。

请注意,如果您到达一个需要自定义 check 命令的地点,则需要使用 global configuration zone 将这些命令从主节点同步到客户端节点。

最后,如果您有大量的服务器来监控,您可能会考虑使用配置管理软件来自动化您的 Icinga 配置更新。

Published At
Categories with 技术
comments powered by Disqus