-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
65 lines (58 loc) · 1.94 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
package main
import (
"flag"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"log"
"net"
"net/http"
"os"
"strconv"
"time"
"upyun-exporter/exporter"
"upyun-exporter/httpRequest"
)
var domainList []string
func FetchDomainList(token string) {
domainList = httpRequest.DoDomainListRequest(token)
}
func main() {
bucketToken := flag.String("bucket_token", os.Getenv("UpYun_Bucket_Token"), "upYun bucket token")
token := flag.String("token", os.Getenv("UpYun_Token"), "upYun token")
host := flag.String("host", "0.0.0.0", "服务监听地址")
port := flag.Int("port", 9300, "服务监听端口")
delayTime := flag.Int64("delayTime", 300, "时间偏移量, 结束时间=now-delay_seconds")
rangeTime := flag.Int64("rangeTime", 1800, "选取时间范围, 开始时间=now-range_seconds, 结束时间=now")
tickerTime := flag.Int("tickerTime", 3600, "刷新域名列表间隔时间")
metricsPath := flag.String("metricsPath", "/metrics", "默认的metrics路径")
flag.Parse()
ticker := time.NewTicker(time.Duration(*tickerTime) * time.Second)
done := make(chan bool)
FetchDomainList(*bucketToken)
go func() {
for {
select {
case <-done:
return
case <-ticker.C:
FetchDomainList(*bucketToken)
}
}
}()
cdn := exporter.CdnCloudExporter(&domainList, *token, *rangeTime, *delayTime)
prometheus.MustRegister(cdn)
listenAddress := net.JoinHostPort(*host, strconv.Itoa(*port))
log.Println(listenAddress)
log.Println("Running on", listenAddress)
http.Handle(*metricsPath, promhttp.Handler()) //注册
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`<html>
<head><title>UPYUN CDN Exporter</title></head>
<body>
<h1>Upyun cdn exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
log.Fatal(http.ListenAndServe(listenAddress, nil))
}