Skip to content

Instantly share code, notes, and snippets.

@BobBurns
Created May 28, 2021 15:11
Show Gist options
  • Save BobBurns/3bfb23b1e3aaa98715c495675619a4fc to your computer and use it in GitHub Desktop.
Save BobBurns/3bfb23b1e3aaa98715c495675619a4fc to your computer and use it in GitHub Desktop.
prometheus zpool check freebsd
package main
import (
"fmt"
"log"
"os/exec"
"bufio"
"strings"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
poolStatus = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "zpool_status",
Help: "zpool_status",
ConstLabels: prometheus.Labels{"version": "1234"},
})
)
const (
gb = 1024.0 * 1024
debug = 1
)
func init() {
prometheus.MustRegister(poolStatus)
}
func checkerr(err error) {
if err != nil {
log.Fatal(err)
}
}
func getZpoolStatus() (degraded bool) {
cmd := exec.Command("zpool", "status")
out, err := cmd.StdoutPipe()
if err != nil {
panic(err)
}
err = cmd.Start()
if err != nil {
log.Fatal(err)
}
scanner := bufio.NewScanner(out)
entry :=0
var fields []string
for scanner.Scan() {
fields = strings.Fields(scanner.Text())
if len(fields) > 2 && fields[1] == "DEGRADED" {
degraded = true
if debug ==1 {
fmt.Println("degraded")
}
}
entry++
}
err = cmd.Wait()
if err != nil {
panic(err)
}
return
}
func main() {
go func() {
for {
d := getZpoolStatus()
// fmt.Println(d)
if d == true {
poolStatus.Set(1)
} else {
poolStatus.Set(0)
}
time.Sleep(60 * time.Second)
}
}()
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment