From b05bdcd83d8ef3b66d545b341a6157f92eed1dc6 Mon Sep 17 00:00:00 2001 From: Jerry Ma Date: Wed, 29 Oct 2025 11:56:36 +0800 Subject: [PATCH] Add snmp and net-snmp support for macOS and Linux (#946) Co-authored-by: Marc --- config/ext.json | 12 +++ config/lib.json | 11 +++ config/source.json | 8 ++ src/SPC/builder/extension/snmp.php | 29 ++++++ src/SPC/builder/linux/library/net_snmp.php | 12 +++ src/SPC/builder/linux/library/openssl.php | 2 +- src/SPC/builder/macos/library/net_snmp.php | 12 +++ src/SPC/builder/macos/library/openssl.php | 2 +- src/SPC/builder/unix/library/net_snmp.php | 49 ++++++++++ src/SPC/doctor/item/LinuxToolCheckList.php | 2 +- .../util/executor/UnixAutoconfExecutor.php | 6 ++ src/globals/extra/snmp-ext-config-old.m4 | 92 +++++++++++++++++++ 12 files changed, 234 insertions(+), 3 deletions(-) create mode 100644 src/SPC/builder/extension/snmp.php create mode 100644 src/SPC/builder/linux/library/net_snmp.php create mode 100644 src/SPC/builder/macos/library/net_snmp.php create mode 100644 src/SPC/builder/unix/library/net_snmp.php create mode 100644 src/globals/extra/snmp-ext-config-old.m4 diff --git a/config/ext.json b/config/ext.json index 615be72b..303e0dde 100644 --- a/config/ext.json +++ b/config/ext.json @@ -756,6 +756,18 @@ "apcu" ] }, + "snmp": { + "support": { + "Windows": "wip", + "BSD": "wip" + }, + "type": "builtin", + "arg-type-unix": "with", + "arg-type-windows": "with", + "lib-depends": [ + "net-snmp" + ] + }, "soap": { "support": { "BSD": "wip" diff --git a/config/lib.json b/config/lib.json index c3d864f4..f40a8181 100644 --- a/config/lib.json +++ b/config/lib.json @@ -697,6 +697,17 @@ "libncurses.a" ] }, + "net-snmp": { + "source": "net-snmp", + "pkg-configs": [ + "netsnmp", + "netsnmp-agent" + ], + "lib-depends": [ + "openssl", + "zlib" + ] + }, "nghttp2": { "source": "nghttp2", "static-libs-unix": [ diff --git a/config/source.json b/config/source.json index 0ba328eb..ee3f8948 100644 --- a/config/source.json +++ b/config/source.json @@ -820,6 +820,14 @@ "path": "COPYING" } }, + "net-snmp": { + "type": "ghtagtar", + "repo": "net-snmp/net-snmp", + "license": { + "type": "file", + "path": "COPYING" + } + }, "nghttp2": { "type": "ghrel", "repo": "nghttp2/nghttp2", diff --git a/src/SPC/builder/extension/snmp.php b/src/SPC/builder/extension/snmp.php new file mode 100644 index 00000000..488fc81e --- /dev/null +++ b/src/SPC/builder/extension/snmp.php @@ -0,0 +1,29 @@ +builder->getPHPVersionID() < 80400) { + FileSystem::copy(ROOT_DIR . '/src/globals/extra/snmp-ext-config-old.m4', "{$this->source_dir}/config.m4"); + } + $libs = implode(' ', PkgConfigUtil::getLibsArray('netsnmp')); + FileSystem::replaceFileStr( + "{$this->source_dir}/config.m4", + 'PHP_EVAL_LIBLINE([$SNMP_LIBS], [SNMP_SHARED_LIBADD])', + "SNMP_LIBS=\"{$libs}\"\nPHP_EVAL_LIBLINE([\$SNMP_LIBS], [SNMP_SHARED_LIBADD])" + ); + return true; + } +} diff --git a/src/SPC/builder/linux/library/net_snmp.php b/src/SPC/builder/linux/library/net_snmp.php new file mode 100644 index 00000000..249b28aa --- /dev/null +++ b/src/SPC/builder/linux/library/net_snmp.php @@ -0,0 +1,12 @@ +source_dir}/configure", 'LIBS="-lssl ${OPENSSL_LIBS}"', 'LIBS="-lssl ${OPENSSL_LIBS} -lpthread -ldl"'); + return true; + } + return false; + } + + protected function build(): void + { + // use --static for PKG_CONFIG + UnixAutoconfExecutor::create($this) + ->setEnv(['PKG_CONFIG' => getenv('PKG_CONFIG') . ' --static']) + ->configure( + '--disable-mibs', + '--without-nl', + '--disable-agent', + '--disable-applications', + '--disable-manuals', + '--disable-scripts', + '--disable-embedded-perl', + '--without-perl-modules', + '--with-out-mib-modules="if-mib host disman/event-mib ucd-snmp/diskio mibII"', + '--with-out-transports="Unix"', + '--with-mib-modules=""', + '--enable-mini-agent', + '--with-default-snmp-version="3"', + '--with-sys-contact="@@no.where"', + '--with-sys-location="Unknown"', + '--with-logfile="/var/log/snmpd.log"', + '--with-persistent-directory="/var/lib/net-snmp"', + '--with-openssl=' . BUILD_ROOT_PATH, + '--with-zlib=' . BUILD_ROOT_PATH, + )->make(with_install: 'installheaders installlibs install_pkgconfig'); + $this->patchPkgconfPrefix(); + } +} diff --git a/src/SPC/doctor/item/LinuxToolCheckList.php b/src/SPC/doctor/item/LinuxToolCheckList.php index a23ad6d5..ab8144a2 100644 --- a/src/SPC/doctor/item/LinuxToolCheckList.php +++ b/src/SPC/doctor/item/LinuxToolCheckList.php @@ -40,7 +40,7 @@ class LinuxToolCheckList 'tar', 'unzip', 'gzip', 'gcc', 'g++', 'bzip2', 'cmake', 'patch', 'which', 'xz', 'libtool', 'gettext-devel', - 'patchelf', + 'patchelf', 'file', ]; public const TOOLS_ARCH = [ diff --git a/src/SPC/util/executor/UnixAutoconfExecutor.php b/src/SPC/util/executor/UnixAutoconfExecutor.php index f196cb1c..e04fe4f9 100644 --- a/src/SPC/util/executor/UnixAutoconfExecutor.php +++ b/src/SPC/util/executor/UnixAutoconfExecutor.php @@ -115,6 +115,12 @@ class UnixAutoconfExecutor extends Executor return $this; } + public function setEnv(array $env): static + { + $this->shell->setEnv($env); + return $this; + } + public function appendEnv(array $env): static { $this->shell->appendEnv($env); diff --git a/src/globals/extra/snmp-ext-config-old.m4 b/src/globals/extra/snmp-ext-config-old.m4 new file mode 100644 index 00000000..22cab40a --- /dev/null +++ b/src/globals/extra/snmp-ext-config-old.m4 @@ -0,0 +1,92 @@ +PHP_ARG_WITH([snmp], + [for SNMP support], + [AS_HELP_STRING([[--with-snmp[=DIR]]], + [Include SNMP support. Use PKG_CONFIG_PATH (or SNMP_CFLAGS and SNMP_LIBS) + environment variables, or alternatively the optional DIR argument to + customize where to look for the net-snmp-config utility of the NET-SNMP + library.])]) + +if test "$PHP_SNMP" != "no"; then + snmp_found=no + AS_VAR_IF([PHP_SNMP], [yes], + [PKG_CHECK_MODULES([SNMP], [netsnmp >= 5.3], [snmp_found=yes], [:])]) + + AS_VAR_IF([snmp_found], [no], [ + AS_VAR_IF([PHP_SNMP], [yes], + [AC_PATH_PROG([SNMP_CONFIG], [net-snmp-config],, [/usr/local/bin:$PATH])], + [SNMP_CONFIG="$PHP_SNMP/bin/net-snmp-config"]) + + AS_IF([test ! -x "$SNMP_CONFIG"], + [AC_MSG_ERROR(m4_text_wrap([ + Could not find net-snmp-config binary. Please check your net-snmp + installation. + ]))]) + + snmp_version=$($SNMP_CONFIG --version) + AS_VERSION_COMPARE([$snmp_version], [5.3], + [AC_MSG_ERROR(m4_text_wrap([ + Net-SNMP version 5.3 or greater required (detected $snmp_version). + ]))]) + + SNMP_PREFIX=$($SNMP_CONFIG --prefix) + SNMP_CFLAGS="-I${SNMP_PREFIX}/include" + SNMP_LIBS=$($SNMP_CONFIG --netsnmp-libs) + SNMP_LIBS="$SNMP_LIBS $($SNMP_CONFIG --external-libs)" + + AS_IF([test -z "$SNMP_LIBS" || test -z "$SNMP_PREFIX"], + [AC_MSG_ERROR(m4_text_wrap([ + Could not find the required paths. Please check your net-snmp + installation. + ]))]) + ]) + + PHP_EVAL_INCLINE([$SNMP_CFLAGS]) + PHP_EVAL_LIBLINE([$SNMP_LIBS], [SNMP_SHARED_LIBADD]) + SNMP_LIBNAME=netsnmp + + dnl Test build. + PHP_CHECK_LIBRARY([$SNMP_LIBNAME], [init_snmp], + [AC_DEFINE([HAVE_SNMP], [1], + [Define to 1 if the PHP extension 'snmp' is available.])], + [AC_MSG_FAILURE([SNMP sanity check failed.])], + [$SNMP_SHARED_LIBADD]) + + dnl Check whether shutdown_snmp_logging() exists. + PHP_CHECK_LIBRARY([$SNMP_LIBNAME], [shutdown_snmp_logging], + [AC_DEFINE([HAVE_SHUTDOWN_SNMP_LOGGING], [1], + [Define to 1 if SNMP library has the 'shutdown_snmp_logging' function.])], + [], + [$SNMP_SHARED_LIBADD]) + + CFLAGS_SAVE=$CFLAGS + LIBS_SAVE=$LIBS + CFLAGS="$CFLAGS $SNMP_CFLAGS" + LIBS="$LIBS $SNMP_LIBS" + + AC_CHECK_DECL([usmHMAC192SHA256AuthProtocol], + [AC_DEFINE([HAVE_SNMP_SHA256], [1], + [Define to 1 if SNMP library has the 'usmHMAC192SHA256AuthProtocol' + array.])], + [], + [ + #include + #include + ]) + + AC_CHECK_DECL([usmHMAC384SHA512AuthProtocol], + [AC_DEFINE([HAVE_SNMP_SHA512], [1], + [Define to 1 if SNMP library has the 'usmHMAC384SHA512AuthProtocol' + array.])], + [], + [ + #include + #include + ]) + + CFLAGS=$CFLAGS_SAVE + LIBS=$LIBS_SAVE + + PHP_NEW_EXTENSION([snmp], [snmp.c], [$ext_shared]) + PHP_ADD_EXTENSION_DEP(snmp, spl) + PHP_SUBST([SNMP_SHARED_LIBADD]) +fi