Add the code

This commit is contained in:
Josh Deprez 2019-01-20 03:30:20 +00:00
parent 770a9f9f37
commit f3a7759726
3 changed files with 83 additions and 0 deletions

64
exporter.go Normal file
View file

@ -0,0 +1,64 @@
package main // import "github.com/DrJosh9000/lifepo4wered-exporter"
import (
"bufio"
"bytes"
"flag"
"fmt"
"log"
"net/http"
"os/exec"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")
lifepo4weredVars = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "lifepo4wered",
Help: "Variables gathered from the lifepo4wered-cli tool",
},
[]string{"var"})
)
func init() {
prometheus.MustRegister(lifepo4weredVars)
}
func getVars() {
cmd := exec.Command("lifepo4wered-cli", "get")
out, err := cmd.Output()
if err != nil {
log.Fatalf("Couldn't execute command: %v", err)
}
s := bufio.NewScanner(bytes.NewReader(out))
for s.Scan() {
// token space equals space number
var name string
var num int
if _, err := fmt.Sscanf(s.Text(), "%s = %d", &name, &num); err != nil {
log.Fatalf("Couldn't scan line: %v", err)
}
lifepo4weredVars.With(prometheus.Labels{"var": name}).Set(float64(num))
}
if err := s.Err(); err != nil {
log.Fatalf("Couldn't scan output: %v", err)
}
}
func main() {
flag.Parse()
getVars()
go func() {
for range time.Tick(15 * time.Second) {
getVars()
}
}()
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(*addr, nil))
}

3
go.mod Normal file
View file

@ -0,0 +1,3 @@
module github.com/DrJosh9000/lifepo4wered-exporter
require github.com/prometheus/client_golang v0.9.2

16
go.sum Normal file
View file

@ -0,0 +1,16 @@
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 h1:xJ4a3vCFaGF/jqvzLMYoU8P317H5OQ+Via4RmuPwCS0=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/prometheus/client_golang v0.9.2 h1:awm861/B8OKDd2I/6o1dy3ra4BamzKhYOiGItCeZ740=
github.com/prometheus/client_golang v0.9.2/go.mod h1:OsXs2jCmiKlQ1lTBmv21f2mNfw4xf/QclQDMrYNZzcM=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910 h1:idejC8f05m9MGOsuEi1ATq9shN03HrxNkD/luQvxCv8=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/common v0.0.0-20181126121408-4724e9255275 h1:PnBWHBf+6L0jOqq0gIVUe6Yk0/QMZ640k6NvkxcBf+8=
github.com/prometheus/common v0.0.0-20181126121408-4724e9255275/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a h1:9a8MnZMP0X2nLJdBg+pBmGgkJlSaKC2KaQmTCk1XDtE=
github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=