-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmain.go
102 lines (86 loc) · 2.24 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
package main
import (
"crypto/tls"
"flag"
"net"
"net/http"
"net/url"
"time"
"github.com/matt-deboer/go-marathon"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
)
var (
listenAddress = flag.String(
"web.listen-address", ":9088",
"Address to listen on for web interface and telemetry.")
metricsPath = flag.String(
"web.telemetry-path", "/metrics",
"Path under which to expose metrics.")
marathonUri = flag.String(
"marathon.uri", "http://marathon.mesos:8080",
"URI of Marathon")
)
func marathonConnect(uri *url.URL) error {
config := marathon.NewDefaultConfig()
config.URL = uri.String()
if uri.User != nil {
if passwd, ok := uri.User.Password(); ok {
config.HTTPBasicPassword = passwd
config.HTTPBasicAuthUser = uri.User.Username()
}
}
config.HTTPClient = &http.Client{
Timeout: 10 * time.Second,
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: 10 * time.Second,
}).Dial,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
log.Debugln("Connecting to Marathon")
client, err := marathon.NewClient(config)
if err != nil {
return err
}
info, err := client.Info()
if err != nil {
return err
}
log.Debugf("Connected to Marathon! Name=%s, Version=%s\n", info.Name, info.Version)
return nil
}
func main() {
flag.Parse()
uri, err := url.Parse(*marathonUri)
if err != nil {
log.Fatal(err)
}
retryTimeout := time.Duration(10 * time.Second)
for {
err := marathonConnect(uri)
if err == nil {
break
}
log.Debugf("Problem connecting to Marathon: %v", err)
log.Infof("Couldn't connect to Marathon! Trying again in %v", retryTimeout)
time.Sleep(retryTimeout)
}
exporter := NewExporter(&scraper{uri}, defaultNamespace)
prometheus.MustRegister(exporter)
http.Handle(*metricsPath, prometheus.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Marathon Exporter</title></head>
<body>
<h1>Marathon Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
log.Info("Starting Server: ", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}