From f4b1f023d71f0917e887ee506785b2c5c107a566 Mon Sep 17 00:00:00 2001 From: Mustafa Khafateh Date: Sun, 17 Nov 2024 22:09:24 -0500 Subject: [PATCH] zfs: Allow space in dataset name Signed-off-by: Mustafa Khafateh --- .../fixtures/proc/spl/kstat/zfs/pool3/io | 3 ++ .../proc/spl/kstat/zfs/pool3/objset-1 | 9 ++++ .../proc/spl/kstat/zfs/pool3/objset-2 | 9 ++++ .../fixtures/proc/spl/kstat/zfs/pool3/state | 1 + collector/zfs_linux.go | 5 +- collector/zfs_linux_test.go | 49 +++++++++++++++++++ 6 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 collector/fixtures/proc/spl/kstat/zfs/pool3/io create mode 100644 collector/fixtures/proc/spl/kstat/zfs/pool3/objset-1 create mode 100644 collector/fixtures/proc/spl/kstat/zfs/pool3/objset-2 create mode 100644 collector/fixtures/proc/spl/kstat/zfs/pool3/state diff --git a/collector/fixtures/proc/spl/kstat/zfs/pool3/io b/collector/fixtures/proc/spl/kstat/zfs/pool3/io new file mode 100644 index 0000000000..ef2a58fe7f --- /dev/null +++ b/collector/fixtures/proc/spl/kstat/zfs/pool3/io @@ -0,0 +1,3 @@ +12 3 0x00 1 80 79205351707403 395818011156865 +nread nwritten reads writes wtime wlentime wupdate rtime rlentime rupdate wcnt rcnt +1884160 3206144 22 132 7155162 104112268 79210489694949 24168078 104112268 79210489849220 0 0 diff --git a/collector/fixtures/proc/spl/kstat/zfs/pool3/objset-1 b/collector/fixtures/proc/spl/kstat/zfs/pool3/objset-1 new file mode 100644 index 0000000000..28a9d2e3ce --- /dev/null +++ b/collector/fixtures/proc/spl/kstat/zfs/pool3/objset-1 @@ -0,0 +1,9 @@ +23 1 0x01 7 2160 221578688875 6665999035587 +name type data +dataset_name 7 pool1 +writes 4 0 +nwritten 4 0 +reads 4 0 +nread 4 0 +nunlinks 4 0 +nunlinked 4 0 diff --git a/collector/fixtures/proc/spl/kstat/zfs/pool3/objset-2 b/collector/fixtures/proc/spl/kstat/zfs/pool3/objset-2 new file mode 100644 index 0000000000..0f912dfc9e --- /dev/null +++ b/collector/fixtures/proc/spl/kstat/zfs/pool3/objset-2 @@ -0,0 +1,9 @@ +24 1 0x01 7 2160 221611904716 7145015038451 +name type data +dataset_name 7 pool1/dataset with space +writes 4 4 +nwritten 4 12302 +reads 4 2 +nread 4 28 +nunlinks 4 3 +nunlinked 4 3 diff --git a/collector/fixtures/proc/spl/kstat/zfs/pool3/state b/collector/fixtures/proc/spl/kstat/zfs/pool3/state new file mode 100644 index 0000000000..1424865cf5 --- /dev/null +++ b/collector/fixtures/proc/spl/kstat/zfs/pool3/state @@ -0,0 +1 @@ +ONLINE diff --git a/collector/zfs_linux.go b/collector/zfs_linux.go index e0d9c3f65b..4baf2b3569 100644 --- a/collector/zfs_linux.go +++ b/collector/zfs_linux.go @@ -301,7 +301,8 @@ func (c *zfsCollector) parsePoolObjsetFile(reader io.Reader, zpoolPath string, h parseLine := false var zpoolName, datasetName string for scanner.Scan() { - parts := strings.Fields(scanner.Text()) + line := scanner.Text() + parts := strings.Fields(line) if !parseLine && len(parts) == 3 && parts[0] == "name" && parts[1] == "type" && parts[2] == "data" { parseLine = true @@ -315,7 +316,7 @@ func (c *zfsCollector) parsePoolObjsetFile(reader io.Reader, zpoolPath string, h zpoolPathElements := strings.Split(zpoolPath, "/") pathLen := len(zpoolPathElements) zpoolName = zpoolPathElements[pathLen-2] - datasetName = parts[2] + datasetName = line[strings.Index(line, parts[2]):] continue } diff --git a/collector/zfs_linux_test.go b/collector/zfs_linux_test.go index c96d91b5b7..ebe5fd89f0 100644 --- a/collector/zfs_linux_test.go +++ b/collector/zfs_linux_test.go @@ -315,6 +315,55 @@ func TestZpoolParsing(t *testing.T) { } } +func TestZpoolObjsetParsingWithSpace(t *testing.T) { + tests := []struct { + path string + expectedDataset string + }{ + { + path: "fixtures/proc/spl/kstat/zfs/pool1/objset-1", + expectedDataset: "pool1", + }, + { + path: "fixtures/proc/spl/kstat/zfs/pool1/objset-2", + expectedDataset: "pool1/dataset1", + }, + { + path: "fixtures/proc/spl/kstat/zfs/pool3/objset-1", + expectedDataset: "pool1", + }, + { + path: "fixtures/proc/spl/kstat/zfs/pool3/objset-2", + expectedDataset: "pool1/dataset with space", + }, + } + + c := zfsCollector{} + + var handlerCalled bool + for _, test := range tests { + file, err := os.Open(test.path) + if err != nil { + t.Fatal(err) + } + + handlerCalled = false + err = c.parsePoolObjsetFile(file, test.path, func(poolName string, datasetName string, s zfsSysctl, v uint64) { + handlerCalled = true + if test.expectedDataset != datasetName { + t.Fatalf("Incorrectly parsed dataset name: expected: '%s', got: '%s'", test.expectedDataset, datasetName) + } + }) + file.Close() + if err != nil { + t.Fatal(err) + } + if !handlerCalled { + t.Fatalf("Zpool parsing handler was not called for '%s'", test.path) + } + } +} + func TestZpoolObjsetParsing(t *testing.T) { zpoolPaths, err := filepath.Glob("fixtures/proc/spl/kstat/zfs/*/objset-*") if err != nil {