forked from prometheus/haproxy_exporter
/
haproxy_exporter.go
398 lines (362 loc) · 14.7 KB
/
haproxy_exporter.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package main
import (
"encoding/csv"
"flag"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
_ "net/http/pprof"
"strconv"
"strings"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/log"
)
const (
namespace = "haproxy" // For Prometheus metrics.
// HAProxy 1.4
// # pxname,svname,qcur,qmax,scur,smax,slim,stot,bin,bout,dreq,dresp,ereq,econ,eresp,wretr,wredis,status,weight,act,bck,chkfail,chkdown,lastchg,downtime,qlimit,pid,iid,sid,throttle,lbtot,tracked,type,rate,rate_lim,rate_max,check_status,check_code,check_duration,hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,req_rate,req_rate_max,req_tot,cli_abrt,srv_abrt,
// HAProxy 1.5
// pxname,svname,qcur,qmax,scur,smax,slim,stot,bin,bout,dreq,dresp,ereq,econ,eresp,wretr,wredis,status,weight,act,bck,chkfail,chkdown,lastchg,downtime,qlimit,pid,iid,sid,throttle,lbtot,tracked,type,rate,rate_lim,rate_max,check_status,check_code,check_duration,hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,req_rate,req_rate_max,req_tot,cli_abrt,srv_abrt,comp_in,comp_out,comp_byp,comp_rsp,lastsess,
expectedCsvFieldCount = 52
)
var (
frontendLabelNames = []string{"frontend"}
backendLabelNames = []string{"backend"}
serverLabelNames = []string{"backend", "server"}
)
func newFrontendMetric(metricName string, docString string, constLabels prometheus.Labels) *prometheus.GaugeVec {
return prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "frontend_" + metricName,
Help: docString,
ConstLabels: constLabels,
},
frontendLabelNames,
)
}
func newBackendMetric(metricName string, docString string, constLabels prometheus.Labels) *prometheus.GaugeVec {
return prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "backend_" + metricName,
Help: docString,
ConstLabels: constLabels,
},
backendLabelNames,
)
}
func newServerMetric(metricName string, docString string, constLabels prometheus.Labels) *prometheus.GaugeVec {
return prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "server_" + metricName,
Help: docString,
ConstLabels: constLabels,
},
serverLabelNames,
)
}
// Exporter collects HAProxy stats from the given URI and exports them using
// the prometheus metrics package.
type Exporter struct {
URI string
mutex sync.RWMutex
up prometheus.Gauge
totalScrapes, csvParseFailures prometheus.Counter
frontendMetrics, backendMetrics, serverMetrics map[int]*prometheus.GaugeVec
client *http.Client
}
// NewExporter returns an initialized Exporter.
func NewExporter(uri string, haProxyServerMetricFields string, timeout time.Duration) *Exporter {
serverMetrics := map[int]*prometheus.GaugeVec{}
serverMetricFields := make(map[int]bool)
if haProxyServerMetricFields != "" {
for _, f := range strings.Split(haProxyServerMetricFields, ",") {
field, err := strconv.Atoi(f)
if err != nil {
log.Fatalf("Invalid field number: %v", f)
}
serverMetricFields[field] = true
}
}
for index, metric := range map[int]*prometheus.GaugeVec{
4: newServerMetric("current_sessions", "Current number of active sessions.", nil),
5: newServerMetric("max_sessions", "Maximum number of active sessions.", nil),
7: newServerMetric("connections_total", "Total number of connections.", nil),
8: newServerMetric("bytes_in_total", "Current total of incoming bytes.", nil),
9: newServerMetric("bytes_out_total", "Current total of outgoing bytes.", nil),
13: newServerMetric("connection_errors_total", "Total of connection errors.", nil),
14: newServerMetric("response_errors_total", "Total of response errors.", nil),
15: newServerMetric("retry_warnings_total", "Total of retry warnings.", nil),
16: newServerMetric("redispatch_warnings_total", "Total of redispatch warnings.", nil),
17: newServerMetric("up", "Current health status of the server (1 = UP, 0 = DOWN).", nil),
18: newServerMetric("weight", "Current weight of the server.", nil),
33: newServerMetric("current_session_rate", "Current number of sessions per second over last elapsed second.", nil),
35: newServerMetric("max_session_rate", "Maximum number of sessions per second.", nil),
39: newServerMetric("http_responses_total", "Total of HTTP responses.", prometheus.Labels{"code": "1xx"}),
40: newServerMetric("http_responses_total", "Total of HTTP responses.", prometheus.Labels{"code": "2xx"}),
41: newServerMetric("http_responses_total", "Total of HTTP responses.", prometheus.Labels{"code": "3xx"}),
42: newServerMetric("http_responses_total", "Total of HTTP responses.", prometheus.Labels{"code": "4xx"}),
43: newServerMetric("http_responses_total", "Total of HTTP responses.", prometheus.Labels{"code": "5xx"}),
44: newServerMetric("http_responses_total", "Total of HTTP responses.", prometheus.Labels{"code": "other"}),
} {
if len(serverMetricFields) == 0 || serverMetricFields[index] {
serverMetrics[index] = metric
}
}
return &Exporter{
URI: uri,
up: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "up",
Help: "Was the last scrape of haproxy successful.",
}),
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "exporter_total_scrapes",
Help: "Current total HAProxy scrapes.",
}),
csvParseFailures: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "exporter_csv_parse_failures",
Help: "Number of errors while parsing CSV.",
}),
frontendMetrics: map[int]*prometheus.GaugeVec{
4: newFrontendMetric("current_sessions", "Current number of active sessions.", nil),
5: newFrontendMetric("max_sessions", "Maximum number of active sessions.", nil),
7: newFrontendMetric("connections_total", "Total number of connections.", nil),
8: newFrontendMetric("bytes_in_total", "Current total of incoming bytes.", nil),
9: newFrontendMetric("bytes_out_total", "Current total of outgoing bytes.", nil),
10: newFrontendMetric("requests_denied_total", "Total of requests denied for security.", nil),
12: newFrontendMetric("request_errors_total", "Total of request errors.", nil),
33: newFrontendMetric("current_session_rate", "Current number of sessions per second over last elapsed second.", nil),
35: newFrontendMetric("max_session_rate", "Maximum number of sessions per second.", nil),
39: newFrontendMetric("http_responses_total", "Total of HTTP responses.", prometheus.Labels{"code": "1xx"}),
40: newFrontendMetric("http_responses_total", "Total of HTTP responses.", prometheus.Labels{"code": "2xx"}),
41: newFrontendMetric("http_responses_total", "Total of HTTP responses.", prometheus.Labels{"code": "3xx"}),
42: newFrontendMetric("http_responses_total", "Total of HTTP responses.", prometheus.Labels{"code": "4xx"}),
43: newFrontendMetric("http_responses_total", "Total of HTTP responses.", prometheus.Labels{"code": "5xx"}),
44: newFrontendMetric("http_responses_total", "Total of HTTP responses.", prometheus.Labels{"code": "other"}),
48: newFrontendMetric("http_requests_total", "Total HTTP requests.", nil),
},
backendMetrics: map[int]*prometheus.GaugeVec{
2: newBackendMetric("current_queue", "Current server queue length.", nil),
3: newBackendMetric("max_queue", "Maximum server queue length.", nil),
4: newBackendMetric("current_sessions", "Current number of active sessions.", nil),
5: newBackendMetric("max_sessions", "Maximum number of active sessions.", nil),
7: newBackendMetric("connections_total", "Total number of connections.", nil),
8: newBackendMetric("bytes_in_total", "Current total of incoming bytes.", nil),
9: newBackendMetric("bytes_out_total", "Current total of outgoing bytes.", nil),
13: newBackendMetric("connection_errors_total", "Total of connection errors.", nil),
14: newBackendMetric("response_errors_total", "Total of response errors.", nil),
15: newBackendMetric("retry_warnings_total", "Total of retry warnings.", nil),
16: newBackendMetric("redispatch_warnings_total", "Total of redispatch warnings.", nil),
17: newBackendMetric("up", "Current health status of the backend (1 = UP, 0 = DOWN).", nil),
18: newBackendMetric("weight", "Total weight of the servers in the backend.", nil),
33: newBackendMetric("current_session_rate", "Current number of sessions per second over last elapsed second.", nil),
35: newBackendMetric("max_session_rate", "Maximum number of sessions per second.", nil),
39: newBackendMetric("http_responses_total", "Total of HTTP responses.", prometheus.Labels{"code": "1xx"}),
40: newBackendMetric("http_responses_total", "Total of HTTP responses.", prometheus.Labels{"code": "2xx"}),
41: newBackendMetric("http_responses_total", "Total of HTTP responses.", prometheus.Labels{"code": "3xx"}),
42: newBackendMetric("http_responses_total", "Total of HTTP responses.", prometheus.Labels{"code": "4xx"}),
43: newBackendMetric("http_responses_total", "Total of HTTP responses.", prometheus.Labels{"code": "5xx"}),
44: newBackendMetric("http_responses_total", "Total of HTTP responses.", prometheus.Labels{"code": "other"}),
},
serverMetrics: serverMetrics,
client: &http.Client{
Transport: &http.Transport{
Dial: func(netw, addr string) (net.Conn, error) {
c, err := net.DialTimeout(netw, addr, timeout)
if err != nil {
return nil, err
}
if err := c.SetDeadline(time.Now().Add(timeout)); err != nil {
return nil, err
}
return c, nil
},
},
},
}
}
// Describe describes all the metrics ever exported by the HAProxy exporter. It
// implements prometheus.Collector.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
for _, m := range e.frontendMetrics {
m.Describe(ch)
}
for _, m := range e.backendMetrics {
m.Describe(ch)
}
for _, m := range e.serverMetrics {
m.Describe(ch)
}
ch <- e.up.Desc()
ch <- e.totalScrapes.Desc()
ch <- e.csvParseFailures.Desc()
}
// Collect fetches the stats from configured HAProxy location and delivers them
// as Prometheus metrics. It implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
csvRows := make(chan []string)
go e.scrape(csvRows)
e.mutex.Lock() // To protect metrics from concurrent collects.
defer e.mutex.Unlock()
e.resetMetrics()
e.setMetrics(csvRows)
ch <- e.up
ch <- e.totalScrapes
ch <- e.csvParseFailures
e.collectMetrics(ch)
}
func (e *Exporter) scrape(csvRows chan<- []string) {
defer close(csvRows)
e.totalScrapes.Inc()
resp, err := e.client.Get(e.URI)
if err != nil {
e.up.Set(0)
log.Printf("Error while scraping HAProxy: %v", err)
return
}
defer resp.Body.Close()
e.up.Set(1)
reader := csv.NewReader(resp.Body)
reader.TrailingComma = true
reader.Comment = '#'
for {
row, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
log.Printf("Error while reading CSV: %v", err)
e.csvParseFailures.Inc()
break
}
if len(row) == 0 {
continue
}
csvRows <- row
}
}
func (e *Exporter) resetMetrics() {
for _, m := range e.frontendMetrics {
m.Reset()
}
for _, m := range e.backendMetrics {
m.Reset()
}
for _, m := range e.serverMetrics {
m.Reset()
}
}
func (e *Exporter) collectMetrics(metrics chan<- prometheus.Metric) {
for _, m := range e.frontendMetrics {
m.Collect(metrics)
}
for _, m := range e.backendMetrics {
m.Collect(metrics)
}
for _, m := range e.serverMetrics {
m.Collect(metrics)
}
}
func (e *Exporter) setMetrics(csvRows <-chan []string) {
for csvRow := range csvRows {
if len(csvRow) < expectedCsvFieldCount {
log.Printf("Wrong CSV field count: %d vs. %d", len(csvRow), expectedCsvFieldCount)
e.csvParseFailures.Inc()
continue
}
pxname, svname, type_ := csvRow[0], csvRow[1], csvRow[32]
const (
frontend = "0"
backend = "1"
server = "2"
listener = "3"
)
switch type_ {
case frontend:
e.exportCsvFields(e.frontendMetrics, csvRow, pxname)
case backend:
e.exportCsvFields(e.backendMetrics, csvRow, pxname)
case server:
e.exportCsvFields(e.serverMetrics, csvRow, pxname, svname)
}
}
}
func (e *Exporter) exportCsvFields(metrics map[int]*prometheus.GaugeVec, csvRow []string, labels ...string) {
for fieldIdx, metric := range metrics {
valueStr := csvRow[fieldIdx]
if valueStr == "" {
continue
}
var value int64
var err error
switch valueStr {
// UP or UP going down
case "UP", "UP 1/3", "UP 2/3":
value = 1
// DOWN or DOWN going up
case "DOWN", "DOWN 1/2":
value = 0
case "OPEN":
value = 0
case "no check":
continue
default:
value, err = strconv.ParseInt(valueStr, 10, 64)
if err != nil {
log.Printf("Error while parsing CSV field value %s: %v", valueStr, err)
e.csvParseFailures.Inc()
continue
}
}
metric.WithLabelValues(labels...).Set(float64(value))
}
}
func main() {
var (
listenAddress = flag.String("web.listen-address", ":9101", "Address to listen on for web interface and telemetry.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
haProxyScrapeUri = flag.String("haproxy.scrape-uri", "http://localhost/;csv", "URI on which to scrape HAProxy.")
haProxyServerMetricFields = flag.String("haproxy.server-metric-fields", "", "If specified, only export the given csv fields. Comma-seperated list of numbers. See http://cbonte.github.io/haproxy-dconv/configuration-1.5.html#9.1")
haProxyTimeout = flag.Duration("haproxy.timeout", 5*time.Second, "Timeout for trying to get stats from HAProxy.")
haProxyPidFile = flag.String("haproxy.pid-file", "", "Path to haproxy's pid file.")
)
flag.Parse()
exporter := NewExporter(*haProxyScrapeUri, *haProxyServerMetricFields, *haProxyTimeout)
prometheus.MustRegister(exporter)
if *haProxyPidFile != "" {
procExporter := prometheus.NewProcessCollectorPIDFn(
func() (int, error) {
content, err := ioutil.ReadFile(*haProxyPidFile)
if err != nil {
return 0, fmt.Errorf("error reading pid file: %s", err)
}
value, err := strconv.Atoi(strings.TrimSpace(string(content)))
if err != nil {
return 0, fmt.Errorf("error parsing pid file: %s", err)
}
return value, nil
}, namespace)
prometheus.MustRegister(procExporter)
}
log.Printf("Starting Server: %s", *listenAddress)
http.Handle(*metricsPath, prometheus.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Haproxy Exporter</title></head>
<body>
<h1>Haproxy Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}