git: 5b63443a6ea8 - main - net-mgmt/collectd5: Use libpfctl

From: Brad Davis <brd_at_FreeBSD.org>
Date: Wed, 29 Nov 2023 23:56:02 UTC
The branch main has been updated by brd:

URL: https://cgit.FreeBSD.org/ports/commit/?id=5b63443a6ea8490165d887f00c250f506f41e0d4

commit 5b63443a6ea8490165d887f00c250f506f41e0d4
Author:     Brad Davis <brd@FreeBSD.org>
AuthorDate: 2023-11-29 23:52:11 +0000
Commit:     Brad Davis <brd@FreeBSD.org>
CommitDate: 2023-11-29 23:55:47 +0000

    net-mgmt/collectd5: Use libpfctl
    
    PR:             275013
    Reviewed by:    kp
    Approved by:    Krzysztof <ports@bsdserwis.com> (maintainer)
    Sponsored by:   Rubicon Communications, LLC ("Netgate")
---
 net-mgmt/collectd5/Makefile              |  9 ++--
 net-mgmt/collectd5/files/patch-src__pf.c | 84 ++++++++++++++++++++++++++++++++
 2 files changed, 90 insertions(+), 3 deletions(-)

diff --git a/net-mgmt/collectd5/Makefile b/net-mgmt/collectd5/Makefile
index 049f6fdebb9a..4cb4de66ccfc 100644
--- a/net-mgmt/collectd5/Makefile
+++ b/net-mgmt/collectd5/Makefile
@@ -1,6 +1,6 @@
 PORTNAME=	collectd
 PORTVERSION=	5.12.0
-PORTREVISION=	9
+PORTREVISION=	10
 CATEGORIES=	net-mgmt
 MASTER_SITES=	https://storage.googleapis.com/collectd-tarballs/
 PKGNAMESUFFIX=	5
@@ -12,7 +12,10 @@ WWW=		https://www.collectd.org/
 LICENSE=	GPLv2
 LICENSE_FILE=	${WRKSRC}/COPYING
 
-USES=		autoreconf cpe gmake libtool pkgconfig shebangfix tar:bzip2
+LIB_DEPENDS=	libpfctl.so:net/libpfctl
+LDFLAGS+=	-lpfctl
+
+USES=		autoreconf cpe gmake libtool localbase:ldflags pkgconfig shebangfix tar:bzip2
 
 GNU_CONFIGURE=	yes
 
@@ -47,7 +50,7 @@ MQTT_DESC=		Enable MQTT broker metrics
 MYSQL_DESC=		Enable mysql-based plugins
 NOTIFYDESKTOP_DESC=	Enable desktop notifications
 NOTIFYEMAIL_DESC=	Enable notifications via email
-NUTUPS_DESC=		Enable nut (ups) plugin
+NUTUPS_DESC=		Enable nut (ups) plugin (sysutils/nut must be configured with DEV option)
 OLSRD_DESC=		Enable olsrd plugin
 ONEWIRE_DESC=		Eanble onewire plugin (via owfs)
 OPENLDAP_DESC=		Enable OpenLDAP plugin
diff --git a/net-mgmt/collectd5/files/patch-src__pf.c b/net-mgmt/collectd5/files/patch-src__pf.c
new file mode 100644
index 000000000000..79ffa899938b
--- /dev/null
+++ b/net-mgmt/collectd5/files/patch-src__pf.c
@@ -0,0 +1,84 @@
+commit 57a1b64ff3ade058a5d8ddd60b3d54f5a2c73c33
+Author: Brad Davis <brd@FreeBSD.org>
+Date:   Tue Nov 7 03:35:47 2023 -0700
+
+    FreeBSD's PF has a new interface so leverage libpfctl to access it so the right interface is used depending on the version
+
+diff --git a/src/pf.c b/src/pf.c
+index 9681d366..eef9540d 100644
+--- a/src/pf.c
++++ b/src/pf.c
+@@ -35,6 +35,9 @@
+ #endif
+ 
+ #include <net/pfvar.h>
++#ifdef __FreeBSD__
++#include <libpfctl.h>
++#endif
+ 
+ #ifndef FCNT_NAMES
+ #if FCNT_MAX != 3
+@@ -76,6 +79,56 @@ static void pf_submit(char const *type, char const *type_instance, uint64_t val,
+   plugin_dispatch_values(&vl);
+ } /* void pf_submit */
+ 
++#ifdef __FreeBSD__
++static int pf_read(void) {
++  struct pfctl_status *state;
++  int fd;
++
++  fd = open(pf_device, O_RDONLY);
++  if (fd < 0) {
++    ERROR("pf plugin: Unable to open %s: %s", pf_device, STRERRNO);
++    return -1;
++  }
++
++  if ((state = pfctl_get_status(fd)) == NULL) {
++    ERROR("pf plugin: ioctl(DIOCGETSTATUS) failed: %s", STRERRNO);
++    close(fd);
++    return -1;
++  }
++
++  close(fd);
++
++  if (!state->running) {
++    pfctl_free_status(state);
++    WARNING("pf plugin: PF is not running.");
++    return -1;
++  }
++
++  for (int i = 0; i < PFRES_MAX; i++) {
++    pf_submit("pf_counters", pf_reasons[i], pfctl_status_counter(state, i),
++              /* is gauge = */ false);
++  }
++  for (int i = 0; i < LCNT_MAX; i++) {
++    pf_submit("pf_limits", pf_lcounters[i], pfctl_status_lcounter(state, i),
++              /* is gauge = */ false);
++  }
++  for (int i = 0; i < FCNT_MAX; i++) {
++    pf_submit("pf_state", pf_fcounters[i], pfctl_status_fcounter(state, i),
++              /* is gauge = */ false);
++  }
++  for (int i = 0; i < SCNT_MAX; i++) {
++    pf_submit("pf_source", pf_scounters[i], pfctl_status_scounter(state, i),
++              /* is gauge = */ false);
++  }
++
++  pf_submit("pf_states", "current", (uint32_t)state->states,
++            /* is gauge = */ true);
++
++  pfctl_free_status(state);
++
++  return 0;
++} /* int pf_read */
++#else
+ static int pf_read(void) {
+   struct pf_status state;
+   int fd;
+@@ -119,5 +172,6 @@ static int pf_read(void) {
+ 
+   return 0;
+ } /* int pf_read */
++#endif
+ 
+ void module_register(void) { plugin_register_read("pf", pf_read); }