forked from antong/prometheus-vmware-exporter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
122 lines (109 loc) · 3.08 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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"prometheus-vmware-exporter/controller"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
var (
listen = ":9879"
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()
controller.RegistredMetrics()
collectMetrics()
}
func collectMetrics() error {
logger, err := initLogger()
if err != nil {
fmt.Println(err.Error())
return err
}
logger.Debugf("Start collect host metrics")
if err := controller.NewVmwareHostMetrics(host, username, password, logger); err != nil {
logger.Errorf("Error collect host metrics")
return err
}
logger.Debugf("End collect host metrics")
logger.Debugf("Start collect VM metrics")
if err := controller.NewVmwareVmMetrics(host, username, password, logger); err != nil {
logger.Errorf("Error collect host metrics")
return err
}
logger.Debugf("End collect VM metrics")
logger.Debugf("Start collect datastore metrics")
if err := controller.NewVmwareDsMetrics(host, username, password, logger); err != nil {
logger.Errorf("Error collect host metrics")
return err
}
logger.Debugf("End collect datastore metrics")
return nil
}
func handler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
if err := collectMetrics(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Error occurred: %s\n", err)))
return
}
}
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))
}