-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
175 lines (163 loc) · 4.83 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
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
//
// riak-statsd
// Sends Riak stats to statsd every 60s.
//
// Usage:
// -nodename="riak": Riak node name
// -riak_host="127.0.0.1": Riak host
// -riak_http_port=8098: Riak HTTP port
// -statsd_host="127.0.0.1": Statsd host
// -statsd_port=8125: Statsd host
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"strings"
"time"
)
// The interesting metric keys and their statsd types
var MetricTypes = map[string]string{
"node_gets": "g",
"node_puts": "g",
"vnode_gets": "g",
"vnode_puts": "g",
"read_repairs": "g",
"read_repairs_total": "g",
"node_get_fsm_objsize_mean": "g",
"node_get_fsm_objsize_median": "g",
"node_get_fsm_objsize_95": "g",
"node_get_fsm_objsize_100": "g",
"node_get_fsm_time_mean": "ms",
"node_get_fsm_time_median": "ms",
"node_get_fsm_time_95": "ms",
"node_get_fsm_time_100": "ms",
"node_put_fsm_time_mean": "ms",
"node_put_fsm_time_median": "ms",
"node_put_fsm_time_95": "ms",
"node_put_fsm_time_100": "ms",
"node_get_fsm_siblings_mean": "g",
"node_get_fsm_siblings_median": "g",
"node_get_fsm_siblings_95": "g",
"node_get_fsm_siblings_100": "g",
"memory_processes_used": "g",
"node_get_fsm_active": "g",
"node_get_fsm_active_60s": "g",
"node_get_fsm_in_rate": "g",
"node_get_fsm_out_rate": "g",
"node_get_fsm_rejected": "g",
"node_get_fsm_rejected_60s": "g",
"node_get_fsm_rejected_total": "g",
"node_put_fsm_active": "g",
"node_put_fsm_active_60s": "g",
"node_put_fsm_in_rate": "g",
"node_put_fsm_out_rate": "g",
"node_put_fsm_rejected": "g",
"node_put_fsm_rejected_60s": "g",
"node_put_fsm_rejected_total": "g",
"index_fsm_create": "g",
"index_fsm_create_error": "g",
"index_fsm_active": "g",
"list_fsm_create": "g",
"list_fsm_create_error": "g",
"list_fsm_active": "g",
"sys_process_count": "g",
"coord_redirs_total": "g",
"pbc_connects": "g",
"pbc_active": "g",
}
func getRiakStats(host string, port int) (*map[string]interface{}, error) {
url := fmt.Sprintf("http://%s:%d/stats", host, port)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data := make(map[string]interface{})
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if err := json.Unmarshal(body, &data); err != nil {
return nil, err
}
return &data, nil
}
func pingRiak(host string, port int) error {
url := fmt.Sprintf("http://%s:%d/ping", host, port)
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return errors.New("Error reading response")
}
msg := string(body)
if msg != "OK" {
return errors.New(fmt.Sprintf("Not OK. Response was: %s", msg))
}
return nil
}
func prepareMetrics(nodename string, riakstats map[string]interface{}) *[]string {
metrics := make([]string, len(MetricTypes))
i := 0
for key, st := range MetricTypes {
value := riakstats[key]
metrics[i] = fmt.Sprintf("%s.%s:%v|%s", nodename, key, value, st)
i++
}
return &metrics
}
func sendRiakMetrics(conn *net.UDPConn, metrics *[]string) error {
data := []byte(strings.Join(*metrics, "\n"))
_, err := conn.Write(data)
if err != nil {
log.Println("Error sending metrics: %v", err)
}
return nil
}
func getAndSendRiakMetrics(conn *net.UDPConn, nodename string, host string, port int) {
data, _ := getRiakStats(host, port)
if data != nil {
metrics := prepareMetrics(nodename, *data)
sendRiakMetrics(conn, metrics)
}
}
func main() {
var statsdHost = flag.String("statsd_host", "127.0.0.1", "Statsd host")
var statsdPort = flag.Int("statsd_port", 8125, "Statsd host")
var nodename = flag.String("nodename", "riak", "Riak node name")
var riakHost = flag.String("riak_host", "127.0.0.1", "Riak host")
var riakHttpPort = flag.Int("riak_http_port", 8098, "Riak HTTP port")
flag.Parse()
// First ping to node to make sure it works
err := pingRiak(*riakHost, *riakHttpPort)
if err != nil {
log.Fatalf("Error: %v", err)
os.Exit(1)
}
statsd := fmt.Sprintf("%s:%d", *statsdHost, *statsdPort)
addr, err := net.ResolveUDPAddr("udp", statsd)
if err != nil {
log.Fatalf("Couldn't resolve UDP addr: %v", err)
os.Exit(1)
}
conn, err := net.DialUDP("udp", nil, addr)
if err != nil {
log.Fatalf("Couldn't connect to statsd at %s", statsd)
os.Exit(1)
}
// every 60s run hit the stats endpoint and then send to statsd
interval := time.NewTicker(time.Second * 60)
for _ = range interval.C {
go getAndSendRiakMetrics(conn, *nodename, *riakHost, *riakHttpPort)
}
}