[comment]: # aside: 8

[comment]: # ({0535e4cb-0535e4cb})
# 在 macOS 上构建 Zabbix agent

[comment]: # ({/0535e4cb-0535e4cb})

[comment]: # ({161fafaa-161fafaa})
#### 概述

本节演示如何从源代码构建带有或不带 TLS 的 Zabbix macOS agent 二进制文件。

[comment]: # ({/161fafaa-161fafaa})

[comment]: # ({ad36333f-7e7224fc})
#### 先决条件

您将需要命令行开发工具（不要求 Xcode）、
Automake、pkg-config 以及 PCRE（v8.x）或 PCRE2（v10.x）。如果您想构建带有 TLS 的 agent
二进制文件，还需要 OpenSSL 或 GnuTLS。

要安装 Automake 和 pkg-config，您需要从 <https://brew.sh/> 获取 Homebrew 包管理器。要安装它，请打开终端并运行
以下命令：

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

然后安装 Automake 和 pkg-config：

    brew install automake
    brew install pkg-config

准备 PCRE、OpenSSL 和 GnuTLS 库取决于它们将以何种方式
链接到 agent。

如果您打算在已经安装了这些库的 macOS 机器上运行 agent 二进制文件，
则可以使用 Homebrew 提供的预编译库。这通常是指那些使用 Homebrew 来
构建 Zabbix agent 二进制文件或用于其他目的的 macOS 机器。

如果 agent 二进制文件将在没有这些库共享版本的 macOS 机器上使用，
则应从源码编译静态库，并将 Zabbix agent 与这些静态库链接。

[comment]: # ({/ad36333f-7e7224fc})

[comment]: # ({8ee327a1-5e97a918})
#### 使用共享库构建 agent 二进制文件

安装 PCRE2（如有需要，请将以下命令中的 *pcre2* 替换为 *pcre*）：

    brew install pcre2

如果使用 TLS 进行构建，请安装 OpenSSL 和/或 GnuTLS：

    brew install openssl
    brew install gnutls

下载 Zabbix 源代码：

    git clone https://git.zabbix.com/scm/zbx/zabbix.git

构建不带 TLS 的 agent：

    cd zabbix
    ./bootstrap.sh
    ./configure --sysconfdir=/usr/local/etc/zabbix --enable-agent --enable-ipv6
    make
    make install

构建带 OpenSSL 的 agent：

    cd zabbix
    ./bootstrap.sh
    ./configure --sysconfdir=/usr/local/etc/zabbix --enable-agent --enable-ipv6 --with-openssl=/usr/local/opt/openssl
    make
    make install

构建带 GnuTLS 的 agent：

    cd zabbix-source/
    ./bootstrap.sh
    ./configure --sysconfdir=/usr/local/etc/zabbix --enable-agent --enable-ipv6 --with-gnutls=/usr/local/opt/gnutls
    make
    make install

[comment]: # ({/8ee327a1-5e97a918})

[comment]: # ({d64f422c-90fe7eb9})
#### 使用静态库构建不带 TLS 的 agent 二进制文件

假设 PCRE 静态库将安装在
`$HOME/static-libs` 中。这里我们将使用 PCRE2 10.39。

    PCRE_PREFIX="$HOME/static-libs/pcre2-10.39"

下载并构建支持 Unicode 属性的 PCRE：

    mkdir static-libs-source
    cd static-libs-source
    curl --remote-name https://github.com/PhilipHazel/pcre2/releases/download/pcre2-10.39/pcre2-10.39.tar.gz
    tar xf pcre2-10.39.tar.gz
    cd pcre2-10.39
    ./configure --prefix="$PCRE_PREFIX" --disable-shared --enable-static --enable-unicode-properties
    make
    make check
    make install

下载 Zabbix 源代码并构建 agent：

    git clone https://git.zabbix.com/scm/zbx/zabbix.git
    cd zabbix
    ./bootstrap.sh
    ./configure --sysconfdir=/usr/local/etc/zabbix --enable-agent --enable-ipv6 --with-libpcre2="$PCRE_PREFIX"
    make
    make install

[comment]: # ({/d64f422c-90fe7eb9})

[comment]: # ({8ba21217-b985c38b})
#### 使用 OpenSSL 静态库构建 agent 二进制文件

构建 OpenSSL 时，建议在成功完成构建后运行 `make test`。即使构建成功，测试有时也可能失败。如果出现这种情况，应在继续之前研究并解决相关问题。

假设 PCRE 和 OpenSSL 静态库将安装到 `$HOME/static-libs`。这里我们将使用 PCRE2 10.39 和 OpenSSL 1.1.1a。

    PCRE_PREFIX="$HOME/static-libs/pcre2-10.39"
    OPENSSL_PREFIX="$HOME/static-libs/openssl-1.1.1a"

让我们在 `static-libs-source` 中构建静态库：

    mkdir static-libs-source
    cd static-libs-source

下载并构建启用了 Unicode 属性支持的 PCRE：

    curl --remote-name https://github.com/PhilipHazel/pcre2/releases/download/pcre2-10.39/pcre2-10.39.tar.gz
    tar xf pcre2-10.39.tar.gz
    cd pcre2-10.39
    ./configure --prefix="$PCRE_PREFIX" --disable-shared --enable-static --enable-unicode-properties
    make
    make check
    make install
    cd ..

下载并构建 OpenSSL：

    curl --remote-name https://www.openssl.org/source/openssl-1.1.1a.tar.gz
    tar xf openssl-1.1.1a.tar.gz
    cd openssl-1.1.1a
    ./Configure --prefix="$OPENSSL_PREFIX" --openssldir="$OPENSSL_PREFIX" --api=1.1.0 no-shared no-capieng no-srp no-gost no-dgram no-dtls1-method no-dtls1_2-method darwin64-x86_64-cc
    make
    make test
    make install_sw
    cd ..

下载 Zabbix 源代码并构建 agent：

    git clone https://git.zabbix.com/scm/zbx/zabbix.git
    cd zabbix
    ./bootstrap.sh
    ./configure --sysconfdir=/usr/local/etc/zabbix --enable-agent --enable-ipv6 --with-libpcre2="$PCRE_PREFIX" --with-openssl="$OPENSSL_PREFIX"
    make
    make install

[comment]: # ({/8ba21217-b985c38b})

[comment]: # ({d90d667e-8165604b})
#### 使用 GnuTLS 和静态库构建 agent 二进制文件

GnuTLS 依赖于 Nettle 加密后端和 GMP 算术库。
本指南将使用 Nettle 中包含的 mini-gmp，而不是完整的 GMP 库。

在构建 GnuTLS 和 Nettle 时，建议在成功构建后运行 `make check`。
即使构建成功，测试有时也会失败。
如果出现这种情况，应在继续之前研究并解决相关问题。

假设 PCRE、Nettle 和 GnuTLS 静态库将安装到 `$HOME/static-libs` 中。我们将使用 PCRE2 10.39、Nettle 3.4.1 和 GnuTLS 3.6.5。

    PCRE_PREFIX="$HOME/static-libs/pcre2-10.39"
    NETTLE_PREFIX="$HOME/static-libs/nettle-3.4.1"
    GNUTLS_PREFIX="$HOME/static-libs/gnutls-3.6.5"

让我们在 `static-libs-source` 中构建静态库：

    mkdir static-libs-source
    cd static-libs-source

下载并构建 Nettle：

    curl --remote-name https://ftp.gnu.org/gnu/nettle/nettle-3.4.1.tar.gz
    tar xf nettle-3.4.1.tar.gz
    cd nettle-3.4.1
    ./configure --prefix="$NETTLE_PREFIX" --enable-static --disable-shared --disable-documentation --disable-assembler --enable-x86-aesni --enable-mini-gmp
    make
    make check
    make install
    cd ..

下载并构建 GnuTLS：

    curl --remote-name https://www.gnupg.org/ftp/gcrypt/gnutls/v3.6/gnutls-3.6.5.tar.xz
    tar xf gnutls-3.6.5.tar.xz
    cd gnutls-3.6.5
    PKG_CONFIG_PATH="$NETTLE_PREFIX/lib/pkgconfig" ./configure --prefix="$GNUTLS_PREFIX" --enable-static --disable-shared --disable-guile --disable-doc --disable-tools --disable-libdane --without-idn --without-p11-kit --without-tpm --with-included-libtasn1 --with-included-unistring --with-nettle-mini
    make
    make check
    make install
    cd ..

下载 Zabbix 源代码并构建 agent：

    git clone https://git.zabbix.com/scm/zbx/zabbix.git
    cd zabbix
    ./bootstrap.sh
    CFLAGS="-Wno-unused-command-line-argument -framework Foundation -framework Security" \
    > LIBS="-lgnutls -lhogweed -lnettle" \
    > LDFLAGS="-L$GNUTLS_PREFIX/lib -L$NETTLE_PREFIX/lib" \
    > ./configure --sysconfdir=/usr/local/etc/zabbix --enable-agent --enable-ipv6 --with-libpcre2="$PCRE_PREFIX" --with-gnutls="$GNUTLS_PREFIX"
    make
    make install

[comment]: # ({/d90d667e-8165604b})
