-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
56 lines (45 loc) · 1.83 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
package main
import (
"net/http"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
)
var (
listenAddress = kingpin.Flag("web.listen-address", "Address on which to expose metrics and web interface.").Default(":9444").String()
metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose Prometheus metrics.").Default("/metrics").String()
emqURL = kingpin.Flag("emq.uri", "HTTP API address of the EMQ node.").Default("http://127.0.0.1:8080").URL()
emqUsername = kingpin.Flag("emq.username", "EMQ username.").Default("admin").String()
emqPassword = kingpin.Flag("emq.password", "EMQ password.").Default("public").String()
emqNodeName = kingpin.Flag("emq.node", "Node name of the emq node to scrape.").Default("[email protected]").String()
)
func init() {
prometheus.MustRegister(version.NewCollector("emq_exporter"))
}
func main() {
log.AddFlags(kingpin.CommandLine)
kingpin.Version(version.Print("emq_exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
log.Infoln("Starting emq_exporter", version.Info())
log.Infoln("Build context", version.BuildContext())
httpClient := &http.Client{}
nodeName := *emqNodeName
username := *emqUsername
password := *emqPassword
prometheus.MustRegister(NewEMQCollector(httpClient, emqURL, nodeName, username, password))
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>EMQ Exporter</title></head>
<body>
<h1>EMQ Exporter</h1>
<p><a href="` + *metricsPath + `">Metrics</a></p>
</body>
</html>`))
})
log.Infoln("Listening on", *listenAddress)
http.ListenAndServe(*listenAddress, nil)
}