simplistic WIP of a prometheus exporter for gpsd

This commit is contained in:
Harald Welte 2022-12-31 18:40:54 +01:00
commit f72477010d
2 changed files with 94 additions and 0 deletions

17
go.mod Normal file
View File

@ -0,0 +1,17 @@
module laf0rge/gps2prom
go 1.19
require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/stratoberry/go-gpsd v1.0.0 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
google.golang.org/protobuf v1.28.1 // indirect
)

77
gps2prom.go Normal file
View File

@ -0,0 +1,77 @@
package main
import (
"fmt"
"github.com/stratoberry/go-gpsd"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
gpsMode = promauto.NewGauge(
prometheus.GaugeOpts {
Subsystem: "gpsd",
Name: "mode",
Help: "gpsd mode (2=2D fix, 3=3D fix)",
})
gpsNumSats = promauto.NewGauge(
prometheus.GaugeOpts {
Subsystem: "gpsd",
Name: "num_sv_total",
Help: "Total number of SV",
})
gpsNumSatsUsed = promauto.NewGauge(
prometheus.GaugeOpts {
Subsystem: "gpsd",
Name: "num_sv_used",
Help: "Used number of SV",
})
)
func main() {
var gps *gpsd.Session
var err error
if gps, err = gpsd.Dial("apu-left:2947"); err != nil {
panic(fmt.Sprintf("Failed to connect to GPSD: %s", err))
}
gps.AddFilter("TPV", func(r interface{}) {
tpv := r.(*gpsd.TPVReport)
//fmt.Println("TPV", tpv.Mode, tpv.Time)
gpsMode.Set(float64(tpv.Mode))
})
/*
gps.AddFilter("DEVICE", func(r interface{}) {
dev := r.(*gpsd.DEVICEReport)
fmt.Println("DEVICE", dev.Path, dev.Flags)
})
*/
gps.AddFilter("SKY", func(r interface{}) {
sky := r.(*gpsd.SKYReport)
fmt.Println("SKY", sky.Satellites)
gpsNumSats.Set(float64(len(sky.Satellites)))
num_sats_used := 0
for i := 0; i < len(sky.Satellites); i++ {
num_sats_used += 1
}
gpsNumSatsUsed.Set(float64(num_sats_used))
})
fmt.Println("Hello, World!")
done := gps.Watch()
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":2112", nil)
<-done
}