forked from mitchellrj/hue_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensors.go
248 lines (232 loc) · 6.7 KB
/
sensors.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package main
import (
"math"
hue "github.com/collinux/gohue"
"github.com/prometheus/client_golang/prometheus"
log "github.com/prometheus/common/log"
)
type sensorCollector struct {
bridge Bridge
ignoreTypes []string
matchNames bool
sensorValue *prometheus.GaugeVec
sensorLastUpdated *prometheus.GaugeVec
sensorOn *prometheus.GaugeVec
sensorBattery *prometheus.GaugeVec
sensorReachable *prometheus.GaugeVec
sensorScrapesFailed prometheus.Counter
bridgeRestarts prometheus.Counter
}
var variableSensorLabelNames = []string{
"name",
"type",
"model_id",
"manufacturer_name",
"product_name",
"unique_id",
"device_id",
}
func contains(a []string, x string) bool {
for _, n := range a {
if x == n {
return true
}
}
return false
}
func max(a, b int64) int64 {
if a > b {
return a
}
return b
}
// NewSensorCollector Create a new Hue collector for sensors
func NewSensorCollector(namespace string, bridge Bridge, ignoreTypes []string, matchNames bool) prometheus.Collector {
c := sensorCollector{
bridge: bridge,
ignoreTypes: ignoreTypes,
matchNames: matchNames,
sensorValue: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "sensor",
Name: "value",
Help: "Sensor values",
},
variableSensorLabelNames,
),
sensorBattery: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "sensor",
Name: "battery",
Help: "Sensor battery levels (%)",
},
variableSensorLabelNames,
),
sensorLastUpdated: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "sensor",
Name: "last_updated",
Help: "Sensor last updated time",
},
variableSensorLabelNames,
),
sensorOn: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "sensor",
Name: "on",
Help: "Sensor on/off (1/0)",
},
variableSensorLabelNames,
),
sensorReachable: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "sensor",
Name: "reachable",
Help: "Sensor reachability (1/0)",
},
variableSensorLabelNames,
),
sensorScrapesFailed: prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "sensor",
Name: "scrapes_failed",
Help: "Count of scrapes of sensor data from the Hue bridge that have failed",
},
),
bridgeRestarts: prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "bridge",
Name: "restarts",
Help: "Count of number of bridge restarts detected",
},
),
}
return c
}
func (c sensorCollector) Describe(ch chan<- *prometheus.Desc) {
c.sensorValue.Describe(ch)
c.sensorBattery.Describe(ch)
c.sensorLastUpdated.Describe(ch)
c.sensorOn.Describe(ch)
c.sensorReachable.Describe(ch)
c.sensorScrapesFailed.Describe(ch)
c.bridgeRestarts.Describe(ch)
}
func (c sensorCollector) recordSensor(sensor hue.Sensor, sensorName string, deviceID string, sensorValue float64) {
sensorLabels := prometheus.Labels{
"name": sensorName,
"model_id": sensor.ModelID,
"manufacturer_name": sensor.ManufacturerName,
"type": sensor.Type,
"unique_id": sensor.UniqueID,
"device_id": deviceID,
"product_name": sensor.ProductName,
}
c.sensorValue.With(sensorLabels).Set(sensorValue)
c.sensorBattery.With(sensorLabels).Set(float64(sensor.Config.Battery))
// let's set a sensible minimum for last updated here: if your sensor last updated before 1970,
// something's clearly not right. No need to set it to 1969 /BCE/.
c.sensorLastUpdated.With(sensorLabels).Set(float64(max(sensor.State.LastUpdated.Unix(), 0)))
if sensor.Config.On {
c.sensorOn.With(sensorLabels).Set(1)
} else {
c.sensorOn.With(sensorLabels).Set(0)
}
if sensor.Config.Reachable {
c.sensorReachable.With(sensorLabels).Set(1)
} else {
c.sensorReachable.With(sensorLabels).Set(0)
}
}
func (c sensorCollector) Collect(ch chan<- prometheus.Metric) {
c.sensorValue.Reset()
c.sensorBattery.Reset()
c.sensorLastUpdated.Reset()
c.sensorOn.Reset()
c.sensorReachable.Reset()
sensors, err := c.bridge.GetAllSensors()
if err != nil {
log.Errorf("Failed to update sensors: %v", err)
c.sensorScrapesFailed.Inc()
}
sensorNames := make(map[string]string)
sensorLastUpdatedHistory := make(map[string]int64)
restartDetected := false
for _, sensor := range sensors {
var sensorValue float64
deviceID := sensor.UniqueID
if contains(c.ignoreTypes, sensor.Type) {
continue
} else if sensor.Type == "Daylight" {
// bridge daylight (sunrise / sunset) sensor
if sensor.State.Daylight {
sensorValue = 1
}
} else if sensor.Type == "ZGPSwitch" {
// Hue tap switch
deviceID = sensor.UniqueID[0:23]
sensorValue = float64(sensor.State.ButtonEvent)
} else if sensor.Type == "ZLLSwitch" {
// Hue dimmer switch
deviceID = sensor.UniqueID[0:23]
sensorValue = float64(sensor.State.ButtonEvent)
} else if sensor.Type == "ClipGenericStatus" {
sensorValue = float64(sensor.State.Status)
} else if sensor.Type == "ZLLPresence" {
deviceID = sensor.UniqueID[0:23]
sensorNames[deviceID] = sensor.Name
if sensor.State.Presence {
sensorValue = 1
}
} else {
continue
}
if sensorLastUpdatedHistory[sensor.UniqueID] > math.MinInt64 && sensor.State.LastUpdated.Unix() == math.MinInt64 {
restartDetected = true
}
sensorLastUpdatedHistory[sensor.UniqueID] = sensor.State.LastUpdated.Unix()
c.recordSensor(sensor, sensor.Name, deviceID, sensorValue)
}
// kinda inefficient looping over them twice, but simplies code when name matching is enabled
for _, sensor := range sensors {
var sensorValue float64
if sensor.Type == "ZLLTemperature" {
sensorValue = float64(sensor.State.Temperature)
} else if sensor.Type == "ZLLLightLevel" {
sensorValue = float64(sensor.State.LightLevel)
} else {
continue
}
deviceID := sensor.UniqueID[0:23]
sensorName := sensor.Name
if c.matchNames {
var ok bool
sensorName, ok = sensorNames[deviceID]
if !ok {
sensorName = sensor.Name
}
}
if sensorLastUpdatedHistory[sensor.UniqueID] > math.MinInt64 && sensor.State.LastUpdated.Unix() == math.MinInt64 {
restartDetected = true
}
sensorLastUpdatedHistory[sensor.UniqueID] = sensor.State.LastUpdated.Unix()
c.recordSensor(sensor, sensorName, deviceID, sensorValue)
}
if restartDetected {
c.bridgeRestarts.Inc()
}
c.sensorValue.Collect(ch)
c.sensorBattery.Collect(ch)
c.sensorLastUpdated.Collect(ch)
c.sensorOn.Collect(ch)
c.sensorReachable.Collect(ch)
c.sensorScrapesFailed.Collect(ch)
c.bridgeRestarts.Collect(ch)
}