Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions internal/check/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
40 changes: 21 additions & 19 deletions internal/check/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
129 changes: 129 additions & 0 deletions internal/check/zfs.go
Original file line number Diff line number Diff line change
@@ -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.Fields(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 pool \"%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
}
Loading