forked from fabware/gostatsd
/
gostatsd.go
53 lines (45 loc) · 1.49 KB
/
gostatsd.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
package main
import (
"flag"
"log"
"time"
"github.com/DeanThompson/gostatsd/statsd"
)
const (
defaultMetricsAddr = "localhost:8125"
defaultConsoleAddr = "localhost:8126"
defaultGraphiteAddr = "localhost:2003"
defaultFlushInterval = 10 * time.Second
)
func main() {
metricsAddr := flag.String("l", defaultMetricsAddr, "address on which to listen for metrics")
graphiteAddr := flag.String("g", defaultGraphiteAddr, "address of the graphite server")
flushInterval := flag.Duration("f", defaultFlushInterval, "how often to flush metrics to the graphite server")
webConsoleAddr := flag.String("web", "", "if set, use as the address of the web-based console")
consoleAddr := flag.String("console", "", "if set, use as the address of the telnet-based console ")
flag.Parse()
// Start the metric aggregator
graphite, err := statsd.NewGraphiteClient(*graphiteAddr)
if err != nil {
log.Fatal(err)
}
aggregator := statsd.NewMetricAggregator(&graphite, *flushInterval)
go aggregator.Aggregate()
// Start the metric receiver
f := func(metric statsd.Metric) {
aggregator.MetricChan <- metric
}
receiver := statsd.MetricReceiver{*metricsAddr, statsd.HandlerFunc(f)}
go receiver.ListenAndReceive()
// Start the console(s)
if *consoleAddr != "" {
console := statsd.ConsoleServer{*consoleAddr, &aggregator}
go console.ListenAndServe()
}
if *webConsoleAddr != "" {
console := statsd.WebConsoleServer{*webConsoleAddr, &aggregator}
go console.ListenAndServe()
}
// Listen forever
select {}
}