mirror of
https://github.com/steinhobelgruen/netatmo-exporter.git
synced 2024-12-22 04:23:55 +00:00
Extend logging
This commit is contained in:
parent
380405edfe
commit
ee49657c75
47
collector.go
47
collector.go
|
@ -1,11 +1,11 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
netatmo "github.com/exzz/netatmo-api-go"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -97,19 +97,22 @@ var (
|
|||
)
|
||||
|
||||
type netatmoCollector struct {
|
||||
log logrus.FieldLogger
|
||||
client *netatmo.Client
|
||||
}
|
||||
|
||||
func (m *netatmoCollector) Describe(dChan chan<- *prometheus.Desc) {
|
||||
func (c *netatmoCollector) Describe(dChan chan<- *prometheus.Desc) {
|
||||
dChan <- updatedDesc
|
||||
dChan <- tempDesc
|
||||
dChan <- humidityDesc
|
||||
dChan <- cotwoDesc
|
||||
}
|
||||
|
||||
func (m *netatmoCollector) Collect(mChan chan<- prometheus.Metric) {
|
||||
devices, err := m.client.Read()
|
||||
func (c *netatmoCollector) Collect(mChan chan<- prometheus.Metric) {
|
||||
devices, err := c.client.Read()
|
||||
if err != nil {
|
||||
c.log.Errorf("Error getting data: %s", err)
|
||||
|
||||
netatmoUp.Set(0)
|
||||
mChan <- netatmoUp
|
||||
return
|
||||
|
@ -119,76 +122,78 @@ func (m *netatmoCollector) Collect(mChan chan<- prometheus.Metric) {
|
|||
|
||||
for _, dev := range devices.Devices() {
|
||||
stationName := dev.StationName
|
||||
collectData(mChan, dev, stationName)
|
||||
c.collectData(mChan, dev, stationName)
|
||||
|
||||
for _, module := range dev.LinkedModules {
|
||||
collectData(mChan, module, stationName)
|
||||
c.collectData(mChan, module, stationName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func collectData(ch chan<- prometheus.Metric, device *netatmo.Device, stationName string) {
|
||||
func (c *netatmoCollector) collectData(ch chan<- prometheus.Metric, device *netatmo.Device, stationName string) {
|
||||
moduleName := device.ModuleName
|
||||
data := device.DashboardData
|
||||
|
||||
if data.LastMeasure == nil {
|
||||
c.log.Debugf("No data available.")
|
||||
return
|
||||
}
|
||||
|
||||
date := time.Unix(*data.LastMeasure, 0)
|
||||
if time.Since(date) > staleDataThreshold {
|
||||
c.log.Warnf("Data is stale: %s > %s", time.Since(date), staleDataThreshold)
|
||||
return
|
||||
}
|
||||
|
||||
sendMetric(ch, updatedDesc, prometheus.CounterValue, float64(date.UTC().Unix()), moduleName, stationName)
|
||||
c.sendMetric(ch, updatedDesc, prometheus.CounterValue, float64(date.UTC().Unix()), moduleName, stationName)
|
||||
|
||||
if data.Temperature != nil {
|
||||
sendMetric(ch, tempDesc, prometheus.GaugeValue, float64(*data.Temperature), moduleName, stationName)
|
||||
c.sendMetric(ch, tempDesc, prometheus.GaugeValue, float64(*data.Temperature), moduleName, stationName)
|
||||
}
|
||||
|
||||
if data.Humidity != nil {
|
||||
sendMetric(ch, humidityDesc, prometheus.GaugeValue, float64(*data.Humidity), moduleName, stationName)
|
||||
c.sendMetric(ch, humidityDesc, prometheus.GaugeValue, float64(*data.Humidity), moduleName, stationName)
|
||||
}
|
||||
|
||||
if data.CO2 != nil {
|
||||
sendMetric(ch, cotwoDesc, prometheus.GaugeValue, float64(*data.CO2), moduleName, stationName)
|
||||
c.sendMetric(ch, cotwoDesc, prometheus.GaugeValue, float64(*data.CO2), moduleName, stationName)
|
||||
}
|
||||
|
||||
if data.Noise != nil {
|
||||
sendMetric(ch, noiseDesc, prometheus.GaugeValue, float64(*data.Noise), moduleName, stationName)
|
||||
c.sendMetric(ch, noiseDesc, prometheus.GaugeValue, float64(*data.Noise), moduleName, stationName)
|
||||
}
|
||||
|
||||
if data.Pressure != nil {
|
||||
sendMetric(ch, pressureDesc, prometheus.GaugeValue, float64(*data.Pressure), moduleName, stationName)
|
||||
c.sendMetric(ch, pressureDesc, prometheus.GaugeValue, float64(*data.Pressure), moduleName, stationName)
|
||||
}
|
||||
|
||||
if data.WindStrength != nil {
|
||||
sendMetric(ch, windStrengthDesc, prometheus.GaugeValue, float64(*data.WindStrength), moduleName, stationName)
|
||||
c.sendMetric(ch, windStrengthDesc, prometheus.GaugeValue, float64(*data.WindStrength), moduleName, stationName)
|
||||
}
|
||||
|
||||
if data.WindAngle != nil {
|
||||
sendMetric(ch, windDirectionDesc, prometheus.GaugeValue, float64(*data.WindAngle), moduleName, stationName)
|
||||
c.sendMetric(ch, windDirectionDesc, prometheus.GaugeValue, float64(*data.WindAngle), moduleName, stationName)
|
||||
}
|
||||
|
||||
if data.Rain != nil {
|
||||
sendMetric(ch, rainDesc, prometheus.GaugeValue, float64(*data.Rain), moduleName, stationName)
|
||||
c.sendMetric(ch, rainDesc, prometheus.GaugeValue, float64(*data.Rain), moduleName, stationName)
|
||||
}
|
||||
|
||||
if device.BatteryPercent != nil {
|
||||
sendMetric(ch, batteryDesc, prometheus.GaugeValue, float64(*device.BatteryPercent), moduleName, stationName)
|
||||
c.sendMetric(ch, batteryDesc, prometheus.GaugeValue, float64(*device.BatteryPercent), moduleName, stationName)
|
||||
}
|
||||
if device.WifiStatus != nil {
|
||||
sendMetric(ch, wifiDesc, prometheus.GaugeValue, float64(*device.WifiStatus), moduleName, stationName)
|
||||
c.sendMetric(ch, wifiDesc, prometheus.GaugeValue, float64(*device.WifiStatus), moduleName, stationName)
|
||||
}
|
||||
if device.RFStatus != nil {
|
||||
sendMetric(ch, rfDesc, prometheus.GaugeValue, float64(*device.RFStatus), moduleName, stationName)
|
||||
c.sendMetric(ch, rfDesc, prometheus.GaugeValue, float64(*device.RFStatus), moduleName, stationName)
|
||||
}
|
||||
}
|
||||
|
||||
func sendMetric(ch chan<- prometheus.Metric, desc *prometheus.Desc, valueType prometheus.ValueType, value float64, moduleName string, stationName string) {
|
||||
func (c *netatmoCollector) sendMetric(ch chan<- prometheus.Metric, desc *prometheus.Desc, valueType prometheus.ValueType, value float64, moduleName string, stationName string) {
|
||||
m, err := prometheus.NewConstMetric(desc, valueType, value, moduleName, stationName)
|
||||
if err != nil {
|
||||
log.Printf("Error creating %s metric: %s", updatedDesc.String(), err)
|
||||
c.log.Errorf("Error creating %s metric: %s", updatedDesc.String(), err)
|
||||
}
|
||||
ch <- m
|
||||
}
|
||||
|
|
1
go.mod
1
go.mod
|
@ -5,6 +5,7 @@ go 1.14
|
|||
require (
|
||||
github.com/exzz/netatmo-api-go v0.0.0-20171026152754-41589231f446
|
||||
github.com/prometheus/client_golang v1.7.0
|
||||
github.com/sirupsen/logrus v1.6.0
|
||||
github.com/spf13/pflag v1.0.5
|
||||
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
|
||||
)
|
||||
|
|
4
go.sum
4
go.sum
|
@ -38,6 +38,8 @@ github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCV
|
|||
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
|
@ -69,6 +71,8 @@ github.com/prometheus/procfs v0.1.3 h1:F0+tqvhOksq22sc6iCHF5WGlWjdwj92p0udFh1VFB
|
|||
github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
|
||||
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
|
||||
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
||||
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
|
||||
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
|
||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
|
|
19
main.go
19
main.go
|
@ -1,13 +1,25 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
netatmo "github.com/exzz/netatmo-api-go"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var (
|
||||
log = &logrus.Logger{
|
||||
Out: os.Stderr,
|
||||
Formatter: &logrus.TextFormatter{
|
||||
DisableTimestamp: true,
|
||||
},
|
||||
Level: logrus.InfoLevel,
|
||||
ExitFunc: os.Exit,
|
||||
ReportCaller: false,
|
||||
}
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -16,13 +28,14 @@ func main() {
|
|||
log.Fatalf("Error in configuration: %s", err)
|
||||
}
|
||||
|
||||
log.Printf("Login as %s", cfg.Netatmo.Username)
|
||||
log.Infof("Login as %s", cfg.Netatmo.Username)
|
||||
client, err := netatmo.NewClient(cfg.Netatmo)
|
||||
if err != nil {
|
||||
log.Fatalf("Error creating client: %s", err)
|
||||
}
|
||||
|
||||
metrics := &netatmoCollector{
|
||||
log: log,
|
||||
client: client,
|
||||
}
|
||||
prometheus.MustRegister(metrics)
|
||||
|
@ -30,6 +43,6 @@ func main() {
|
|||
http.Handle("/metrics", promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{}))
|
||||
http.Handle("/", http.RedirectHandler("/metrics", http.StatusFound))
|
||||
|
||||
log.Printf("Listen on %s...", cfg.Addr)
|
||||
log.Infof("Listen on %s...", cfg.Addr)
|
||||
log.Fatal(http.ListenAndServe(cfg.Addr, nil))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue