-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
134 lines (112 loc) · 3.3 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
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/vad/elasticsearch_exporter/collectors"
"github.com/vad/elasticsearch_exporter/parser"
)
type Metrics []parser.Metric
var (
es = flag.String("es", "http://localhost:9200", "ES URL")
bind = flag.String("bind", ":9092", "Address to bind to")
timeInterval = flag.Int("time", 5, "Time interval between scrape runs, in seconds")
username = flag.String("username", "", "Username when XPack security is enabled")
password = flag.String("password", "", "Password for the user when XPack security is enabled")
enableSiren = flag.Bool("enable-siren", false, "Enable Siren Federate Plugin scraping")
up = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "es_up",
Help: "Current status of ES",
})
sirenUp = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "es_up",
Help: "Current status of Siren Federate",
})
client = &http.Client{
Timeout: time.Second * 10,
}
)
func init() {
flag.Parse()
log.Println(fmt.Sprintf("Scraping %s every %d seconds", *es, *timeInterval))
if *timeInterval < 1 {
log.Fatal("Time interval must be >= 1")
}
}
func queryEs(url string) (*http.Response, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
if len(*username) > 0 && len(*password) > 0 {
req.SetBasicAuth(*username, *password)
}
return client.Do(req)
}
func scrape(ns string, upMetric prometheus.Gauge, metrics []parser.Metric) {
resp, err := queryEs(ns)
if err != nil {
upMetric.Set(0)
log.Println(err.Error())
return
}
defer resp.Body.Close()
v, err := parser.NewNodeStatsJson(resp.Body)
if err != nil {
upMetric.Set(0)
log.Println("Error decoding ES JSON:", err.Error())
return
}
for nodeName, jobject := range v.Nodes {
var object interface{}
err := json.Unmarshal(*jobject, &object)
if err != nil {
log.Println("Error decoding JSON for node", nodeName, ":", err.Error())
continue
}
for _, metric := range metrics {
err := metric.Observe(object)
if err != nil {
log.Println("Error observing metric from '", metric.String(), "' ", err.Error())
}
}
}
upMetric.Set(1)
}
func scrapeForever(endpoint string, up prometheus.Gauge, metrics []parser.Metric) {
scrapeUrl := strings.TrimRight(*es, "/") + endpoint
t := time.NewTicker(time.Duration(*timeInterval) * time.Second)
for range t.C {
scrape(scrapeUrl, up, metrics)
}
}
func main() {
// plain es
nm := []*parser.NodeMetric{
parser.NewRawMetric("indices.recovery.throttle_time_in_millis"),
}
nodeMetrics := make([]parser.Metric, len(nm))
prometheus.MustRegister(collectors.NewClusterHealthCollector(*es, *username, *password))
for i, metric := range nm {
prometheus.MustRegister(metric.Gauge)
nodeMetrics[i] = metric
}
go scrapeForever("/_nodes/stats", up, nodeMetrics)
// siren
if *enableSiren {
m := parser.NewSirenMemoryMetric()
prometheus.MustRegister(m.Peak)
prometheus.MustRegister(m.Limit)
sirenMetrics := []parser.Metric{m}
go scrapeForever("/_siren/nodes/stats", sirenUp, sirenMetrics)
}
http.Handle("/metrics", promhttp.Handler())
log.Println("Listen on address", *bind)
log.Fatal(http.ListenAndServe(*bind, nil))
}