-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
128 lines (112 loc) · 4.21 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
package main
import (
"flag"
"fmt"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/go-kit/kit/log/level"
"github.com/evergage/elasticsearch_exporter/collector"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/version"
)
func main() {
var (
Name = "elasticsearch_exporter"
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.")
esURI = flag.String("es.uri", "http://localhost:9200", "HTTP API address of an Elasticsearch node.")
esTimeout = flag.Duration("es.timeout", 5*time.Second, "Timeout for trying to get stats from Elasticsearch.")
esAllNodes = flag.Bool("es.all", false, "Export stats for all nodes in the cluster.")
esExportIndices = flag.Bool("es.indices", false, "Export stats for indices in the cluster.")
esExportShards = flag.Bool("es.shards", false, "Export stats for shards in the cluster (implies es.indices=true).")
esCA = flag.String("es.ca", "", "Path to PEM file that contains 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.")
esInsecureSkipVerify = flag.Bool("es.ssl-skip-verify", false, "Skip SSL verification when connecting to Elasticsearch.")
logLevel = flag.String("log.level", "info", "Sets the loglevel. Valid levels are debug, info, warn, error")
logFormat = flag.String("log.format", "logfmt", "Sets the log format. Valid formats are json and logfmt")
logOutput = flag.String("log.output", "stdout", "Sets the log output. Valid outputs are stdout and stderr")
showVersion = flag.Bool("version", false, "Show version and exit")
)
flag.Parse()
if *showVersion {
fmt.Print(version.Print(Name))
os.Exit(0)
}
logger := getLogger(*logLevel, *logOutput, *logFormat)
esURIEnv, ok := os.LookupEnv("ES_URI")
if ok {
*esURI = esURIEnv
}
esURL, err := url.Parse(*esURI)
if err != nil {
level.Error(logger).Log(
"msg", "failed to parse es.uri",
"err", err,
)
os.Exit(1)
}
// returns nil if not provided and falls back to simple TCP.
tlsConfig := createTLSConfig(*esCA, *esClientCert, *esClientPrivateKey, *esInsecureSkipVerify)
httpClient := &http.Client{
Timeout: *esTimeout,
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
},
}
// version metric
versionMetric := version.NewCollector(Name)
prometheus.MustRegister(versionMetric)
clusterHealth := collector.NewClusterHealth(logger, httpClient, esURL)
fetchClusterName := func() string {
clusterName, err := clusterHealth.FetchClusterName()
if err != nil {
level.Error(logger).Log(
"msg", "unable to find cluster name",
"err", err,
)
return ""
}
return clusterName
}
prometheus.MustRegister(clusterHealth)
prometheus.MustRegister(collector.NewNodes(logger, httpClient, esURL, *esAllNodes))
if *esExportIndices || *esExportShards {
prometheus.MustRegister(collector.NewIndices(logger, httpClient, esURL, *esExportShards, fetchClusterName))
}
http.Handle(*metricsPath, prometheus.Handler())
http.HandleFunc("/", IndexHandler(*metricsPath))
level.Info(logger).Log(
"msg", "starting elasticsearch_exporter",
"addr", *listenAddress,
)
if err := http.ListenAndServe(*listenAddress, nil); err != nil {
level.Error(logger).Log(
"msg", "http server quit",
"err", 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)
}
}