forked from EricTsai1107/nutanix_exporter
/
main.go
125 lines (106 loc) · 3.39 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
//
// nutanix-exporter
//
// Prometheus Exportewr for Nutanix API
//
// Origin : Martin Weber <martin.weber@de.clara.net>
// Author : Eric Tsai <crlin.tsai@msa.hinet.net>
// Original Company: Claranet GmbH
// Maintainer : Eric Tsai
//
package main
import (
"flag"
"net/http"
"github.com/PeterRooze/nutanix_exporter/collector"
"github.com/PeterRooze/nutanix_exporter/nutanix"
// "time"
// "regexp"
// "strconv"
"fmt"
"io/ioutil"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/log"
yaml "gopkg.in/yaml.v2"
)
var (
namespace = "nutanix"
nutanixURL = flag.String("nutanix.url", "", "Nutanix URL to connect to API https://nutanix.local.host:9440")
nutanixUser = flag.String("nutanix.username", "", "Nutanix API User")
nutanixPassword = flag.String("nutanix.password", "", "Nutanix API User Password")
listenAddress = flag.String("listen-address", ":9405", "The address to lisiten on for HTTP requests.")
nutanixConfig = flag.String("nutanix.conf", "", "Which Nutanixconf.yml file should be used")
)
type cluster struct {
Host string `yaml:"nutanix_host"`
Username string `yaml:"nutanix_user"`
Password string `yaml:"nutanix_password"`
}
func main() {
flag.Parse()
//Use locale configfile
var config map[string]cluster
var file []byte
var err error
log.Infof("Config: %d", len(*nutanixConfig))
if len(*nutanixConfig) > 0 {
//Read complete Config
file, err = ioutil.ReadFile(*nutanixConfig)
if err != nil {
log.Fatal(err)
}
} else {
file = []byte(fmt.Sprintf("default: {nutanix_host: %s, nutanix_user: %s, nutanix_password: %s}",
*nutanixURL, *nutanixUser, *nutanixPassword))
}
log.Debug(string(file))
err = yaml.Unmarshal(file, &config)
if err != nil {
log.Fatal(err)
}
// http.Handle("/metrics", prometheus.Handler())
http.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
params := r.URL.Query()
section := params.Get("section")
if len(section) == 0 {
section = "default"
}
log.Infof("Queried section: %s", section)
log.Debug("Create Nutanix instance")
//Write new Parameters
if conf, ok := config[section]; ok {
*nutanixURL = conf.Host
*nutanixUser = conf.Username
*nutanixPassword = conf.Password
} else {
log.Errorf("Section '%s' not found in config file", section)
return
}
log.Debugf("Used Host:%s\nUsed username:%s\n", *nutanixURL, *nutanixUser)
nutanixAPI := nutanix.NewNutanix(*nutanixURL, *nutanixUser, *nutanixPassword)
registry := prometheus.NewRegistry()
registry.MustRegister(collector.NewStorageExporter(nutanixAPI))
registry.MustRegister(collector.NewClusterExporter(nutanixAPI))
registry.MustRegister(collector.NewHostExporter(nutanixAPI))
registry.MustRegister(collector.NewVmsExporter(nutanixAPI))
registry.MustRegister(collector.NewDisksExporter(nutanixAPI))
registry.MustRegister(collector.NewVirtualDisksExporter(nutanixAPI))
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Nutanix Exporter</title></head>
<body>
<h1>Nutanix Exporter</h1>
<p><a href="/metrics">Metrics</a></p>
</body>
</html>`))
})
log.Printf("Starting Server: %s", *listenAddress)
err = http.ListenAndServe(*listenAddress, nil)
if err != nil {
log.Fatal(err)
}
}