forked from grafana/carbon-relay-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
carbon-relay-ng.go
324 lines (293 loc) · 7.26 KB
/
carbon-relay-ng.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
// carbon-relay-ng
// route traffic to anything that speaks the Graphite Carbon protocol,
// such as Graphite's carbon-cache.py, influxdb, ...
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"github.com/BurntSushi/toml"
"github.com/Dieterbe/statsd-go"
"github.com/graphite-ng/carbon-relay-ng/admin"
"github.com/graphite-ng/carbon-relay-ng/routing"
"github.com/rcrowley/goagain"
"io"
"log"
"net"
"os"
"runtime/pprof"
"strings"
)
type StatsdConfig struct {
Enabled bool
Instance string
Host string
Port int
}
type Blacklist struct {
Patt string
Comment string
}
type Config struct {
Listen_addr string
Admin_addr string
Http_addr string
Spool_dir string
First_only bool
Routes map[string]*routing.Route
Statsd StatsdConfig
Blacklist []Blacklist
}
var (
config_file string
config Config
to_dispatch = make(chan []byte)
routes *routing.Routes
statsdClient statsd.Client
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
)
func init() {
log.SetFlags(log.Ltime | log.Lmicroseconds | log.Lshortfile)
}
func accept(l *net.TCPListener, config Config) {
for {
c, err := l.AcceptTCP()
if nil != err {
log.Println(err)
break
}
go handle(c, config)
}
}
func handle(c *net.TCPConn, config Config) {
defer c.Close()
// TODO c.SetTimeout(60e9)
r := bufio.NewReaderSize(c, 4096)
LineReader:
for {
buf, isPrefix, err := r.ReadLine()
if nil != err {
if io.EOF != err {
log.Println(err)
}
break
}
if isPrefix { // TODO Recover from partial reads.
log.Println("isPrefix: true")
break
}
for _, blacklist := range config.Blacklist {
if strings.Contains(string(buf), blacklist.Patt) {
statsdClient.Increment("target_type=count.unit=Metric.direction=blacklist")
continue LineReader
}
}
buf = append(buf, '\n')
buf_copy := make([]byte, len(buf), len(buf))
copy(buf_copy, buf)
statsdClient.Increment("target_type=count.unit=Metric.direction=in")
to_dispatch <- buf_copy
}
}
func Router() {
for buf := range to_dispatch {
routed := routes.Dispatch(buf, config.First_only)
if !routed {
log.Printf("unrouteable: %s\n", buf)
}
}
}
func tcpListHandler(req admin.Req) (err error) {
if len(req.Command) != 2 {
return errors.New("extraneous arguments")
}
longest_key := 9
longest_patt := 9
longest_addr := 9
list := routes.List()
for key, route := range list {
if len(key) > longest_key {
longest_key = len(key)
}
if len(route.Patt) > longest_patt {
longest_patt = len(route.Patt)
}
if len(route.Addr) > longest_addr {
longest_addr = len(route.Addr)
}
}
fmt_str := fmt.Sprintf("%%%ds %%%ds %%%ds %%8v\n", longest_key+1, longest_patt+1, longest_addr+1)
(*req.Conn).Write([]byte(fmt.Sprintf(fmt_str, "key", "pattern", "addr", "spool")))
for key, route := range list {
(*req.Conn).Write([]byte(fmt.Sprintf(fmt_str, key, route.Patt, route.Addr, route.Spool)))
}
(*req.Conn).Write([]byte("--\n"))
return
}
func tcpAddHandler(req admin.Req) (err error) {
key := req.Command[2]
var patt, addr, spool_str string
if len(req.Command) == 5 {
patt = ""
addr = req.Command[3]
spool_str = req.Command[4]
} else if len(req.Command) == 6 {
patt = req.Command[3]
addr = req.Command[4]
spool_str = req.Command[5]
} else {
return errors.New("bad number of arguments")
}
spool := false
if spool_str == "1" {
spool = true
}
pickle := false
err = routes.Add(key, patt, addr, spool, pickle, &statsdClient)
if err != nil {
return err
}
(*req.Conn).Write([]byte("added\n"))
return
}
func tcpDelHandler(req admin.Req) (err error) {
if len(req.Command) != 3 {
return errors.New("bad number of arguments")
}
key := req.Command[2]
err = routes.Del(key)
if err != nil {
return err
}
(*req.Conn).Write([]byte("deleted\n"))
return
}
func tcpPattHandler(req admin.Req) (err error) {
key := req.Command[2]
var patt string
if len(req.Command) == 4 {
patt = req.Command[3]
} else if len(req.Command) == 3 {
patt = ""
} else {
return errors.New("bad number of arguments")
}
err = routes.Update(key, nil, &patt)
if err != nil {
return err
}
(*req.Conn).Write([]byte("updated\n"))
return
}
func tcpHelpHandler(req admin.Req) (err error) {
writeHelp(*req.Conn, []byte(""))
return
}
func tcpDefaultHandler(req admin.Req) (err error) {
writeHelp(*req.Conn, []byte("unknown command\n"))
return
}
func writeHelp(conn net.Conn, write_first []byte) { // bytes.Buffer
//write_first.WriteTo(conn)
conn.Write(write_first)
help := `
commands:
help show this menu
route list list routes
route add <key> [pattern] <addr> <spool> add the route. (empty pattern allows all). (spool has to be 1 or 0)
route del <key> delete the matching route
route patt <key> [pattern] update pattern for given route key. (empty pattern allows all)
`
conn.Write([]byte(help))
}
func adminListener() {
admin.HandleFunc("route list", tcpListHandler)
admin.HandleFunc("route add", tcpAddHandler)
admin.HandleFunc("route del", tcpDelHandler)
admin.HandleFunc("route patt", tcpPattHandler)
admin.HandleFunc("help", tcpHelpHandler)
admin.HandleFunc("", tcpDefaultHandler)
log.Printf("admin TCP listener starting on %v", config.Admin_addr)
err := admin.ListenAndServe(config.Admin_addr)
if err != nil {
fmt.Println("Error listening:", err.Error())
os.Exit(1)
}
}
func usage() {
fmt.Fprintln(
os.Stderr,
"Usage: carbon-relay-ng <path-to-config>",
)
flag.PrintDefaults()
}
func main() {
flag.Usage = usage
flag.Parse()
config_file = "/etc/carbon-relay-ng.ini"
if 1 == flag.NArg() {
config_file = flag.Arg(0)
}
if _, err := toml.DecodeFile(config_file, &config); err != nil {
fmt.Printf("Cannot use config file '%s':\n", config_file)
fmt.Println(err)
return
}
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
log.Println("initializing routes...")
var err error
routes, err = routing.NewRoutes(config.Routes, config.Spool_dir, &statsdClient)
if err != nil {
log.Println(err)
os.Exit(1)
}
err = routes.Run()
if err != nil {
log.Println(err)
os.Exit(1)
}
statsdPrefix := fmt.Sprintf("service=carbon-relay-ng.instance=%s.", config.Statsd.Instance)
statsdClient = *statsd.NewClient(config.Statsd.Enabled, config.Statsd.Host, config.Statsd.Port, statsdPrefix)
// Follow the goagain protocol, <https://github.com/rcrowley/goagain>.
l, ppid, err := goagain.GetEnvs()
if nil != err {
laddr, err := net.ResolveTCPAddr("tcp", config.Listen_addr)
if nil != err {
log.Println(err)
os.Exit(1)
}
l, err = net.ListenTCP("tcp", laddr)
if nil != err {
log.Println(err)
os.Exit(1)
}
log.Printf("listening on %v", laddr)
go accept(l.(*net.TCPListener), config)
} else {
log.Printf("resuming listening on %v", l.Addr())
go accept(l.(*net.TCPListener), config)
if err := goagain.KillParent(ppid); nil != err {
log.Println(err)
os.Exit(1)
}
}
if config.Admin_addr != "" {
go adminListener()
}
if config.Http_addr != "" {
go admin.HttpListener(config.Http_addr, routes, &statsdClient)
}
go Router()
if err := goagain.AwaitSignals(l); nil != err {
log.Println(err)
os.Exit(1)
}
}