From a6bd17ab2ca448583010e51d0e420927b181d3c5 Mon Sep 17 00:00:00 2001 From: Daniele Sluijters Date: Sat, 4 May 2019 18:30:50 +0200 Subject: [PATCH] :sparkles: Add ampere and volts to power collector (#2) --- README.md | 2 ++ collectors/power.go | 38 +++++++++++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2287f52..79df52a 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,8 @@ MQTT topic. * `contactSensorState`: gauge `sensor_contact_state` * `currentPower`: gauge `sensor_power_current_watts` * `energyUsed`: counter `sensor_power_total_kwh` +* `currentVoltage`: gauge `sensor_power_current_voltage` +* `currentAmpere`: gauge `sensor_power_current_ampere` * `batteryLevel`: gauge `sensor_battery_level_percent` Additionally a time series is exposed for humiture, also known as diff --git a/collectors/power.go b/collectors/power.go index 48efdb4..bc8fda9 100644 --- a/collectors/power.go +++ b/collectors/power.go @@ -9,9 +9,11 @@ import ( // PowerCollector gets power data from sensors type PowerCollector struct { - powerCurrent *prometheus.Desc - powerTotal *prometheus.Desc - m *server.Manager + powerCurrent *prometheus.Desc + powerTotal *prometheus.Desc + voltageCurrent *prometheus.Desc + ampereCurrent *prometheus.Desc + m *server.Manager } // NewPowerCollector returns a collector fetching power sensor data @@ -28,6 +30,16 @@ func NewPowerCollector(m *server.Manager) (prometheus.Collector, error) { "Total power usage in kWh", []string{"source"}, nil, ), + voltageCurrent: prometheus.NewDesc( + prometheus.BuildFQName(namespace, "power", "current_voltage"), + "Current power draw in Volts", + []string{"source"}, nil, + ), + ampereCurrent: prometheus.NewDesc( + prometheus.BuildFQName(namespace, "power", "current_ampere"), + "Current power draw in Amperes", + []string{"source"}, nil, + ), }, nil } @@ -35,6 +47,8 @@ func NewPowerCollector(m *server.Manager) (prometheus.Collector, error) { func (c *PowerCollector) Describe(ch chan<- *prometheus.Desc) { ch <- c.powerCurrent ch <- c.powerTotal + ch <- c.voltageCurrent + ch <- c.ampereCurrent } // Collect sends metric updates into the channel @@ -59,5 +73,23 @@ func (c *PowerCollector) Collect(ch chan<- prometheus.Metric) { ch <- prometheus.MustNewConstMetric(c.powerTotal, prometheus.CounterValue, v, s.Info().Topic) } + if s.Feature("currentVoltage").Exists() { + v, err := toFloat(s.Feature("currentVoltage").Value()) + if err != nil { + log.Print(err.Error()) + continue + } + ch <- prometheus.MustNewConstMetric(c.voltageCurrent, + prometheus.GaugeValue, v, s.Info().Topic) + } + if s.Feature("currentAmpere").Exists() { + v, err := toFloat(s.Feature("currentAmpere").Value()) + if err != nil { + log.Print(err.Error()) + continue + } + ch <- prometheus.MustNewConstMetric(c.ampereCurrent, + prometheus.GaugeValue, v, s.Info().Topic) + } } }