forked from mitchellrj/hue_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
groups.go
122 lines (111 loc) · 3.04 KB
/
groups.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
package main
import (
"github.com/prometheus/client_golang/prometheus"
log "github.com/prometheus/common/log"
)
type groupCollector struct {
bridge Bridge
groupBrightness *prometheus.GaugeVec
groupHue *prometheus.GaugeVec
groupSaturation *prometheus.GaugeVec
groupOn *prometheus.GaugeVec
groupScrapesFailed prometheus.Counter
}
var variableGroupLabelNames = []string{
"name",
"type",
}
// NewGroupCollector Create a new Hue collector for groups
func NewGroupCollector(namespace string, bridge Bridge) prometheus.Collector {
c := groupCollector{
bridge: bridge,
groupBrightness: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "group",
Name: "brightness",
Help: "Group brightness level",
},
variableLightLabelNames,
),
groupHue: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "group",
Name: "hue",
Help: "Group hue",
},
variableLightLabelNames,
),
groupSaturation: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "group",
Name: "saturation",
Help: "Group saturation",
},
variableLightLabelNames,
),
groupOn: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "group",
Name: "on",
Help: "Group on (2 = all group members on, 1 = some group members on, 0 = all group members off)",
},
variableLightLabelNames,
),
groupScrapesFailed: prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "group",
Name: "scrapes_failed",
Help: "Count of scrapes of group data from the Hue bridge that have failed",
},
),
}
return c
}
func (c groupCollector) Describe(ch chan<- *prometheus.Desc) {
c.groupOn.Describe(ch)
c.groupBrightness.Describe(ch)
c.groupHue.Describe(ch)
c.groupSaturation.Describe(ch)
c.groupScrapesFailed.Describe(ch)
}
func (c groupCollector) Collect(ch chan<- prometheus.Metric) {
c.groupOn.Reset()
c.groupBrightness.Reset()
c.groupHue.Reset()
c.groupSaturation.Reset()
groups, err := c.bridge.GetAllGroups()
if err != nil {
log.Errorf("Failed to update groups: %v", err)
c.groupScrapesFailed.Inc()
}
for _, group := range groups {
groupLabels := prometheus.Labels{
"name": group.Name,
"type": group.Type,
"model_id": "",
"manufacturer_name": "",
"product_name": "",
"unique_id": "",
}
if group.State.AllOn {
c.groupOn.With(groupLabels).Set(2)
} else if group.State.AnyOn {
c.groupOn.With(groupLabels).Set(1)
} else {
c.groupOn.With(groupLabels).Set(0)
}
c.groupBrightness.With(groupLabels).Set(float64(group.Action.Bri))
c.groupHue.With(groupLabels).Set(float64(group.Action.Hue))
c.groupSaturation.With(groupLabels).Set(float64(group.Action.Sat))
}
c.groupOn.Collect(ch)
c.groupBrightness.Collect(ch)
c.groupHue.Collect(ch)
c.groupSaturation.Collect(ch)
c.groupScrapesFailed.Collect(ch)
}