sensor temperature added

This commit is contained in:
aexel90 2020-12-28 08:49:18 +01:00
parent 0c2424e8b0
commit 5663b27f4a
3 changed files with 99 additions and 109 deletions

View file

@ -116,8 +116,11 @@ func (collector *Collector) getResult() (err error) {
if err != nil { if err != nil {
return err return err
} }
if resultValue == nil {
continue
}
result := metric.PrometheusResult{PromDesc: m.PromDesc, PromValueType: m.PromType, Value: resultValue, LabelValues: labelValues} result := metric.PrometheusResult{PromDesc: m.PromDesc, PromValueType: m.PromType, Value: *resultValue, LabelValues: labelValues}
m.PromResult = append(m.PromResult, &result) m.PromResult = append(m.PromResult, &result)
} }
} }
@ -128,16 +131,6 @@ func (collector *Collector) initDescAndType() {
for _, metric := range collector.metrics { for _, metric := range collector.metrics {
var help string
switch metric.HueType {
case hue.TypeLight:
metric.FqName = "hue_light"
help = "status of lights registered at hue bridge"
case hue.TypeSesnor:
metric.FqName = "hue_sensor"
help = "status of sensors registered at hue bridge"
}
metric.PromType = prometheus.GaugeValue metric.PromType = prometheus.GaugeValue
labels := []string{} labels := []string{}
@ -145,20 +138,31 @@ func (collector *Collector) initDescAndType() {
labels = append(labels, strings.ToLower(label)) labels = append(labels, strings.ToLower(label))
} }
metric.PromDesc = prometheus.NewDesc(metric.FqName, help, labels, nil) metric.PromDesc = prometheus.NewDesc(metric.FqName, metric.Help, labels, nil)
} }
} }
func getResultValue(resultKey string, result map[string]interface{}) (float64, error) { func getResultValue(resultKey string, result map[string]interface{}) (*float64, error) {
var floatValue float64
if resultKey == "" {
floatValue = 1
return &floatValue, nil
}
value := result[resultKey] value := result[resultKey]
var floatValue float64 if value == nil {
return nil, nil
}
switch tval := value.(type) { switch tval := value.(type) {
case float64: case float64:
floatValue = tval floatValue = tval
case int: case int:
floatValue = float64(tval) floatValue = float64(tval)
case uint8:
floatValue = float64(tval)
case uint64: case uint64:
floatValue = float64(tval) floatValue = float64(tval)
case bool: case bool:
@ -168,9 +172,9 @@ func getResultValue(resultKey string, result map[string]interface{}) (float64, e
floatValue = 0 floatValue = 0
} }
default: default:
return 0, fmt.Errorf("[getResultValue] %v in %v - unknown type: %T", resultKey, result, value) return nil, fmt.Errorf("[getResultValue] %v in %v - unknown type: %T", resultKey, result, value)
} }
return floatValue, nil return &floatValue, nil
} }
func getLabelValues(labelNames []string, result map[string]interface{}) ([]string, error) { func getLabelValues(labelNames []string, result map[string]interface{}) ([]string, error) {

View file

@ -5,7 +5,7 @@ import (
"log" "log"
"github.com/aexel90/hue_exporter/metric" "github.com/aexel90/hue_exporter/metric"
hue "github.com/collinux/gohue" hue "github.com/shamx9ir/gohue" // github.com/collinux/gohue
) )
// Exporter data // Exporter data
@ -16,7 +16,7 @@ type Exporter struct {
const ( const (
TypeLight = "light" TypeLight = "light"
TypeSesnor = "sensor" TypeSensor = "sensor"
LabelName = "Name" LabelName = "Name"
LabelType = "Type" LabelType = "Type"
@ -34,7 +34,8 @@ const (
LabelStateDaylight = "State_Daylight" LabelStateDaylight = "State_Daylight"
LabelStateLastUpdated = "State_Last_Updated" LabelStateLastUpdated = "State_Last_Updated"
LabelStateLastUpdatedTime = "State_Last_Updated_Time" LabelStateLastUpdatedTime = "State_Last_Updated_Time"
LabelConfigBatery = "Config_Battery" LabelStateTemperature = "State_Temperature"
LabelConfigBattery = "Config_Battery"
LabelConfigOn = "Config_On" LabelConfigOn = "Config_On"
LabelConfigReachable = "Config_Reachable" LabelConfigReachable = "Config_Reachable"
) )
@ -43,14 +44,34 @@ const (
func (exporter *Exporter) InitMetrics() (metrics []*metric.Metric) { func (exporter *Exporter) InitMetrics() (metrics []*metric.Metric) {
metrics = append(metrics, &metric.Metric{ metrics = append(metrics, &metric.Metric{
HueType: TypeLight, HueType: TypeLight,
Labels: []string{LabelName, LabelType, LabelModelID, LabelManufacturerName, LabelSWVersion, LabelUniqueID, LabelStateOn, LabelStateAlert, LabelStateBri, LabelStateCT, LabelStateReachable, LabelStateSaturation}, FqName: "hue_light_info",
ResultKey: LabelStateOn}) Help: "Non-numeric data, value is always 1",
Labels: []string{LabelName, LabelType, LabelModelID, LabelManufacturerName, LabelSWVersion, LabelUniqueID, LabelStateOn, LabelStateAlert, LabelStateBri, LabelStateCT, LabelStateReachable, LabelStateSaturation},
})
metrics = append(metrics, &metric.Metric{ metrics = append(metrics, &metric.Metric{
HueType: TypeSesnor, HueType: TypeSensor,
Labels: []string{LabelName, LabelType, LabelModelID, LabelManufacturerName, LabelSWVersion, LabelUniqueID, LabelStateButtonEvent, LabelStateDaylight, LabelStateLastUpdated, LabelStateLastUpdatedTime, LabelConfigBatery, LabelConfigOn, LabelConfigReachable}, FqName: "hue_sensor_info",
ResultKey: LabelConfigOn}) Help: "Non-numeric data, value is always 1",
Labels: []string{LabelName, LabelType, LabelModelID, LabelManufacturerName, LabelSWVersion, LabelUniqueID, LabelStateButtonEvent, LabelStateDaylight, LabelStateLastUpdated, LabelStateLastUpdatedTime, LabelStateTemperature, LabelConfigBattery, LabelConfigOn, LabelConfigReachable},
})
metrics = append(metrics, &metric.Metric{
HueType: TypeSensor,
FqName: "hue_sensor_battery",
Help: "battery level percentage",
Labels: []string{LabelName, LabelType, LabelModelID, LabelManufacturerName, LabelSWVersion, LabelUniqueID},
ResultKey: LabelConfigBattery,
})
metrics = append(metrics, &metric.Metric{
HueType: TypeSensor,
FqName: "hue_sensor_temperature",
Help: "temperature level celsius degree",
Labels: []string{LabelName, LabelType, LabelModelID, LabelManufacturerName, LabelSWVersion, LabelUniqueID},
ResultKey: LabelStateTemperature,
})
return metrics return metrics
} }
@ -65,121 +86,85 @@ func (exporter *Exporter) Collect(metrics []*metric.Metric) (err error) {
return fmt.Errorf("[error Login()] '%v'", err) return fmt.Errorf("[error Login()] '%v'", err)
} }
sensorData, err := collectSensors(bridge)
if err != nil {
return err
}
lightData, err := collectLights(bridge)
if err != nil {
return err
}
for _, metric := range metrics { for _, metric := range metrics {
var err error
switch metric.HueType { switch metric.HueType {
case TypeLight: case TypeLight:
err = collectLights(bridge, metric) metric.MetricResult = lightData
case TypeSesnor: case TypeSensor:
err = collectSensors(bridge, metric) metric.MetricResult = sensorData
}
if err != nil {
return err
} }
} }
return nil return nil
} }
func collectSensors(bridge *hue.Bridge, metric *metric.Metric) (err error) { func collectSensors(bridge *hue.Bridge) (sensorData []map[string]interface{}, err error) {
metric.MetricResult = nil
sensors, err := bridge.GetAllSensors() sensors, err := bridge.GetAllSensors()
if err != nil { if err != nil {
return fmt.Errorf("[error GetAllSensors()] '%v'", err) return nil, fmt.Errorf("[error GetAllSensors()] '%v'", err)
} }
for _, sensor := range sensors { for _, sensor := range sensors {
result := make(map[string]interface{}) result := make(map[string]interface{})
for _, label := range metric.Labels { result[LabelName] = sensor.Name
result[LabelType] = sensor.Type
result[LabelModelID] = sensor.ModelID
result[LabelManufacturerName] = sensor.ManufacturerName
result[LabelSWVersion] = sensor.SWVersion
result[LabelUniqueID] = sensor.UniqueID
result[LabelStateButtonEvent] = float64(sensor.State.ButtonEvent)
result[LabelStateDaylight] = sensor.State.Daylight
result[LabelStateLastUpdated] = sensor.State.LastUpdated.Unix()
result[LabelStateLastUpdatedTime] = sensor.State.LastUpdated.Time.Unix()
result[LabelConfigBattery] = sensor.Config.Battery
result[LabelConfigOn] = sensor.Config.On
result[LabelConfigReachable] = sensor.Config.Reachable
switch label { if sensor.Type == "ZLLTemperature" {
case LabelName: result[LabelStateTemperature] = float64(sensor.State.Temperature)
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) sensorData = append(sensorData, result)
} }
return sensorData, nil
return nil
} }
func collectLights(bridge *hue.Bridge, metric *metric.Metric) (err error) { func collectLights(bridge *hue.Bridge) (lightData []map[string]interface{}, err error) {
metric.MetricResult = nil
lights, err := bridge.GetAllLights() lights, err := bridge.GetAllLights()
if err != nil { if err != nil {
return fmt.Errorf("[error GetAllLights()] '%v'", err) return nil, fmt.Errorf("[error GetAllLights()] '%v'", err)
} }
for _, light := range lights { for _, light := range lights {
result := make(map[string]interface{}) result := make(map[string]interface{})
for _, label := range metric.Labels { result[LabelName] = light.Name
result[LabelType] = light.Type
result[LabelModelID] = light.ModelID
result[LabelManufacturerName] = light.ManufacturerName
result[LabelSWVersion] = light.SWVersion
result[LabelUniqueID] = light.UniqueID
result[LabelStateOn] = light.State.On
result[LabelStateAlert] = light.State.Alert
result[LabelStateBri] = light.State.Bri
result[LabelStateCT] = light.State.CT
result[LabelStateReachable] = light.State.Reachable
result[LabelStateSaturation] = light.State.Saturation
switch label { lightData = append(lightData, result)
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
}
}
metric.MetricResult = append(metric.MetricResult, result)
} }
return lightData, nil
return nil
} }
func newBridge(ipAddr string) *hue.Bridge { func newBridge(ipAddr string) *hue.Bridge {

View file

@ -19,6 +19,7 @@ type Metric struct {
MetricResult []map[string]interface{} MetricResult []map[string]interface{}
ResultKey string ResultKey string
FqName string FqName string
Help string
PromType prometheus.ValueType PromType prometheus.ValueType
PromDesc *prometheus.Desc PromDesc *prometheus.Desc