mirror of
https://github.com/steinhobelgruen/netatmo-exporter.git
synced 2024-11-22 01:13:56 +00:00
First version.
This commit is contained in:
parent
a350bcef30
commit
149a6f6330
116
collector.go
Normal file
116
collector.go
Normal file
|
@ -0,0 +1,116 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
netatmo "github.com/exzz/netatmo-api-go"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
const (
|
||||
staleDataThreshold = 30 * time.Minute
|
||||
)
|
||||
|
||||
var (
|
||||
netatmoUp = prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "netatmo_up",
|
||||
Help: "Zero if there was an error scraping the Netatmo API.",
|
||||
})
|
||||
|
||||
varLabels = []string{
|
||||
"module",
|
||||
}
|
||||
|
||||
prefix = "netatmo_sensor_"
|
||||
|
||||
updatedDesc = prometheus.NewDesc(
|
||||
prefix+"updated",
|
||||
"Timestamp of last update",
|
||||
varLabels,
|
||||
nil)
|
||||
|
||||
tempDesc = prometheus.NewDesc(
|
||||
prefix+"temperature_celsius",
|
||||
"Temperature measurement in celsius",
|
||||
varLabels,
|
||||
nil)
|
||||
|
||||
humidityDesc = prometheus.NewDesc(
|
||||
prefix+"humidity_percent",
|
||||
"Relative humidity measurement in percent",
|
||||
varLabels,
|
||||
nil)
|
||||
|
||||
cotwoDesc = prometheus.NewDesc(
|
||||
prefix+"co2_ppm",
|
||||
"Carbondioxide measurement in parts per million",
|
||||
varLabels,
|
||||
nil)
|
||||
)
|
||||
|
||||
type netatmoCollector struct {
|
||||
client *netatmo.Client
|
||||
}
|
||||
|
||||
func (m *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()
|
||||
if err != nil {
|
||||
netatmoUp.Set(0)
|
||||
mChan <- netatmoUp
|
||||
return
|
||||
}
|
||||
netatmoUp.Set(1)
|
||||
mChan <- netatmoUp
|
||||
|
||||
for _, dev := range devices.Devices() {
|
||||
collectData(mChan, dev)
|
||||
|
||||
for _, module := range dev.LinkedModules {
|
||||
collectData(mChan, module)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func collectData(ch chan<- prometheus.Metric, device *netatmo.Device) {
|
||||
moduleName := device.ModuleName
|
||||
data := device.DashboardData
|
||||
|
||||
if data.LastMesure == nil {
|
||||
return
|
||||
}
|
||||
|
||||
date := time.Unix(*data.LastMesure, 0)
|
||||
if time.Since(date) > staleDataThreshold {
|
||||
return
|
||||
}
|
||||
|
||||
sendMetric(ch, updatedDesc, prometheus.CounterValue, float64(date.UTC().Unix()), moduleName)
|
||||
|
||||
if data.Temperature != nil {
|
||||
sendMetric(ch, tempDesc, prometheus.GaugeValue, float64(*data.Temperature), moduleName)
|
||||
}
|
||||
|
||||
if data.Humidity != nil {
|
||||
sendMetric(ch, humidityDesc, prometheus.GaugeValue, float64(*data.Humidity), moduleName)
|
||||
}
|
||||
|
||||
if data.CO2 != nil {
|
||||
sendMetric(ch, cotwoDesc, prometheus.GaugeValue, float64(*data.CO2), moduleName)
|
||||
}
|
||||
}
|
||||
|
||||
func sendMetric(ch chan<- prometheus.Metric, desc *prometheus.Desc, valueType prometheus.ValueType, value float64, moduleName string) {
|
||||
m, err := prometheus.NewConstMetric(desc, valueType, value, moduleName)
|
||||
if err != nil {
|
||||
log.Printf("Error creating %s metric: %s", updatedDesc.String(), err)
|
||||
}
|
||||
ch <- m
|
||||
}
|
72
main.go
Normal file
72
main.go
Normal file
|
@ -0,0 +1,72 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
netatmo "github.com/exzz/netatmo-api-go"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
type config struct {
|
||||
Addr string
|
||||
Netatmo netatmo.Config
|
||||
}
|
||||
|
||||
func parseConfig() (config, error) {
|
||||
cfg := config{}
|
||||
pflag.StringVarP(&cfg.Addr, "addr", "a", ":8080", "Address to listen on.")
|
||||
pflag.StringVarP(&cfg.Netatmo.ClientID, "client-id", "i", "", "Client ID for NetAtmo app.")
|
||||
pflag.StringVarP(&cfg.Netatmo.ClientSecret, "client-secret", "s", "", "Client secret for NetAtmo app.")
|
||||
pflag.StringVarP(&cfg.Netatmo.Username, "username", "u", "", "Username of NetAtmo account.")
|
||||
pflag.StringVarP(&cfg.Netatmo.Password, "password", "p", "", "Password of NetAtmo account.")
|
||||
pflag.Parse()
|
||||
|
||||
if len(cfg.Addr) == 0 {
|
||||
return cfg, errors.New("no listen address")
|
||||
}
|
||||
|
||||
if len(cfg.Netatmo.ClientID) == 0 {
|
||||
return cfg, errors.New("need a NetAtmo client ID")
|
||||
}
|
||||
|
||||
if len(cfg.Netatmo.ClientSecret) == 0 {
|
||||
return cfg, errors.New("need a NetAtmo client secret")
|
||||
}
|
||||
|
||||
if len(cfg.Netatmo.Username) == 0 {
|
||||
return cfg, errors.New("username can not be blank")
|
||||
}
|
||||
|
||||
if len(cfg.Netatmo.Password) == 0 {
|
||||
return cfg, errors.New("password can not be blank")
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
cfg, err := parseConfig()
|
||||
if err != nil {
|
||||
log.Fatalf("Error in configuration: %s", err)
|
||||
}
|
||||
|
||||
log.Printf("Login as %s", cfg.Netatmo.Username)
|
||||
client, err := netatmo.NewClient(cfg.Netatmo)
|
||||
if err != nil {
|
||||
log.Fatalf("Error creating client: %s", err)
|
||||
}
|
||||
|
||||
metrics := &netatmoCollector{
|
||||
client: client,
|
||||
}
|
||||
prometheus.MustRegister(metrics)
|
||||
|
||||
http.Handle("/metrics", prometheus.UninstrumentedHandler())
|
||||
http.Handle("/", http.RedirectHandler("/metrics", http.StatusFound))
|
||||
|
||||
log.Printf("Listen on %s...", cfg.Addr)
|
||||
log.Fatal(http.ListenAndServe(cfg.Addr, nil))
|
||||
}
|
Loading…
Reference in a new issue