-
Notifications
You must be signed in to change notification settings - Fork 43
/
main.go
137 lines (120 loc) · 4.02 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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/huaweicloud/cloudeye-exporter/collector"
"github.com/huaweicloud/cloudeye-exporter/logs"
)
var (
clientConfig = flag.String("config", "./clouds.yml", "Path to the cloud configuration file")
// 安全模式,从用户交互输入获取ak/sk,避免明文ak/sk敏感信息存储在配置文件中
securityMod = flag.Bool("s", false, "Get ak sk from command line")
// 以https协议启动cloudeye-exporter,需要从用户交互输入获取ca证书路径,服务端https证书路径,服务端私钥路径以及私钥密码
httpsEnabled = flag.Bool("k", false, "Start the cloudeye exporter service in https mode")
getVersion = flag.Bool("v", false, "Get version from command line")
proxyEnabled = flag.Bool("p", false, "Start the cloudeye exporter service and start the proxy")
ak, sk, proxyUserName, proxyPassword string
)
func handler(w http.ResponseWriter, r *http.Request) {
target := r.URL.Query().Get("services")
if target == "" {
http.Error(w, "'target' parameter must be specified", 400)
return
}
targets := strings.Split(target, ",")
if len(targets) > collector.MaxNamespacesCount {
http.Error(w, "namespaces not allowed to exceed 1000", 400)
return
}
registry := prometheus.NewRegistry()
logs.Logger.Infof("Start to monitor services: %s", targets)
exporter := collector.GetMonitoringCollector(targets)
registry.MustRegister(exporter)
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
logs.Logger.Infof("End to monitor services: %s", targets)
}
func epHandler(w http.ResponseWriter, r *http.Request) {
epsInfo, err := collector.GetEPSInfo()
if err != nil {
http.Error(w, fmt.Sprintf("get eps info error: %s", err.Error()), http.StatusInternalServerError)
return
}
_, err = w.Write([]byte(epsInfo))
if err != nil {
logs.Logger.Errorf("Response to caller error: %s", err.Error())
}
}
func getAkSkFromCommandLine() {
if *securityMod {
collector.SecurityMod = *securityMod
// 用户交互输入ak/sk,避免明文配置敏感信息
fmt.Print("Please input ak&sk split with space(eg: {example_ak example_sk}): \n")
_, err := fmt.Scanln(&ak, &sk)
if err != nil {
fmt.Printf("Read ak sk error: %s", err.Error())
return
}
collector.TmpAK = ak
collector.TmpSK = sk
}
}
func getProxyInfoFromCommandLine() {
if *proxyEnabled {
collector.ProxyEnabled = *proxyEnabled
// 用户交互输入代理userName/password,避免明文配置敏感信息
fmt.Print("Please input proxy userName&proxy password split with space(eg: {example_proxy_user_name example_proxy_password}): \n")
_, err := fmt.Scanln(&proxyUserName, &proxyPassword)
if err != nil {
fmt.Printf("Read proxy info error: %s", err.Error())
return
}
collector.TmpProxyUserName = proxyUserName
collector.TmpProxyPassword = proxyPassword
}
}
func getHttpsEnabledFromCommandLine() {
if *httpsEnabled {
collector.HttpsEnabled = *httpsEnabled
}
}
func getVersionFunc() {
if *getVersion {
fmt.Printf("Cloudeye-exporter version: %s", collector.Version)
os.Exit(0)
}
}
func main() {
flag.Parse()
getVersionFunc()
getAkSkFromCommandLine()
getHttpsEnabledFromCommandLine()
getProxyInfoFromCommandLine()
initConf()
initCache()
http.HandleFunc(collector.CloudConf.Global.MetricPath, handler)
http.HandleFunc(collector.CloudConf.Global.EpsInfoPath, epHandler)
collector.StartServer()
}
func initConf() {
err := collector.InitCloudConf(*clientConfig)
if err != nil {
fmt.Printf("Init Cloud Config From File error: %s", err.Error())
os.Exit(1)
}
logs.InitLog(collector.CloudConf.Global.LogsConfPath)
err = collector.InitMetricConf()
if err != nil {
logs.Logger.Errorf("Init metric Config error: %s", err.Error())
logs.FlushLogAndExit(1)
}
collector.InitEndpointConfig(collector.CloudConf.Global.EndpointsConfPath)
}
func initCache() {
collector.GetAgentDimensionRefresher().RefreshAgentDimensionWithInterval()
}