| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | //go:build linux || freebsd || openbsd || netbsd || dragonfly |
| 4 | |
| 5 | package zfspool |
| 6 | |
| 7 | import ( |
| 8 | "errors" |
| 9 | "os" |
| 10 | "os/exec" |
| 11 | "strings" |
| 12 | |
| 13 | "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/pathvalidate" |
| 14 | ) |
| 15 | |
| 16 | func (c *Collector) validateConfig() error { |
| 17 | if c.BinaryPath == "" { |
| 18 | return errors.New("no zpool binary path specified") |
| 19 | } |
| 20 | return nil |
| 21 | } |
| 22 | |
| 23 | func (c *Collector) initZPoolCLIExec() (zpoolCli, error) { |
| 24 | binPath := c.BinaryPath |
| 25 | |
| 26 | if !strings.HasPrefix(binPath, "/") { |
| 27 | path, err := exec.LookPath(binPath) |
| 28 | if err != nil { |
| 29 | return nil, err |
| 30 | } |
| 31 | binPath = path |
| 32 | } |
| 33 | |
| 34 | if _, err := os.Stat(binPath); err != nil { |
| 35 | return nil, err |
| 36 | } |
| 37 | |
| 38 | validatedPath, err := pathvalidate.ValidateBinaryPath(binPath) |
| 39 | if err != nil { |
| 40 | return nil, err |
| 41 | } |
| 42 | |
| 43 | zpoolExec := newZpoolCLIExec(validatedPath, c.Timeout.Duration()) |
| 44 | zpoolExec.Logger = c.Logger |
| 45 | |
| 46 | return zpoolExec, nil |
| 47 | } |