猿教程 Logo

如何在Ubuntu 16.04上安装Icinga和Icinga Web (How To Install Icinga and Icinga Web on Ubuntu 16.04)


Introduction

Icinga is a flexible and powerful open-source monitoring system used to oversee the health of networked hosts and services. It could be used to monitor the load and uptime of a cluster of web workers, free disk space on a storage device, memory consumption on a caching service, and so on. Once properly set up, Icinga can give you an at-a-glance overview of the status of large numbers of hosts and services, as well as notifications, downtime scheduling, and long-term storage of performance data.

This tutorial will cover installing the Icinga core, its database backend, and the Icinga Web interface. Finally, we'll set up email notification so you can receive alerts in your inbox when a service is misbehaving.


Prerequisites

Before starting this tutorial you will need:

  • An Ubuntu 16.04 server with the LAMP stack installed, which is detailed in our tutorial How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 16.04.
  • Because Icinga Web has a login page and also asks you to input sensitive information during setup, you should enable SSL encryption for Apache. You can learn how to fetch and install free SSL certificates in our tutorial How To Secure Apache with Let's Encrypt on Ubuntu 16.04.

Step 1 — Installing Icinga

To get the latest version of Icinga, we first need to add a software repository maintained by the Icinga team. We will then install the software with apt-get and run through a few configuration screens to set up Icinga's database backend.

First, download the Icinga developers' package signing key and add it to the apt system:

$ curl -sSL https://packages.icinga.com/icinga.key | sudo apt-key add -
curl -sSL https://packages.icinga.com/icinga.key | sudo apt-key add -

This key will be used to automatically verify the integrity of any software we download from the Icinga repository. Now we need to add the repository address to an apt configuration file. Open up the file with your favorite text editor. We'll use nano throughout this tutorial:

$ sudo nano /etc/apt/sources.list.d/icinga.list
sudo nano /etc/apt/sources.list.d/icinga.list

This will open a new blank text file. Paste in the following line:

deb https://packages.icinga.com/ubuntu icinga-xenial main

Save and close the file, then refresh your package cache:

$ sudo apt-get update
sudo apt-get update

apt-get will now download information from the repository we just added, making the Icinga packages available to install:

$ sudo apt-get install icinga2 icinga2-ido-mysql
sudo apt-get install icinga2 icinga2-ido-mysql

This will install the main Icinga software, along with a database adapter that enables Icinga to put historical data and other information into a MySQL database. You'll be presented with a few configuration screens for the database adapter:

Now we need to actually enable the Icinga database backend. The icinga2 command can enable and disable features on the command line. While we're at it, we'll also enable the command feature which will eventually let us run manual health checks from the web interface.

$ sudo icinga2 feature enable ido-mysql command
sudo icinga2 feature enable ido-mysql command

Now restart icinga2 to use the new features:

$ sudo systemctl restart icinga2
sudo systemctl restart icinga2

And finally, let's check the status of icinga2 to make sure it's running properly:

$ sudo systemctl status icinga2
sudo systemctl status icinga2
Output
       ● icinga2.service - Icinga host/service/network monitoring system
   Loaded: loaded (/lib/systemd/system/icinga2.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2017-04-20 00:54:55 UTC; 3s ago
  Process: 15354 ExecStartPre=/usr/lib/icinga2/prepare-dirs /usr/lib/icinga2/icinga2 (code=exited, status=0/SUCCESS)
 Main PID: 15416 (icinga2)
    Tasks: 11
   Memory: 7.7M
      CPU: 488ms
. . .

If you see Active: active (running), Icinga is up and running. Now that we've set up the Icinga core system and database backend, it's time to get the web interface up and running.


Step 2 – Installing the Icinga Web Interface

The Icinga core is completely configurable and usable without a web interface, but Icinga Web provides a nice browsable overview of the health of your hosts and services, and allows you to schedule downtime, acknowledge issues, manually trigger health checks, and send notifications, right from your browser.

Let's install Icinga Web with apt-get:

$ sudo apt-get install icingaweb2
sudo apt-get install icingaweb2

The rest of Icinga Web's setup is done in a web browser, but before we switch over, there's one setting we need to update. Icinga Web needs a timezone to be set for the PHP environment, so let's edit the PHP config file:

$ sudo nano /etc/php/7.0/apache2/php.ini
sudo nano /etc/php/7.0/apache2/php.ini

We need to find a specific line to update. In nano we can press CTRL-W to bring up a search interface, type in date.timezone, then hit ENTER. The cursor will move to the line we need to update. First, uncomment the line by removing the initial ; semicolon, and then type in your correct timezone. You can find the correct timezone format in the PHP manual's timezone section. It should look something like this when you're finished:

date.timezone = America/New_York

Save and close the file. Restart Apache to update:

$ sudo systemctl restart apache2
sudo systemctl restart apache2

Now it's time to work through Incinga Web's browser-based setup.


Step 3 — Setting up the Icinga Web Interface

Before we switch over to our browser for the web-based setup process, we need to create a setup token. This is a key we generate on the command line that authorizes us to use the web setup tool. We create this key with the icingacli command:

$ sudo icingacli setup token create
sudo icingacli setup token create

A short token will be printed:

Output
       1558c2c0ec4572ab

Copy the token to your clipboard, then switch to your browser and load the Icinga Web address. By default this is your server’s domain name or IP address followed by /icingaweb2:

https://icinga-master.example.com/icingaweb2

You'll be presented with a configuration screen. Paste in the token you copied to your clipboard, and press Next to begin the process. There are many pages of options to go through. We'll step through them one at a time.


Module Setup

On the second page, you'll have the option to enable some extra modules for the web interface. We can safely accept the default of only enabling the Monitoring module. Click Next to continue.


Environment Status

The third page shows the status of our PHP environment. You shouldn't see any red boxes, which would indicate an issue or misconfiguration. You may see some yellow boxes mentioning PostgreSQL modules being missing. We can safely ignore these, as we're using MySQL, not PostgreSQL. Click Next to continue.


Icinga Web Authentication

The fourth page lets us choose how we want to authenticate Icinga Web users. If you wanted to integrate with an LDAP service for authentication, this would be the place to choose that. We'll use the default, Database, to store users in our MySQL database. Click Next to continue.


User Database Setup

The fifth page asks us to set up a database to store the user data. This is separate from the database we previously set up during the command line install.

Most of the defaults are fine, but we also need to choose a database name and user/password combination:

Hit Next to continue.


Create User Database

The next page will say that your database doesn't exist and you don't have the credentials to create it. Enter root for the username, type in the MySQL root password and click Next to create the Icinga Web database and user.


Name the Authentication Provider

Now we need to name the authentication backend we just created. The default icingaweb2 is fine. Click Next.


Create Admin Account

Now that we've set up our user database, we can create our first Icinga Web administrative account. Choose a username and password and click Next to continue.


Preferences and Log Storage

Next we're presented with options on how to store user preferences and logs. The defaults are fine and will store preferences in the database while logging to syslog. Hit Next to continue.


Configuration Review

We are presented with a page to review all of our configurations. Click Next to confirm the configuration details and move on to configuring the monitoring module.


Introduction to Monitoring Configuration

Now we start configuring the actual monitoring module for Icinga Web. Click Next to start.


Select Monitoring Backend

First up, we select our monitoring backend. The default name of icinga and type of IDO are fine. This indicates that Icinga Web will retrieve information from the ido-mysql database we configured earlier when installing things on the command line.


Set up Monitoring Database

We need to enter the connection details for the ido-mysql database. We created this password during installation.

This page has all the same options as the user-database setup screen:

Click Next to continue.


Select Command Transport Method

Next is a Command Transport prompt. This lets us specify how Icinga Web will pass commands to Icinga when we manually run health checks in the web interface. The default of Local Command File is fine and will work with the command feature we enabled back in Step 1. Click Next to continue.


Set Up Monitoring Interface Security

This lets you specify data that should be masked in the web interface, to prevent any potential onlookers from seeing passwords and other sensitive information. The defaults are fine. Hit Next to continue.


Monitoring Module Configuration Summary

Once again, we're presented with a summary of our configuration. Hit Finish to finish the setup of Icinga Web. A Congratulations! message will load.

Click Login to Icinga Web 2 and log in with your administrator username and password.

The main interface of Icinga Web will load. Explore a little and familiarize yourself with the interface. If your server has no swap space set up, you may see a red Critical Error box. We can ignore this for now, or you can Acknowledge the issue by clicking the red box, selecting Acknowledge from the right-hand column, filling out a comment, and finally clicking the Acknowledge problem button.

Now that we've finished setting up Icinga, and Icinga Web, let's set email notifications.


Step 4 – Setting up Email

Monitoring isn't too helpful if you can't receive alerts when something goes wrong. Icinga's default config has some scripts to email an administrator, but we need to set up email on our server before they'll work. The simplest way to do that is to use a program called ssmtp to route all the server's mail through a standard SMTP server.

First, install ssmtp and some helper mail utilities:

$ sudo apt-get install ssmtp mailutils
sudo apt-get install ssmtp mailutils

And now we edit the ssmtp configuration file with our SMTP details. These should be provided by your ISP, email provider, or IT department. You'll need a username, pasword, and the address of your SMTP server:

$ sudo nano /etc/ssmtp/ssmtp.conf
sudo nano /etc/ssmtp/ssmtp.conf

There will be some existing configuration in the file. Delete it and replace it with this very basic setup that should work with most SMTP servers:

mailhub=mail.example.com:465
UseTLS=yes
FromLineOverride=yes
AuthUser=smtp_username
AuthPass=smtp_password

Save and close the file. To test the connection, use the mail command:

$ echo "hello world" | mail -s "test subject" sammy@example.com
echo "hello world" | mail -s "test subject" sammy@example.com

You should see an email in your inbox shortly. Now we need to update a few settings for Icinga to send mail.


Step 5 — Setting up and Testing Notifications

To get email notifications working, update the email address Icinga is sending to:

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

Change the email line to the address you'd like to receive notifications at:

. . .
email = "sammy@example.com"
. . .

Restart Icinga one last time:

$ sudo systemctl restart icinga2
sudo systemctl restart icinga2

The icinga-master host is already configured to send notifications when problems arise. Let's cause a problem and see what happens. We'll use a command called stress to increase the system's load in order to trigger a warning.

Install stress:

$ sudo apt-get install stress
sudo apt-get install stress

stress can manipulate load, disk IO, memory, and other system metrics. The Icinga default configuration will trigger a warning when the system's load is over five. Let's cause that now:

$ stress --cpu 6
stress --cpu 6

Switch back to the Icinga Web interface and you'll see the load metric slowly rise. After a few checks it will enter a soft Warning state. Soft means that the check has to fail a few more times before it's considered a hard state, at which time notifications will be sent. This is to avoid sending notifications for transient issues that quickly fix themselves.

Wait for the warning to reach a hard state and send the notification. You should receive an email with the details of what's going wrong.

Press CTRL-C to exit the stress command. The system load will recover fairly quickly and revert to Ok in the Icinga Web interface. You'll also receive another email telling you that the issue has cleared up.


Conclusion

In this tutorial we have successfully set up Icinga and Icinga Web, including Icinga's email notification feature. Currently we are only monitoring the Icinga host itself though. Continue on to our next tutorial How To Monitor Hosts and Services with Icinga on Ubuntu 16.04 where we will set up remote monitoring.

介绍

Icinga是一个灵活而强大的开源监控系统,用于监督网络主机和服务的健康状况。 它可以用于监视网络工作者群集的负载和正常运行时间,存储设备上的可用磁盘空间,缓存服务上的内存消耗等。 一旦正确设置,Icinga可以让您全面了解大量主机和服务的状态,以及通知,停机计划和长期存储性能数据。

本教程将介绍如何安装Icinga内核,其数据库后端和Icinga Web界面。 最后,我们将设置电子邮件通知,以便在服务行为不当时,您可以在收件箱中收到提醒。


先决条件

在开始本教程之前,您将需要:

  • 安装了LAMP堆栈的Ubuntu 16.04服务器,这在我们的教程中详细介绍了如何在Ubuntu 16.04上安装Linux,Apache,MySQL,PHP(LAMP)堆栈。
  • 因为Icinga Web有一个登录页面,并且还要求您在安装过程中输入敏感信息,您应该为Apache启用SSL加密。 您可以在我们的教程中学习如何获取和安装免费的SSL证书,如何使用Ubuntu 16.04加密Apache。

步骤1 - 安装Icinga

要获得最新版本的Icinga,我们首先需要添加由Icinga团队维护的软件仓库。 然后,我们将使用apt-get安装软件,并运行几个配置屏幕来设置Icinga的数据库后端。

首先,下载Icinga开发人员的软件包签名密钥并将其添加到apt系统中:

$ curl -sSL https://packages.icinga.com/icinga.key | sudo apt-key add -
curl -sSL https://packages.icinga.com/icinga.key | sudo apt-key add -

该密钥将用于自动验证我们从Icinga存储库下载的任何软件的完整性。 现在我们需要将存储库地址添加到apt配置文件中。 用您最喜欢的文本编辑器打开文件。 我们将在本教程中使用nano:

$ sudo nano /etc/apt/sources.list.d/icinga.list
sudo nano /etc/apt/sources.list.d/icinga.list

这将打开一个新的空白文本文件。 粘贴在以下行:

deb https://packages.icinga.com/ubuntu icinga-xenial main

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

$ sudo apt-get update
sudo apt-get update

apt-get将从我们刚刚添加的存储库中下载信息,使Icinga软件包可用于安装:

$ sudo apt-get install icinga2 icinga2-ido-mysql
sudo apt-get install icinga2 icinga2-ido-mysql

这将安装主要的Icinga软件,以及数据库适配器,使Icinga能够将历史数据和其他信息放入MySQL数据库。 您将看到数据库适配器的几个配置屏幕:

现在我们需要实际启用Icinga数据库后端。 icinga2命令可以在命令行上启用和禁用功能。 当我们在此时,我们还将启用命令功能,最终将让我们从Web界面运行手动运行状况检查。

$ sudo icinga2 feature enable ido-mysql command
sudo icinga2 feature enable ido-mysql command

现在重新启动icinga2以使用新功能:

$ sudo systemctl restart icinga2
sudo systemctl restart icinga2

最后,我们检查icinga2的状态,以确保它正常运行:

$ sudo systemctl status icinga2
sudo systemctl status icinga2
Output
       ● icinga2.service - Icinga host/service/network monitoring system
   Loaded: loaded (/lib/systemd/system/icinga2.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2017-04-20 00:54:55 UTC; 3s ago
  Process: 15354 ExecStartPre=/usr/lib/icinga2/prepare-dirs /usr/lib/icinga2/icinga2 (code=exited, status=0/SUCCESS)
 Main PID: 15416 (icinga2)
    Tasks: 11
   Memory: 7.7M
      CPU: 488ms
. . .

如果您看到Active:active(running),Icinga已启动并运行。 现在我们已经设置了Icinga核心系统和数据库后端,现在是让Web界面启动并运行的时候了。


步骤2 - 安装Icinga Web界面

Icinga内核是完全可配置和可用的,没有Web界面,但Icinga Web提供了一个很好的可浏览的主机和服务的健康概述,并允许您安排停机时间,确认问题,手动触发健康状况检查和发送通知,正确 从您的浏览器

让我们用apt-get安装Icinga Web:

$ sudo apt-get install icingaweb2
sudo apt-get install icingaweb2

Icinga Web的其他设置是在Web浏览器中完成的,但在我们切换之前,需要更新一个设置。 Icinga Web需要为PHP环境设置时区,所以我们来编辑PHP配置文件:

$ sudo nano /etc/php/7.0/apache2/php.ini
sudo nano /etc/php/7.0/apache2/php.ini

我们需要找到要更新的特定行。 在nano中,我们可以按CTRL-W打开一个搜索界面,输入date.timezone,然后按ENTER键。 光标将移动到我们需要更新的行。 首先,删除初始的行; 然后输入正确的时区。 您可以在PHP手册的时区部分找到正确的时区格式。 完成后应该看起来像这样:

date.timezone = America/New_York

保存并关闭文件。 重新启动Apache进行更新:

$ sudo systemctl restart apache2
sudo systemctl restart apache2

现在是时候通过Icinga Web浏览器进行安装了。


步骤3 - 设置Icinga Web界面

在我们将浏览器切换到基于Web的安装过程之前,我们需要创建一个安装令牌。 这是我们在命令行上生成的一个关键,它授权我们使用Web设置工具。 我们用icingacli命令创建这个密钥:

$ sudo icingacli setup token create
sudo icingacli setup token create

将打印一个简短的标记:

Output
       1558c2c0ec4572ab

将令牌复制到剪贴板,然后切换到浏览器并加载Icinga Web地址。 默认情况下,这是您的服务器的域名或IP地址,后跟/ icingaweb2:

https://icinga-master.example.com/icingaweb2

您将看到一个配置屏幕。 粘贴在您复制到剪贴板的令牌中,然后按下一步开始该过程。 有很多页面的选项可以通过。 我们一次一个通过他们。


模块设置

在第二页上,您可以选择为Web界面启用一些额外的模块。 我们可以安全地接受仅启用监控模块的默认设置。 单击下一步继续。


环境状况

第三页显示了PHP环境的状态。 您不应该看到任何红色的框,这表示一个问题或配置错误。 您可能会看到一些黄色框提到PostgreSQL模块丢失。 我们可以安全地忽略这些,因为我们使用的是MySQL,而不是PostgreSQL。 单击下一步继续。


Icinga网络认证

第四页让我们选择我们如何对Icinga Web用户进行身份验证。 如果您想要与LDAP服务集成以进行身份验证,那么这将是您选择的地方。 我们将使用默认数据库将用户存储在我们的MySQL数据库中。 单击下一步继续。


用户数据库设置

第五页要求我们设置一个数据库来存储用户数据。 这与我们以前在命令行安装期间设置的数据库是分开的。

大多数默认值都不错,但是我们还需要选择一个数据库名称和用户/密码组合:

点击下一步继续。


创建用户数据库

下一页将会说您的数据库不存在,您没有凭据来创建它。 输入root用户名,输入MySQL根密码,然后单击下一步创建Icinga Web数据库和用户。


命名验证提供程序

现在我们需要命名我们刚刚创建的身份验证后端。 默认的icingaweb2很好。 点击下一步。


创建管理员帐户

现在我们设置了我们的用户数据库,我们可以创建我们的第一个Icinga Web管理帐户。 选择用户名和密码,然后单击下一步继续。


首选项和日志存储

接下来,我们将介绍如何存储用户首选项和日志的选项。 默认值是正常的,并且将记录到syslog的数据库中存储首选项。 点击下一步继续。


配置审查

我们会看到一个页面来查看我们所有的配置。 单击“下一步”确认配置详细信息,并继续配置监控模块。


监控配置简介

现在我们开始配置Icinga Web的实际监控模块。 单击下一步开始。


选择监控后端

首先,我们选择我们的监控后端。 icinga的默认名称和IDO的类型是正确的。 这表明Icinga Web将在命令行上安装东西时从我们之前配置的ido-mysql数据库中检索信息。


设置监控数据库

我们需要输入ido-mysql数据库的连接详细信息。 我们在安装过程中创建了这个密码。

此页面与用户数据库设置屏幕具有所有相同的选项:

单击下一步继续。


选择命令传输方法

接下来是命令传输提示符。 这样,当我们在Web界面中手动运行运行状况检查时,我们可以指定Icinga Web如何将命令传递给Icinga。 本地命令文件的默认值是正常的,并且可以使用我们在步骤1中启用的命令功能。单击下一步继续。


设置监控接口安全

这允许您指定Web界面中应该被屏蔽的数据,以防止任何潜在的旁观者看到密码和其他敏感信息。 默认值是罚款。 点击下一步继续。


监控模块配置摘要

我们再次提供了我们的配置摘要。 点击Finish完成Icinga Web的设置。 恭喜! 消息将加载。

单击登录到Icinga Web 2并使用您的管理员用户名和密码登录。

Icinga Web的主界面将加载。 探索一下,熟悉界面。 如果您的服务器没有设置交换空间,您可能会看到一个红色的严重错误框。 我们现在可以忽略这一点,或者您可以通过点击红色框,从右侧列中选择确认,填写评论,最后点击确认问题按钮来确认问题。

现在我们已经完成了设置Icinga和Icinga Web,我们设置了电子邮件通知。


步骤4 - 设置电子邮件

如果出现问题时无法收到警报,则监控功能不会太有帮助。 Icinga的默认配置有一些脚本给管理员发送电子邮件,但是我们需要在我们的服务器上设置电子邮件,然后才能工作。 最简单的方法是使用名为ssmtp的程序来通过标准SMTP服务器路由所有服务器的邮件。

首先,安装ssmtp和一些帮助邮件实用程序:

$ sudo apt-get install ssmtp mailutils
sudo apt-get install ssmtp mailutils

现在我们使用我们的SMTP详细信息编辑ssmtp配置文件。 这些应由您的ISP,电子邮件提供商或IT部门提供。 您将需要一个用户名,pasword和SMTP服务器的地址:

$ sudo nano /etc/ssmtp/ssmtp.conf
sudo nano /etc/ssmtp/ssmtp.conf

文件中将存在一些现有配置。 删除它并替换它与这个非常基本的设置,应该适用于大多数SMTP服务器:

mailhub=mail.example.com:465
UseTLS=yes
FromLineOverride=yes
AuthUser=smtp_username
AuthPass=smtp_password

保存并关闭文件。 要测试连接,请使用mail命令:

$ echo "hello world" | mail -s "test subject" sammy@example.com
echo "hello world" | mail -s "test subject" sammy@example.com

您很快就会在收件箱中看到电子邮件。 现在我们需要更新Icinga的一些设置来发送邮件。


步骤5 - 设置和测试通知

要使电子邮件通知正常工作,请更新Icinga发送的电子邮件地址:

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

将电子邮件行更改为您希望接收通知的地址:

. . .
email = "sammy@example.com"
. . .

最后一次重新启动Icinga:

$ sudo systemctl restart icinga2
sudo systemctl restart icinga2

icinga-master主机已配置为在出现问题时发送通知。 让我们引起一个问题,看看会发生什么。 我们将使用一个名为stress的命令来增加系统的负载,以触发警告。

安装压力:

$ sudo apt-get install stress
sudo apt-get install stress

压力可以操纵负载,磁盘IO,内存和其他系统度量。 当系统的负载超过5时,Icinga默认配置将触发警告。 现在我们来说吧

$ stress --cpu 6
stress --cpu 6

切换回Icinga Web界面,您将看到负载指标缓慢上升。 经过几次检查,它将进入一个软警告状态。 Soft意味着支票在被认为是硬状态之前必须再次失败,此时将发送通知。 这是为了避免发送快速修复自身的短暂问题的通知。

等待警告到达硬状态并发送通知。 您应该收到一封电子邮件,其中包含有什么问题。

按CTRL-C退出压力指令。 系统负载将恢复得相当快,并在Icinga Web界面中恢复为OK。 您还将收到另一封电子邮件,通知您问题已清除。


结论

在本教程中,我们已成功设置了Icinga和Icinga Web,包括Icinga的电子邮件通知功能。 目前,我们只是监视Icinga主机本身。 继续阅读我们的下一个教程如何使用Icinga在Ubuntu 16.04上监视主机和服务,我们将在此设置远程监控。