2015-07-22 23:38:12 +00:00
|
|
|
package haproxy
|
|
|
|
|
|
|
|
import (
|
2016-05-10 18:08:11 +00:00
|
|
|
"encoding/csv"
|
|
|
|
"fmt"
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2015-07-22 23:38:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type haproxy struct {
|
2016-05-10 18:08:11 +00:00
|
|
|
Servers []string
|
2015-07-22 23:38:12 +00:00
|
|
|
|
2016-05-10 18:08:11 +00:00
|
|
|
client *http.Client
|
2015-07-22 23:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## An array of address to gather stats about. Specify an ip on hostname
|
|
|
|
## with optional port. ie localhost, 10.10.3.33:1936, etc.
|
|
|
|
## If no servers are specified, then default to 127.0.0.1:1936
|
2015-10-15 21:53:29 +00:00
|
|
|
servers = ["http://myhaproxy.com:1936", "http://anotherhaproxy.com:1936"]
|
2016-05-04 07:22:34 +00:00
|
|
|
## Or you can also use local socket
|
|
|
|
## servers = ["socket:/run/haproxy/admin.sock"]
|
2015-07-22 23:38:12 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
func (r *haproxy) SampleConfig() string {
|
2016-05-10 18:08:11 +00:00
|
|
|
return sampleConfig
|
2015-07-22 23:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *haproxy) Description() string {
|
2016-05-10 18:08:11 +00:00
|
|
|
return "Read metrics of haproxy, via socket or csv stats page"
|
2015-07-22 23:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reads stats from all configured servers accumulates stats.
|
|
|
|
// Returns one of the errors encountered while gather stats (if any).
|
2016-01-27 21:21:36 +00:00
|
|
|
func (g *haproxy) Gather(acc telegraf.Accumulator) error {
|
2016-05-10 18:08:11 +00:00
|
|
|
if len(g.Servers) == 0 {
|
|
|
|
return g.gatherServer("http://127.0.0.1:1936", acc)
|
|
|
|
}
|
2015-07-22 23:38:12 +00:00
|
|
|
|
2016-05-10 18:08:11 +00:00
|
|
|
var wg sync.WaitGroup
|
2015-07-22 23:38:12 +00:00
|
|
|
|
2016-05-10 18:08:11 +00:00
|
|
|
var outerr error
|
2015-07-22 23:38:12 +00:00
|
|
|
|
2016-05-10 18:08:11 +00:00
|
|
|
for _, serv := range g.Servers {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(serv string) {
|
|
|
|
defer wg.Done()
|
|
|
|
outerr = g.gatherServer(serv, acc)
|
|
|
|
}(serv)
|
|
|
|
}
|
2015-07-22 23:38:12 +00:00
|
|
|
|
2016-05-10 18:08:11 +00:00
|
|
|
wg.Wait()
|
2015-07-22 23:38:12 +00:00
|
|
|
|
2016-05-10 18:08:11 +00:00
|
|
|
return outerr
|
2015-07-22 23:38:12 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 23:17:27 +00:00
|
|
|
func (g *haproxy) gatherServerSocket(addr string, acc telegraf.Accumulator) error {
|
2016-05-10 18:08:11 +00:00
|
|
|
var socketPath string
|
|
|
|
socketAddr := strings.Split(addr, ":")
|
2016-05-03 23:17:27 +00:00
|
|
|
|
2016-05-10 18:08:11 +00:00
|
|
|
if len(socketAddr) >= 2 {
|
|
|
|
socketPath = socketAddr[1]
|
|
|
|
} else {
|
|
|
|
socketPath = socketAddr[0]
|
|
|
|
}
|
2016-05-03 23:17:27 +00:00
|
|
|
|
2016-05-10 18:08:11 +00:00
|
|
|
c, err := net.Dial("unix", socketPath)
|
2016-05-03 23:17:27 +00:00
|
|
|
|
2016-05-10 18:08:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Could not connect to socket '%s': %s", addr, err)
|
|
|
|
}
|
2016-05-03 23:17:27 +00:00
|
|
|
|
2016-05-10 18:08:11 +00:00
|
|
|
_, errw := c.Write([]byte("show stat\n"))
|
2016-05-03 23:17:27 +00:00
|
|
|
|
2016-05-10 18:08:11 +00:00
|
|
|
if errw != nil {
|
|
|
|
return fmt.Errorf("Could not write to socket '%s': %s", addr, errw)
|
|
|
|
}
|
2016-05-03 23:17:27 +00:00
|
|
|
|
2016-05-10 18:08:11 +00:00
|
|
|
return importCsvResult(c, acc, socketPath)
|
2016-05-03 23:17:27 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
func (g *haproxy) gatherServer(addr string, acc telegraf.Accumulator) error {
|
2016-05-10 18:08:11 +00:00
|
|
|
if !strings.HasPrefix(addr, "http") {
|
|
|
|
return g.gatherServerSocket(addr, acc)
|
|
|
|
}
|
|
|
|
|
|
|
|
if g.client == nil {
|
|
|
|
tr := &http.Transport{ResponseHeaderTimeout: time.Duration(3 * time.Second)}
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: tr,
|
|
|
|
Timeout: time.Duration(4 * time.Second),
|
|
|
|
}
|
|
|
|
g.client = client
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(addr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable parse server address '%s': %s", addr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("%s://%s%s/;csv", u.Scheme, u.Host, u.Path), nil)
|
|
|
|
if u.User != nil {
|
|
|
|
p, _ := u.User.Password()
|
|
|
|
req.SetBasicAuth(u.User.Username(), p)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := g.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to connect to haproxy server '%s': %s", addr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.StatusCode != 200 {
|
|
|
|
return fmt.Errorf("Unable to get valid stat result from '%s': %s", addr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return importCsvResult(res.Body, acc, u.Host)
|
2015-07-22 23:38:12 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
func importCsvResult(r io.Reader, acc telegraf.Accumulator, host string) error {
|
2016-05-10 18:08:11 +00:00
|
|
|
csv := csv.NewReader(r)
|
|
|
|
result, err := csv.ReadAll()
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
var keys []string
|
|
|
|
px_sv_status := make(map[string]map[string]int)
|
|
|
|
non_stat_fields := map[string]bool{
|
|
|
|
"pxname": true,
|
|
|
|
"svname": true,
|
|
|
|
"status": true,
|
|
|
|
"tracked": true,
|
|
|
|
"check_status": true,
|
|
|
|
"last_chk": true,
|
|
|
|
"pid": true,
|
|
|
|
"iid": true,
|
|
|
|
"sid": true,
|
|
|
|
"lastchg": true,
|
|
|
|
"type": true,
|
|
|
|
"check_code": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range result {
|
|
|
|
if i == 0 {
|
|
|
|
keys = result[i]
|
|
|
|
keys[0] = strings.Replace(keys[0], "# ", "", -1)
|
|
|
|
} else {
|
|
|
|
|
|
|
|
row := make(map[string]string, len(result[i]))
|
|
|
|
|
|
|
|
for f, v := range result[i] {
|
|
|
|
row[keys[f]] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
tags := map[string]string{
|
|
|
|
"server": host,
|
|
|
|
"proxy": row["pxname"],
|
|
|
|
"sv": row["svname"],
|
|
|
|
}
|
|
|
|
|
|
|
|
if row["svname"] != "BACKEND" && row["svname"] != "FRONTEND" {
|
|
|
|
if len(px_sv_status[row["pxname"]]) == 0 {
|
|
|
|
px_sv_status[row["pxname"]] = map[string]int{
|
|
|
|
"_status_act": 0,
|
|
|
|
"_status_act_up": 0,
|
|
|
|
"_status_act_down": 0,
|
|
|
|
"_status_act_maint": 0,
|
|
|
|
"_status_act_drain": 0,
|
|
|
|
"_status_act_other": 0,
|
|
|
|
"_status_bck": 0,
|
|
|
|
"_status_bck_up": 0,
|
|
|
|
"_status_bck_down": 0,
|
|
|
|
"_status_bck_maint": 0,
|
|
|
|
"_status_bck_drain": 0,
|
|
|
|
"_status_bck_other": 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if row["act"] == "1" {
|
|
|
|
px_sv_status[row["pxname"]]["_status_act"]++
|
|
|
|
switch row["status"] {
|
|
|
|
case "UP":
|
|
|
|
px_sv_status[row["pxname"]]["_status_act_up"]++
|
|
|
|
case "DOWN":
|
|
|
|
px_sv_status[row["pxname"]]["_status_act_down"]++
|
|
|
|
case "MAINT":
|
|
|
|
px_sv_status[row["pxname"]]["_status_act_maint"]++
|
|
|
|
case "DRAIN":
|
|
|
|
px_sv_status[row["pxname"]]["_status_act_drain"]++
|
|
|
|
default:
|
|
|
|
px_sv_status[row["pxname"]]["_status_act_other"]++
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
px_sv_status[row["pxname"]]["_status_bck"]++
|
|
|
|
switch row["status"] {
|
|
|
|
case "UP":
|
|
|
|
px_sv_status[row["pxname"]]["_status_bck_up"]++
|
|
|
|
case "DOWN":
|
|
|
|
px_sv_status[row["pxname"]]["_status_bck_down"]++
|
|
|
|
case "MAINT":
|
|
|
|
px_sv_status[row["pxname"]]["_status_bck_maint"]++
|
|
|
|
case "DRAIN":
|
|
|
|
px_sv_status[row["pxname"]]["_status_bck_drain"]++
|
|
|
|
default:
|
|
|
|
px_sv_status[row["pxname"]]["_status_bck_other"]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if row["svname"] == "BACKEND" && len(px_sv_status[row["pxname"]]) > 0 {
|
|
|
|
for s, c := range px_sv_status[row["pxname"]] {
|
|
|
|
row[s] = strconv.Itoa(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := make(map[string]interface{})
|
|
|
|
for field, v := range row {
|
|
|
|
if non_stat_fields[field] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ival, err := strconv.ParseUint(v, 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
fields[field] = ival
|
|
|
|
}
|
|
|
|
}
|
|
|
|
acc.AddFields("haproxy", fields, tags, now)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
2015-07-22 23:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-05-10 18:08:11 +00:00
|
|
|
inputs.Add("haproxy", func() telegraf.Input {
|
|
|
|
return &haproxy{}
|
|
|
|
})
|
2015-07-22 23:38:12 +00:00
|
|
|
}
|