From fdb58a6dafba4735acac036bfb53046192afddee Mon Sep 17 00:00:00 2001 From: deadc0de6 <8973919+deadc0de6@users.noreply.github.com.> Date: Sun, 15 Mar 2026 14:12:45 +0100 Subject: [PATCH 1/3] adding zfs check --- README.md | 3 + internal/check/disk.go | 40 +++++++------ internal/check/zfs.go | 129 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 153 insertions(+), 19 deletions(-) create mode 100644 internal/check/zfs.go diff --git a/README.md b/README.md index 41207cd..e929ac2 100644 --- a/README.md +++ b/README.md @@ -176,6 +176,9 @@ The following checks are available: * **disk**: check disk space used * *mount*: mount point (optional, default to `/`) * *limit*: if disk use percent crosses this value, an alert is triggered +* **zfs**: check zfs pool disk space used + * *pool*: pool name (optional, default to `tank`) + * *limit*: if zfs pool disk use percent crosses this value, an alert is triggered * **loadavg**: check load average * *limit_1min*: if load average over 1 min crosses this value, an alert is triggered * *limit_5min*: if load average over 5 min crosses this value, an alert is triggered diff --git a/internal/check/disk.go b/internal/check/disk.go index f68c130..fe602b1 100644 --- a/internal/check/disk.go +++ b/internal/check/disk.go @@ -40,31 +40,33 @@ func (c *Disk) Run(t transport.Transport) *Result { lines := strings.Split(stdout, "\n") for _, line := range lines { fields := strings.Split(line, " ") - if fields[len(fields)-1] == c.mountPoint { - // find first percent value - var value = "" - for _, f := range fields { - if strings.Contains(f, "%") { - value = f - break - } - } - v, err := c.getValue(value) - if err != nil { - return c.returnCheck("", err) + if fields[len(fields)-1] != c.mountPoint { + // bad mountpoint + continue + } + // find first percent value + var value = "" + for _, f := range fields { + if strings.Contains(f, "%") { + value = f + break } + } + v, err := c.getValue(value) + if err != nil { + return c.returnCheck("", err) + } - // check with limit - if v > c.limit { - err := fmt.Errorf("disk used of mount point \"%s\" above %d%%: %s", c.mountPoint, c.limit, value) - return c.returnCheck(value, err) - } - return c.returnCheck(value, nil) + // check with limit + if v > c.limit { + err := fmt.Errorf("disk used of mount point \"%s\" above %d%%: %s", c.mountPoint, c.limit, value) + return c.returnCheck(value, err) } + return c.returnCheck(value, nil) } // mount point not found - return c.returnCheck("", fmt.Errorf("mount point %s not found", c.mountPoint)) + return c.returnCheck("", fmt.Errorf("mount point \"%s\" not found", c.mountPoint)) } func (c *Disk) returnCheck(value string, err error) *Result { diff --git a/internal/check/zfs.go b/internal/check/zfs.go new file mode 100644 index 0000000..dfe3b86 --- /dev/null +++ b/internal/check/zfs.go @@ -0,0 +1,129 @@ +// Copyright (c) 2026 deadc0de6 + +package check + +import ( + "fmt" + "strconv" + "strings" + + "github.com/deadc0de6/checkah/internal/transport" +) + +// Zfs the zfs struct +type Zfs struct { + command string + poolName string + limit int + options map[string]string +} + +func (c *Zfs) getInt(str string) (int, error) { + if len(str) < 1 { + return -1, fmt.Errorf("empty percent value") + } + val, err := strconv.Atoi(str) + if err != nil { + return -1, err + } + return val, nil +} + +// Run executes the check +func (c *Zfs) Run(t transport.Transport) *Result { + stdout, _, err := t.Execute(c.command) + if err != nil { + return c.returnCheck("", err) + } + + // NAME USED AVAIL MOUNTPOINT + lines := strings.Split(stdout, "\n") + for _, line := range lines { + fields := strings.Split(line, " ") + + if fields[0] != c.poolName { + // bad pool + continue + } + if len(fields) != 4 { + // bad number of fields + continue + } + + used, err := c.getInt(fields[1]) + if err != nil { + return c.returnCheck("", err) + } + avail, err := c.getInt(fields[2]) + if err != nil { + return c.returnCheck("", err) + } + total := used + avail + percent := used * 100 / total + percentStr := fmt.Sprintf("%d%%", percent) + + if percent > c.limit { + err := fmt.Errorf("zfs pool \"%s\" used above %d%%: %s", c.poolName, c.limit, percentStr) + return c.returnCheck(percentStr, err) + } + + return c.returnCheck(percentStr, nil) + } + + // mount point not found + return c.returnCheck("", fmt.Errorf("zfs pool \"%s\" not found", c.poolName)) +} + +func (c *Zfs) returnCheck(value string, err error) *Result { + return &Result{ + Name: c.GetName(), + Description: c.GetDescription(), + Value: value, + Limit: fmt.Sprintf("%d", c.limit), + Error: err, + } +} + +// GetName returns the check name +func (c *Zfs) GetName() string { + return "zfs" +} + +// GetDescription returns description +func (c *Zfs) GetDescription() string { + return fmt.Sprintf("zfs \"%s\" used", c.poolName) +} + +// GetOptions returns the options +func (c *Zfs) GetOptions() map[string]string { + return c.options +} + +// NewCheckZfs creates a zfs check instance +func NewCheckZfs(options map[string]string) (*Zfs, error) { + mount := "tank" + v, ok := options["pool"] + if ok { + mount = v + } + + limit := 90 + v, ok = options["limit"] + if ok { + i, err := strconv.Atoi(v) + if err != nil { + return nil, err + } + limit = i + } + + command := fmt.Sprintf("zfs list -p -H -o name,used,available,mountpoint %s", mount) + c := Zfs{ + command: command, + poolName: mount, + limit: limit, + options: options, + } + + return &c, nil +} From cd1d87fe3dd876536769e7fd3b70ce5cf6bd638f Mon Sep 17 00:00:00 2001 From: deadc0de6 <8973919+deadc0de6@users.noreply.github.com.> Date: Sun, 15 Mar 2026 14:16:38 +0100 Subject: [PATCH 2/3] add check --- internal/check/check.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/check/check.go b/internal/check/check.go index 34b9023..9bcdcd1 100644 --- a/internal/check/check.go +++ b/internal/check/check.go @@ -56,6 +56,8 @@ func GetCheck(name string, options map[string]string) (Check, error) { return NewCheckCommand(options) case "uptime": return NewCheckUptime(options) + case "zfs": + return NewCheckZfs(options) } return nil, fmt.Errorf("no such check: %s", name) } From d8a89405931cd7e228c2da0d0f06d9f47d62b871 Mon Sep 17 00:00:00 2001 From: deadc0de6 <8973919+deadc0de6@users.noreply.github.com.> Date: Sun, 15 Mar 2026 14:16:44 +0100 Subject: [PATCH 3/3] fix parsing --- internal/check/zfs.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/check/zfs.go b/internal/check/zfs.go index dfe3b86..dbd461d 100644 --- a/internal/check/zfs.go +++ b/internal/check/zfs.go @@ -39,7 +39,7 @@ func (c *Zfs) Run(t transport.Transport) *Result { // NAME USED AVAIL MOUNTPOINT lines := strings.Split(stdout, "\n") for _, line := range lines { - fields := strings.Split(line, " ") + fields := strings.Fields(line) if fields[0] != c.poolName { // bad pool @@ -91,7 +91,7 @@ func (c *Zfs) GetName() string { // GetDescription returns description func (c *Zfs) GetDescription() string { - return fmt.Sprintf("zfs \"%s\" used", c.poolName) + return fmt.Sprintf("zfs pool \"%s\" used", c.poolName) } // GetOptions returns the options