-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
145 lines (121 loc) · 4.56 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"flag"
"fmt"
"log"
"net/http"
_ "net/http/pprof"
"os"
"strings"
"time"
"github.com/monitoring-tools/prom-elasticsearch-exporter/collector"
"github.com/monitoring-tools/prom-elasticsearch-exporter/elasticsearch"
"github.com/monitoring-tools/prom-elasticsearch-exporter/encryption"
"github.com/monitoring-tools/prom-elasticsearch-exporter/httpclient"
"github.com/monitoring-tools/prom-elasticsearch-exporter/httpclient/decorator"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const usage = `prom-elasticsearch-exporter - tool for exporting ElasticSearch metrics for Prometheus
Usage:
prom-elasticsearch-exporter [commands|flags]
The commands & flags are:
version print the version to stdout
--web.listen-address address to listen on for web interface and telemetry. Default - :9108
--web.telemetry-path path under which to expose metrics. Default - /metrics
--es.timeout timeout for trying to get stats from ElasticSearch. Default - 5s
--es.uri ElasticSearch node URI. Default - http://localhost:9200
--es.all export stats for all nodes in the cluster. Default - false
--es.ca path to PEM file that conains trusted CAs for the ElasticSearch connection
--es.client-private-key path to PEM file that conains the private key for client auth when connecting to ElasticSearch
--es.client-cert path to PEM file that conains the corresponding cert for the private key to connect to Elasticsearch
`
// Variables passed through ldflags
var (
version string
goVersion string
gitBranch string
)
func main() {
var (
listenAddress = flag.String("web.listen-address", ":9108", "Address to listen on for web interface and telemetry")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics")
esTimeout = flag.Duration("es.timeout", 5*time.Second, "Timeout for trying to get stats from ElasticSearch")
esURI = flag.String("es.uri", "http://localhost:9200", "HTTP API address of an Elasticsearch node")
esAllNodes = flag.Bool("es.all", false, "Export stats for all nodes in the cluster")
esCA = flag.String("es.ca", "", "Path to PEM file that conains trusted CAs for the ElasticSearch connection")
esClientPrivateKey = flag.String("es.client-private-key", "", "Path to PEM file that conains the private key for client auth when connecting to ElasticSearch")
esClientCert = flag.String("es.client-cert", "", "Path to PEM file that conains the corresponding cert for the private key to connect to ElasticSearch")
)
flag.Usage = func() { printUsage() }
flag.Parse()
args := flag.Args()
if len(args) > 0 {
switch args[0] {
case "version":
if version == "" {
println("n/a")
}
fmt.Println(version)
return
case "help":
printUsage()
}
}
// returns nil if not provided and falls back to simple TCP.
tlsConfig := encryption.CreateTLSConfig(*esCA, *esClientCert, *esClientPrivateKey)
httpClient := &http.Client{
Timeout: *esTimeout,
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
},
}
decoratedClient := httpclient.Decorate(
httpClient,
decorator.BaseURLDecorator(*esURI),
decorator.RecoverDecorator(), // better to place it last to recover panics from decorators too
)
prometheus.MustRegister(collector.NewCompositeCollector(
elasticsearch.NewClient(decoratedClient),
*esAllNodes,
version, goVersion, gitBranch,
))
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", IndexHandler(*metricsPath))
log.Println("Listening on:", formatListenAddr(*listenAddress))
if err := http.ListenAndServe(*listenAddress, nil); err != nil {
log.Println("Unable to start HTTP server:", err)
}
}
// IndexHandler returns a http handler with the correct metricsPath
func IndexHandler(metricsPath string) http.HandlerFunc {
indexHTML := `
<html>
<head>
<title>Elasticsearch Exporter</title>
</head>
<body>
<h1>Elasticsearch Exporter</h1>
<p>
<a href='%s'>Metrics</a>
</p>
</body>
</html>
`
index := []byte(fmt.Sprintf(strings.TrimSpace(indexHTML), metricsPath))
return func(w http.ResponseWriter, r *http.Request) {
w.Write(index)
}
}
// formatListenAddr returns formatted UNIX addr
func formatListenAddr(addr string) string {
parts := strings.Split(addr, ":")
if len(parts) == 2 && parts[0] == "" {
addr = fmt.Sprintf("localhost:%s", parts[1])
}
return "http://" + addr
}
func printUsage() {
fmt.Println(usage)
os.Exit(0)
}