From adf324583817243a2a2c132d9af8e97e2dbe36cf Mon Sep 17 00:00:00 2001 From: vigneshakaviki Date: Fri, 31 Jul 2026 23:14:06 -0700 Subject: [PATCH] zfs: fix dataset name parsing Signed-off-by: vigneshakaviki --- collector/zfs_linux.go | 3 ++- collector/zfs_linux_test.go | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/collector/zfs_linux.go b/collector/zfs_linux.go index 9e968f73fe..53b1aa493e 100644 --- a/collector/zfs_linux.go +++ b/collector/zfs_linux.go @@ -315,7 +315,8 @@ func (c *zfsCollector) parsePoolObjsetFile(reader io.Reader, zpoolPath string, h zpoolPathElements := strings.Split(zpoolPath, "/") pathLen := len(zpoolPathElements) zpoolName = zpoolPathElements[pathLen-2] - datasetName = line[strings.Index(line, parts[2]):] + dataStart := strings.Index(line, parts[1]) + len(parts[1]) + datasetName = strings.TrimSpace(line[dataStart:]) continue } diff --git a/collector/zfs_linux_test.go b/collector/zfs_linux_test.go index 29b9ede2d3..7435a24edb 100644 --- a/collector/zfs_linux_test.go +++ b/collector/zfs_linux_test.go @@ -18,6 +18,7 @@ package collector import ( "os" "path/filepath" + "strings" "testing" ) @@ -363,6 +364,22 @@ func TestZpoolObjsetParsingWithSpace(t *testing.T) { } } +func TestZpoolObjsetParsingDatasetInFieldName(t *testing.T) { + const input = "name type data\ndataset_name 7 data\nnwritten 4 1\n" + + c := zfsCollector{} + var datasetName string + err := c.parsePoolObjsetFile(strings.NewReader(input), "/proc/spl/kstat/zfs/data/objset-1", func(_ string, dataset string, _ zfsSysctl, _ uint64) { + datasetName = dataset + }) + if err != nil { + t.Fatal(err) + } + if datasetName != "data" { + t.Fatalf("Incorrectly parsed dataset name: expected: %q, got: %q", "data", datasetName) + } +} + func TestZpoolObjsetParsing(t *testing.T) { zpoolPaths, err := filepath.Glob("fixtures/proc/spl/kstat/zfs/*/objset-*") if err != nil {