adds counter for total WH consumed

This commit is contained in:
2022-11-27 10:40:30 -06:00
parent 5a25bb1c09
commit 77c0e64cbb
3 changed files with 29 additions and 15 deletions

View File

@@ -63,6 +63,7 @@ type configPrometheus struct {
CurrentMa *prometheus.GaugeVec
VoltageMv *prometheus.GaugeVec
PowerMw *prometheus.GaugeVec
TotalWh *prometheus.CounterVec
}
func defaultConfig() *config {
@@ -87,6 +88,11 @@ func defaultConfig() *config {
Name: "watts",
Help: "current wattage",
}, []string{"device"}),
TotalWh: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "tplink",
Name: "watt_hours",
Help: "total watt hours",
}, []string{"device"}),
},
}
}
@@ -323,7 +329,7 @@ func initialize() *config {
}
time.Now().Format(cfg.TimeFormat)
prometheus.MustRegister(cfg.Prometheus.CurrentMa, cfg.Prometheus.VoltageMv, cfg.Prometheus.PowerMw)
prometheus.MustRegister(cfg.Prometheus.CurrentMa, cfg.Prometheus.VoltageMv, cfg.Prometheus.PowerMw, cfg.Prometheus.TotalWh)
// print running config
cfg.printRunningConfig(cfgInfo)

View File

@@ -10,8 +10,14 @@ import (
)
func (cfg *config) gatherMetrics() {
tracker := make(map[string]float64)
for {
for _, host := range cfg.Hosts {
log.Printf("[TRACE] Reading Metrics from %s.", host)
if _, isMapKey := tracker[host]; !isMapKey {
tracker[host] = float64(0)
}
d := tplink.Tplink{
Host: host,
}
@@ -20,9 +26,11 @@ func (cfg *config) gatherMetrics() {
log.Printf("[WARNING] Unable to read from device: %s: %v", host, err)
continue
}
cfg.Prometheus.CurrentMa.With(prometheus.Labels{"device": host}).Set(float64(m.Emeter.GetRealtime.CurrentMa))
cfg.Prometheus.VoltageMv.With(prometheus.Labels{"device": host}).Set(float64(m.Emeter.GetRealtime.VoltageMv))
cfg.Prometheus.PowerMw.With(prometheus.Labels{"device": host}).Set(float64(m.Emeter.GetRealtime.PowerMw))
cfg.Prometheus.CurrentMa.With(prometheus.Labels{"device": host}).Set(float64(m.Emeter.GetRealtime.CurrentMa / 1000.0))
cfg.Prometheus.VoltageMv.With(prometheus.Labels{"device": host}).Set(float64(m.Emeter.GetRealtime.VoltageMv / 1000.0))
cfg.Prometheus.PowerMw.With(prometheus.Labels{"device": host}).Set(float64(m.Emeter.GetRealtime.PowerMw / 1000.0))
cfg.Prometheus.TotalWh.With(prometheus.Labels{"device": host}).Add(float64((m.Emeter.GetRealtime.TotalWh - tracker[host]) / 1000.0))
tracker[host] = m.Emeter.GetRealtime.TotalWh
}
time.Sleep(time.Duration(time.Second * 15))
}