-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
257 lines (227 loc) · 8.33 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package main
import (
"bytes"
"github.com/antchfx/htmlquery"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
str2duration "github.com/xhit/go-str2duration"
"golang.org/x/net/html"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
)
var (
uptimeMetrics = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "pon_stick_uptime",
Help: "运行时间",
}, []string{})
uptimeDescMetrics = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "pon_stick_uptime_desc",
Help: "运行时间",
}, []string{"desc"})
cpuUsageMetrics = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "pon_stick_cpu_usage",
Help: "CPU使用率(%)",
}, []string{})
memoryUsageMetrics = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "pon_stick_memory_usage",
Help: "内存使用率(%)",
}, []string{})
temperatureMetrics = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "pon_stick_temperature",
Help: "温度(C)",
}, []string{})
voltageMetrics = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "pon_stick_voltage",
Help: "电压(V)",
}, []string{})
transmitPowerMetrics = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "pon_stick_transmit_power",
Help: "发送功率(dBm)",
}, []string{})
receivePowerMetrics = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "pon_stick_receive_power",
Help: "接收功率(dBm)",
}, []string{})
currentMetrics = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "pon_stick_current",
Help: "偏置电流(mA)",
}, []string{})
llidStatusMetrics = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "pon_stick_llid_status",
Help: "EPON LLID Status",
}, []string{})
)
func init() {
log.Println("服务初始化")
prometheus.MustRegister(uptimeMetrics)
prometheus.MustRegister(uptimeDescMetrics)
prometheus.MustRegister(cpuUsageMetrics)
prometheus.MustRegister(memoryUsageMetrics)
prometheus.MustRegister(temperatureMetrics)
prometheus.MustRegister(voltageMetrics)
prometheus.MustRegister(transmitPowerMetrics)
prometheus.MustRegister(receivePowerMetrics)
prometheus.MustRegister(currentMetrics)
prometheus.MustRegister(llidStatusMetrics)
}
func main() {
log.Println("服务开始启动")
routerAddr := getEnv("ROUTER_ADDR", "192.168.1.1")
username := getEnv("USERNAME", "admin")
password := getEnv("PASSWORD", "admin")
log.Println("ROUTER_ADDR:", routerAddr, ", USERNAME:", username, ", PASSWORD:", password)
go func() {
intervalStr := getEnv("SCRAP_INTERVAL", "60")
interval, err := strconv.Atoi(intervalStr)
if err != nil {
log.Fatal("无法将环境变量转换为整数:", err)
}
log.Println("SCRAP_INTERVAL: ", interval)
for range time.Tick(time.Duration(interval) * time.Second) {
log.Println("开始获取 Pon Stick 状态")
login(routerAddr, username, password)
ponStatus(routerAddr)
status(routerAddr)
}
}()
log.Println("服务启动完成")
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(":9001", nil))
}
func getEnv(name, defaultValue string) string {
value := os.Getenv(name)
if value != "" {
return value
} else {
return defaultValue
}
}
func login(routerAddr, username, password string) {
log.Println("开始登陆")
url := "http://" + routerAddr + "/boaform/admin/formLogin"
payload := "challenge=&username=" + username + "&password=" + password + "&save=%E7%99%BB%E5%BD%95&submit-url=%2Fadmin%2Flogin.asp"
req, err := http.NewRequest("POST", url, bytes.NewBufferString(payload))
if err != nil {
log.Fatal("Error creating request:", err)
}
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7")
req.Header.Set("Accept-Language", "zh,en;q=0.9,zh-CN;q=0.8,en-US;q=0.7")
req.Header.Set("Cache-Control", "max-age=0")
req.Header.Set("Connection", "keep-alive")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("DNT", "1")
req.Header.Set("Origin", "http://192.168.1.1")
req.Header.Set("Referer", "http://192.168.1.1/admin/login.asp")
req.Header.Set("Upgrade-Insecure-Requests", "1")
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36")
req.Header.Set("sec-gpc", "1")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal("Error sending request:", err)
}
defer resp.Body.Close()
log.Println("登陆成功")
}
func status(routerAddr string) {
log.Println("开始获取状态")
doc, _ := getData("http://" + routerAddr + "/admin/status.asp")
uptimeDoc := htmlquery.FindOne(doc, "/html/body/blockquote/form[1]/table[1]/tbody/tr[3]/td[2]/font")
uptimeDesc := uptimeDoc.FirstChild.Data
uptime := parseDuration(uptimeDoc.LastChild.Data)
cpuUsage := parsePercentValue(htmlquery.FindOne(doc, "/html/body/blockquote/form[1]/table[1]/tbody/tr[5]/td[2]/font"))
memoryUsage := parsePercentValue(htmlquery.FindOne(doc, "/html/body/blockquote/form[1]/table[1]/tbody/tr[6]/td[2]/font"))
log.Println("运行时间:", uptime)
log.Println("CPU使用率:", cpuUsage)
log.Println("内存使用率:", memoryUsage)
uptimeMetrics.WithLabelValues().Set(uptime)
uptimeDescMetrics.WithLabelValues(uptimeDesc).Set(uptime)
cpuUsageMetrics.WithLabelValues().Set(cpuUsage)
memoryUsageMetrics.WithLabelValues().Set(memoryUsage)
}
func parseDuration(durationStr string) float64 {
durationStr = strings.ReplaceAll(durationStr, ",", "")
durationStr = strings.ReplaceAll(durationStr, " ", "")
if strings.Contains(durationStr, "days") {
durationStr = strings.ReplaceAll(durationStr, "days", "d")
}
if strings.Contains(durationStr, "day") {
durationStr = strings.ReplaceAll(durationStr, "day", "d")
}
if strings.Contains(durationStr, ":") {
durationStr = strings.ReplaceAll(durationStr, ":", "h")
}
if strings.Contains(durationStr, "min") {
durationStr = strings.ReplaceAll(durationStr, "min", "m")
}
if !strings.HasSuffix(durationStr, "m") {
durationStr = durationStr + "m"
}
duration, err := str2duration.Str2Duration(durationStr)
if err != nil {
log.Println("解析时间错误: ", durationStr, err)
return -1
}
return duration.Seconds()
}
func ponStatus(routerAddr string) {
log.Println("开始获取 pon 状态")
doc, _ := getData("http://" + routerAddr + "/status_pon.asp")
temperature := parseFloatValue(htmlquery.FindOne(doc, "/html/body/blockquote/table[2]/tbody/tr[2]/td[2]/font"))
voltage := parseFloatValue(htmlquery.FindOne(doc, "/html/body/blockquote/table[2]/tbody/tr[3]/td[2]/font"))
transmitPower := parseFloatValue(htmlquery.FindOne(doc, "/html/body/blockquote/table[2]/tbody/tr[4]/td[2]/font"))
receivePower := parseFloatValue(htmlquery.FindOne(doc, "/html/body/blockquote/table[2]/tbody/tr[5]/td[2]/font"))
current := parseFloatValue(htmlquery.FindOne(doc, "/html/body/blockquote/table[2]/tbody/tr[6]/td[2]/font"))
llidStatus := parseFloatValue(htmlquery.FindOne(doc, "/html/body/blockquote/table[4]/tbody/tr[3]/td[2]/font/b"))
log.Println("温度:", temperature)
log.Println("电压:", voltage)
log.Println("发送功率:", transmitPower)
log.Println("接收功率:", receivePower)
log.Println("偏置电流:", current)
log.Println("EPON LLID Status:", llidStatus)
temperatureMetrics.WithLabelValues().Set(temperature)
voltageMetrics.WithLabelValues().Set(voltage)
transmitPowerMetrics.WithLabelValues().Set(transmitPower)
receivePowerMetrics.WithLabelValues().Set(receivePower)
currentMetrics.WithLabelValues().Set(current)
llidStatusMetrics.WithLabelValues().Set(llidStatus)
}
func parsePercentValue(node *html.Node) float64 {
value := strings.Split(node.FirstChild.Data, "%")[0]
// 将字符串转换为浮点数
result, err := strconv.ParseFloat(value, 64)
if err != nil {
log.Fatal("转换失败")
}
return result
}
func parseFloatValue(node *html.Node) float64 {
value := strings.Split(node.FirstChild.Data, " ")[0]
// 将字符串转换为浮点数
result, err := strconv.ParseFloat(value, 64)
if err != nil {
log.Fatal("转换失败")
}
return result
}
func getData(url string) (*html.Node, error) {
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
doc, err := htmlquery.Parse(strings.NewReader(string(body)))
if err != nil {
log.Fatal(err)
}
return doc, nil
}