-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
145 lines (129 loc) · 3.41 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"encoding/json"
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
dimensions = []string{"ip", "alias", "deviceId", "mac", "model"}
errCodeDesc = prometheus.NewDesc("hs110_err_code", "Error Code",
dimensions, nil)
currentMaDesc = prometheus.NewDesc("hs110_current_ma", "Current (mA)",
dimensions, nil)
powerMwDesc = prometheus.NewDesc("hs110_power_mw", "Power (mW)",
dimensions, nil)
totalWhDesc = prometheus.NewDesc("hs110_total_wh", "Total (Wh)",
dimensions, nil)
voltageMvDesc = prometheus.NewDesc("hs110_voltage_mv", "Voltage (mV)",
dimensions, nil)
onTimeDesc = prometheus.NewDesc("hs110_on_time", "On Time",
dimensions, nil)
)
type Result struct {
Emeter struct {
GetRealtime struct {
CurrentMa int64 `json:"current_ma"`
ErrCode int64 `json:"err_code"`
PowerMw int64 `json:"power_mw"`
TotalWh int64 `json:"total_wh"`
VoltageMv int64 `json:"voltage_mv"`
} `json:"get_realtime"`
} `json:"emeter"`
System struct {
GetSysinfo struct {
Alias string `json:"alias"`
DevName string `json:"dev_name"`
DeviceId string `json:"deviceId"`
ErrCode int64 `json:"err_code"`
Mac string `json:"mac"`
Model string `json:"model"`
OnTime int64 `json:"on_time"`
} `json:"get_sysinfo"`
} `json:"system"`
}
type PlugCollector struct {
ip string
plug Hs1xxPlug
}
func (c *PlugCollector) Describe(ch chan<- *prometheus.Desc) {
prometheus.DescribeByCollect(c, ch)
}
func (c *PlugCollector) Collect(ch chan<- prometheus.Metric) {
data, err := c.plug.MeterInfo()
if err != nil {
log.Printf("Unable to collect meter info: %v", err)
return
}
var result Result
err = json.Unmarshal([]byte(data), &result)
if err != nil {
log.Printf("Unable to decode response: %v", err)
return
}
dimensions := []string{
c.ip,
result.System.GetSysinfo.Alias,
result.System.GetSysinfo.DeviceId,
result.System.GetSysinfo.Mac,
result.System.GetSysinfo.Model,
}
ch <- prometheus.MustNewConstMetric(
errCodeDesc,
prometheus.GaugeValue,
float64(result.Emeter.GetRealtime.ErrCode),
dimensions...,
)
ch <- prometheus.MustNewConstMetric(
currentMaDesc,
prometheus.GaugeValue,
float64(result.Emeter.GetRealtime.CurrentMa),
dimensions...,
)
ch <- prometheus.MustNewConstMetric(
powerMwDesc,
prometheus.GaugeValue,
float64(result.Emeter.GetRealtime.PowerMw),
dimensions...,
)
ch <- prometheus.MustNewConstMetric(
totalWhDesc,
prometheus.GaugeValue,
float64(result.Emeter.GetRealtime.TotalWh),
dimensions...,
)
ch <- prometheus.MustNewConstMetric(
voltageMvDesc,
prometheus.GaugeValue,
float64(result.Emeter.GetRealtime.VoltageMv),
dimensions...,
)
ch <- prometheus.MustNewConstMetric(
onTimeDesc,
prometheus.GaugeValue,
float64(result.System.GetSysinfo.OnTime),
dimensions...,
)
}
func handle(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
ip := q.Get("ip")
if ip == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
handleIp(ip, w, r)
}
func handleIp(ip string, w http.ResponseWriter, r *http.Request) {
plug := Hs1xxPlug{IPAddress: ip}
collector := &PlugCollector{ip, plug}
registry := prometheus.NewRegistry()
registry.MustRegister(collector)
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}
func main() {
http.HandleFunc("/metrics", handle)
http.ListenAndServe(":9119", nil)
}