From 461785c2c8be9de643f88943fcbaa139cf4a96ae Mon Sep 17 00:00:00 2001 From: Areg Vrtanesyan <2562730+vrtareg@users.noreply.github.com> Date: Mon, 24 Feb 2025 13:03:58 +0000 Subject: [PATCH 1/4] Update collector_unix.go to allow FreeBSD build. --- diagnostic/network/collector_unix.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diagnostic/network/collector_unix.go b/diagnostic/network/collector_unix.go index 2db2d262..94bfaa15 100644 --- a/diagnostic/network/collector_unix.go +++ b/diagnostic/network/collector_unix.go @@ -1,4 +1,4 @@ -//go:build darwin || linux +//go:build darwin || linux || freebsd package diagnostic From 914d70fe781173bde2e7231a5bfa107864692ef7 Mon Sep 17 00:00:00 2001 From: Areg Vrtanesyan <2562730+vrtareg@users.noreply.github.com> Date: Mon, 24 Feb 2025 13:04:26 +0000 Subject: [PATCH 2/4] Update collector_unix_test.go to allow FreeBSD build. --- diagnostic/network/collector_unix_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diagnostic/network/collector_unix_test.go b/diagnostic/network/collector_unix_test.go index 5ec231a3..5d52c591 100644 --- a/diagnostic/network/collector_unix_test.go +++ b/diagnostic/network/collector_unix_test.go @@ -1,4 +1,4 @@ -//go:build darwin || linux +//go:build darwin || linux || freebsd package diagnostic_test From 6b3294838c9c6f45210a003dbf3f66462b56e327 Mon Sep 17 00:00:00 2001 From: Areg Vrtanesyan <2562730+vrtareg@users.noreply.github.com> Date: Mon, 24 Feb 2025 13:11:50 +0000 Subject: [PATCH 3/4] Create system_collector_freebsd.go to allow FreeBSD build. --- diagnostic/system_collector_freebsd.go | 172 +++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 diagnostic/system_collector_freebsd.go diff --git a/diagnostic/system_collector_freebsd.go b/diagnostic/system_collector_freebsd.go new file mode 100644 index 00000000..3f81cfb9 --- /dev/null +++ b/diagnostic/system_collector_freebsd.go @@ -0,0 +1,172 @@ +//go:build freebsd + +package diagnostic + +import ( + "context" + "fmt" + "os/exec" + "runtime" + "strconv" +) + +type SystemCollectorImpl struct { + version string +} + +func NewSystemCollectorImpl( + version string, +) *SystemCollectorImpl { + return &SystemCollectorImpl{ + version, + } +} + +func (collector *SystemCollectorImpl) Collect(ctx context.Context) (*SystemInformation, error) { + memoryInfo, memoryInfoRaw, memoryInfoErr := collectMemoryInformation(ctx) + fdInfo, fdInfoRaw, fdInfoErr := collectFileDescriptorInformation(ctx) + disks, disksRaw, diskErr := collectDiskVolumeInformationUnix(ctx) + osInfo, osInfoRaw, osInfoErr := collectOSInformationUnix(ctx) + + var memoryMaximum, memoryCurrent, fileDescriptorMaximum, fileDescriptorCurrent uint64 + var osSystem, name, osVersion, osRelease, architecture string + + err := SystemInformationGeneralError{ + OperatingSystemInformationError: nil, + MemoryInformationError: nil, + FileDescriptorsInformationError: nil, + DiskVolumeInformationError: nil, + } + + if memoryInfoErr != nil { + err.MemoryInformationError = SystemInformationError{ + Err: memoryInfoErr, + RawInfo: memoryInfoRaw, + } + } else { + memoryMaximum = memoryInfo.MemoryMaximum + memoryCurrent = memoryInfo.MemoryCurrent + } + + if fdInfoErr != nil { + err.FileDescriptorsInformationError = SystemInformationError{ + Err: fdInfoErr, + RawInfo: fdInfoRaw, + } + } else { + fileDescriptorMaximum = fdInfo.FileDescriptorMaximum + fileDescriptorCurrent = fdInfo.FileDescriptorCurrent + } + + if diskErr != nil { + err.DiskVolumeInformationError = SystemInformationError{ + Err: diskErr, + RawInfo: disksRaw, + } + } + + if osInfoErr != nil { + err.OperatingSystemInformationError = SystemInformationError{ + Err: osInfoErr, + RawInfo: osInfoRaw, + } + } else { + osSystem = osInfo.OsSystem + name = osInfo.Name + osVersion = osInfo.OsVersion + osRelease = osInfo.OsRelease + architecture = osInfo.Architecture + } + + cloudflaredVersion := collector.version + info := NewSystemInformation( + memoryMaximum, + memoryCurrent, + fileDescriptorMaximum, + fileDescriptorCurrent, + osSystem, + name, + osVersion, + osRelease, + architecture, + cloudflaredVersion, + runtime.Version(), + runtime.GOARCH, + disks, + ) + + return info, err +} + +func collectFileDescriptorInformation(ctx context.Context) ( + *FileDescriptorInformation, + string, + error, +) { + const ( + fileDescriptorMaximumKey = "kern.maxfiles" + fileDescriptorCurrentKey = "kern.num_files" + ) + + command := exec.CommandContext(ctx, "sysctl", fileDescriptorMaximumKey, fileDescriptorCurrentKey) + + stdout, err := command.Output() + if err != nil { + return nil, "", fmt.Errorf("error retrieving output from command '%s': %w", command.String(), err) + } + + output := string(stdout) + + fileDescriptorInfo, err := ParseFileDescriptorInformationFromKV( + output, + fileDescriptorMaximumKey, + fileDescriptorCurrentKey, + ) + if err != nil { + return nil, output, err + } + + // returning raw output in case other collected information + // resulted in errors + return fileDescriptorInfo, output, nil +} + +func collectMemoryInformation(ctx context.Context) ( + *MemoryInformation, + string, + error, +) { + const ( + memoryMaximumKey = "hw.memsize" + memoryAvailableKey = "hw.memsize_usable" + ) + + command := exec.CommandContext( + ctx, + "sysctl", + memoryMaximumKey, + memoryAvailableKey, + ) + + stdout, err := command.Output() + if err != nil { + return nil, "", fmt.Errorf("error retrieving output from command '%s': %w", command.String(), err) + } + + output := string(stdout) + + mapper := func(field string) (uint64, error) { + const kiloBytes = 1024 + value, err := strconv.ParseUint(field, 10, 64) + return value / kiloBytes, err + } + + memoryInfo, err := ParseMemoryInformationFromKV(output, memoryMaximumKey, memoryAvailableKey, mapper) + if err != nil { + return nil, output, err + } + + // returning raw output in case other collected information + // resulted in errors + return memoryInfo, output, nil +} From 73c11bc923b0bfe771befb6a3c51a8b229d18abe Mon Sep 17 00:00:00 2001 From: Areg Vrtanesyan <2562730+vrtareg@users.noreply.github.com> Date: Mon, 24 Feb 2025 13:40:14 +0000 Subject: [PATCH 4/4] Update Makefile - adapting for FreeBSD paths. --- Makefile | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index bfdf2ff9..1e56189f 100644 --- a/Makefile +++ b/Makefile @@ -54,8 +54,11 @@ endif IMPORT_PATH := github.com/cloudflare/cloudflared PACKAGE_DIR := $(CURDIR)/packaging PREFIX := /usr -INSTALL_BINDIR := $(PREFIX)/bin/ -INSTALL_MANDIR := $(PREFIX)/share/man/man1/ +ifeq ($(LOCAL_OS),freebsd) + PREFIX := /usr/local +endif +INSTALL_BINDIR := $(PREFIX)/bin +INSTALL_MANDIR := $(PREFIX)/share/man/man1 CF_GO_PATH := /tmp/go PATH := $(CF_GO_PATH)/bin:$(PATH)