[comment]: # translation:outdated

[comment]: # ({d5a30f21-22c598e9})
# 在Windows中构建Zabbix agent 2 

[comment]: # ({/d5a30f21-22c598e9})

[comment]: # ({042c565e-84651997})
#### 概述

本节将演示如何从源代码构建Zabbix agent 2 (Windows)。

[comment]: # ({/042c565e-84651997})

[comment]: # ({new-0c4c3de2})
#### Building Zabbix agent 2 with vcpkg

This section contains instructions for building Zabbix agent with [vcpkg](https://learn.microsoft.com/en-us/vcpkg/get_started/overview), a package manager that simplifies dependency management and integration with C++ projects.

1\. Download and install [Build Tools for Visual Studio 2022](https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2022).
During installation, make sure to select the *Desktop development with C++* workload, which includes the vcpkg package manager.

2\. Download and install [Go](https://go.dev/dl/) (available as an MSI installer).
During installation, make sure to specify `C:\Zabbix\Go` as the installation folder.

3\. Download the [MinGW distribution](https://github.com/niXman/mingw-builds-binaries/releases) that uses the Microsoft Visual C runtime library; for example:

- For 64-bit builds: `x86_64-15.1.0-release-win32-seh-msvcrt-rt_v12-rev0.7z`
- For 32-bit builds: `i686-15.1.0-release-win32-dwarf-msvcrt-rt_v12-rev0.7z`

Then, extract it to `C:\Zabbix\mingw64` (or `C:\Zabbix\mingw32` for 32-bit builds).

4\. Initialize vcpkg and install the dependencies required for building Zabbix agent 2 (note that this may take some time):

```bash
cd C:\Zabbix

set PATH=%PATH%;"C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\vcpkg"
vcpkg new --application
vcpkg add port pcre2
vcpkg add port libiconv
vcpkg add port openssl

# For 64-bit builds:
set PATH=C:\Zabbix\mingw64\bin;%PATH%
vcpkg install --triplet x64-mingw-static --x-install-root=x64

# For 32-bit builds:
set PATH=C:\Zabbix\mingw32\bin;%PATH%
vcpkg install --triplet x86-mingw-static --x-install-root=x86
```

5\. Download the [Zabbix source archive](https://www.zabbix.com/download_sources#72) and extract it to `C:\Zabbix\zabbix-7.2.0`.

6\. Navigate to the Zabbix build directory (`C:\Zabbix\zabbix-7.2.0\build\mingw`) and create the following `build.bat` script:

- For 64-bit builds:

```bash
:: Add MinGW and Go to the system `PATH` variable for the current session:
set PATH=C:\Zabbix\mingw64\bin;%PATH%
set PATH=C:\Zabbix\Go\bin;%PATH%

:: Set vcpkg installation path:
set vcpkg="C:\Zabbix\x64\x64-mingw-static"

:: Set linker flags for Crypt32 library:
SET CGO_LDFLAGS="-lCrypt32"

:: Run the build process:
mingw32-make GOFLAGS="-buildvcs=false" ARCH=AMD64 ^
    PCRE2="%vcpkg%" ^
    OPENSSL="%vcpkg%" ^
    all
```

- For 32-bit builds:

```bash
:: Add MinGW and Go to the system `PATH` variable for the current session:
set PATH=C:\Zabbix\mingw32\bin;%PATH%
set PATH=C:\Zabbix\Go\bin;%PATH%

:: Set vcpkg installation path:
set vcpkg="C:\Zabbix\x86\x86-mingw-static"

:: Set linker flags for Crypt32 library:
SET CGO_LDFLAGS="-lCrypt32"

:: Run the build process:
mingw32-make GOFLAGS="-buildvcs=false" ARCH=x86 ^
    PCRE2="%vcpkg%" ^
    OPENSSL="%vcpkg%" ^
    all
```

7\. Compile Zabbix agent 2 by executing the script:

```bash
build.bat
```

After compilation, the Zabbix agent 2 binary will be located in `C:\Zabbix\zabbix-7.2.0\bin\win64` (for 64-bit builds) or `C:\Zabbix\zabbix-7.2.0\bin\win32` (for 32-bit builds).
Zabbix agent 2 configuration files are located in `C:\Zabbix\zabbix-7.2.0\src\go\conf`.

To run the agent, `zabbix_agent2.exe` and its configuration files to a dedicated folder (e.g., `C:\Zabbix\agent2`) and then run the agent:

```bash
mkdir C:\Zabbix\agent2

# For 64-bit builds:
copy C:\Zabbix\zabbix-7.2.0\bin\win64\zabbix_agent2.exe C:\Zabbix\agent2\

# For 32-bit builds:
copy C:\Zabbix\zabbix-7.2.0\bin\win32\zabbix_agent2.exe C:\Zabbix\agent2\

copy C:\Zabbix\zabbix-7.2.0\src\go\conf\zabbix_agent2.win.conf C:\Zabbix\agent2\
xcopy /E /I C:\Zabbix\zabbix-7.2.0\src\go\conf\zabbix_agent2.d C:\Zabbix\agent2\zabbix_agent2.d\

C:\Zabbix\agent2\zabbix_agent2.exe -c C:\Zabbix\agent2\zabbix_agent2.win.conf
```

If necessary, continue with compiling Zabbix agent 2 loadable plugins.

[comment]: # ({/new-0c4c3de2})

[comment]: # ({new-83a3edaf})
##### Compiling Zabbix agent 2 loadable plugins

1\. Download the [Zabbix plugin source](https://cdn.zabbix.com/zabbix-agent2-plugins/sources/) that matches your Zabbix agent 2 version (e.g., `zabbix-agent2-plugin-ember-plus-7.2.0.tar.gz`) and extract it to `C:\Zabbix`.

2\. Navigate to the extracted plugin directory and compile the plugin:

```bash
cd C:\Zabbix\zabbix-agent2-plugin-ember-plus-7.2.0

# For 64-bit builds:
mingw32-make ARCH=AMD64

# For 32-bit builds:
mingw32-make ARCH=x86
```

After compilation, the Zabbix agent 2 plugin binary and its configuration file will be located in the same plugin directory.

The plugin executable may be placed anywhere as long as it is loadable by Zabbix agent 2.
Specify the path to the plugin binary in the plugin configuration file, e.g., in ember.conf for the [Ember+ plugin](/manual/appendix/config/zabbix_agent2_plugins/ember_plus_plugin):

```ini
Plugins.EmberPlus.System.Path=/path/to/executable/zabbix-agent2-plugin-ember-plus
```

The path to the plugin configuration file must be specified in the [Include](/manual/appendix/config/zabbix_agent2_win#include) parameter of the Zabbix agent 2 configuration file:

```ini
Include=/path/to/plugin/configuration/file/ember.conf
```

[comment]: # ({/new-83a3edaf})

[comment]: # ({new-87d14870})
#### Building Zabbix agent 2 manually

::: noteimportant
This method of building Zabbix agent 2 is suitable for users who require full control over the build environment or are in a restricted environment where [using vcpkg](#building-zabbix-agent-with-vcpkg) is not possible.
:::

This section contains instructions for building Zabbix agent 2 manually, which includes installing the required build tools and dependencies, and then compiling the agent.

[comment]: # ({/new-87d14870})

[comment]: # ({0d2d458c-2a4da91c})
#### 安装MinGW编译器

1\. 下载带有SJLJ (设置跳转/长跳转) 异常处理和窗口线程的MinGW-w64 (例如 *x86\_64-8.1.0-release-win32-sjlj-rt\_v6-rev0.7z*)\
2. 提取并移动到 *c:\\mingw*\
3. 设置环境变量

    @echo off
    set PATH=%PATH%;c:\mingw\bin
    cmd

编译时使用Windows提示符代替MinGW提供的MSYS终端。

[comment]: # ({/0d2d458c-2a4da91c})

[comment]: # ({new-822e5b3f})
##### Installing OpenSSL

::: noteclassic
To compile Zabbix agent without TLS support, proceed to the [Installing PCRE2](#installing-pcre2) section.
:::

1\. Open the MSYS2 MSYS terminal with administrator privileges and run the following commands:

```bash
pacman -S perl-Locale-Maketext-Simple
pacman -S nasm
pacman -S make
pacman -S cmake
```

2\. Download the [OpenSSL source archive](https://openssl-library.org/source/) and extract it to `C:\Zabbix\openssl-3.5.0`.

3\. Navigate to the extracted OpenSSL directory and create the following `build.sh` script:

- For 64-bit builds:

```bash
#!/usr/bin/env bash

export PATH="/c/Zabbix/mingw64/bin:$PATH"
export d="/c/Zabbix/x64/OpenSSL-Win64-350-static"

perl Configure mingw64 no-shared no-capieng no-winstore no-srp no-gost no-dgram no-dtls1-method no-dtls1_2-method thread_scheme=winthreads --api=1.1.0 --prefix=$d --openssldir=$d

make
make install
```

- For 32-bit builds:

```bash
#!/usr/bin/env bash

export PATH="/c/Zabbix/mingw32/bin:$PATH"
export d="/c/Zabbix/x86/OpenSSL-Win64-350-static"

perl Configure mingw no-shared no-capieng no-winstore no-srp no-gost no-dgram no-dtls1-method no-dtls1_2-method thread_scheme=winthreads --api=1.1.0 --prefix=$d --openssldir=$d

make
make install
```

::: noteimportant
Make sure to revoke write access from non-administrator users to the `C:\Zabbix\x64\OpenSSL-Win64-350-static` directory.
Otherwise, the agent will load SSL settings from a path that can be modified by unprivileged users, resulting in a potential security vulnerability.
:::

- The `no-shared` option makes libcrypto.lib and libssl.lib OpenSSL static libraries self-contained, so Zabbix binaries include OpenSSL without needing external DLLs.
  This means that Zabbix binaries can be copied to other Windows machines without OpenSSL libraries; however, when a new OpenSSL bugfix version is released, Zabbix agent will need to be recompiled.
- Without the `no-shared` option, Zabbix relies on OpenSSL DLLs at runtime.
  This means that OpenSSL updates may not require recompiling Zabbix agent; however, when copying it to other machines, the OpenSSL DLLs must be also be copied.

For more information about other OpenSSL configuration options, refer to [OpenSSL documentation](https://github.com/openssl/openssl/blob/master/INSTALL.md#configuration-options).

4\. Configure and install OpenSSL by executing the script (note that this may take some time):

```bash
cd /c/Zabbix/openssl-3.5.0
./build.sh
```

[comment]: # ({/new-822e5b3f})

[comment]: # ({new-bf2f316b})
##### Installing PCRE2

1\. Download [PCRE2 source archive](https://github.com/PCRE2Project/pcre2/releases/latest) and extract it to `C:\Zabbix\pcre2-10.45`.

2\. Open the MSYS2 MSYS terminal with administrator privileges.
Then, create a `build` directory in the extracted PCRE2 directory and navigate to it:

```bash
mkdir /c/Zabbix/pcre2-10.45/build
cd /c/Zabbix/pcre2-10.45/build
```

3\. Configure PCRE2:

```bash
# For 64-bit builds:
export PATH="/c/Zabbix/mingw64/bin:$PATH"
cmake -DCMAKE_C_COMPILER=gcc -DCMAKE_C_FLAGS="-O2 -g" -DCMAKE_INSTALL_PREFIX="/c/Zabbix/x64/PCRE2" ..

# For 32-bit builds:
export PATH="/c/Zabbix/mingw32/bin:$PATH"
cmake -DCMAKE_C_COMPILER=gcc -DCMAKE_C_FLAGS="-m32 -O2 -g" -DCMAKE_EXE_LINKER_FLAGS="-Wl,-mi386pe" -DCMAKE_INSTALL_PREFIX="/c/Zabbix/x86/PCRE2" ..
```

::: noteclassic
If any errors occur, it is recommended to delete the CMake cache before attempting to repeat the CMake build process.
The cache (`CMakeCachecache.txt`) can be located in the build directory of the extracted PCRE2 directory.
:::

4\. Install PCRE2:

```bash
make install
```

[comment]: # ({/new-bf2f316b})

[comment]: # ({new-1d340ab8})
##### Compiling Zabbix agent 2

1\. Download the [Zabbix source archive](https://www.zabbix.com/download_sources#72) and extract it to `C:\Zabbix\zabbix-7.2.0`.

If you need to generate a source archive from the raw source repository (e.g., to apply custom patches or build from the latest source code), run the following commands on a **Linux** machine with [Go](https://go.dev/) installed (required for configuring Zabbix agent 2):

```bash
git clone https://git.zabbix.com/scm/zbx/zabbix.git
cd zabbix
./bootstrap.sh
./configure --enable-agent2 --enable-ipv6 --prefix=`pwd`
make dist
```

This will create a source archive, which can then be copied to a Windows machine.

2\. Open the Command Prompt with administrator privileges.
Then, navigate to the Zabbix build directory and compile Zabbix agent;
make sure to correctly specify the directories where OpenSSL and PCRE2 are installed:

- For 64-bit builds:

```bash
cd C:\Zabbix\zabbix-7.2.0\build\mingw
set PATH=C:\Zabbix\mingw64\bin;%PATH%
mklink /D C:\Zabbix\x64\OpenSSL-Win64-350-static\lib C:\Zabbix\x64\OpenSSL-Win64-350-static\lib64

# With TLS support:
mingw32-make ARCH=AMD64 PCRE2="C:\Zabbix\x64\PCRE2" OPENSSL="C:\Zabbix\x64\OpenSSL-Win64-350-static"

# Without TLS support:
mingw32-make ARCH=AMD64 PCRE2="C:\Zabbix\x64\PCRE2"
```

- For 32-bit builds:

```bash
cd C:\Zabbix\zabbix-7.2.0\build\mingw
set PATH=C:\Zabbix\mingw32\bin;%PATH%

# With TLS support:
mingw32-make ARCH=x86 PCRE2="C:\Zabbix\x86\PCRE2" OPENSSL="C:\Zabbix\x86\OpenSSL-Win64-350-static"

# Without TLS support:
mingw32-make ARCH=x86 PCRE2="C:\Zabbix\x86\PCRE2"
```

After compilation, the Zabbix agent 2 binary will be located in `C:\Zabbix\zabbix-7.2.0\bin\win64` (or `C:\Zabbix\zabbix-7.2.0\bin\win32` for 32-bit builds).
Zabbix agent 2 configuration files are located in `C:\Zabbix\zabbix-7.2.0\src\go\conf`.

To run the agent, copy the `zabbix_agent2.exe` binary and its configuration files to a dedicated folder (e.g., `C:\Zabbix\agent2`) and then run the agent:

```bash
mkdir C:\Zabbix\agent2
copy C:\Zabbix\zabbix-7.2.0\bin\win64\zabbix_agent2.exe C:\Zabbix\agent2\
copy C:\Zabbix\zabbix-7.2.0\src\go\conf\zabbix_agent2.win.conf C:\Zabbix\agent2\
xcopy /E /I C:\Zabbix\zabbix-7.2.0\src\go\conf\zabbix_agent2.d C:\Zabbix\agent2\zabbix_agent2.d\

C:\Zabbix\agent2\zabbix_agent2.exe -c C:\Zabbix\agent2\zabbix_agent2.win.conf
```

If necessary, continue with [compiling Zabbix agent 2 loadable plugins](#compiling-zabbix-agent-2-loadable-plugins).

[comment]: # ({/new-1d340ab8})
