forked from go-graphite/go-carbon
/
carbon-agent.go
423 lines (349 loc) · 9.05 KB
/
carbon-agent.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package main
import (
"bytes"
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"os/user"
"runtime"
"strconv"
"strings"
"syscall"
"time"
"github.com/BurntSushi/toml"
"github.com/Sirupsen/logrus"
"github.com/lomik/go-carbon/cache"
"github.com/lomik/go-carbon/logging"
"github.com/lomik/go-carbon/persister"
"github.com/lomik/go-carbon/receiver"
"github.com/lomik/go-daemon"
)
import _ "net/http/pprof"
// Version of go-carbon
const Version = "0.5.3"
// Duration wrapper time.Duration for TOML
type Duration struct {
time.Duration
}
var _ toml.TextMarshaler = &Duration{}
// UnmarshalText from TOML
func (d *Duration) UnmarshalText(text []byte) error {
var err error
d.Duration, err = time.ParseDuration(string(text))
return err
}
// MarshalText encode text with TOML format
func (d *Duration) MarshalText() ([]byte, error) {
return []byte(d.Duration.String()), nil
}
// Value return time.Duration value
func (d *Duration) Value() time.Duration {
return d.Duration
}
type commonConfig struct {
User string `toml:"user"`
Logfile string `toml:"logfile"`
LogLevel string `toml:"log-level"`
GraphPrefix string `toml:"graph-prefix"`
MaxCPU int `toml:"max-cpu"`
}
type whisperConfig struct {
DataDir string `toml:"data-dir"`
Schemas string `toml:"schemas-file"`
Aggregation string `toml:"aggregation-file"`
Workers int `toml:"workers"`
MaxUpdatesPerSecond int `toml:"max-updates-per-second"`
Enabled bool `toml:"enabled"`
}
type cacheConfig struct {
MaxSize int `toml:"max-size"`
InputBuffer int `toml:"input-buffer"`
}
type udpConfig struct {
Listen string `toml:"listen"`
Enabled bool `toml:"enabled"`
LogIncomplete bool `toml:"log-incomplete"`
}
type tcpConfig struct {
Listen string `toml:"listen"`
Enabled bool `toml:"enabled"`
}
type carbonlinkConfig struct {
Listen string `toml:"listen"`
Enabled bool `toml:"enabled"`
ReadTimeout *Duration `toml:"read-timeout"`
QueryTimeout *Duration `toml:"query-timeout"`
}
type pprofConfig struct {
Listen string `toml:"listen"`
Enabled bool `toml:"enabled"`
}
// Config ...
type Config struct {
Common commonConfig `toml:"common"`
Whisper whisperConfig `toml:"whisper"`
Cache cacheConfig `toml:"cache"`
Udp udpConfig `toml:"udp"`
Tcp tcpConfig `toml:"tcp"`
Pickle tcpConfig `toml:"pickle"`
Carbonlink carbonlinkConfig `toml:"carbonlink"`
Pprof pprofConfig `toml:"pprof"`
}
func newConfig() *Config {
cfg := &Config{
Common: commonConfig{
Logfile: "/var/log/go-carbon/go-carbon.log",
LogLevel: "info",
GraphPrefix: "carbon.agents.{host}.",
MaxCPU: 1,
User: "",
},
Whisper: whisperConfig{
DataDir: "/data/graphite/whisper/",
Schemas: "/data/graphite/schemas",
Aggregation: "",
MaxUpdatesPerSecond: 0,
Enabled: true,
Workers: 1,
},
Cache: cacheConfig{
MaxSize: 1000000,
InputBuffer: 51200,
},
Udp: udpConfig{
Listen: ":2003",
Enabled: true,
LogIncomplete: false,
},
Tcp: tcpConfig{
Listen: ":2003",
Enabled: true,
},
Pickle: tcpConfig{
Listen: ":2004",
Enabled: true,
},
Carbonlink: carbonlinkConfig{
Listen: "127.0.0.1:7002",
Enabled: true,
ReadTimeout: &Duration{
Duration: 30 * time.Second,
},
QueryTimeout: &Duration{
Duration: 100 * time.Millisecond,
},
},
Pprof: pprofConfig{
Listen: "localhost:7007",
Enabled: false,
},
}
return cfg
}
// PrintConfig ...
func PrintConfig(cfg interface{}) error {
buf := new(bytes.Buffer)
encoder := toml.NewEncoder(buf)
encoder.Indent = ""
if err := encoder.Encode(cfg); err != nil {
return err
}
fmt.Print(buf.String())
return nil
}
// ParseConfig ...
func ParseConfig(filename string, cfg interface{}) error {
if filename != "" {
if _, err := toml.DecodeFile(filename, cfg); err != nil {
return err
}
}
return nil
}
func main() {
var err error
/* CONFIG start */
configFile := flag.String("config", "", "Filename of config")
printDefaultConfig := flag.Bool("config-print-default", false, "Print default config")
checkConfig := flag.Bool("check-config", false, "Check config and exit")
printVersion := flag.Bool("version", false, "Print version")
isDaemon := flag.Bool("daemon", false, "Run in background")
pidfile := flag.String("pidfile", "", "Pidfile path (only for daemon)")
flag.Parse()
if *printVersion {
fmt.Print(Version)
return
}
cfg := newConfig()
if *printDefaultConfig {
if err = PrintConfig(cfg); err != nil {
log.Fatal(err)
}
return
}
if err = ParseConfig(*configFile, cfg); err != nil {
log.Fatal(err)
}
var runAsUser *user.User
if cfg.Common.User != "" {
runAsUser, err = user.Lookup(cfg.Common.User)
if err != nil {
log.Fatal(err)
}
}
var whisperSchemas *persister.WhisperSchemas
var whisperAggregation *persister.WhisperAggregation
if cfg.Whisper.Enabled {
whisperSchemas, err = persister.ReadWhisperSchemas(cfg.Whisper.Schemas)
if err != nil {
log.Fatal(err)
}
if cfg.Whisper.Aggregation != "" {
whisperAggregation, err = persister.ReadWhisperAggregation(cfg.Whisper.Aggregation)
if err != nil {
log.Fatal(err)
}
} else {
whisperAggregation = persister.NewWhisperAggregation()
}
}
if err := logging.SetLevel(cfg.Common.LogLevel); err != nil {
log.Fatal(err)
}
if *checkConfig {
return
}
if err := logging.PrepareFile(cfg.Common.Logfile, runAsUser); err != nil {
logrus.Fatal(err)
}
if err := logging.SetFile(cfg.Common.Logfile); err != nil {
logrus.Fatal(err)
}
if *isDaemon {
runtime.LockOSThread()
context := new(daemon.Context)
if *pidfile != "" {
context.PidFileName = *pidfile
context.PidFilePerm = 0644
}
if runAsUser != nil {
uid, err := strconv.ParseInt(runAsUser.Uid, 10, 0)
if err != nil {
log.Fatal(err)
}
gid, err := strconv.ParseInt(runAsUser.Gid, 10, 0)
if err != nil {
log.Fatal(err)
}
context.Credential = &syscall.Credential{
Uid: uint32(uid),
Gid: uint32(gid),
}
}
child, _ := context.Reborn()
if child != nil {
return
}
defer context.Release()
runtime.UnlockOSThread()
}
runtime.GOMAXPROCS(cfg.Common.MaxCPU)
/* CONFIG end */
// pprof
if cfg.Pprof.Enabled {
go func() {
logrus.Fatal(http.ListenAndServe(cfg.Pprof.Listen, nil))
}()
}
// carbon-cache prefix
if hostname, err := os.Hostname(); err == nil {
hostname = strings.Replace(hostname, ".", "_", -1)
cfg.Common.GraphPrefix = strings.Replace(cfg.Common.GraphPrefix, "{host}", hostname, -1)
} else {
cfg.Common.GraphPrefix = strings.Replace(cfg.Common.GraphPrefix, "{host}", "localhost", -1)
}
core := cache.New()
core.SetGraphPrefix(cfg.Common.GraphPrefix)
core.SetMaxSize(cfg.Cache.MaxSize)
core.SetInputCapacity(cfg.Cache.InputBuffer)
core.Start()
defer core.Stop()
/* UDP start */
udpCfg := cfg.Udp
if udpCfg.Enabled {
udpAddr, err := net.ResolveUDPAddr("udp", udpCfg.Listen)
if err != nil {
log.Fatal(err)
}
udpListener := receiver.NewUDP(core.In())
udpListener.SetGraphPrefix(cfg.Common.GraphPrefix)
if udpCfg.LogIncomplete {
udpListener.SetLogIncomplete(true)
}
defer udpListener.Stop()
if err = udpListener.Listen(udpAddr); err != nil {
log.Fatal(err)
}
}
/* UDP end */
/* TCP start */
tcpCfg := cfg.Tcp
if tcpCfg.Enabled {
tcpAddr, err := net.ResolveTCPAddr("tcp", tcpCfg.Listen)
if err != nil {
log.Fatal(err)
}
tcpListener := receiver.NewTCP(core.In())
tcpListener.SetGraphPrefix(cfg.Common.GraphPrefix)
defer tcpListener.Stop()
if err = tcpListener.Listen(tcpAddr); err != nil {
log.Fatal(err)
}
}
/* TCP end */
/* PICKLE start */
pickleCfg := cfg.Pickle
if pickleCfg.Enabled {
pickleAddr, err := net.ResolveTCPAddr("tcp", pickleCfg.Listen)
if err != nil {
log.Fatal(err)
}
pickleListener := receiver.NewPickle(core.In())
pickleListener.SetGraphPrefix(cfg.Common.GraphPrefix)
defer pickleListener.Stop()
if err = pickleListener.Listen(pickleAddr); err != nil {
log.Fatal(err)
}
}
/* PICKLE end */
/* WHISPER start */
if cfg.Whisper.Enabled {
whisperPersister := persister.NewWhisper(cfg.Whisper.DataDir, whisperSchemas, whisperAggregation, core.Out())
whisperPersister.SetGraphPrefix(cfg.Common.GraphPrefix)
whisperPersister.SetMaxUpdatesPerSecond(cfg.Whisper.MaxUpdatesPerSecond)
whisperPersister.SetWorkers(cfg.Whisper.Workers)
whisperPersister.Start()
defer whisperPersister.Stop()
}
/* WHISPER end */
/* CARBONLINK start */
if cfg.Carbonlink.Enabled {
linkAddr, err := net.ResolveTCPAddr("tcp", cfg.Carbonlink.Listen)
if err != nil {
log.Fatal(err)
}
carbonlink := cache.NewCarbonlinkListener(core.Query())
carbonlink.SetReadTimeout(cfg.Carbonlink.ReadTimeout.Value())
carbonlink.SetQueryTimeout(cfg.Carbonlink.QueryTimeout.Value())
defer carbonlink.Stop()
if err = carbonlink.Listen(linkAddr); err != nil {
log.Fatal(err)
}
}
/* CARBONLINK end */
logrus.Info("started")
select {}
}