sensor added

This commit is contained in:
aexel90 2020-12-27 14:58:21 +01:00
parent 3283847b34
commit 0c2424e8b0
4 changed files with 115 additions and 57 deletions

View file

@ -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="..."} 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:

View file

@ -134,8 +134,11 @@ func (collector *Collector) initDescAndType() {
case hue.TypeLight:
metric.FqName = "hue_light"
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{}
for _, label := range metric.Labels {

View file

@ -16,20 +16,27 @@ type Exporter struct {
const (
TypeLight = "light"
TypeOther = "???"
TypeSesnor = "sensor"
LightLabelName = "Name"
LightLabelType = "Type"
LightLabelModelID = "Model_ID"
LightLabelManufacturerName = "Manufacturer_Name"
LightLabelSWVersion = "SW_Version"
LightLabelUniqueID = "Unique_ID"
LightLabelStateOn = "State_On"
LightLabelStateAlert = "State_Alert"
LightLabelStateBri = "State_Bri"
LightLabelStateCT = "State_CT"
LightLabelStateReachable = "State_Reachable"
LightLabelStateSaturation = "State_Saturation"
LabelName = "Name"
LabelType = "Type"
LabelModelID = "Model_ID"
LabelManufacturerName = "Manufacturer_Name"
LabelSWVersion = "SW_Version"
LabelUniqueID = "Unique_ID"
LabelStateOn = "State_On"
LabelStateAlert = "State_Alert"
LabelStateBri = "State_Bri"
LabelStateCT = "State_CT"
LabelStateReachable = "State_Reachable"
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
@ -37,8 +44,13 @@ func (exporter *Exporter) InitMetrics() (metrics []*metric.Metric) {
metrics = append(metrics, &metric.Metric{
HueType: TypeLight,
Labels: []string{LightLabelName, LightLabelType, LightLabelModelID, LightLabelManufacturerName, LightLabelSWVersion, LightLabelUniqueID, LightLabelStateOn, LightLabelStateAlert, LightLabelStateBri, LightLabelStateCT, LightLabelStateReachable, LightLabelStateSaturation},
ResultKey: LightLabelStateOn})
Labels: []string{LabelName, LabelType, LabelModelID, LabelManufacturerName, LabelSWVersion, LabelUniqueID, LabelStateOn, LabelStateAlert, LabelStateBri, LabelStateCT, LabelStateReachable, LabelStateSaturation},
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
}
@ -60,6 +72,8 @@ func (exporter *Exporter) Collect(metrics []*metric.Metric) (err error) {
switch metric.HueType {
case TypeLight:
err = collectLights(bridge, metric)
case TypeSesnor:
err = collectSensors(bridge, metric)
}
if err != nil {
@ -70,6 +84,56 @@ func (exporter *Exporter) Collect(metrics []*metric.Metric) (err error) {
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) {
metric.MetricResult = nil
@ -85,30 +149,30 @@ func collectLights(bridge *hue.Bridge, metric *metric.Metric) (err error) {
for _, label := range metric.Labels {
switch label {
case LightLabelName:
result[LightLabelName] = light.Name
case LightLabelType:
result[LightLabelType] = light.Type
case LightLabelModelID:
result[LightLabelModelID] = light.ModelID
case LightLabelManufacturerName:
result[LightLabelManufacturerName] = light.ManufacturerName
case LightLabelSWVersion:
result[LightLabelSWVersion] = light.SWVersion
case LightLabelUniqueID:
result[LightLabelUniqueID] = light.UniqueID
case LightLabelStateOn:
result[LightLabelStateOn] = light.State.On
case LightLabelStateAlert:
result[LightLabelStateAlert] = light.State.Alert
case LightLabelStateBri:
result[LightLabelStateBri] = light.State.Bri
case LightLabelStateCT:
result[LightLabelStateCT] = light.State.CT
case LightLabelStateReachable:
result[LightLabelStateReachable] = light.State.Reachable
case LightLabelStateSaturation:
result[LightLabelStateSaturation] = light.State.Saturation
case LabelName:
result[LabelName] = light.Name
case LabelType:
result[LabelType] = light.Type
case LabelModelID:
result[LabelModelID] = light.ModelID
case LabelManufacturerName:
result[LabelManufacturerName] = light.ManufacturerName
case LabelSWVersion:
result[LabelSWVersion] = light.SWVersion
case LabelUniqueID:
result[LabelUniqueID] = light.UniqueID
case LabelStateOn:
result[LabelStateOn] = light.State.On
case LabelStateAlert:
result[LabelStateAlert] = light.State.Alert
case LabelStateBri:
result[LabelStateBri] = light.State.Bri
case LabelStateCT:
result[LabelStateCT] = light.State.CT
case LabelStateReachable:
result[LabelStateReachable] = light.State.Reachable
case LabelStateSaturation:
result[LabelStateSaturation] = light.State.Saturation
}
}

View file

@ -4,6 +4,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
)
// PrometheusResult struct
type PrometheusResult struct {
PromDesc *prometheus.Desc
PromValueType prometheus.ValueType
@ -13,24 +14,9 @@ type PrometheusResult struct {
// 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
Labels []string
MetricResult []map[string]interface{} //filled during collect
MetricResult []map[string]interface{}
ResultKey string
FqName string