-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
82 lines (74 loc) · 2.84 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
package main
import (
"aliyun-exporter/exporter"
"flag"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
"github.com/aliyun/alibaba-cloud-sdk-go/services/cdn"
"github.com/aliyun/alibaba-cloud-sdk-go/services/cms"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"log"
"net"
"net/http"
"os"
"strconv"
"strings"
)
var config struct {
accessKeyId string
accessKeySecret string
regionId string
host string
port int
service string
metricsPath string
rangeTime int64
delayTime int64
}
func main() {
flag.StringVar(&(config.accessKeyId), "id", os.Getenv("ACCESS_KEY_ID"), "阿里云AccessKey ID")
flag.StringVar(&(config.accessKeySecret), "secret", os.Getenv("ACCESS_KEY_SECRET"), "阿里云AccessKey Secret")
flag.StringVar(&(config.regionId), "region", os.Getenv("REGIONID"), "阿里云Region ID")
flag.StringVar(&(config.host), "host", "0.0.0.0", "服务监听地址")
flag.IntVar(&(config.port), "port", 9180, "服务监听端口")
flag.StringVar(&(config.service), "service", "acs_cdn", "输出Metrics的服务,默认为全部")
flag.StringVar(&(config.metricsPath), "metricsPath", "/metrics", "metrics path 路径, 默认为 /metrics ")
flag.Int64Var(&(config.rangeTime), "rangeTime", 3600, "时间范围, 开始时间=now-rangeTime")
flag.Int64Var(&(config.delayTime), "delayTime", 180, "时间偏移量, 结束时间=now-delayTime")
flag.Parse()
cdnConfig := sdk.NewConfig()
credential := credentials.NewAccessKeyCredential(config.accessKeyId, config.accessKeySecret)
cdnClient, err := cdn.NewClientWithOptions(config.regionId, cdnConfig, credential)
if err != nil {
log.Fatal("init cdn client error")
}
cmsClient, err := cms.NewClientWithOptions(config.regionId, cdnConfig, credential)
if err != nil {
log.Fatal("init cms client error")
}
serviceArr := strings.Split(config.service, ",")
for _, ae := range serviceArr {
switch ae {
case "acs_cdn":
cdn := exporter.CdnCloudExporter(cmsClient, cdnClient, config.rangeTime, config.delayTime)
prometheus.MustRegister(cdn)
default:
log.Println("暂不支持该服务,请根据提示选择服务。")
}
}
listenAddress := net.JoinHostPort(config.host, strconv.Itoa(config.port))
log.Println(listenAddress)
log.Println("Running on", listenAddress)
http.Handle(config.metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`<html>
<head><title>Aliyun Cloud CDN Exporter</title></head>
<body>
<h1>Aliyun cloud cdn exporter</h1>
<p><a href='` + config.metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
log.Fatal(http.ListenAndServe(listenAddress, nil))
}