package main import ( "log" "time" "tplink/internal/tplink" "github.com/prometheus/client_golang/prometheus" ) 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, } m, err := d.GetMeterInto() if err != nil { 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 / 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])) tracker[host] = m.Emeter.GetRealtime.TotalWh } time.Sleep(time.Duration(time.Second * 15)) } }