This repository has been archived by the owner on Mar 6, 2024. It is now read-only.
forked from xperimental/netatmo-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollector.go
136 lines (109 loc) · 2.91 KB
/
collector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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)
noiseDesc = prometheus.NewDesc(
prefix+"noise_db",
"Noise measurement in decibels",
varLabels,
nil)
pressureDesc = prometheus.NewDesc(
prefix+"pressure_mb",
"Atmospheric pressure measurement in mb",
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)
}
if data.Noise != nil {
sendMetric(ch, noiseDesc, prometheus.GaugeValue, float64(*data.Noise), moduleName)
}
if data.Pressure != nil {
sendMetric(ch, pressureDesc, prometheus.GaugeValue, float64(*data.Pressure), 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
}