netatmo-exporter/internal/collector/collector.go

267 lines
7.4 KiB
Go
Raw Normal View History

2020-06-27 15:53:13 +00:00
package collector
2017-01-08 17:12:34 +00:00
import (
2020-06-27 15:46:02 +00:00
"sync"
2017-01-08 17:12:34 +00:00
"time"
netatmo "github.com/exzz/netatmo-api-go"
"github.com/prometheus/client_golang/prometheus"
2020-06-21 13:22:19 +00:00
"github.com/sirupsen/logrus"
2017-01-08 17:12:34 +00:00
)
var (
2020-06-27 15:13:40 +00:00
prefix = "netatmo_"
netatmoUpDesc = prometheus.NewDesc(prefix+"up",
2020-06-27 15:46:02 +00:00
"Zero if there was an error during the last refresh try.",
nil, nil)
refreshIntervalDesc = prometheus.NewDesc(
prefix+"refresh_interval_seconds",
"Contains the configured refresh interval in seconds. This is provided as a convenience for calculations with the cache update time.",
nil, nil)
refreshPrefix = prefix + "last_refresh_"
2020-06-27 15:46:02 +00:00
refreshTimestampDesc = prometheus.NewDesc(
refreshPrefix+"time",
2020-06-27 15:46:02 +00:00
"Contains the time of the last refresh try, successful or not.",
nil, nil)
2020-06-27 15:59:17 +00:00
refreshDurationDesc = prometheus.NewDesc(
refreshPrefix+"duration_seconds",
2020-06-27 15:59:17 +00:00
"Contains the time it took for the last refresh to complete, even if it was unsuccessful.",
nil, nil)
2020-06-27 15:46:02 +00:00
cacheTimestampDesc = prometheus.NewDesc(
prefix+"cache_updated_time",
"Contains the time of the cached data.",
2020-06-27 15:13:40 +00:00
nil, nil)
2017-01-08 17:12:34 +00:00
varLabels = []string{
"module",
2018-09-02 15:54:15 +00:00
"station",
2017-01-08 17:12:34 +00:00
}
2020-06-27 15:13:40 +00:00
sensorPrefix = prefix + "sensor_"
2017-01-08 17:12:34 +00:00
updatedDesc = prometheus.NewDesc(
2020-06-27 15:13:40 +00:00
sensorPrefix+"updated",
2017-01-08 17:12:34 +00:00
"Timestamp of last update",
varLabels,
nil)
tempDesc = prometheus.NewDesc(
2020-06-27 15:13:40 +00:00
sensorPrefix+"temperature_celsius",
2017-01-08 17:12:34 +00:00
"Temperature measurement in celsius",
varLabels,
nil)
humidityDesc = prometheus.NewDesc(
2020-06-27 15:13:40 +00:00
sensorPrefix+"humidity_percent",
2017-01-08 17:12:34 +00:00
"Relative humidity measurement in percent",
varLabels,
nil)
cotwoDesc = prometheus.NewDesc(
2020-06-27 15:13:40 +00:00
sensorPrefix+"co2_ppm",
2017-01-08 17:12:34 +00:00
"Carbondioxide measurement in parts per million",
varLabels,
nil)
2017-01-08 17:55:45 +00:00
noiseDesc = prometheus.NewDesc(
2020-06-27 15:13:40 +00:00
sensorPrefix+"noise_db",
2017-01-08 17:55:45 +00:00
"Noise measurement in decibels",
varLabels,
nil)
pressureDesc = prometheus.NewDesc(
2020-06-27 15:13:40 +00:00
sensorPrefix+"pressure_mb",
"Atmospheric pressure measurement in millibar",
varLabels,
nil)
windStrengthDesc = prometheus.NewDesc(
2020-06-27 15:13:40 +00:00
sensorPrefix+"wind_strength_kph",
"Wind strength in kilometers per hour",
varLabels,
nil)
windDirectionDesc = prometheus.NewDesc(
2020-06-27 15:13:40 +00:00
sensorPrefix+"wind_direction_degrees",
"Wind direction in degrees",
varLabels,
nil)
rainDesc = prometheus.NewDesc(
2020-06-27 15:13:40 +00:00
sensorPrefix+"rain_amount_mm",
"Rain amount in millimeters",
2017-01-08 17:55:45 +00:00
varLabels,
nil)
batteryDesc = prometheus.NewDesc(
2020-06-27 15:13:40 +00:00
sensorPrefix+"battery_percent",
"Battery remaining life (10: low)",
varLabels,
nil)
wifiDesc = prometheus.NewDesc(
2020-06-27 15:13:40 +00:00
sensorPrefix+"wifi_signal_strength",
"Wifi signal strength (86: bad, 71: avg, 56: good)",
varLabels,
nil)
rfDesc = prometheus.NewDesc(
2020-06-27 15:13:40 +00:00
sensorPrefix+"rf_signal_strength",
"RF signal strength (90: lowest, 60: highest)",
varLabels,
nil)
2017-01-08 17:12:34 +00:00
)
2020-06-27 16:20:51 +00:00
// NetatmoCollector is a Prometheus collector for Netatmo sensor values.
2020-06-27 15:53:13 +00:00
type NetatmoCollector struct {
2020-06-27 15:59:17 +00:00
Log logrus.FieldLogger
RefreshInterval time.Duration
StaleThreshold time.Duration
Client *netatmo.Client
lastRefresh time.Time
lastRefreshError error
lastRefreshDuration time.Duration
cacheLock sync.RWMutex
cacheTimestamp time.Time
cachedData *netatmo.DeviceCollection
2017-01-08 17:12:34 +00:00
}
2020-07-03 14:47:41 +00:00
// Describe implements prometheus.Collector
2020-06-27 15:53:13 +00:00
func (c *NetatmoCollector) Describe(dChan chan<- *prometheus.Desc) {
2017-01-08 17:12:34 +00:00
dChan <- updatedDesc
dChan <- tempDesc
dChan <- humidityDesc
dChan <- cotwoDesc
}
2020-07-03 14:47:41 +00:00
// Collect implements prometheus.Collector
2020-06-27 15:53:13 +00:00
func (c *NetatmoCollector) Collect(mChan chan<- prometheus.Metric) {
2020-06-27 15:46:02 +00:00
now := time.Now()
2020-06-27 15:53:13 +00:00
if now.Sub(c.lastRefresh) >= c.RefreshInterval {
2020-06-27 16:20:51 +00:00
go c.RefreshData(now)
2020-06-27 15:46:02 +00:00
}
2020-06-21 13:22:19 +00:00
2020-06-27 15:46:02 +00:00
upValue := 1.0
if c.lastRefresh.IsZero() || c.lastRefreshError != nil {
upValue = 0
2017-01-08 17:12:34 +00:00
}
2020-06-27 15:46:02 +00:00
c.sendMetric(mChan, netatmoUpDesc, prometheus.GaugeValue, upValue)
c.sendMetric(mChan, refreshIntervalDesc, prometheus.GaugeValue, c.RefreshInterval.Seconds())
2020-06-27 15:46:02 +00:00
c.sendMetric(mChan, refreshTimestampDesc, prometheus.GaugeValue, convertTime(c.lastRefresh))
2020-06-27 15:59:17 +00:00
c.sendMetric(mChan, refreshDurationDesc, prometheus.GaugeValue, c.lastRefreshDuration.Seconds())
2020-06-27 15:46:02 +00:00
c.cacheLock.RLock()
defer c.cacheLock.RUnlock()
2017-01-08 17:12:34 +00:00
2020-06-27 15:46:02 +00:00
c.sendMetric(mChan, cacheTimestampDesc, prometheus.GaugeValue, convertTime(c.cacheTimestamp))
if c.cachedData != nil {
for _, dev := range c.cachedData.Devices() {
stationName := dev.StationName
c.collectData(mChan, dev, stationName)
2017-01-08 17:12:34 +00:00
2020-06-27 15:46:02 +00:00
for _, module := range dev.LinkedModules {
c.collectData(mChan, module, stationName)
}
2017-01-08 17:12:34 +00:00
}
}
}
2020-06-27 16:20:51 +00:00
// RefreshData causes the collector to try to refresh the cached data.
func (c *NetatmoCollector) RefreshData(now time.Time) {
c.Log.Debugf("Refreshing data. Time since last refresh: %s", now.Sub(c.lastRefresh))
2020-06-27 15:46:02 +00:00
c.lastRefresh = now
2020-06-27 15:59:17 +00:00
defer func(start time.Time) {
c.lastRefreshDuration = time.Since(start)
}(time.Now())
2020-06-27 15:53:13 +00:00
devices, err := c.Client.Read()
2020-06-27 15:46:02 +00:00
if err != nil {
2020-06-27 15:53:13 +00:00
c.Log.Errorf("Error during refresh: %s", err)
2020-06-27 15:46:02 +00:00
c.lastRefreshError = err
return
}
c.cacheLock.Lock()
defer c.cacheLock.Unlock()
c.cacheTimestamp = now
c.cachedData = devices
}
2020-06-27 15:53:13 +00:00
func (c *NetatmoCollector) collectData(ch chan<- prometheus.Metric, device *netatmo.Device, stationName string) {
2017-01-08 17:12:34 +00:00
moduleName := device.ModuleName
data := device.DashboardData
if data.LastMeasure == nil {
2020-06-27 15:53:13 +00:00
c.Log.Debugf("No data available.")
2017-01-08 17:12:34 +00:00
return
}
date := time.Unix(*data.LastMeasure, 0)
2020-06-27 15:53:13 +00:00
if time.Since(date) > c.StaleThreshold {
c.Log.Debugf("Data is stale for %s: %s > %s", moduleName, time.Since(date), c.StaleThreshold)
2017-01-08 17:12:34 +00:00
return
}
2020-07-03 14:46:05 +00:00
c.sendMetric(ch, updatedDesc, prometheus.GaugeValue, float64(date.UTC().Unix()), moduleName, stationName)
2017-01-08 17:12:34 +00:00
if data.Temperature != nil {
2020-06-21 13:22:19 +00:00
c.sendMetric(ch, tempDesc, prometheus.GaugeValue, float64(*data.Temperature), moduleName, stationName)
2017-01-08 17:12:34 +00:00
}
if data.Humidity != nil {
2020-06-21 13:22:19 +00:00
c.sendMetric(ch, humidityDesc, prometheus.GaugeValue, float64(*data.Humidity), moduleName, stationName)
2017-01-08 17:12:34 +00:00
}
if data.CO2 != nil {
2020-06-21 13:22:19 +00:00
c.sendMetric(ch, cotwoDesc, prometheus.GaugeValue, float64(*data.CO2), moduleName, stationName)
2017-01-08 17:12:34 +00:00
}
2017-01-08 17:55:45 +00:00
if data.Noise != nil {
2020-06-21 13:22:19 +00:00
c.sendMetric(ch, noiseDesc, prometheus.GaugeValue, float64(*data.Noise), moduleName, stationName)
2017-01-08 17:55:45 +00:00
}
if data.Pressure != nil {
2020-06-21 13:22:19 +00:00
c.sendMetric(ch, pressureDesc, prometheus.GaugeValue, float64(*data.Pressure), moduleName, stationName)
2017-01-08 17:55:45 +00:00
}
if data.WindStrength != nil {
2020-06-21 13:22:19 +00:00
c.sendMetric(ch, windStrengthDesc, prometheus.GaugeValue, float64(*data.WindStrength), moduleName, stationName)
}
if data.WindAngle != nil {
2020-06-21 13:22:19 +00:00
c.sendMetric(ch, windDirectionDesc, prometheus.GaugeValue, float64(*data.WindAngle), moduleName, stationName)
}
if data.Rain != nil {
2020-06-21 13:22:19 +00:00
c.sendMetric(ch, rainDesc, prometheus.GaugeValue, float64(*data.Rain), moduleName, stationName)
}
if device.BatteryPercent != nil {
2020-06-21 13:22:19 +00:00
c.sendMetric(ch, batteryDesc, prometheus.GaugeValue, float64(*device.BatteryPercent), moduleName, stationName)
}
if device.WifiStatus != nil {
2020-06-21 13:22:19 +00:00
c.sendMetric(ch, wifiDesc, prometheus.GaugeValue, float64(*device.WifiStatus), moduleName, stationName)
}
if device.RFStatus != nil {
2020-06-21 13:22:19 +00:00
c.sendMetric(ch, rfDesc, prometheus.GaugeValue, float64(*device.RFStatus), moduleName, stationName)
}
2017-01-08 17:12:34 +00:00
}
2020-06-27 15:53:13 +00:00
func (c *NetatmoCollector) sendMetric(ch chan<- prometheus.Metric, desc *prometheus.Desc, valueType prometheus.ValueType, value float64, labelValues ...string) {
2020-06-27 15:13:40 +00:00
m, err := prometheus.NewConstMetric(desc, valueType, value, labelValues...)
2017-01-08 17:12:34 +00:00
if err != nil {
2020-06-27 15:53:13 +00:00
c.Log.Errorf("Error creating %s metric: %s", updatedDesc.String(), err)
2020-06-27 15:13:40 +00:00
return
2017-01-08 17:12:34 +00:00
}
ch <- m
}
2020-06-27 15:46:02 +00:00
func convertTime(t time.Time) float64 {
if t.IsZero() {
return 0.0
}
return float64(t.Unix())
}