[Ubuntu] Use Zabbix template to easily implement Docker container monitoring

Prerequisites for this article: The Zabbix server has been installed and is running normally, and the Docker container environment is running on Ubuntu 22.04.

 directory:
1. Import the Docker template
2. Install and configure Zabbix Agent2
3. Zabbix Agent2 specific plugin configuration
4. Add hosts in Zabbix

1. Import Docker template

To use this template [Docker by Zabbix agent 2] in Zabbix, you need to make sure it is already available in Zabbix. This template is available in Zabbix 5.0, 5.4 and 6.0 versions. If you can’t find this template in Configuration -> Templates, it might not be imported into the environment after upgrading Zabbix version. Note that Zabbix does not automatically modify or import any templates during the upgrade process, so this template must be imported manually. You can download this template file from the official Zabbix Git (or simply click here) and import it into your Zabbix instance using the Import button in the Configuration -> Templates page.

2. Install and configure Zabbix Agent2

Now, we will install Zabbix Agent from the official Zabbix repository. First, add the Zabbix repository to your system:

wget https://repo.zabbix.com/zabbix/6.2/ubuntu/pool/main/z/zabbix-release/zabbix-release_6.2-4+ubuntu22.04_all.deb
sudo dpkg -i zabbix-release_6.2-4+ubuntu22.04_all.deb
sudo apt update

The following is the result of my test installation, already in SUDO SU mode:

root@Virtual-Machine:/home/KnightTest# wget https://repo.zabbix.com/zabbix/6.2/ubuntu/pool/main/z/zabbix-release/zabbix-release_6.2-4+ubuntu22 .04_all.deb
--2023-05-22 09:44:00-- https://repo.zabbix.com/zabbix/6.2/ubuntu/pool/main/z/zabbix-release/zabbix-release_6.2-4+ubuntu22. 04_all.deb
Resolving repo.zabbix.com (repo.zabbix.com)... 178.128.6.101, 2604:a880:2:d0::2062:d001
Connecting to repo.zabbix.com (repo.zabbix.com)|178.128.6.101|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3780 (3.7K) [application/octet-stream]
Saving to: 'zabbix-release_6.2-4 + ubuntu22.04_all.deb'

zabbix-release_6.2-4 + ubuntu22.04_all.deb 100%[==================================== ==================================================== ==================================================== =================================>] 3.69K --.-KB/s in 0s

2023-05-22 09:44:00 (1.85 GB/s) - 'zabbix-release_6.2-4 + ubuntu22.04_all.deb' saved [3780/3780]

root@Virtual-Machine:/home/KnightTest# dpkg -i zabbix-release_6.2-4 + ubuntu22.04_all.deb
(Reading database ... 209178 files and directories currently installed.)
Preparing to unpack zabbix-release_6.2-4 + ubuntu22.04_all.deb...
Unpacking zabbix-release (1:6.2-4 + ubuntu22.04) over (1:6.2-4 + ubuntu20.04) ...
Setting up zabbix-release (1:6.2-4 + ubuntu22.04) ...
Installing new version of config file /etc/apt/sources.list.d/zabbix-agent2-plugins.list ?…
Installing new version of config file /etc/apt/sources.list.d/zabbix.list ?…
root@Virtual-Machine:/home/KnightTest# apt update
Hit: 1 https://dl.google.com/linux/chrome/deb stable InRelease
Hit: 2 http://download.zerotier.com/debian/jammy jammy InRelease
Hit: 3 http://cn.archive.ubuntu.com/ubuntu jammy InRelease
Get: 4 https://repo.zabbix.com/zabbix-agent2-plugins/1/ubuntu jammy InRelease [4,952 B]
Hit: 5 http://cn.archive.ubuntu.com/ubuntu jammy-updates InRelease
Get: 6 https://repo.zabbix.com/zabbix/6.2/ubuntu jammy InRelease [4,958 B]
Hit: 7 http://cn.archive.ubuntu.com/ubuntu jammy-backports InRelease
Get: 8 https://repo.zabbix.com/zabbix-agent2-plugins/1/ubuntu jammy/main Sources [1,002 B]
Get:9 https://repo.zabbix.com/zabbix-agent2-plugins/1/ubuntu jammy/main amd64 Packages [624 B]
Get: 10 https://repo.zabbix.com/zabbix/6.2/ubuntu jammy/main Sources [1,952 B]
Get: 11 https://repo.zabbix.com/zabbix/6.2/ubuntu jammy/main amd64 Packages [5,491 B]
Get:12 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Fetched 129 kB in 4s (35.2 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
13 packages can be upgraded. Run 'apt list --upgradable' to see them.
N: Skipping acquire of configured file 'main/binary-i386/Packages' as repository 'https://repo.zabbix.com/zabbix/6.2/ubuntu jammy InRelease' doesn't support architecture 'i386'
root@Virtual-Machine:/home/KnightTest#

Install and configure Zabbix Agent2

sudo apt install zabbix-agent2

The following is the result of my test installation, already in SUDO SU mode:

root@Virtual-Machine:/home/KnightTest# apt install zabbix-agent2
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libllvm13 zabbix-agent2-plugin-mongodb zabbix-agent2-plugin-postgresql
Use 'sudo apt autoremove' to remove them.
The following packages will be upgraded:
  zabbix-agent2
1 upgraded, 0 newly installed, 0 to remove and 12 not upgraded.
Need to get 4,155 kB of archives.
After this operation, 205 kB disk space will be freed.
Get:1 https://repo.zabbix.com/zabbix/6.2/ubuntu jammy/main amd64 zabbix-agent2 amd64 1:6.2.9-1+ubuntu22.04 [4,155 kB]
Fetched 4,155 kB in 2s (1,945 kB/s)
(Reading database ... 209178 files and directories currently installed.)
Preparing to unpack .../zabbix-agent2_1:6.2.9-1 + ubuntu22.04_amd64.deb ...
Unpacking zabbix-agent2 (1:6.2.9-1 + ubuntu22.04) over (1:6.0.17-1 + ubuntu22.04) ...
Setting up zabbix-agent2 (1:6.2.9-1 + ubuntu22.04) ...
Installing new version of config file /etc/zabbix/zabbix_agent2.conf ?…
Processing triggers for man-db (2.10.2-1) ...
root@Virtual-Machine:/home/KnightTest#

Change the parameters of the corresponding zabbix server in the /etc/zabbix/zabbix_agent2.conf file. The main ones are as follows:

Server=<Zabbix_Server_IP>
ServerActive=<Zabbix_Server_IP>
Hostname=<Hostname_Of_Ubuntu_Client>

3. Zabbix Agent2 specific plugin configuration

Zabbix agent2 provides some specific plug-in configuration parameters, usually, these specific plug-in related parameters are optional. You can find a complete list of plugin configuration parameters in Zabbix documentation. In new versions of Zabbix agent2, plugin-specific parameters are defined in separate plugin configuration files, located in /etc/zabbix/zabbix_agent2.d/plugins.d/ directory. However, in older versions these parameters were defined directly in the zabbix_agent2.conf file.

For the Zabbix agent2 Docker plugin (for monitoring Docker), we have to configure the unix-socket file location of the Docker service in docker.conf. This can be achieved with the following plugin parameters:

root@Virtual-Machine:/home/KnightTest# cat /etc/zabbix/zabbix_agent2.d/plugins.d/docker.conf
### Option: Plugins.Docker.Endpoint
# Docker API endpoint.
#
# Mandatory: no
# Default: unix:///var/run/docker.sock
Plugins.Docker.Endpoint=unix:///var/run/docker.sock

In the above case, the default location of the unix-socket file for Docker service, if there is any modification, it should be modified according to your own settings.

Add the zabbix user to the Docker group by executing the following command:

usermod -aG docker zabbix

After everything is configured, restart the zabbix-agent2 service and check the status:

sudo systemctl restart zabbix-agent
sudo systemctl enable zabbix-agent
sudo systemctl status zabbix-agent
root@Virtual-Machine:/home/KnightTest# systemctl status zabbix-agent2
● zabbix-agent2.service - Zabbix Agent 2
     Loaded: loaded (/lib/systemd/system/zabbix-agent2.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2023-05-22 10:41:48 CST; 3s ago
   Main PID: 3049063 (zabbix_agent2)
      Tasks: 8 (limit: 4614)
     Memory: 6.8M
        CPU: 89ms
     CGroup: /system.slice/zabbix-agent2.service
             └─3049063 /usr/sbin/zabbix_agent2 -c /etc/zabbix/zabbix_agent2.conf

May 22 10:41:48 knight-Virtual-Machine systemd[1]: Started Zabbix Agent 2.
May 22 10:41:48 knight-Virtual-Machine zabbix_agent2[3049063]: Starting Zabbix Agent 2 (6.2.9)
May 22 10:41:48 knight-Virtual-Machine zabbix_agent2[3049063]: Zabbix Agent2 hostname: [KnightTestEnv]
May 22 10:41:48 knight-Virtual-Machine zabbix_agent2[3049063]: Press Ctrl + C to exit.

4. Add hosts in Zabbix

Now a host needs to be created for the Docker instance. Set the hostname and assign a hostgroup – I will assign this to the Linux servers hostgroup. Associate the Docker by Zabbix agent 2 template and the Linux by Zabbix Agent to the host. Because the Zabbix agent2 template is used to collect data, an agent interface needs to be added to this host. The address of the interface is the address of the machine running the Docker container. Click the Add button to complete the host configuration.

You can view information about Docker in Zabbix later, and the following are only partial screenshots.

Supplement:

How to solve N: Skipping acquire of configured file 'main/binary-i386/Packages' as repository 'https://repo.zabbix.com/zabbix/6.2/ubuntu jammy InRelease' doesn't support architecture 'i386'

Modify **/etc/apt/sources.list.d/zabbix.list ** to add the “[arch=amd64]” option before the URL of the repository to restrict it to the amd64 architecture:

deb [arch=amd64] https://repo.zabbix.com/zabbix/6.2/ubuntu jammy main

Save and close the file, then run the “sudo apt update” command to refresh the package list. This skips the i386 package from the zabbix repository and prevents the error message.

If you need to install a software package that is only applicable to the i386 architecture in the zabbix software library, you can try to manually download the .deb package file on the warehouse website and use “dpkg” to install it. However, be aware that it may cause dependency issues and may not work as expected.