-
Notifications
You must be signed in to change notification settings - Fork 20
/
lights.go
142 lines (131 loc) · 3.51 KB
/
lights.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
package main
import (
"github.com/prometheus/client_golang/prometheus"
log "github.com/prometheus/common/log"
)
type lightCollector struct {
bridge Bridge
lightBrightness *prometheus.GaugeVec
lightHue *prometheus.GaugeVec
lightSaturation *prometheus.GaugeVec
lightOn *prometheus.GaugeVec
lightReachable *prometheus.GaugeVec
lightScrapesFailed prometheus.Counter
}
var variableLightLabelNames = []string{
"name",
"type",
"model_id",
"manufacturer_name",
"product_name",
"unique_id",
}
// NewLightCollector Create a new Hue collector for lights
func NewLightCollector(namespace string, bridge Bridge) prometheus.Collector {
c := lightCollector{
bridge: bridge,
lightBrightness: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "light",
Name: "brightness",
Help: "Light brightness level",
},
variableLightLabelNames,
),
lightHue: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "light",
Name: "hue",
Help: "Light hue",
},
variableLightLabelNames,
),
lightSaturation: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "light",
Name: "saturation",
Help: "Light saturation",
},
variableLightLabelNames,
),
lightOn: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "light",
Name: "on",
Help: "Light on (1 = on, 0 = off)",
},
variableLightLabelNames,
),
lightReachable: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "light",
Name: "reachable",
Help: "Light reachability (1/0)",
},
variableLightLabelNames,
),
lightScrapesFailed: prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "light",
Name: "scrapes_failed",
Help: "Count of scrapes of light data from the Hue bridge that have failed",
},
),
}
return c
}
func (c lightCollector) Describe(ch chan<- *prometheus.Desc) {
c.lightOn.Describe(ch)
c.lightBrightness.Describe(ch)
c.lightHue.Describe(ch)
c.lightSaturation.Describe(ch)
c.lightReachable.Describe(ch)
c.lightScrapesFailed.Describe(ch)
}
func (c lightCollector) Collect(ch chan<- prometheus.Metric) {
c.lightOn.Reset()
c.lightBrightness.Reset()
c.lightHue.Reset()
c.lightSaturation.Reset()
c.lightReachable.Reset()
lights, err := c.bridge.GetAllLights()
if err != nil {
log.Errorf("Failed to update lights: %v", err)
c.lightScrapesFailed.Inc()
}
for _, light := range lights {
lightLabels := prometheus.Labels{
"name": light.Name,
"type": light.Type,
"model_id": light.ModelID,
"manufacturer_name": light.ManufacturerName,
"unique_id": light.UniqueID,
"product_name": light.ProductName,
}
if light.State.On {
c.lightOn.With(lightLabels).Set(1)
} else {
c.lightOn.With(lightLabels).Set(0)
}
c.lightBrightness.With(lightLabels).Set(float64(light.State.Bri))
c.lightHue.With(lightLabels).Set(float64(light.State.Hue))
c.lightSaturation.With(lightLabels).Set(float64(light.State.Saturation))
if light.State.Reachable {
c.lightReachable.With(lightLabels).Set(1)
} else {
c.lightReachable.With(lightLabels).Set(0)
}
}
c.lightOn.Collect(ch)
c.lightBrightness.Collect(ch)
c.lightHue.Collect(ch)
c.lightSaturation.Collect(ch)
c.lightReachable.Collect(ch)
c.lightScrapesFailed.Collect(ch)
}