-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
90 lines (77 loc) · 2.5 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
package main
import (
"flag"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/ucloud/ucloud-sdk-go/services/ucdn"
"github.com/ucloud/ucloud-sdk-go/ucloud"
"github.com/ucloud/ucloud-sdk-go/ucloud/auth"
"log"
"net"
"net/http"
"os"
"strconv"
"time"
"ucloud-exporter/collector"
"ucloud-exporter/exporter"
)
const (
CONFIG_URL = "https://api.ucloud.cn"
)
var (
domainList []ucdn.DomainBaseInfo
)
func FetchDomainList(projectID string, client *ucdn.UCDNClient) {
domainList = collector.RetrieveInfoList(projectID, client).DomainInfoList
if len(domainList) == 0{
log.Fatal("domainList is empty")
}
}
func main() {
publicKey := flag.String("pubkey", os.Getenv("UCLOUD_PUB_KEY"), "ucloud api public key")
privateKey := flag.String("privatekey", os.Getenv("UCLOUD_PRIVATE_KEY"), "ucloud api private key")
projectid := flag.String("projectId", os.Getenv("ucloud_project_id"), "confirm the project")
host := flag.String("host", "0.0.0.0", "服务监听地址")
port := flag.Int("port", 9200, "服务监听端口")
rangeTime := flag.Int64("rangeTime", 3000, "rangeTime")
delayTime := flag.Int64("delayTime", 600, "delayTime")
tickerTime := flag.Int("tickerTime", 10, "tickerTime")
metricsPath := flag.String("metricsPath", "/metrics", "default metrics path")
flag.Parse()
cfg := ucloud.NewConfig()
cfg.BaseUrl = CONFIG_URL
cred := auth.NewCredential()
cred.PublicKey = *publicKey
cred.PrivateKey = *privateKey
projectId := *projectid
uCdnClient := ucdn.NewClient(&cfg, &cred)
FetchDomainList(projectId, uCdnClient)
ticker := time.NewTicker(time.Duration(*tickerTime) * time.Second)
done := make(chan bool)
go func() {
for {
select {
case <-done:
return
case <-ticker.C:
FetchDomainList(projectId, uCdnClient)
}
}
}()
cdn := exporter.CdnCloudExporter(&domainList, projectId, *rangeTime, *delayTime, uCdnClient)
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>UCloud CDN Exporter</title></head>
<body>
<h1>UCloud cdn exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
log.Fatal(http.ListenAndServe(listenAddress, nil))
}