From f86a5f07fbeab811f6e8c80f0350c2b7be873603 Mon Sep 17 00:00:00 2001 From: Mauri de Souza Meneguzzo Date: Mon, 9 Dec 2024 14:37:33 -0300 Subject: [PATCH] metricbeat: handle nf_conntrack module not loaded in linux integration (#41930) * metricbeat: handle nf_conntrack module not loaded in linux integration * ci lint fixes * fix error check to prevent windows failure --- .../module/linux/conntrack/conntrack.go | 9 +++++++- .../module/linux/conntrack/conntrack_test.go | 22 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/metricbeat/module/linux/conntrack/conntrack.go b/metricbeat/module/linux/conntrack/conntrack.go index 602a786b574..42c2114f92c 100644 --- a/metricbeat/module/linux/conntrack/conntrack.go +++ b/metricbeat/module/linux/conntrack/conntrack.go @@ -19,6 +19,7 @@ package conntrack import ( "fmt" + "os" "github.com/prometheus/procfs" @@ -50,7 +51,10 @@ type MetricSet struct { func New(base mb.BaseMetricSet) (mb.MetricSet, error) { cfgwarn.Beta("The linux conntrack metricset is beta.") - sys := base.Module().(resolve.Resolver) + sys, ok := base.Module().(resolve.Resolver) + if !ok { + return nil, fmt.Errorf("unexpected module type: %T", base.Module()) + } return &MetricSet{ BaseMetricSet: base, @@ -68,6 +72,9 @@ func (m *MetricSet) Fetch(report mb.ReporterV2) error { } conntrackStats, err := newFS.ConntrackStat() if err != nil { + if os.IsNotExist(err) { + err = mb.PartialMetricsError{Err: fmt.Errorf("nf_conntrack kernel module not loaded: %w", err)} + } return fmt.Errorf("error fetching conntrack stats: %w", err) } diff --git a/metricbeat/module/linux/conntrack/conntrack_test.go b/metricbeat/module/linux/conntrack/conntrack_test.go index cc3b2a05230..0d1ad2a4677 100644 --- a/metricbeat/module/linux/conntrack/conntrack_test.go +++ b/metricbeat/module/linux/conntrack/conntrack_test.go @@ -18,10 +18,15 @@ package conntrack import ( + "errors" + "os" + "path/filepath" "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/elastic/beats/v7/metricbeat/mb" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" _ "github.com/elastic/beats/v7/metricbeat/module/linux" "github.com/elastic/elastic-agent-libs/mapstr" @@ -60,6 +65,23 @@ func TestFetch(t *testing.T) { assert.Equal(t, testConn, rawEvent) } +func TestFetchConntrackModuleNotLoaded(t *testing.T) { + // Create a temporary directory to simulate a missing /proc/net/stat/nf_conntrack file + tmpDir := t.TempDir() + assert.NoError(t, os.MkdirAll(filepath.Join(tmpDir, "proc/net/stat"), 0755)) + c := getConfig() + c["hostfs"] = tmpDir + + f := mbtest.NewReportingMetricSetV2Error(t, c) + events, errs := mbtest.ReportingFetchV2Error(f) + + require.Len(t, errs, 1) + err := errors.Join(errs...) + assert.ErrorAs(t, err, &mb.PartialMetricsError{}) + assert.Contains(t, err.Error(), "error fetching conntrack stats: nf_conntrack kernel module not loaded") + require.Empty(t, events) +} + func getConfig() map[string]interface{} { return map[string]interface{}{ "module": "linux",