-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathhaproxy.go
More file actions
283 lines (242 loc) · 6.54 KB
/
haproxy.go
File metadata and controls
283 lines (242 loc) · 6.54 KB
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
//go:generate ../../../tools/readme_config_includer/generator
package haproxy
import (
_ "embed"
"encoding/csv"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/common/tls"
"github.com/influxdata/telegraf/plugins/inputs"
)
//go:embed sample.conf
var sampleConfig string
var (
typeNames = []string{"frontend", "backend", "server", "listener"}
fieldRenames = map[string]string{
"pxname": "proxy",
"svname": "sv",
"act": "active_servers",
"bck": "backup_servers",
"cli_abrt": "cli_abort",
"srv_abrt": "srv_abort",
"hrsp_1xx": "http_response.1xx",
"hrsp_2xx": "http_response.2xx",
"hrsp_3xx": "http_response.3xx",
"hrsp_4xx": "http_response.4xx",
"hrsp_5xx": "http_response.5xx",
"hrsp_other": "http_response.other",
}
)
// CSV format: https://cbonte.github.io/haproxy-dconv/1.5/configuration.html#9.1
type HAProxy struct {
Servers []string `toml:"servers"`
KeepFieldNames bool `toml:"keep_field_names"`
Username string `toml:"username"`
Password string `toml:"password"`
tls.ClientConfig
client *http.Client
}
func (*HAProxy) SampleConfig() string {
return sampleConfig
}
func (h *HAProxy) Gather(acc telegraf.Accumulator) error {
if len(h.Servers) == 0 {
return h.gatherServer("http://127.0.0.1:1936/haproxy?stats", acc)
}
endpoints := make([]string, 0, len(h.Servers))
for _, endpoint := range h.Servers {
if strings.HasPrefix(endpoint, "http://") || strings.HasPrefix(endpoint, "https://") || strings.HasPrefix(endpoint, "tcp://") {
endpoints = append(endpoints, endpoint)
continue
}
socketPath := getSocketAddr(endpoint)
matches, err := filepath.Glob(socketPath)
if err != nil {
return err
}
if len(matches) == 0 {
endpoints = append(endpoints, socketPath)
} else {
endpoints = append(endpoints, matches...)
}
}
var wg sync.WaitGroup
wg.Add(len(endpoints))
for _, server := range endpoints {
go func(serv string) {
defer wg.Done()
if err := h.gatherServer(serv, acc); err != nil {
acc.AddError(err)
}
}(server)
}
wg.Wait()
return nil
}
func (h *HAProxy) gatherServerSocket(addr string, acc telegraf.Accumulator) error {
var network, address string
if strings.HasPrefix(addr, "tcp://") {
network = "tcp"
address = strings.TrimPrefix(addr, "tcp://")
} else {
network = "unix"
address = getSocketAddr(addr)
}
c, err := net.Dial(network, address)
if err != nil {
return fmt.Errorf("could not connect to '%s://%s': %w", network, address, err)
}
_, errw := c.Write([]byte("show stat\n"))
if errw != nil {
return fmt.Errorf("could not write to socket '%s://%s': %w", network, address, errw)
}
return h.importCsvResult(c, acc, address)
}
func (h *HAProxy) gatherServer(addr string, acc telegraf.Accumulator) error {
if !strings.HasPrefix(addr, "http") {
return h.gatherServerSocket(addr, acc)
}
if h.client == nil {
tlsCfg, err := h.ClientConfig.TLSConfig()
if err != nil {
return err
}
tr := &http.Transport{
ResponseHeaderTimeout: 3 * time.Second,
TLSClientConfig: tlsCfg,
}
client := &http.Client{
Transport: tr,
Timeout: 4 * time.Second,
}
h.client = client
}
if !strings.HasSuffix(addr, ";csv") {
addr += "/;csv"
}
u, err := url.Parse(addr)
if err != nil {
return fmt.Errorf("unable parse server address %q: %w", addr, err)
}
req, err := http.NewRequest("GET", addr, nil)
if err != nil {
return fmt.Errorf("unable to create new request %q: %w", addr, err)
}
if u.User != nil {
p, _ := u.User.Password()
req.SetBasicAuth(u.User.Username(), p)
u.User = &url.Userinfo{}
addr = u.String()
}
if h.Username != "" || h.Password != "" {
req.SetBasicAuth(h.Username, h.Password)
}
res, err := h.client.Do(req)
if err != nil {
return fmt.Errorf("unable to connect to haproxy server %q: %w", addr, err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
return fmt.Errorf("unable to get valid stat result from %q, http response code : %d", addr, res.StatusCode)
}
if err := h.importCsvResult(res.Body, acc, u.Host); err != nil {
return fmt.Errorf("unable to parse stat result from %q: %w", addr, err)
}
return nil
}
func getSocketAddr(sock string) string {
socketAddr := strings.Split(sock, ":")
if len(socketAddr) >= 2 {
return socketAddr[1]
}
return socketAddr[0]
}
func (h *HAProxy) importCsvResult(r io.Reader, acc telegraf.Accumulator, host string) error {
csvr := csv.NewReader(r)
now := time.Now()
headers, err := csvr.Read()
if err != nil {
return err
}
if len(headers[0]) <= 2 || headers[0][:2] != "# " {
return errors.New("did not receive standard haproxy headers")
}
headers[0] = headers[0][2:]
for {
row, err := csvr.Read()
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return err
}
fields := make(map[string]interface{})
tags := map[string]string{
"server": host,
}
if len(row) != len(headers) {
return fmt.Errorf("number of columns does not match number of headers. headers=%d columns=%d", len(headers), len(row))
}
for i, v := range row {
if v == "" {
continue
}
colName := headers[i]
fieldName := colName
if !h.KeepFieldNames {
if fieldRename, ok := fieldRenames[colName]; ok {
fieldName = fieldRename
}
}
switch colName {
case "pxname", "svname":
tags[fieldName] = v
case "type":
vi, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return fmt.Errorf("unable to parse type value %q", v)
}
if vi >= int64(len(typeNames)) {
return fmt.Errorf("received unknown type value: %d", vi)
}
tags[fieldName] = typeNames[vi]
case "check_desc", "agent_desc":
// do nothing. These fields are just a more verbose description of the check_status & agent_status fields
case "status", "check_status", "last_chk", "mode", "tracked", "agent_status", "last_agt", "addr", "cookie":
// these are string fields
fields[fieldName] = v
case "lastsess":
vi, err := strconv.ParseInt(v, 10, 64)
if err != nil {
// TODO log the error. And just once (per column) so we don't spam the log
continue
}
fields[fieldName] = vi
default:
vi, err := strconv.ParseUint(v, 10, 64)
if err != nil {
// TODO log the error. And just once (per column) so we don't spam the log
continue
}
fields[fieldName] = vi
}
}
acc.AddFields("haproxy", fields, tags, now)
}
return err
}
func init() {
inputs.Add("haproxy", func() telegraf.Input {
return &HAProxy{}
})
}