-
Notifications
You must be signed in to change notification settings - Fork 40
/
main.go
104 lines (92 loc) · 2.76 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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"strings"
"time"
"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")
securityMod = flag.Bool("s", false, "Get ak sk from command line")
getVersion = flag.Bool("v", false, "Get version from command line")
ak, sk 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, ",")
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
fmt.Print("Please input ak&sk split with space: (eg: {example_ak example_sk})")
_, err := fmt.Scanln(&ak, &sk)
if err != nil {
logs.Logger.Error("Read ak sk error: ", err.Error())
return
}
collector.TmpAK = ak
collector.TmpSK = sk
}
}
func getVersionFunc() {
if *getVersion {
fmt.Printf("cloudeye-exporter version: %s", collector.Version)
os.Exit(0)
}
}
func main() {
flag.Parse()
getVersionFunc()
logs.InitLog()
getAkSkFromCommandLine()
err := collector.InitCloudConf(*clientConfig)
if err != nil {
logs.Logger.Error("Init Cloud Config From File error: ", err.Error())
logs.FlushLogAndExit(1)
}
err = collector.InitMetricConf()
if err != nil {
logs.Logger.Error("Init metric Config error: ", err.Error())
logs.FlushLogAndExit(1)
}
http.HandleFunc(collector.CloudConf.Global.MetricPath, handler)
http.HandleFunc(collector.CloudConf.Global.EpsInfoPath, epHandler)
server := &http.Server{
Addr: collector.CloudConf.Global.Port,
ReadTimeout: 30 * time.Second,
WriteTimeout: 60 * time.Second,
}
logs.Logger.Info("Start server at ", collector.CloudConf.Global.Port)
if err := server.ListenAndServe(); err != nil {
logs.Logger.Errorf("Error occur when start server %s", err.Error())
logs.FlushLogAndExit(1)
}
}