sensor temperature added
This commit is contained in:
parent
0c2424e8b0
commit
5663b27f4a
3 changed files with 99 additions and 109 deletions
|
@ -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) {
|
||||||
|
|
131
hue/hue.go
131
hue/hue.go
|
@ -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"
|
||||||
)
|
)
|
||||||
|
@ -44,13 +45,33 @@ func (exporter *Exporter) InitMetrics() (metrics []*metric.Metric) {
|
||||||
|
|
||||||
metrics = append(metrics, &metric.Metric{
|
metrics = append(metrics, &metric.Metric{
|
||||||
HueType: TypeLight,
|
HueType: TypeLight,
|
||||||
|
FqName: "hue_light_info",
|
||||||
|
Help: "Non-numeric data, value is always 1",
|
||||||
Labels: []string{LabelName, LabelType, LabelModelID, LabelManufacturerName, LabelSWVersion, LabelUniqueID, LabelStateOn, LabelStateAlert, LabelStateBri, LabelStateCT, LabelStateReachable, LabelStateSaturation},
|
Labels: []string{LabelName, LabelType, LabelModelID, LabelManufacturerName, LabelSWVersion, LabelUniqueID, LabelStateOn, LabelStateAlert, LabelStateBri, LabelStateCT, LabelStateReachable, LabelStateSaturation},
|
||||||
ResultKey: LabelStateOn})
|
})
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, metric := range metrics {
|
sensorData, err := collectSensors(bridge)
|
||||||
|
|
||||||
var err error
|
|
||||||
|
|
||||||
switch metric.HueType {
|
|
||||||
case TypeLight:
|
|
||||||
err = collectLights(bridge, metric)
|
|
||||||
case TypeSesnor:
|
|
||||||
err = collectSensors(bridge, metric)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lightData, err := collectLights(bridge)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, metric := range metrics {
|
||||||
|
switch metric.HueType {
|
||||||
|
case TypeLight:
|
||||||
|
metric.MetricResult = lightData
|
||||||
|
case TypeSensor:
|
||||||
|
metric.MetricResult = sensorData
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
|
||||||
|
|
||||||
switch label {
|
|
||||||
case LabelName:
|
|
||||||
result[LabelName] = sensor.Name
|
result[LabelName] = sensor.Name
|
||||||
case LabelType:
|
|
||||||
result[LabelType] = sensor.Type
|
result[LabelType] = sensor.Type
|
||||||
case LabelModelID:
|
|
||||||
result[LabelModelID] = sensor.ModelID
|
result[LabelModelID] = sensor.ModelID
|
||||||
case LabelManufacturerName:
|
|
||||||
result[LabelManufacturerName] = sensor.ManufacturerName
|
result[LabelManufacturerName] = sensor.ManufacturerName
|
||||||
case LabelSWVersion:
|
|
||||||
result[LabelSWVersion] = sensor.SWVersion
|
result[LabelSWVersion] = sensor.SWVersion
|
||||||
case LabelUniqueID:
|
|
||||||
result[LabelUniqueID] = sensor.UniqueID
|
result[LabelUniqueID] = sensor.UniqueID
|
||||||
case LabelStateButtonEvent:
|
result[LabelStateButtonEvent] = float64(sensor.State.ButtonEvent)
|
||||||
result[LabelStateButtonEvent] = sensor.State.ButtonEvent
|
|
||||||
case LabelStateDaylight:
|
|
||||||
result[LabelStateDaylight] = sensor.State.Daylight
|
result[LabelStateDaylight] = sensor.State.Daylight
|
||||||
case LabelStateLastUpdated:
|
result[LabelStateLastUpdated] = sensor.State.LastUpdated.Unix()
|
||||||
result[LabelStateLastUpdated] = sensor.State.LastUpdated
|
result[LabelStateLastUpdatedTime] = sensor.State.LastUpdated.Time.Unix()
|
||||||
case LabelStateLastUpdatedTime:
|
result[LabelConfigBattery] = sensor.Config.Battery
|
||||||
result[LabelStateLastUpdatedTime] = sensor.State.LastUpdated.Time
|
|
||||||
case LabelConfigBatery:
|
|
||||||
result[LabelConfigBatery] = sensor.Config.Battery
|
|
||||||
case LabelConfigOn:
|
|
||||||
result[LabelConfigOn] = sensor.Config.On
|
result[LabelConfigOn] = sensor.Config.On
|
||||||
case LabelConfigReachable:
|
|
||||||
result[LabelConfigReachable] = sensor.Config.Reachable
|
result[LabelConfigReachable] = sensor.Config.Reachable
|
||||||
}
|
|
||||||
|
if sensor.Type == "ZLLTemperature" {
|
||||||
|
result[LabelStateTemperature] = float64(sensor.State.Temperature)
|
||||||
}
|
}
|
||||||
|
|
||||||
metric.MetricResult = append(metric.MetricResult, result)
|
sensorData = append(sensorData, result)
|
||||||
|
}
|
||||||
|
return sensorData, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
func collectLights(bridge *hue.Bridge) (lightData []map[string]interface{}, err error) {
|
||||||
}
|
|
||||||
|
|
||||||
func collectLights(bridge *hue.Bridge, metric *metric.Metric) (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 {
|
|
||||||
|
|
||||||
switch label {
|
|
||||||
case LabelName:
|
|
||||||
result[LabelName] = light.Name
|
result[LabelName] = light.Name
|
||||||
case LabelType:
|
|
||||||
result[LabelType] = light.Type
|
result[LabelType] = light.Type
|
||||||
case LabelModelID:
|
|
||||||
result[LabelModelID] = light.ModelID
|
result[LabelModelID] = light.ModelID
|
||||||
case LabelManufacturerName:
|
|
||||||
result[LabelManufacturerName] = light.ManufacturerName
|
result[LabelManufacturerName] = light.ManufacturerName
|
||||||
case LabelSWVersion:
|
|
||||||
result[LabelSWVersion] = light.SWVersion
|
result[LabelSWVersion] = light.SWVersion
|
||||||
case LabelUniqueID:
|
|
||||||
result[LabelUniqueID] = light.UniqueID
|
result[LabelUniqueID] = light.UniqueID
|
||||||
case LabelStateOn:
|
|
||||||
result[LabelStateOn] = light.State.On
|
result[LabelStateOn] = light.State.On
|
||||||
case LabelStateAlert:
|
|
||||||
result[LabelStateAlert] = light.State.Alert
|
result[LabelStateAlert] = light.State.Alert
|
||||||
case LabelStateBri:
|
|
||||||
result[LabelStateBri] = light.State.Bri
|
result[LabelStateBri] = light.State.Bri
|
||||||
case LabelStateCT:
|
|
||||||
result[LabelStateCT] = light.State.CT
|
result[LabelStateCT] = light.State.CT
|
||||||
case LabelStateReachable:
|
|
||||||
result[LabelStateReachable] = light.State.Reachable
|
result[LabelStateReachable] = light.State.Reachable
|
||||||
case LabelStateSaturation:
|
|
||||||
result[LabelStateSaturation] = light.State.Saturation
|
result[LabelStateSaturation] = light.State.Saturation
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
metric.MetricResult = append(metric.MetricResult, result)
|
lightData = append(lightData, result)
|
||||||
}
|
}
|
||||||
|
return lightData, nil
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newBridge(ipAddr string) *hue.Bridge {
|
func newBridge(ipAddr string) *hue.Bridge {
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue