2017-01-08 17:12:34 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
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",
|
2018-09-02 15:54:15 +00:00
|
|
|
"station",
|
2017-01-08 17:12:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2017-01-08 17:55:45 +00:00
|
|
|
|
|
|
|
noiseDesc = prometheus.NewDesc(
|
|
|
|
prefix+"noise_db",
|
|
|
|
"Noise measurement in decibels",
|
|
|
|
varLabels,
|
|
|
|
nil)
|
|
|
|
|
|
|
|
pressureDesc = prometheus.NewDesc(
|
|
|
|
prefix+"pressure_mb",
|
2017-03-08 12:21:35 +00:00
|
|
|
"Atmospheric pressure measurement in millibar",
|
|
|
|
varLabels,
|
|
|
|
nil)
|
|
|
|
|
|
|
|
windStrengthDesc = prometheus.NewDesc(
|
|
|
|
prefix+"wind_strength_kph",
|
|
|
|
"Wind strength in kilometers per hour",
|
|
|
|
varLabels,
|
|
|
|
nil)
|
|
|
|
|
|
|
|
windDirectionDesc = prometheus.NewDesc(
|
|
|
|
prefix+"wind_direction_degrees",
|
|
|
|
"Wind direction in degrees",
|
|
|
|
varLabels,
|
|
|
|
nil)
|
|
|
|
|
|
|
|
rainDesc = prometheus.NewDesc(
|
|
|
|
prefix+"rain_amount_mm",
|
|
|
|
"Rain amount in millimeters",
|
2017-01-08 17:55:45 +00:00
|
|
|
varLabels,
|
|
|
|
nil)
|
2018-10-06 13:24:55 +00:00
|
|
|
|
|
|
|
batteryDesc = prometheus.NewDesc(
|
|
|
|
prefix+"battery_percent",
|
|
|
|
"Battery remaining life (10: low)",
|
|
|
|
varLabels,
|
|
|
|
nil)
|
|
|
|
wifiDesc = prometheus.NewDesc(
|
|
|
|
prefix+"wifi_signal_strength",
|
|
|
|
"Wifi signal strength (86: bad, 71: avg, 56: good)",
|
|
|
|
varLabels,
|
|
|
|
nil)
|
|
|
|
rfDesc = prometheus.NewDesc(
|
|
|
|
prefix+"rf_signal_strength",
|
|
|
|
"RF signal strength (90: lowest, 60: highest)",
|
|
|
|
varLabels,
|
|
|
|
nil)
|
2017-01-08 17:12:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type netatmoCollector struct {
|
2020-06-21 13:22:19 +00:00
|
|
|
log logrus.FieldLogger
|
2017-01-08 17:12:34 +00:00
|
|
|
client *netatmo.Client
|
|
|
|
}
|
|
|
|
|
2020-06-21 13:22:19 +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-06-21 13:22:19 +00:00
|
|
|
func (c *netatmoCollector) Collect(mChan chan<- prometheus.Metric) {
|
|
|
|
devices, err := c.client.Read()
|
2017-01-08 17:12:34 +00:00
|
|
|
if err != nil {
|
2020-06-21 13:22:19 +00:00
|
|
|
c.log.Errorf("Error getting data: %s", err)
|
|
|
|
|
2017-01-08 17:12:34 +00:00
|
|
|
netatmoUp.Set(0)
|
|
|
|
mChan <- netatmoUp
|
|
|
|
return
|
|
|
|
}
|
|
|
|
netatmoUp.Set(1)
|
|
|
|
mChan <- netatmoUp
|
|
|
|
|
|
|
|
for _, dev := range devices.Devices() {
|
2018-09-02 15:54:15 +00:00
|
|
|
stationName := dev.StationName
|
2020-06-21 13:22:19 +00:00
|
|
|
c.collectData(mChan, dev, stationName)
|
2017-01-08 17:12:34 +00:00
|
|
|
|
|
|
|
for _, module := range dev.LinkedModules {
|
2020-06-21 13:22:19 +00:00
|
|
|
c.collectData(mChan, module, stationName)
|
2017-01-08 17:12:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 13:22:19 +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
|
|
|
|
|
2017-11-26 21:16:24 +00:00
|
|
|
if data.LastMeasure == nil {
|
2020-06-21 13:22:19 +00:00
|
|
|
c.log.Debugf("No data available.")
|
2017-01-08 17:12:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-26 21:16:24 +00:00
|
|
|
date := time.Unix(*data.LastMeasure, 0)
|
2017-01-08 17:12:34 +00:00
|
|
|
if time.Since(date) > staleDataThreshold {
|
2020-06-21 13:22:19 +00:00
|
|
|
c.log.Warnf("Data is stale: %s > %s", time.Since(date), staleDataThreshold)
|
2017-01-08 17:12:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-21 13:22:19 +00:00
|
|
|
c.sendMetric(ch, updatedDesc, prometheus.CounterValue, 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
|
|
|
}
|
2017-03-08 12:21:35 +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)
|
2017-03-08 12:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if data.WindAngle != nil {
|
2020-06-21 13:22:19 +00:00
|
|
|
c.sendMetric(ch, windDirectionDesc, prometheus.GaugeValue, float64(*data.WindAngle), moduleName, stationName)
|
2017-03-08 12:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if data.Rain != nil {
|
2020-06-21 13:22:19 +00:00
|
|
|
c.sendMetric(ch, rainDesc, prometheus.GaugeValue, float64(*data.Rain), moduleName, stationName)
|
2017-03-08 12:21:35 +00:00
|
|
|
}
|
2018-10-06 13:24:55 +00:00
|
|
|
|
|
|
|
if device.BatteryPercent != nil {
|
2020-06-21 13:22:19 +00:00
|
|
|
c.sendMetric(ch, batteryDesc, prometheus.GaugeValue, float64(*device.BatteryPercent), moduleName, stationName)
|
2018-10-06 13:24:55 +00:00
|
|
|
}
|
|
|
|
if device.WifiStatus != nil {
|
2020-06-21 13:22:19 +00:00
|
|
|
c.sendMetric(ch, wifiDesc, prometheus.GaugeValue, float64(*device.WifiStatus), moduleName, stationName)
|
2018-10-06 13:24:55 +00:00
|
|
|
}
|
|
|
|
if device.RFStatus != nil {
|
2020-06-21 13:22:19 +00:00
|
|
|
c.sendMetric(ch, rfDesc, prometheus.GaugeValue, float64(*device.RFStatus), moduleName, stationName)
|
2018-10-06 13:24:55 +00:00
|
|
|
}
|
2017-01-08 17:12:34 +00:00
|
|
|
}
|
|
|
|
|
2020-06-21 13:22:19 +00:00
|
|
|
func (c *netatmoCollector) sendMetric(ch chan<- prometheus.Metric, desc *prometheus.Desc, valueType prometheus.ValueType, value float64, moduleName string, stationName string) {
|
2018-09-02 15:54:15 +00:00
|
|
|
m, err := prometheus.NewConstMetric(desc, valueType, value, moduleName, stationName)
|
2017-01-08 17:12:34 +00:00
|
|
|
if err != nil {
|
2020-06-21 13:22:19 +00:00
|
|
|
c.log.Errorf("Error creating %s metric: %s", updatedDesc.String(), err)
|
2017-01-08 17:12:34 +00:00
|
|
|
}
|
|
|
|
ch <- m
|
|
|
|
}
|