Skip to content

Commit

Permalink
zfs: Allow space in dataset name
Browse files Browse the repository at this point in the history
Signed-off-by: Mustafa Khafateh <[email protected]>
  • Loading branch information
khafatech committed Nov 18, 2024
1 parent 49d177b commit f4b1f02
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 2 deletions.
3 changes: 3 additions & 0 deletions collector/fixtures/proc/spl/kstat/zfs/pool3/io
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions collector/fixtures/proc/spl/kstat/zfs/pool3/objset-1
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions collector/fixtures/proc/spl/kstat/zfs/pool3/objset-2
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions collector/fixtures/proc/spl/kstat/zfs/pool3/state
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ONLINE
5 changes: 3 additions & 2 deletions collector/zfs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
49 changes: 49 additions & 0 deletions collector/zfs_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f4b1f02

Please sign in to comment.