| 1 | package exporter |
| 2 | |
| 3 | import ( |
| 4 | "github.com/fffonion/tplink-plug-exporter/kasa" |
| 5 | "github.com/prometheus/client_golang/prometheus" |
| 6 | log "github.com/sirupsen/logrus" |
| 7 | ) |
| 8 | |
| 9 | type Exporter struct { |
| 10 | target string |
| 11 | client *kasa.KasaClient |
| 12 | |
| 13 | metricsUp, |
| 14 | metricsMetadata, |
| 15 | metricsRelayState, |
| 16 | metricsOnTime, |
| 17 | metricsRssi, |
| 18 | metricsCurrent, |
| 19 | metricsVoltage, |
| 20 | metricsPowerLoad, |
| 21 | metricsPowerTotal *prometheus.Desc |
| 22 | } |
| 23 | |
| 24 | type ExporterTarget struct { |
| 25 | Host string |
| 26 | } |
| 27 | |
| 28 | func NewExporter(t *ExporterTarget) *Exporter { |
| 29 | var ( |
| 30 | constLabels = prometheus.Labels{} |
| 31 | labelNames = []string{"alias", "id"} |
| 32 | ) |
| 33 | |
| 34 | e := &Exporter{ |
| 35 | target: t.Host, |
| 36 | client: kasa.New(&kasa.KasaClientConfig{ |
| 37 | Host: t.Host, |
| 38 | }), |
| 39 | metricsUp: prometheus.NewDesc("kasa_online", |
| 40 | "Device online.", |
| 41 | nil, constLabels, |
| 42 | ), |
| 43 | |
| 44 | metricsMetadata: prometheus.NewDesc("kasa_metadata", |
| 45 | "Device metadata.", |
| 46 | []string{ |
| 47 | "alias", "hw_ver", "sw_ver", "model", "feature", |
| 48 | }, constLabels, |
| 49 | ), |
| 50 | |
| 51 | metricsRelayState: prometheus.NewDesc("kasa_relay_state", |
| 52 | "Relay state (switch on/off).", |
| 53 | labelNames, constLabels, |
| 54 | ), |
| 55 | metricsOnTime: prometheus.NewDesc("kasa_on_time", |
| 56 | "Time in seconds since online.", |
| 57 | labelNames, constLabels), |
| 58 | metricsRssi: prometheus.NewDesc("kasa_rssi", |
| 59 | "Wifi received signal strength indicator.", |
| 60 | labelNames, constLabels), |
| 61 | |
| 62 | metricsCurrent: prometheus.NewDesc("kasa_current", |
| 63 | "Current flowing through device in Ampere.", |
| 64 | labelNames, constLabels), |
| 65 | metricsVoltage: prometheus.NewDesc("kasa_voltage", |
| 66 | "Current voltage connected to device in Volt.", |
| 67 | labelNames, constLabels), |
| 68 | metricsPowerLoad: prometheus.NewDesc("kasa_power_load", |
| 69 | "Current power in Watt.", |
| 70 | labelNames, constLabels), |
| 71 | metricsPowerTotal: prometheus.NewDesc("kasa_power_total", |
| 72 | "Power consumption since device connected in kWh.", |
| 73 | labelNames, constLabels), |
| 74 | } |
| 75 | return e |
| 76 | } |
| 77 | |
| 78 | func (k *Exporter) Describe(ch chan<- *prometheus.Desc) { |
| 79 | ch <- k.metricsUp |
| 80 | ch <- k.metricsRelayState |
| 81 | ch <- k.metricsOnTime |
| 82 | ch <- k.metricsRssi |
| 83 | ch <- k.metricsCurrent |
| 84 | ch <- k.metricsVoltage |
| 85 | ch <- k.metricsPowerLoad |
| 86 | ch <- k.metricsPowerTotal |
| 87 | |
| 88 | } |
| 89 | |
| 90 | func (k *Exporter) Collect(ch chan<- prometheus.Metric) { |
| 91 | s := k.client.SystemService(nil) |
| 92 | r, err := s.GetSysInfo() |
| 93 | |
| 94 | if err != nil { |
| 95 | ch <- prometheus.MustNewConstMetric(k.metricsUp, prometheus.GaugeValue, |
| 96 | 0) |
| 97 | log.Errorln("error collecting", k.target, ":", err) |
| 98 | return |
| 99 | } |
| 100 | |
| 101 | // "alias", "hw_ver", "sw_ver", "model", "feature", |
| 102 | ch <- prometheus.MustNewConstMetric(k.metricsMetadata, prometheus.GaugeValue, |
| 103 | 1, r.Alias, r.HardwareVersion, r.SoftwareVersion, r.Model, r.Feature) |
| 104 | |
| 105 | ch <- prometheus.MustNewConstMetric(k.metricsRelayState, prometheus.GaugeValue, |
| 106 | float64(r.RelayState), r.Alias, r.DeviceID) |
| 107 | ch <- prometheus.MustNewConstMetric(k.metricsOnTime, prometheus.CounterValue, |
| 108 | float64(r.OnTime), r.Alias, r.DeviceID) |
| 109 | ch <- prometheus.MustNewConstMetric(k.metricsRssi, prometheus.GaugeValue, |
| 110 | float64(r.RSSI), r.Alias, r.DeviceID) |
| 111 | |
| 112 | aliases := map[string]string{} |
| 113 | emeterContexts := []*kasa.KasaRequestContext{ |
| 114 | nil, // a nil context, represent the single plug or the parent strip |
| 115 | } |
| 116 | |
| 117 | // iterrate over every child plug in a power strip |
| 118 | for _, children := range r.Children { |
| 119 | aliases[children.ID] = children.Alias |
| 120 | emeterContexts = append(emeterContexts, &kasa.KasaRequestContext{ |
| 121 | ChildIDs: []string{children.ID}, |
| 122 | }) |
| 123 | |
| 124 | ch <- prometheus.MustNewConstMetric(k.metricsRelayState, prometheus.GaugeValue, |
| 125 | float64(children.State), children.Alias, children.ID) |
| 126 | |
| 127 | ch <- prometheus.MustNewConstMetric(k.metricsOnTime, prometheus.CounterValue, |
| 128 | float64(children.OnTime), children.Alias, children.ID) |
| 129 | } |
| 130 | |
| 131 | if s.EmeterSupported(r) { |
| 132 | for _, ctx := range emeterContexts { |
| 133 | m := k.client.EmeterService(ctx) |
| 134 | re, err := m.GetRealtime() |
| 135 | |
| 136 | labels := []string{r.Alias, r.DeviceID} |
| 137 | // if this is a child plug in a powerstrip, set the alias and ID |
| 138 | if ctx != nil { |
| 139 | id := ctx.ChildIDs[0] |
| 140 | labels[0] = aliases[id] |
| 141 | labels[1] = id |
| 142 | } |
| 143 | |
| 144 | // TODO: only set the child up to 0 on error |
| 145 | if err != nil { |
| 146 | ch <- prometheus.MustNewConstMetric(k.metricsUp, prometheus.GaugeValue, |
| 147 | 0) |
| 148 | log.Errorln("error collecting", k.target, ":", err) |
| 149 | return |
| 150 | } |
| 151 | |
| 152 | ch <- prometheus.MustNewConstMetric(k.metricsCurrent, prometheus.GaugeValue, |
| 153 | float64(re.Current), labels...) |
| 154 | ch <- prometheus.MustNewConstMetric(k.metricsVoltage, prometheus.GaugeValue, |
| 155 | float64(re.Voltage), labels...) |
| 156 | ch <- prometheus.MustNewConstMetric(k.metricsPowerLoad, prometheus.GaugeValue, |
| 157 | float64(re.Power), labels...) |
| 158 | ch <- prometheus.MustNewConstMetric(k.metricsPowerTotal, prometheus.CounterValue, |
| 159 | float64(re.Total), labels...) |
| 160 | } |
| 161 | |
| 162 | } |
| 163 | |
| 164 | ch <- prometheus.MustNewConstMetric(k.metricsUp, prometheus.GaugeValue, |
| 165 | 1) |
| 166 | } |