sensor added
This commit is contained in:
parent
3283847b34
commit
0c2424e8b0
4 changed files with 115 additions and 57 deletions
|
@ -39,6 +39,11 @@ Usage:
|
||||||
hue_light_status{manufacturer_name="...",model_id="...",name="...",state_alert="...",state_bri="...",state_ct="...",state_on="...",state_reachable="...",state_saturation="...",sw_version="...",type="...",unique_id="..."} 1
|
hue_light_status{manufacturer_name="...",model_id="...",name="...",state_alert="...",state_bri="...",state_ct="...",state_on="...",state_reachable="...",state_saturation="...",sw_version="...",type="...",unique_id="..."} 1
|
||||||
hue_light_status{manufacturer_name="...",model_id="...",name="...",state_alert="...",state_bri="...",state_ct="...",state_on="...",state_reachable="...",state_saturation="...",sw_version="...",type="...",unique_id="..."} 0
|
hue_light_status{manufacturer_name="...",model_id="...",name="...",state_alert="...",state_bri="...",state_ct="...",state_on="...",state_reachable="...",state_saturation="...",sw_version="...",type="...",unique_id="..."} 0
|
||||||
...
|
...
|
||||||
|
# HELP hue_sensor status of sensors registered at hue bridge
|
||||||
|
# TYPE hue_sensor gauge
|
||||||
|
hue_sensor{config_battery="...",config_on="...",config_reachable="...",manufacturer_name="...",model_id="...",name="...",state_button_event="...",state_daylight="...",state_last_updated="...",state_last_updated_time="...",sw_version="...",type="...",unique_id="..."} 1
|
||||||
|
hue_sensor{config_battery="...",config_on="...",config_reachable="...",manufacturer_name="...",model_id="...",name="...",state_button_event="...",state_daylight="...",state_last_updated="...",state_last_updated_time="...",sw_version="...",type="...",unique_id="..."} 0
|
||||||
|
...
|
||||||
|
|
||||||
### Test exporter:
|
### Test exporter:
|
||||||
|
|
||||||
|
|
|
@ -134,8 +134,11 @@ func (collector *Collector) initDescAndType() {
|
||||||
case hue.TypeLight:
|
case hue.TypeLight:
|
||||||
metric.FqName = "hue_light"
|
metric.FqName = "hue_light"
|
||||||
help = "status of lights registered at hue bridge"
|
help = "status of lights registered at hue bridge"
|
||||||
metric.PromType = prometheus.GaugeValue
|
case hue.TypeSesnor:
|
||||||
|
metric.FqName = "hue_sensor"
|
||||||
|
help = "status of sensors registered at hue bridge"
|
||||||
}
|
}
|
||||||
|
metric.PromType = prometheus.GaugeValue
|
||||||
|
|
||||||
labels := []string{}
|
labels := []string{}
|
||||||
for _, label := range metric.Labels {
|
for _, label := range metric.Labels {
|
||||||
|
|
142
hue/hue.go
142
hue/hue.go
|
@ -16,20 +16,27 @@ type Exporter struct {
|
||||||
|
|
||||||
const (
|
const (
|
||||||
TypeLight = "light"
|
TypeLight = "light"
|
||||||
TypeOther = "???"
|
TypeSesnor = "sensor"
|
||||||
|
|
||||||
LightLabelName = "Name"
|
LabelName = "Name"
|
||||||
LightLabelType = "Type"
|
LabelType = "Type"
|
||||||
LightLabelModelID = "Model_ID"
|
LabelModelID = "Model_ID"
|
||||||
LightLabelManufacturerName = "Manufacturer_Name"
|
LabelManufacturerName = "Manufacturer_Name"
|
||||||
LightLabelSWVersion = "SW_Version"
|
LabelSWVersion = "SW_Version"
|
||||||
LightLabelUniqueID = "Unique_ID"
|
LabelUniqueID = "Unique_ID"
|
||||||
LightLabelStateOn = "State_On"
|
LabelStateOn = "State_On"
|
||||||
LightLabelStateAlert = "State_Alert"
|
LabelStateAlert = "State_Alert"
|
||||||
LightLabelStateBri = "State_Bri"
|
LabelStateBri = "State_Bri"
|
||||||
LightLabelStateCT = "State_CT"
|
LabelStateCT = "State_CT"
|
||||||
LightLabelStateReachable = "State_Reachable"
|
LabelStateReachable = "State_Reachable"
|
||||||
LightLabelStateSaturation = "State_Saturation"
|
LabelStateSaturation = "State_Saturation"
|
||||||
|
LabelStateButtonEvent = "State_Button_Event"
|
||||||
|
LabelStateDaylight = "State_Daylight"
|
||||||
|
LabelStateLastUpdated = "State_Last_Updated"
|
||||||
|
LabelStateLastUpdatedTime = "State_Last_Updated_Time"
|
||||||
|
LabelConfigBatery = "Config_Battery"
|
||||||
|
LabelConfigOn = "Config_On"
|
||||||
|
LabelConfigReachable = "Config_Reachable"
|
||||||
)
|
)
|
||||||
|
|
||||||
// InitMetrics func
|
// InitMetrics func
|
||||||
|
@ -37,8 +44,13 @@ func (exporter *Exporter) InitMetrics() (metrics []*metric.Metric) {
|
||||||
|
|
||||||
metrics = append(metrics, &metric.Metric{
|
metrics = append(metrics, &metric.Metric{
|
||||||
HueType: TypeLight,
|
HueType: TypeLight,
|
||||||
Labels: []string{LightLabelName, LightLabelType, LightLabelModelID, LightLabelManufacturerName, LightLabelSWVersion, LightLabelUniqueID, LightLabelStateOn, LightLabelStateAlert, LightLabelStateBri, LightLabelStateCT, LightLabelStateReachable, LightLabelStateSaturation},
|
Labels: []string{LabelName, LabelType, LabelModelID, LabelManufacturerName, LabelSWVersion, LabelUniqueID, LabelStateOn, LabelStateAlert, LabelStateBri, LabelStateCT, LabelStateReachable, LabelStateSaturation},
|
||||||
ResultKey: LightLabelStateOn})
|
ResultKey: LabelStateOn})
|
||||||
|
|
||||||
|
metrics = append(metrics, &metric.Metric{
|
||||||
|
HueType: TypeSesnor,
|
||||||
|
Labels: []string{LabelName, LabelType, LabelModelID, LabelManufacturerName, LabelSWVersion, LabelUniqueID, LabelStateButtonEvent, LabelStateDaylight, LabelStateLastUpdated, LabelStateLastUpdatedTime, LabelConfigBatery, LabelConfigOn, LabelConfigReachable},
|
||||||
|
ResultKey: LabelConfigOn})
|
||||||
|
|
||||||
return metrics
|
return metrics
|
||||||
}
|
}
|
||||||
|
@ -60,6 +72,8 @@ func (exporter *Exporter) Collect(metrics []*metric.Metric) (err error) {
|
||||||
switch metric.HueType {
|
switch metric.HueType {
|
||||||
case TypeLight:
|
case TypeLight:
|
||||||
err = collectLights(bridge, metric)
|
err = collectLights(bridge, metric)
|
||||||
|
case TypeSesnor:
|
||||||
|
err = collectSensors(bridge, metric)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -70,6 +84,56 @@ func (exporter *Exporter) Collect(metrics []*metric.Metric) (err error) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func collectSensors(bridge *hue.Bridge, metric *metric.Metric) (err error) {
|
||||||
|
|
||||||
|
metric.MetricResult = nil
|
||||||
|
|
||||||
|
sensors, err := bridge.GetAllSensors()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("[error GetAllSensors()] '%v'", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, sensor := range sensors {
|
||||||
|
|
||||||
|
result := make(map[string]interface{})
|
||||||
|
for _, label := range metric.Labels {
|
||||||
|
|
||||||
|
switch label {
|
||||||
|
case LabelName:
|
||||||
|
result[LabelName] = sensor.Name
|
||||||
|
case LabelType:
|
||||||
|
result[LabelType] = sensor.Type
|
||||||
|
case LabelModelID:
|
||||||
|
result[LabelModelID] = sensor.ModelID
|
||||||
|
case LabelManufacturerName:
|
||||||
|
result[LabelManufacturerName] = sensor.ManufacturerName
|
||||||
|
case LabelSWVersion:
|
||||||
|
result[LabelSWVersion] = sensor.SWVersion
|
||||||
|
case LabelUniqueID:
|
||||||
|
result[LabelUniqueID] = sensor.UniqueID
|
||||||
|
case LabelStateButtonEvent:
|
||||||
|
result[LabelStateButtonEvent] = sensor.State.ButtonEvent
|
||||||
|
case LabelStateDaylight:
|
||||||
|
result[LabelStateDaylight] = sensor.State.Daylight
|
||||||
|
case LabelStateLastUpdated:
|
||||||
|
result[LabelStateLastUpdated] = sensor.State.LastUpdated
|
||||||
|
case LabelStateLastUpdatedTime:
|
||||||
|
result[LabelStateLastUpdatedTime] = sensor.State.LastUpdated.Time
|
||||||
|
case LabelConfigBatery:
|
||||||
|
result[LabelConfigBatery] = sensor.Config.Battery
|
||||||
|
case LabelConfigOn:
|
||||||
|
result[LabelConfigOn] = sensor.Config.On
|
||||||
|
case LabelConfigReachable:
|
||||||
|
result[LabelConfigReachable] = sensor.Config.Reachable
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
metric.MetricResult = append(metric.MetricResult, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func collectLights(bridge *hue.Bridge, metric *metric.Metric) (err error) {
|
func collectLights(bridge *hue.Bridge, metric *metric.Metric) (err error) {
|
||||||
|
|
||||||
metric.MetricResult = nil
|
metric.MetricResult = nil
|
||||||
|
@ -85,30 +149,30 @@ func collectLights(bridge *hue.Bridge, metric *metric.Metric) (err error) {
|
||||||
for _, label := range metric.Labels {
|
for _, label := range metric.Labels {
|
||||||
|
|
||||||
switch label {
|
switch label {
|
||||||
case LightLabelName:
|
case LabelName:
|
||||||
result[LightLabelName] = light.Name
|
result[LabelName] = light.Name
|
||||||
case LightLabelType:
|
case LabelType:
|
||||||
result[LightLabelType] = light.Type
|
result[LabelType] = light.Type
|
||||||
case LightLabelModelID:
|
case LabelModelID:
|
||||||
result[LightLabelModelID] = light.ModelID
|
result[LabelModelID] = light.ModelID
|
||||||
case LightLabelManufacturerName:
|
case LabelManufacturerName:
|
||||||
result[LightLabelManufacturerName] = light.ManufacturerName
|
result[LabelManufacturerName] = light.ManufacturerName
|
||||||
case LightLabelSWVersion:
|
case LabelSWVersion:
|
||||||
result[LightLabelSWVersion] = light.SWVersion
|
result[LabelSWVersion] = light.SWVersion
|
||||||
case LightLabelUniqueID:
|
case LabelUniqueID:
|
||||||
result[LightLabelUniqueID] = light.UniqueID
|
result[LabelUniqueID] = light.UniqueID
|
||||||
case LightLabelStateOn:
|
case LabelStateOn:
|
||||||
result[LightLabelStateOn] = light.State.On
|
result[LabelStateOn] = light.State.On
|
||||||
case LightLabelStateAlert:
|
case LabelStateAlert:
|
||||||
result[LightLabelStateAlert] = light.State.Alert
|
result[LabelStateAlert] = light.State.Alert
|
||||||
case LightLabelStateBri:
|
case LabelStateBri:
|
||||||
result[LightLabelStateBri] = light.State.Bri
|
result[LabelStateBri] = light.State.Bri
|
||||||
case LightLabelStateCT:
|
case LabelStateCT:
|
||||||
result[LightLabelStateCT] = light.State.CT
|
result[LabelStateCT] = light.State.CT
|
||||||
case LightLabelStateReachable:
|
case LabelStateReachable:
|
||||||
result[LightLabelStateReachable] = light.State.Reachable
|
result[LabelStateReachable] = light.State.Reachable
|
||||||
case LightLabelStateSaturation:
|
case LabelStateSaturation:
|
||||||
result[LightLabelStateSaturation] = light.State.Saturation
|
result[LabelStateSaturation] = light.State.Saturation
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// PrometheusResult struct
|
||||||
type PrometheusResult struct {
|
type PrometheusResult struct {
|
||||||
PromDesc *prometheus.Desc
|
PromDesc *prometheus.Desc
|
||||||
PromValueType prometheus.ValueType
|
PromValueType prometheus.ValueType
|
||||||
|
@ -13,24 +14,9 @@ type PrometheusResult struct {
|
||||||
|
|
||||||
// Metric struct
|
// Metric struct
|
||||||
type Metric struct {
|
type Metric struct {
|
||||||
// PromDesc PromDesc `json:"promDesc"`
|
|
||||||
// PromType string `json:"promType"`
|
|
||||||
// ResultKey string `json:"resultKey"`
|
|
||||||
// OkValue string `json:"okValue"`
|
|
||||||
// ResultPath string `json:"resultPath"`
|
|
||||||
// Page string `json:"page"`
|
|
||||||
// Service string `json:"service"`
|
|
||||||
// Action string `json:"action"`
|
|
||||||
// ActionArgument *ActionArg `json:"actionArgument"`
|
|
||||||
|
|
||||||
// Desc *prometheus.Desc
|
|
||||||
|
|
||||||
// Value float64
|
|
||||||
// labelValues []string
|
|
||||||
|
|
||||||
HueType string
|
HueType string
|
||||||
Labels []string
|
Labels []string
|
||||||
MetricResult []map[string]interface{} //filled during collect
|
MetricResult []map[string]interface{}
|
||||||
ResultKey string
|
ResultKey string
|
||||||
FqName string
|
FqName string
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue