3 在macOS上构建Zabbix agent

概述

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

先决条件

您需要命令行开发者工具(无需Xcode)、 Automake、pkg-config以及PCRE(v8.x)或PCRE2(v10.x)。若需构建支持TLS的agent 二进制文件,还需安装OpenSSL或GnuTLS。

安装Automake和pkg-config需通过https://brew.sh/提供的Homebrew包管理器。 执行安装时,请在终端run以下命令:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

随后安装Automake和pkg-config:

brew install automake
       brew install pkg-config

PCRE、OpenSSL和GnuTLS库的准备工作取决于它们将如何与agent进行链接。

若计划在已安装这些库的macOS设备上runagent二进制文件, 可直接使用Homebrew提供的预编译库。这类设备通常已通过Homebrew 构建Zabbix agent二进制文件或用于其他用途。

若agent二进制文件将在未安装共享version库的macOS设备上使用, 则应从源码编译静态库并与Zabbix agent进行静态链接。

使用共享库构建agent二进制文件

安装PCRE2(如需使用PCRE,请将以下命令中的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

编译agent(不启用TLS):

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

编译agent(启用OpenSSL):

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

编译agent(启用GnuTLS):

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

不使用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

使用OpenSSL的静态库构建agent二进制文件

构建OpenSSL时,建议在成功构建后执行run 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

使用GnuTLS的静态库构建agent二进制文件

GnuTLS依赖于Nettle加密后端和GMP算术库。 本指南将使用Nettle内置的mini-gmp而非完整的GMP库。

构建GnuTLS和Nettle时,建议在成功构建后runmake 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