如何在Ubuntu 20.04上安装和配置Zabbix以安全地监视远程服务器

The author selected the Computer History Museum to receive a donation as part of the Write for DOnations program.

作者选择“ 计算机历史博物馆”作为“ Write for DOnations”计划的一部分接受捐赠。

介绍 (Introduction)

Zabbix is open-source monitoring software for networks and applications. It offers real-time monitoring of thousands of metrics collected from servers, virtual machines, network devices, and web applications. These metrics can help you determine the current health of your IT infrastructure and detect problems with hardware or software components before customers complain. Useful information is stored in a database so you can analyze data over time and improve the quality of provided services or plan upgrades of your equipment.

Zabbix是用于网络和应用程序的开源监视软件。 它提供对从服务器,虚拟机,网络设备和Web应用程序收集的数千个指标的实时监视。 这些指标可以帮助您确定IT基础架构的当前运行状况,并在客户抱怨之前检测硬件或软件组件的问题。 有用的信息存储在数据库中,因此您可以随时间分析数据并提高所提供服务的质量或计划设备的升级。

Zabbix uses several options for collecting metrics, including agentless monitoring of user services and client-server architecture. To collect server metrics, it uses a small agent on the monitored client to gather data and send it to the Zabbix server. Zabbix supports encrypted communication between the server and connected clients, so your data is protected while it travels over insecure networks.

Zabbix使用多种选项来收集指标,包括对用户服务和客户端-服务器体系结构的无代理监视。 为了收集服务器指标,它在受监视的客户端上使用小型代理来收集数据并将其发送到Zabbix服务器。 Zabbix支持服务器与连接的客户端之间的加密通信,因此在不安全的网络上传输数据时,您的数据将受到保护。

The Zabbix server stores its data in a relational database powered by MySQL or PostgreSQL. You can also store historical data in NoSQL databases like Elasticsearch and TimescaleDB. Zabbix provides a web interface so you can view data and configure system settings.

Zabbix服务器将其数据存储在由MySQL或PostgreSQL支持的关系数据库中。 您还可以将历史数据存储在NoSQL数据库(例如Elasticsearch和TimescaleDB)中 。 Zabbix提供了一个Web界面,因此您可以查看数据和配置系统设置。

In this tutorial, you will configure Zabbix on two Ubuntu 20.04 machines. One will be configured as the Zabbix server, and the other as a client that you’ll monitor. The Zabbix server will use a MySQL database to record monitoring data and use Nginx to serve the web interface.

在本教程中,您将在两台Ubuntu 20.04计算机上配置Zabbix。 一台将配置为Zabbix服务器,另一台将配置为您将监视的客户端。 Zabbix服务器将使用MySQL数据库记录监视数据,并使用Nginx服务Web界面。

先决条件 (Prerequisites)

To follow this tutorial, you will need:

要遵循本教程,您将需要:

  • Two Ubuntu 20.04 servers set up by following the Initial Server Setup Guide for Ubuntu 20.04, including a non-root user with sudo privileges and a firewall configured with ufw. On one server, you will install Zabbix; this tutorial will refer to this as the Zabbix server. It will monitor your second server; this second server will be referred to as the second Ubuntu server.

    遵循《 Ubuntu 20.04初始服务器设置指南》设置了两台Ubuntu 20.04服务器,其中包括具有sudo特权的非root用户和使用ufw配置的防火墙。 在一台服务器上,您将安装Zabbix。 本教程将其称为Zabbix服务器 。 它将监视您的第二台服务器; 第二台服务器将被称为第二台Ubuntu服务器

  • The server that will run the Zabbix server needs Nginx, MySQL, and PHP installed. Follow Steps 1–3 of our Ubuntu 20.04 LEMP Stack guide to configure those on your Zabbix server.

    将运行Zabbix服务器的服务器需要安装Nginx,MySQL和PHP。 按照我们的Ubuntu 20.04 LEMP Stack指南的步骤1-3,在您的Zabbix服务器上进行配置。

  • A registered domain name. This tutorial will use your_domain throughout. You can purchase a domain name from Namecheap, get one for free with Freenom, or use the domain registrar of your choice.

    注册域名。 本教程将整个使用your_domain 。 你可以购买一个域名Namecheap ,免费获得一个与Freenom ,或使用你选择的域名注册商。

  • Both of the following DNS records set up for your Zabbix server. If you are using DigitalOcean, please see our DNS documentation for details on how to add them.

    以下两个DNS记录均已为您的Zabbix服务器设置。 如果您使用的是DigitalOcean,请参阅我们的DNS文档以获取有关如何添加它们的详细信息。

    • An A record with your_domain pointing to your Zabbix server’s public IP address.

      A记录,其中your_domain指向您的Zabbix服务器的公共IP地址。

    • An A record with www.your_domain pointing to your Zabbix server’s public IP address.

      www. your_domain的A记录www. your_domain www. your_domain指向您的Zabbix服务器的公共IP地址。

Additionally, because the Zabbix Server is used to access valuable information about your infrastructure that you would not want unauthorized users to access, it’s important that you keep your server secure by installing a TLS/SSL certificate. This is optional but strongly encouraged. If you would like to secure your server, follow the Let’s Encrypt on Ubuntu 20.04 guide after Step 3 of this tutorial.

此外,由于Zabbix服务器用于访问您不希望未经授权的用户访问的有关基础结构的有价值的信息,因此,通过安装TLS / SSL证书来确保服务器的安全非常重要。 这是可选的,但强烈建议您这样做。 如果您想保护服务器安全,请遵循本教程第3步之后的“在Ubuntu 20.04上让我们加密”指南。

第1步-安装Zabbix服务器 (Step 1 — Installing the Zabbix Server)

First, you need to install Zabbix on the server where you installed MySQL, Nginx, and PHP. Log in to this machine as your non-root user:

首先,您需要在安装MySQL,Nginx和PHP的服务器上安装Zabbix。 以您的非root用户身份登录到此计算机:

  • ssh sammy@zabbix_server_ip_address

    ssh sammy @ zabbix_server_ip_address

Zabbix is available in Ubuntu’s package manager, but it’s outdated, so use the official Zabbix repository to install the latest stable version. Download and install the repository configuration package:

Zabbix在Ubuntu的软件包管理器中可用,但是已经过时了,因此请使用官方的Zabbix存储库来安装最新的稳定版本。 下载并安装存储库配置包:

  • wget https://repo.zabbix.com/zabbix/5.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_5.0-1+focal_all.deb

    wget https://repo.zabbix.com/zabbix/5.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_ 5.0-1 + focal_all.deb

  • sudo dpkg -i zabbix-release_5.0-1+focal_all.deb

    须藤dpkg -i zabbix -release_ 5.0-1 + focal_all.deb

You will see the following output:

您将看到以下输出:


   
   
     
     
     
     
Output
Selecting previously unselected package zabbix-release. (Reading database ... 64058 files and directories currently installed.) Preparing to unpack zabbix-release_5.0-1+focal_all.deb ... Unpacking zabbix-release (1:5.0-1+focal) ... Setting up zabbix-release (1:5.0-1+focal) ...

Update the package index so the new repository is included:

更新软件包索引,以便包括新的存储库:

  • sudo apt update

    sudo apt更新

Then install the Zabbix server and web frontend with MySQL database support:

然后安装带有MySQL数据库支持的Zabbix服务器和Web前端:

  • sudo apt install zabbix-server-mysql zabbix-frontend-php

    sudo apt安装zabbix-server-mysql zabbix-frontend-php

Also, install the Zabbix agent, which will let you collect data about the Zabbix server status itself.

另外,安装Zabbix代理,它将允许您收集有关Zabbix服务器状态本身的数据。

  • sudo apt install zabbix-agent

    sudo apt安装zabbix-agent

Before you can use Zabbix, you have to set up a database to hold the data that the Zabbix server will collect from its agents. You can do this in the next step.

在使用Zabbix之前,您必须建立一个数据库来保存Zabbix服务器将从其代理收集的数据。 您可以在下一步中执行此操作。

第2步-为Zabbix配置MySQL数据库 (Step 2 — Configuring the MySQL Database for Zabbix)

You need to create a new MySQL database and populate it with some basic information in order to make it suitable for Zabbix. You’ll also create a specific user for this database so Zabbix isn’t logging in to MySQL with the root account.

您需要创建一个新MySQL数据库并使用一些基本信息填充它,以使其适合Zabbix。 您还将为此数据库创建一个特定的用户,以便Zabbix不会使用root帐户登录到MySQL。

Log in to MySQL as the root user:

root用户登录MySQL:

  • sudo mysql

    须藤MySQL

Create the Zabbix database with UTF-8 character support:

创建具有UTF-8字符支持的Zabbix数据库:

  • create database zabbix character set utf8 collate utf8_bin;

    创建数据库zabbix字符集utf8整理utf8_bin;

Then create a user that the Zabbix server will use, give it access to the new database, and set the password for the user:

然后创建一个Zabbix服务器将使用的用户,授予它对新数据库的访问权限,并设置该用户的密码:

  • create user zabbix@localhost identified by 'your_zabbix_mysql_password';

    创建用户ZABBIX @localhost鉴定“your_zabbix_mysql_password”;

  • grant all privileges on zabbix.* to zabbix@localhost;

    将zabbix。*的所有特权授予zabbix @localhost;

That takes care of the user and the database. Exit out of the database console.

那会照顾用户和数据库。 退出数据库控制台。

  • quit;

    退出;

Next you have to import the initial schema and data. The Zabbix installation provided you with a file that sets this up.

接下来,您必须导入初始架构和数据。 Zabbix安装为您提供了一个设置文件。

Run the following command to set up the schema and import the data into the zabbix database. Use zcat since the data in the file is compressed:

运行以下命令来设置架构并将数据导入到zabbix数据库中。 使用zcat因为文件中的数据已压缩:

  • zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix

    zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix

Enter the password for the zabbix MySQL user that you configured when prompted.

输入在提示时配置的zabbix MySQL用户的密码。

This command may take a minute or two to execute. If you see the error ERROR 1045 (28000): Access denied for userzabbix@'localhost' (using password: YES) then make sure you used the right password for the zabbix user.

该命令可能需要一两分钟才能执行。 如果看到错误ERROR 1045 (28000): Access denied for user zabbix @'localhost' (using password: YES) ERROR 1045 (28000): Access denied for user @'localhost' (using password: YES)确保为zabbix用户使用了正确的密码。

In order for the Zabbix server to use this database, you need to set the database password in the Zabbix server configuration file. Open the configuration file in your preferred text editor. This tutorial will use nano:

为了让Zabbix服务器使用此数据库,您需要在Zabbix服务器配置文件中设置数据库密码。 在首选的文本编辑器中打开配置文件。 本教程将使用nano

  • sudo nano /etc/zabbix/zabbix_server.conf

    须藤纳米/etc/zabbix/zabbix_server.conf

Look for the following section of the file:

查找文件的以下部分:

/etc/zabbix/zabbix_server.conf
/etc/zabbix/zabbix_server.conf
...
### Option: DBPassword                           
#       Database password. Ignored for SQLite.   
#       Comment this line if no password is used.
#                                                
# Mandatory: no                                  
# Default:                                       
# DBPassword=
...

These comments in the file explain how to connect to the database. You need to set the DBPassword value in the file to the password for your database user. Add this line after those comments to configure the database:

文件中的这些注释说明了如何连接到数据库。 您需要将文件中的DBPassword值设置为数据库用户的密码。 在这些注释之后添加以下行以配置数据库:

/etc/zabbix/zabbix_server.conf
/etc/zabbix/zabbix_server.conf
...
DBPassword=your_zabbix_mysql_password
...

Save and close zabbix_server.conf by pressing CTRL+X, followed by Y and then ENTER if you’re using nano.

保存并关闭zabbix_server.conf是按CTRL+X ,然后按Y ,然后按ENTER如果您使用的是nano

You’ve now configured the Zabbix server to connect to the database. Next, you will configure the Nginx web server to serve the Zabbix frontend.

现在,您已经配置了Zabbix服务器以连接到数据库。 接下来,您将配置Nginx Web服务器以服务Zabbix前端。

步骤3 —为Zabbix配置Nginx (Step 3 — Configuring Nginx for Zabbix)

To configure Nginx automatically, install the automatic configuration package:

要自动配置Nginx,请安装自动配置包:

  • sudo apt install zabbix-nginx-conf

    sudo apt安装zabbix-nginx-conf

As a result, you will get the configuration file /etc/zabbix/nginx.conf, as well as a link to it in the Nginx configuration directory /etc/nginx/conf.d/zabbix.conf.

结果,您将获得配置文件/etc/zabbix/nginx.conf以及在Nginx配置目录/etc/nginx/conf.d/zabbix.conf

Next, you need to make changes to this file. Open the configuration file:

接下来,您需要对此文件进行更改。 打开配置文件:

  • sudo nano /etc/zabbix/nginx.conf

    须藤纳米/etc/zabbix/nginx.conf

The file contains an automatically generated Nginx server block configuration. It contains two lines that determine the server name and what port it is listening on:

该文件包含自动生成的Nginx服务器块配置。 它包含两行,用于确定服务器名称和它侦听的端口:

/etc/zabbix/nginx.conf
/etc/zabbix/nginx.conf
server {
#        listen          80;
#        server_name     example.com;
...

Uncomment the two lines, and replace example.com with your domain name. Your settings will look like this:

取消注释这两行,并用您的域名替换example.com 。 您的设置将如下所示:

/etc/zabbix/nginx.conf
/etc/zabbix/nginx.conf
server {
        listen          80;
        server_name     your_domain;
...

Save and close the file. Next, test to make sure that there are no syntax errors in any of your Nginx files and reload the configuration:

保存并关闭文件。 接下来,测试以确保您的所有Nginx文件中都没有语法错误,然后重新加载配置:

  • sudo nginx -t

    须藤Nginx -t
  • sudo nginx -s reload

    sudo nginx -s重新加载

Now that Nginx is set up to serve the Zabbix frontend, you will make some modifications to your PHP setup in order for the Zabbix web interface to work properly.

现在已经设置了Nginx来服务Zabbix前端,您将对PHP设置进行一些修改,以使Zabbix Web界面正常工作。

Note: As mentioned in the Prerequisites section, it is recommended that you enable SSL/TLS on your server. If you would like to do this, follow our Ubuntu 20.04 Let’s Encrypt tutorial before you move on to Step 4 to obtain a free SSL certificate for Nginx. This process will automatically detect your Zabbix server block and configure it for HTTPS. After obtaining your SSL/TLS certificates, you can come back and complete this tutorial.

注意:如“先决条件”部分所述,建议您在服务器上启用SSL / TLS。 如果要执行此操作,请继续执行我们的Ubuntu 20.04 Let's Encrypt教程,然后继续执行步骤4,以获取Nginx的免费SSL证书。 此过程将自动检测您的Zabbix服务器块并将其配置为HTTPS。 获得SSL / TLS证书后,您可以返回并完成本教程。

第4步-为Zabbix配置PHP (Step 4 — Configuring PHP for Zabbix)

The Zabbix web interface is written in PHP and requires some special PHP server settings. The Zabbix installation process created a PHP-FPM configuration file that contains these settings. It is located in the directory /etc/zabbix and is loaded automatically by PHP-FPM. You need to make a small change to this file, so open it up with the following:

Zabbix Web界面是用PHP编写的,并且需要一些特殊PHP服务器设置。 Zabbix安装过程创建了一个包含这些设置的PHP-FPM配置文件。 它位于目录/etc/zabbix ,并由PHP-FPM自动加载。 您需要对此文件进行一些小的更改,因此请使用以下命令将其打开:

  • sudo nano /etc/zabbix/php-fpm.conf

    须藤纳米/etc/zabbix/php-fpm.conf

The file contains PHP settings that meet the necessary requirements for the Zabbix web interface. However, the timezone setting is commented out by default. To make sure that Zabbix uses the correct time, you need to set the appropriate timezone:

该文件包含满足Zabbix Web界面必要要求PHP设置。 但是,默认情况下已注释掉时区设置。 为了确保Zabbix使用正确的时间,您需要设置适当的时区:

/etc/zabbix/php-fpm.conf
/etc/zabbix/php-fpm.conf
...
php_value[max_execution_time] = 300
php_value[memory_limit] = 128M
php_value[post_max_size] = 16M
php_value[upload_max_filesize] = 2M
php_value[max_input_time] = 300
php_value[max_input_vars] = 10000
; php_value[date.timezone] = Europe/Riga

Uncomment the timezone line highlighted in the preceding code block and change it to your timezone. You can use this list of supported time zones to find the right one for you. Then save and close the file.

取消注释前面代码块中突出显示的时区行,并将其更改为您的时区。 您可以使用此支持的时区列表为您找到合适的时区 。 然后保存并关闭文件。

Now restart PHP-FPM to apply these new settings:

现在重新启动PHP-FPM以应用以下新设置:

  • sudo systemctl restart php7.4-fpm.service

    sudo systemctl重新启动php 7.4 -fpm.service

You can now start the Zabbix server:

现在,您可以启动Zabbix服务器:

  • sudo systemctl start zabbix-server

    sudo systemctl启动zabbix服务器

Then check whether the Zabbix server is running properly:

然后检查Zabbix服务器是否正常运行:

  • sudo systemctl status zabbix-server

    sudo systemctl状态zabbix服务器

You will see the following status:

您将看到以下状态:


   
   
     
     
     
     
Output
● zabbix-server.service - Zabbix Server Loaded: loaded (/lib/systemd/system/zabbix-server.service; disabled; vendor preset: enabled) Active: active (running) since Fri 2020-06-12 05:59:32 UTC; 36s ago Process: 27026 ExecStart=/usr/sbin/zabbix_server -c $CONFFILE (code=exited, status=0/SUCCESS) ...

Finally, enable the server to start at boot time:

最后,启用服务器在引导时启动:

  • sudo systemctl enable zabbix-server

    sudo systemctl启用zabbix服务器

The server is set up and connected to the database. Next, set up the web frontend.

服务器已设置并连接到数据库。 接下来,设置Web前端。

第5步—配置Zabbix Web界面的设置 (Step 5 — Configuring Settings for the Zabbix Web Interface)

The web interface lets you see reports and add hosts that you want to monitor, but it needs some initial setup before you can use it. Launch your browser and go to the address http://zabbix_server_name or https://zabbix_server_name if you set up Let’s Encrypt. On the first screen, you will see a welcome message. Click Next step to continue.

Web界面使您可以查看报告并添加要监视的主机,但是在使用之前需要进行一些初始设置。 启动浏览器,如果设置了“加密”,则转到地址http:// zabbix_server_namehttps:// zabbix_server_name 。 在第一个屏幕上,您将看到一条欢迎消息。 单击下一步继续。

On the next screen, you will see the table that lists all of the prerequisites to run Zabbix.

在下一个屏幕上,您将看到该表,其中列出了运行Zabbix的所有先决条件。

All of the values in this table must be OK, so verify that they are. Be sure to scroll down and look at all of the prerequisites. Once you’ve verified that everything is ready to go, click Next step to proceed.

该表中的所有值都必须为OK ,因此请验证它们是否正确。 确保向下滚动并查看所有先决条件。 确认一切准备就绪后,请单击下一步继续。

The next screen asks for database connection information.

下一个屏幕询问数据库连接信息。

You told the Zabbix server about your database, but the Zabbix web interface also needs access to the database to manage hosts and read data. Therefore enter the MySQL credentials you configured in Step 2. Click Next step to proceed.

您向Zabbix服务器介绍了数据库,但是Zabbix Web界面还需要访问数据库以管理主机和读取数据。 因此,输入在步骤2中配置MySQL凭据。单击“下一步”继续。

On the next screen, you can leave the options at their default values.

在下一个屏幕上,您可以将选项保留为默认值。

The Name is optional; it is used in the web interface to distinguish one server from another in case you have several monitoring servers. Click Next step to proceed.

名称是可选的; 如果您有多台监视服务器,则在Web界面中使用它将一台服务器与另一台服务器区分开。 单击下一步继续。

The next screen will show the pre-installation summary so you can confirm everything is correct.

下一个屏幕将显示安装前摘要,因此您可以确认所有内容正确。

Click Next step to proceed to the final screen.

单击下一步进入最终屏幕。

The web interface setup is now complete. This process creates the configuration file /usr/share/zabbix/conf/zabbix.conf.php, which you could back up and use in the future. Click Finish to proceed to the login screen. The default user is Admin and the password is zabbix.

Web界面设置现已完成。 此过程将创建配置文件/usr/share/zabbix/conf/zabbix.conf.php ,您将来可以备份和使用它。 单击完成进入登录屏幕。 默认用户为Admin ,密码为zabbix

Before you log in, set up the Zabbix agent on your second Ubuntu server.

登录之前,请在第二台Ubuntu服务器上设置Zabbix代理。

步骤6 —安装和配置Zabbix代理 (Step 6 — Installing and Configuring the Zabbix Agent)

Now you need to configure the agent software that will send monitoring data to the Zabbix server.

现在,您需要配置代理软件,该代理软件会将监视数据发送到Zabbix服务器。

Log in to the second Ubuntu server:

登录第二台Ubuntu服务器:

  • ssh sammy@second_ubuntu_server_ip_address

    ssh sammy @ second_ubuntu_server_ip_address

Just like on the Zabbix server, run the following commands to install the repository configuration package:

就像在Zabbix服务器上一样,运行以下命令来安装存储库配置包:

  • wget https://repo.zabbix.com/zabbix/5.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_5.0-1+focal_all.deb

    wget https://repo.zabbix.com/zabbix/5.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_ 5.0-1 + focal_all.deb

  • sudo dpkg -i zabbix-release_5.0-1+focal_all.deb

    须藤dpkg -i zabbix -release_ 5.0-1 + focal_all.deb

Next, update the package index:

接下来,更新包索引:

  • sudo apt update

    sudo apt更新

Then install the Zabbix agent:

然后安装Zabbix代理:

  • sudo apt install zabbix-agent

    sudo apt安装zabbix-agent

While Zabbix supports certificate-based encryption, setting up a certificate authority is beyond the scope of this tutorial. But you can use pre-shared keys (PSK) to secure the connection between the server and agent.

尽管Zabbix支持基于证书的加密,但是设置证书颁发机构不在本教程的范围之内。 但是您可以使用预共享密钥(PSK)来保护服务器和代理之间的连接。

First, generate a PSK:

首先,生成一个PSK:

  • sudo sh -c "openssl rand -hex 32 > /etc/zabbix/zabbix_agentd.psk"

    sudo sh -c“ openssl rand -hex 32> /etc/zabbix/zabbix_agentd.psk”

Show the key by using cat so you can copy it somewhere:

使用cat显示密钥,以便将其复制到某处:

  • cat /etc/zabbix/zabbix_agentd.psk

    猫/etc/zabbix/zabbix_agentd.psk

The key will look something like this:

密钥将如下所示:


   
   
     
     
     
     
Output
75ad6cb5e17d244ac8c00c96a1b074d0550b8e7b15d0ab3cde60cd79af280fca

Save this for later; you will need it to configure the host.

保存下来供以后使用; 您将需要它来配置主机。

Now edit the Zabbix agent settings to set up its secure connection to the Zabbix server. Open the agent configuration file in your text editor:

现在,编辑Zabbix代理设置以设置其与Zabbix服务器的安全连接。 在文本编辑器中打开代理配置文件:

  • sudo nano /etc/zabbix/zabbix_agentd.conf

    须藤纳米/etc/zabbix/zabbix_agentd.conf

Each setting within this file is documented via informative comments throughout the file, but you only need to edit some of them.

该文件中的每个设置都会通过整个文件中的信息性注释进行记录,但是您只需要对其进行一些编辑即可。

First you have to edit the IP address of the Zabbix server. Find the following section:

首先,您必须编辑Zabbix服务器的IP地址。 查找以下部分:

/etc/zabbix/zabbix_agentd.conf
/etc/zabbix/zabbix_agentd.conf
...
### Option: Server
#       List of comma delimited IP addresses, optionally in CIDR notation, or DNS names of Zabbix servers and Zabbix proxies.
#       Incoming connections will be accepted only from the hosts listed here.
#       If IPv6 support is enabled then '127.0.0.1', '::127.0.0.1', '::ffff:127.0.0.1' are treated equally
#       and '::/0' will allow any IPv4 or IPv6 address.
#       '0.0.0.0/0' can be used to allow any IPv4 address.
#       Example: Server=127.0.0.1,192.168.1.0/24,::1,2001:db8::/32,zabbix.example.com
#
# Mandatory: yes, if StartAgents is not explicitly set to 0
# Default:
# Server=

Server=127.0.0.1
...

Change the default value to the IP of your Zabbix server:

将默认值更改为Zabbix服务器的IP:

/etc/zabbix/zabbix_agentd.conf
/etc/zabbix/zabbix_agentd.conf
...
Server=zabbix_server_ip_address
...

By default, Zabbix server connects to the agent. But for some checks (for example, monitoring the logs), a reverse connection is required. For correct operation, you need to specify the Zabbix server address and a unique host name.

默认情况下,Zabbix服务器连接到代理。 但是对于某些检查(例如,监视日志),需要反向连接。 为了正确操作,您需要指定Zabbix服务器地址和唯一的主机名。

Find the section that configures the active checks and change the default values:

找到配置活动检查并更改默认值的部分:

/etc/zabbix/zabbix_agentd.conf
/etc/zabbix/zabbix_agentd.conf
...
##### Active checks related

### Option: ServerActive
#       List of comma delimited IP:port (or DNS name:port) pairs of Zabbix servers and Zabbix proxies for active checks.
#       If port is not specified, default port is used.
#       IPv6 addresses must be enclosed in square brackets if port for that host is specified.
#       If port is not specified, square brackets for IPv6 addresses are optional.
#       If this parameter is not specified, active checks are disabled.
#       Example: ServerActive=127.0.0.1:20051,zabbix.domain,[::1]:30051,::1,[12fc::1]
#
# Mandatory: no
# Default:
# ServerActive=

ServerActive=zabbix_server_ip_address

### Option: Hostname
#       Unique, case sensitive hostname.
#       Required for active checks and must match hostname as configured on the server.
#       Value is acquired from HostnameItem if undefined.
#
# Mandatory: no
# Default:
# Hostname=

Hostname=Second Ubuntu Server
...

Next, find the section that configures the secure connection to the Zabbix server and enable pre-shared key support. Find the TLSConnect section, which looks like this:

接下来,找到配置与Zabbix服务器的安全连接并启用预共享密钥支持的部分。 找到TLSConnect部分,如下所示:

/etc/zabbix/zabbix_agentd.conf
/etc/zabbix/zabbix_agentd.conf
...
### Option: TLSConnect
#       How the agent should connect to server or proxy. Used for active checks.
#       Only one value can be specified:
#               unencrypted - connect without encryption
#               psk         - connect using TLS and a pre-shared key
#               cert        - connect using TLS and a certificate
#
# Mandatory: yes, if TLS certificate or PSK parameters are defined (even for 'unencrypted' connection)
# Default:
# TLSConnect=unencrypted
...

Then add this line to configure pre-shared key support:

然后添加以下行以配置预共享密钥支持:

/etc/zabbix/zabbix_agentd.conf
/etc/zabbix/zabbix_agentd.conf
...
TLSConnect=psk
...

Next, locate the TLSAccept section, which looks like this:

接下来,找到TLSAccept部分,如下所示:

/etc/zabbix/zabbix_agentd.conf
/etc/zabbix/zabbix_agentd.conf
...
### Option: TLSAccept
#       What incoming connections to accept.
#       Multiple values can be specified, separated by comma:
#               unencrypted - accept connections without encryption
#               psk         - accept connections secured with TLS and a pre-shared key
#               cert        - accept connections secured with TLS and a certificate
#
# Mandatory: yes, if TLS certificate or PSK parameters are defined (even for 'unencrypted' connection)
# Default:
# TLSAccept=unencrypted
...

Configure incoming connections to support pre-shared keys by adding this line:

通过添加以下行来配置传入连接以支持预共享密钥:

/etc/zabbix/zabbix_agentd.conf
/etc/zabbix/zabbix_agentd.conf
...
TLSAccept=psk
...

Next, find the TLSPSKIdentity section, which looks like this:

接下来,找到TLSPSKIdentity部分,如下所示:

/etc/zabbix/zabbix_agentd.conf
/etc/zabbix/zabbix_agentd.conf
...
### Option: TLSPSKIdentity
#       Unique, case sensitive string used to identify the pre-shared key.
#
# Mandatory: no
# Default:
# TLSPSKIdentity=
...

Choose a unique name to identify your pre-shared key by adding this line:

通过添加以下行,选择一个唯一的名称来标识您的预共享密钥:

/etc/zabbix/zabbix_agentd.conf
/etc/zabbix/zabbix_agentd.conf
...
TLSPSKIdentity=PSK 001
...

You’ll use this as the PSK ID when you add your host through the Zabbix web interface.

通过Zabbix Web界面添加主机时,将其用作PSK ID

Then set the option that points to your previously created pre-shared key. Locate the TLSPSKFile option:

然后设置指向您先前创建的预共享密钥的选项。 找到TLSPSKFile选项:

/etc/zabbix/zabbix_agentd.conf
/etc/zabbix/zabbix_agentd.conf
...
### Option: TLSPSKFile
#       Full pathname of a file containing the pre-shared key.
#
# Mandatory: no
# Default:
# TLSPSKFile=
...

Add this line to point the Zabbix agent to your PSK file you created:

添加以下行以将Zabbix代理指向您创建的PSK文件:

/etc/zabbix/zabbix_agentd.conf
/etc/zabbix/zabbix_agentd.conf
...
TLSPSKFile=/etc/zabbix/zabbix_agentd.psk
...

Save and close the file. Now you can restart the Zabbix agent and set it to start at boot time:

保存并关闭文件。 现在,您可以重新启动Zabbix代理并将其设置为在启动时启动:

  • sudo systemctl restart zabbix-agent

    sudo systemctl重新启动zabbix-agent
  • sudo systemctl enable zabbix-agent

    sudo systemctl启用zabbix-agent

For good measure, check that the Zabbix agent is running properly:

为确保正常,请检查Zabbix代理是否正常运行:

  • sudo systemctl status zabbix-agent

    sudo systemctl状态zabbix-agent

You will see the following status, indicating the agent is running:

您将看到以下状态,指示代理正在运行:


   
   
     
     
     
     
Output
● zabbix-agent.service - Zabbix Agent Loaded: loaded (/lib/systemd/system/zabbix-agent.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2020-06-12 08:19:54 UTC; 25s ago ...

The agent will listen on port 10050 for connections from the server. Configure UFW to allow connections to this port:

代理将在端口10050上侦听来自服务器的连接。 配置UFW以允许连接到该端口:

  • sudo ufw allow 10050/tcp

    sudo ufw允许10050 / tcp

You can learn more about UFW in How To Set Up a Firewall with UFW on Ubuntu 20.04.

您可以在如何在Ubuntu 20.04上使用UFW设置防火墙中了解有关UFW的更多信息。

Your agent is now ready to send data to the Zabbix server. But in order to use it, you have to link to it from the server’s web console. In the next step, you will complete the configuration.

您的代理现在可以将数据发送到Zabbix服务器了。 但是要使用它,您必须从服务器的Web控制台链接到它。 在下一步中,您将完成配置。

步骤7 —将新主机添加到Zabbix服务器 (Step 7 — Adding the New Host to the Zabbix Server)

Installing an agent on a server you want to monitor is only half of the process. Each host you want to monitor needs to be registered on the Zabbix server, which you can do through the web interface.

在要监视的服务器上安装代理程序仅完成了一半。 您要监视的每个主机都需要在Zabbix服务器上注册,您可以通过Web界面进行注册。

Log in to the Zabbix Server web interface by navigating to the address http://zabbix_server_name or https://zabbix_server_name:

通过导航到地址http:// zabbix_server_namehttps:// zabbix_server_name登录到Zabbix Server Web界面:

When you have logged in, click on Configuration and then Hosts in the left navigation bar. Then click the Create host button in the top right corner of the screen. This will open the host configuration page.

登录后,单击配置 ,然后单击左侧导航栏中的主机 。 然后,单击屏幕右上角的“ 创建主机”按钮。 这将打开主机配置页面。

Adjust the Host name and IP address to reflect the host name and IP address of your second Ubuntu server, then add the host to a group. You can select an existing group, for example Linux servers, or create your own group. The host can be in multiple groups. To do this, enter the name of an existing or new group in the Groups field and select the desired value from the proposed list.

调整主机名IP地址以反映第二台Ubuntu服务器的主机名和IP地址,然后将该主机添加到组中。 您可以选择一个现有的组,例如Linux服务器 ,或创建自己的组。 主机可以位于多个组中。 为此,请在“ 组”字段中输入现有或新组的名称,然后从建议的列表中选择所需的值。

Before adding the group, click the Templates tab.

添加组之前,请单击“ 模板”选项卡。

Type Template OS Linux by Zabbix agent in the Search field and then select it from the list to add this template to the host.

搜索字段中输入Template OS Linux by Zabbix agent ,然后从列表中选择它以将该模板添加到主机。

Next, navigate to the Encryption tab. Select PSK for both Connections to host and Connections from host. Then set PSK identity to PSK 001, which is the value of the TLSPSKIdentity setting of the Zabbix agent you configured previously. Then set PSK value to the key you generated for the Zabbix agent. It’s the one stored in the file /etc/zabbix/zabbix_agentd.psk on the agent machine.

接下来,导航到“ 加密”选项卡。 选择“ PSK”作为“ 主机 连接”和“主机连接” 。 然后将PSK身份设置为PSK 001 ,这是您先前配置的Zabbix代理的TLSPSKIdentity设置的值。 然后将PSK值设置为您为Zabbix代理生成的密钥。 它是存储在代理计算机上的文件/etc/zabbix/zabbix_agentd.psk中的文件。

Finally, click the Add button at the bottom of the form to create the host.

最后,单击表单底部的“ 添加”按钮以创建主机。

You will see your new host in the list. Wait for a minute and reload the page to see green labels indicating that everything is working fine and the connection is encrypted.

您将在列表中看到新的主机。 等待一分钟,然后重新加载页面,以查看绿色标签,指示一切正常,并且连接已加密。

If you have additional servers you need to monitor, log in to each host, install the Zabbix agent, generate a PSK, configure the agent, and add the host to the web interface following the same steps you followed to add your first host.

如果需要监视其他服务器,请登录到每个主机,安装Zabbix代理,生成PSK,配置代理,然后按照添加第一个主机的相同步骤将主机添加到Web界面。

The Zabbix server is now monitoring your second Ubuntu server. Now, set up email notifications to be notified about problems.

Zabbix服务器现在正在监视第二台Ubuntu服务器。 现在,设置电子邮件通知以通知有关问题。

步骤8 —配置电子邮件通知 (Step 8 — Configuring Email Notifications)

Zabbix automatically supports many types of notifications: email, OTRS, Slack, Telegram, SMS, etc. You can see the full list of integrations at the Zabbix website.

Zabbix自动支持许多类型的通知:电子邮件, OTRS , Slack , Telegram ,SMS等。您可以在Zabbix网站上查看集成的完整列表。

As an example, this tutorial will configure notifications for the Email media type.

例如,本教程将为电子邮件媒体类型配置通知。

Click on Administration, and then Media types in the left navigation bar. You will see the list of all media types. There are two preconfigured options for emails: for the plain text notification and for the HTML notifications. In this tutorial you will use plain text notification. Click on Email.

单击管理 ,然后单击左侧导航栏中的媒体类型 。 您将看到所有媒体类型的列表。 电子邮件有两个预配置的选项:用于纯文本通知和用于HTML通知。 在本教程中,您将使用纯文本通知。 点击电子邮件

Adjust the SMTP options according to the settings provided by your email service. This tutorial uses Gmail’s SMTP capabilities to set up email notifications; if you would like more information about setting this up, see How To Use Google’s SMTP Server.

根据您的电子邮件服务提供的设置调整SMTP选项。 本教程使用Gmail的SMTP功能来设置电子邮件通知。 如果您想了解有关设置的更多信息,请参见如何使用Google的SMTP服务器 。

Note: If you use 2-Step Verification with Gmail, you need to generate an App Password for Zabbix. You’ll only have to enter an App password once during setup. You will find instructions on how to generate this password in the Google Help Center.

注意:如果对Gmail使用两步验证,则需要为Zabbix生成一个应用密码。 在设置过程中,您只需输入一次应用密码即可。 您可以在Google帮助中心中找到有关如何生成此密码的说明 。

If you are using Gmail, type in smtp.gmail.com for the SMTP server field, 465 for the SMTP server port field, gmail.com for SMTP helo, and your email for SMTP email. Then choose SSL/TLS for Connection security and Username and password for Authentication. Enter your Gmail address as the Username, and the App Password you generated from your Google account as the Password.

如果您使用的是Gmail,请在SMTP服务器字段中输入smtp.gmail.com ,在SMTP 服务器端口字段中输入465 ,在SMTP helo中输入 gmail.com ,并在SMTP电子邮件中输入您的电子邮件 。 然后选择SSL / TLS作为连接安全性 ,选择用户名和密码作为Authentication 。 输入您的Gmail地址作为“ 用户名” ,并输入从您的Google帐户生成的“应用程序密码”作为“ 密码”

On the Message templates tab you can see the list of predefined messages for various types of notifications. Finally, click the Update button at the bottom of the form to update the email parameters.

在“ 消息模板”选项卡上,您可以查看各种通知的预定义消息列表。 最后,单击表单底部的“ 更新”按钮以更新电子邮件参数。

Now you can test sending notifications. To do this, click the Test underlined link in the corresponding line.

现在,您可以测试发送通知了。 为此,请单击相应行中的测试带下划线的链接。

You will see a pop-up window. Enter your email address in the Send to field and click the Test button. You will see a message about the successful sending and you will receive a test message.

您将看到一个弹出窗口。 在发送至字段中输入您的电子邮件地址,然后单击测试按钮。 您将看到一条有关成功发送的消息,并且您将收到一条测试消息。

Close the pop-up by clicking the Cancel button.

单击取消按钮,关闭弹出窗口。

Now, create a new user. Click on Administration, and then Users in the left navigation bar. You will see the list of users. Then click the Create user button in the top right corner of the screen. This will open the user configuration page:

现在,创建一个新用户。 单击管理 ,然后单击左侧导航栏中的用户 。 您将看到用户列表。 然后,单击屏幕右上角的“ 创建用户”按钮。 这将打开用户配置页面:

Enter the new username in the Alias field and set up a new password. Next, add the user to the administrator’s group. Type Zabbix administrators in the Groups field and select it from the proposed list.

别名字段中输入新的用户名并设置新密码。 接下来,将用户添加到管理员组。 在“ 组”字段中键入Zabbix administrators ,然后从建议的列表中选择它。

Once you’ve added the group, click the Media tab and click on the Add underlined link (not the Add button below it). You will see a pop-up window.

添加组后,单击“ 媒体”选项卡,然后单击“ 添加带下划线的链接”(而不是其下方的“ 添加”按钮)。 您将看到一个弹出窗口。

Select the Email option from the Type drop down. Enter your email address in the Send to field. You can leave the rest of the options at the default values. Click the Add button at the bottom to submit.

从“ 类型”下拉列表中选择“ 电子邮件”选项。 在发送到字段中输入您的电子邮件地址。 您可以将其余选项保留为默认值。 单击底部的添加按钮以提交。

Now navigate to the Permissions tab. Select Zabbix Super Admin from the User type drop-down menu.

现在导航到“ 权限”选项卡。 从用户类型下拉菜单中选择Zabbix Super Admin

Finally, click the Add button at the bottom of the form to create the user.

最后,单击表单底部的“ 添加”按钮以创建用户。

Note: Using the default password is not safe. In order to change the password of the built-in user Admin click on the alias in the list of users. Then click Change password, enter a new password, and confirm the changes by clicking Update button.

注意:使用默认密码是不安全的。 为了更改内置用户Admin的密码,请单击用户列表中的别名。 然后单击更改密码 ,输入新密码,然后单击更新按钮确认更改。

Now you need to enable notifications. Click on the Configuration tab and then Actions in the left navigation bar. You will see a pre-configured action, which is responsible for sending notifications to all Zabbix administrators. You can review and change the settings by clicking on its name. For the purposes of this tutorial, use the default parameters. To enable the action, click on the red Disabled link in the Status column.

现在,您需要启用通知。 单击配置选项卡,然后在左侧导航栏上的行动 。 您将看到一个预配置的操作,该操作负责向所有Zabbix管理员发送通知。 您可以通过单击其名称来查看和更改设置。 就本教程而言,请使用默认参数。 要启用该操作,请单击“ 状态”列中红色的“ 已禁用”链接。

Now you are ready to receive alerts. In the next step, you will generate one to test your notification setup.

现在,您可以接收警报了。 在下一步中,您将生成一个用于测试通知设置。

第9步-生成测试警报 (Step 9 — Generating a Test Alert)

In this step, you will generate a test alert to ensure everything is connected. By default, Zabbix keeps track of the amount of free disk space on your server. It automatically detects all disk mounts and adds the corresponding checks. This discovery is executed every hour, so you need to wait a while for the notification to be triggered.

在此步骤中,您将生成一个测试警报,以确保一切均已连接。 默认情况下,Zabbix会跟踪服务器上的可用磁盘空间量。 它会自动检测所有磁盘安装并添加相应的检查。 该发现每小时执行一次,因此您需要等待一段时间才能触发通知。

Create a temporary file that’s large enough to trigger Zabbix’s file system usage alert. To do this, log in to your second Ubuntu server if you’re not already connected:

创建一个足以触发Zabbix的文件系统使用情况警报的临时文件。 为此,如果尚未连接,请登录第二台Ubuntu服务器:

  • ssh sammy@second_ubuntu_server_ip_address

    ssh sammy @ second_ubuntu_server_ip_address

Next, determine how much free space you have on the server. You can use the df command to find out:

接下来,确定服务器上有多少可用空间。 您可以使用df命令找出:

  • df -h

    df -h

The command df will report the disk space usage of your file system, and the -h will make the output human-readable. You’ll see output like the following:

命令df将报告文件系统的磁盘空间使用情况, -h将使输出易于阅读。 您将看到类似以下的输出:


   
   
     
     
     
     
Output
Filesystem Size Used Avail Use% Mounted on /dev/vda1 78G 1.4G 77G 2% /

In this case, the free space is 77G. Your free space may differ.

在这种情况下,可用空间为77G 。 您的可用空间可能有所不同。

Use the fallocate command, which allows you to pre-allocate or de-allocate space to a file, to create a file that takes up more than 80% of the available disk space. This will be enough to trigger the alert:

使用fallocate命令(该命令可让您为文件预分配或取消分配空间)来创建占用超过80%的可用磁盘空间的文件。 这足以触发警报:

  • fallocate -l 70G /tmp/temp.img

    fallocate -l 70G /tmp/temp.img

After around an hour, Zabbix will trigger an alert about the amount of free disk space and will run the action you configured, sending the notification message. You can check your inbox for the message from the Zabbix server. You will see a message like:

大约一个小时后,Zabbix将触发有关可用磁盘空间量的警报,并将运行您配置的操作,并发送通知消息。 您可以在收件箱中查看来自Zabbix服务器的消息。 您将看到类似以下的消息:

Problem started at 09:49:08 on 2020.06.12
Problem name: /: Disk space is low (used > 80%)
Host: Second Ubuntu Server
Severity: Warning
Operational data: Space used: 71.34 GB of 77.36 GB (92.23 %)
Original problem ID: 106

You can also navigate to the Monitoring tab and then Dashboard to see the notification and its details.

您还可以导航到“ 监视”选项卡,然后导航到“ 仪表板”以查看通知及其详细信息。

Now that you know the alerts are working, delete the temporary file you created so you can reclaim your disk space:

现在您知道警报正在运行,请删除您创建的临时文件,以便可以回收磁盘空间:

  • rm -f /tmp/temp.img

    rm -f /tmp/temp.img

After a minute Zabbix will send the recovery message and the alert will disappear from the main dashboard.

一分钟后,Zabbix将发送恢复消息,并且警报将从主仪表板中消失。

结论 (Conclusion)

In this tutorial, you learned how to set up a simple and secure monitoring solution that will help you monitor the state of your servers. It can now warn you of problems, and you have the opportunity to analyze the processes occurring in your IT infrastructure.

在本教程中,您学习了如何设置一个简单而安全的监视解决方案,该解决方案将帮助您监视服务器的状态。 它现在可以警告您出现问题,并且您有机会分析IT基础结构中发生的流程。

To learn more about setting up monitoring infrastructure, check out our Monitoring topic page.

要了解有关设置监控基础架构的更多信息,请查看我们的监控主题页面 。

翻译自: https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-zabbix-to-securely-monitor-remote-servers-on-ubuntu-20-04

你可能感兴趣的:(数据库,linux,java,python,运维)