/
main.go
104 lines (83 loc) · 2.51 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
package main
import (
"encoding/csv"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"time"
"flag"
"github.com/samuel/go-librato/librato"
)
var pollIntervalSeconds int
func parseField(data string) (value float64) {
value, err := strconv.ParseFloat(data, 64)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return value
}
func addGauge(gauges []interface{}, source string, prefix string, name string, data string) (result []interface{}) {
if data == "" {
return gauges
}
name = strings.Join([]string{prefix, name}, ".")
return append(gauges, librato.Metric{Source: source, Name: name, Value: parseField(data)})
}
func poll(client librato.Client) {
resp, err := http.Get(os.Getenv("HAPROXY_URL"))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer resp.Body.Close()
reader := csv.NewReader(resp.Body)
reader.Comment = '#'
reader.FieldsPerRecord = -1
data, err := reader.ReadAll()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
gauges := make([]interface{}, 0)
for _, row := range data {
source := os.Getenv("LIBRATO_SOURCE")
prefix := strings.Join([]string{"haproxy", row[0]}, ".")
switch row[1] {
case "BACKEND":
prefix = strings.Join([]string{prefix, "backend"}, ".")
case "FRONTEND":
prefix = strings.Join([]string{prefix, "frontend"}, ".")
default:
source = strings.Join([]string{source, row[1]}, ".")
prefix = strings.Join([]string{prefix, "upstream"}, ".")
}
gauges = addGauge(gauges, source, prefix, "qcur", row[2])
gauges = addGauge(gauges, source, prefix, "qmax", row[3])
gauges = addGauge(gauges, source, prefix, "scur", row[4])
gauges = addGauge(gauges, source, prefix, "smax", row[5])
gauges = addGauge(gauges, source, prefix, "downtime", row[24])
gauges = addGauge(gauges, source, prefix, "hrsp_1xx", row[39])
gauges = addGauge(gauges, source, prefix, "hrsp_2xx", row[40])
gauges = addGauge(gauges, source, prefix, "hrsp_3xx", row[41])
gauges = addGauge(gauges, source, prefix, "hrsp_4xx", row[42])
gauges = addGauge(gauges, source, prefix, "hrsp_5xx", row[43])
}
metrics := &librato.Metrics{Gauges: gauges}
err = client.PostMetrics(metrics)
if err != nil {
fmt.Println(err)
}
}
func main() {
flag.IntVar(&pollIntervalSeconds, "poll-interval", 30, "the interval in seconds at which stats will be sent to librato")
flag.Parse()
println(pollIntervalSeconds)
client := librato.Client{os.Getenv("LIBRATO_USER"), os.Getenv("LIBRATO_TOKEN")}
ticker := time.Tick(time.Duration(pollIntervalSeconds) * time.Second)
for _ = range ticker {
poll(client)
}
}