-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
166 lines (133 loc) · 4.49 KB
/
main.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"log"
"net/http"
"os"
"strings"
"time"
"github.com/simonkrenger/avelon-cloud-prometheus-exporter/avelon"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
fetchInterval = 60 * time.Minute
observedDevicesList []avelonDeviceMetrics
)
type avelonDeviceMetrics struct {
Device *avelon.Device
SingalStrengthGauge prometheus.Gauge
BatteryLevelGauge prometheus.Gauge
AltitudeGauge prometheus.Gauge
TemperatureGauge prometheus.Gauge
HumidityGauge prometheus.Gauge
PressureGauge prometheus.Gauge
}
func init() {
log.Printf("init() started")
d := os.Getenv("DEVICE_LIST")
if d == "" {
log.Fatal("FATAL: DEVICE_LIST not specified, aborting...")
os.Exit(1)
}
for _, code := range strings.Split(d, ",") {
log.Printf("Setting up device %s...", code)
device := new(avelon.Device)
device.ActivationCode = code
deviceMetrics := avelonDeviceMetrics{
Device: device,
SingalStrengthGauge: prometheus.NewGauge(prometheus.GaugeOpts{
Name: "avelon_device_signal_strength",
Help: "Current device signal strength",
ConstLabels: prometheus.Labels{"activationcode": device.ActivationCode},
}),
BatteryLevelGauge: prometheus.NewGauge(prometheus.GaugeOpts{
Name: "avelon_device_battery_level_percent",
Help: "Current device battery level",
ConstLabels: prometheus.Labels{"activationcode": device.ActivationCode},
}),
AltitudeGauge: prometheus.NewGauge(prometheus.GaugeOpts{
Name: "avelon_device_altitude_msl",
Help: "Current device altitude",
ConstLabels: prometheus.Labels{"activationcode": device.ActivationCode},
}),
TemperatureGauge: prometheus.NewGauge(prometheus.GaugeOpts{
Name: "avelon_record_last_temperature_celsius",
Help: "Latest temperature measurement",
ConstLabels: prometheus.Labels{"activationcode": device.ActivationCode},
}),
HumidityGauge: prometheus.NewGauge(prometheus.GaugeOpts{
Name: "avelon_record_last_humidity_percent",
Help: "Latest air humidity measurement",
ConstLabels: prometheus.Labels{"activationcode": device.ActivationCode},
}),
PressureGauge: prometheus.NewGauge(prometheus.GaugeOpts{
Name: "avelon_record_last_pressure_hpa",
Help: "Latest air pressure measurement",
ConstLabels: prometheus.Labels{"activationcode": device.ActivationCode},
}),
}
observedDevicesList = append(observedDevicesList, deviceMetrics)
prometheus.MustRegister(deviceMetrics.SingalStrengthGauge)
prometheus.MustRegister(deviceMetrics.BatteryLevelGauge)
prometheus.MustRegister(deviceMetrics.AltitudeGauge)
prometheus.MustRegister(deviceMetrics.TemperatureGauge)
prometheus.MustRegister(deviceMetrics.HumidityGauge)
prometheus.MustRegister(deviceMetrics.PressureGauge)
}
}
func main() {
http.Handle("/metrics", promhttp.Handler())
go fetchAvelonData()
log.Printf("Serving metrics at ':8080/metrics'")
log.Fatal(http.ListenAndServe(":8080", nil))
}
func fetchAvelonData() {
for {
log.Printf("Fetch started.")
for _, dev := range observedDevicesList {
info, err := dev.Device.FetchDeviceInfo()
if err != nil {
log.Printf("Fetching device information failed: '%v'", err)
continue
}
dev.BatteryLevelGauge.Set(info.BatteryLevel)
dev.SingalStrengthGauge.Set(float64(info.SignalStrength))
dev.AltitudeGauge.Set(info.Altitude)
records, err := dev.Device.FetchRecords()
if err != nil {
log.Printf("Fetching device records failed: '%v'", err)
continue
}
dev.TemperatureGauge.Set(findLatestValueForType(*records, *info, "TEMPERATURE"))
dev.HumidityGauge.Set(findLatestValueForType(*records, *info, "HUMIDITY"))
dev.PressureGauge.Set(findLatestValueForType(*records, *info, "ATMOSPHERIC_PRESSURE_AVERAGE"))
}
log.Printf("Fetch finished.")
time.Sleep(fetchInterval)
}
}
func findLatestValueForType(records []avelon.AvelonRecordsResponse, info avelon.AvelonDeviceResponse, t string) (float64) {
var latestTime int64
var latestValue float64
id := 0
for _, dataPoints := range info.DataPoints {
if dataPoints.IotType == t {
id = dataPoints.ID
}
}
if id == 0 {
log.Printf("Warning: DatapointID for %s not found, returning 0.", t)
return 0
}
for _, dataPoints := range records {
if dataPoints.DataPointID == id {
for _, m := range dataPoints.Values {
if m.T > latestTime {
latestTime = m.T
latestValue = m.V
}
}
}
}
return latestValue
}