forked from antong/prometheus-vmware-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (99 loc) · 2.69 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
package main
import (
"./controller"
"flag"
"fmt"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"net/http"
"os"
)
var (
listen = ":9512"
host = ""
username = ""
password = ""
logLevel = "info"
)
func env(key, def string) string {
if x := os.Getenv(key); x != "" {
return x
}
return def
}
func init() {
flag.StringVar(&listen, "listen", env("ESX_LISTEN", listen), "listen port")
flag.StringVar(&host, "host", env("ESX_HOST", host), "URL ESX host ")
flag.StringVar(&username, "username", env("ESX_USERNAME", username), "User for ESX")
flag.StringVar(&password, "password", env("ESX_PASSWORD", password), "password for ESX")
flag.StringVar(&logLevel, "log", env("ESX_LOG", logLevel), "Log level must be, debug or info")
flag.Parse()
controllers.RegistredMetrics()
collectMetrics()
}
func collectMetrics() {
logger, err := initLogger()
if err != nil {
fmt.Println(err.Error())
}
go func() {
logger.Debugf("Start collect host metrics")
controllers.NewVmwareHostMetrics(host, username, password, logger)
logger.Debugf("End collect host metrics")
}()
go func() {
logger.Debugf("Start collect datastore metrics")
controllers.NewVmwareDsMetrics(host, username, password, logger)
logger.Debugf("End collect datastore metrics")
}()
go func() {
logger.Debugf("Start collect VM metrics")
controllers.NewVmwareVmMetrics(host, username, password, logger)
logger.Debugf("End collect VM metrics")
}()
}
func handler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
collectMetrics()
}
h := promhttp.Handler()
h.ServeHTTP(w, r)
}
func initLogger() (*log.Logger, error) {
logger := log.New()
logrusLogLevel, err := log.ParseLevel(logLevel)
if err != nil {
return logger, err
}
logger.SetLevel(logrusLogLevel)
logger.Formatter = &log.TextFormatter{DisableTimestamp: false, FullTimestamp: true}
return logger, nil
}
func main() {
logger, err := initLogger()
if err != nil {
logger.Fatal(err)
}
if host == "" {
logger.Fatal("Yor must configured systemm env ESX_HOST or key -host")
}
if username == "" {
logger.Fatal("Yor must configured system env ESX_USERNAME or key -username")
}
if password == "" {
logger.Fatal("Yor must configured system env ESX_PASSWORD or key -password")
}
msg := fmt.Sprintf("Exporter start on port %s", listen)
logger.Info(msg)
http.HandleFunc("/metrics", handler)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>VMware Exporter</title></head>
<body>
<h1>VMware Exporter</h1>
<p><a href="` + "/metrics" + `">Metrics</a></p>
</body>
</html>`))
})
logger.Fatal(http.ListenAndServe(listen, nil))
}