-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
124 lines (108 loc) · 2.96 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
package main
import (
"flag"
"github.com/op/go-logging"
"github.com/openmetric/a2graphite/ceilometer"
"github.com/openmetric/graphite-client"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"runtime/debug"
"syscall"
"time"
)
var log = logging.MustGetLogger("main")
func main() {
configFile := flag.String("config", "", "Path to the `config file`.")
flag.Parse()
config := loadConfig(*configFile)
// setup log facility
setupLogging(config.Log)
var receivers []Receiver
metrics := make(chan *graphite.Metric, 10000)
// start graphite client for receiving metrics
client, err := graphite.NewClient(
config.Graphite.URL,
config.Graphite.Prefix,
config.Graphite.ReconnectDelay,
)
if err != nil {
log.Fatal("Failed to create graphite client:", err)
}
go client.SendChan(metrics)
var statsClient *graphite.Client
if config.Stats.Enabled {
// start graphite client for stats
statsClient, err = graphite.NewClient(
config.Stats.URL,
config.Stats.Prefix,
config.Stats.ReconnectDelay,
)
if err != nil {
log.Fatal("Failed to create graphite client for stats:", err)
}
}
// start receivers
if config.Ceilometer.Enabled {
if ceilo, err := ceilometer.NewReceiver(config.Ceilometer); err == nil {
receivers = append(receivers, ceilo)
ceilo.Start(metrics)
} else {
log.Fatal("Failed to initialize ceilometer receiver:", err)
}
}
// check if there are receivers enabled
if len(receivers) == 0 {
log.Fatal("You must enable at least one receiver.")
}
// enable profiler if configured
if config.Profiler.Enabled {
go func() {
log.Info("Profiler enabled, on:", config.Profiler.ListenAddr)
log.Info(http.ListenAndServe(config.Profiler.ListenAddr, nil))
}()
}
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
var statsTicker <-chan time.Time
if config.Stats.Enabled {
statsTicker = time.Tick(config.Stats.Interval)
} else {
statsTicker = make(chan time.Time)
}
healthCheckTicker := time.Tick(1 * time.Second)
var gcstats debug.GCStats
for {
select {
case <-c:
log.Info("Got stop signal, stopping ...")
for _, receiver := range receivers {
log.Info("Stopping", receiver.GetName(), "...")
receiver.Stop()
}
log.Info("Shuting down graphite client ...")
client.Shutdown(1 * time.Second)
log.Info("Shuting down graphite client for stats ...")
statsClient.Shutdown(1 * time.Second)
log.Info("Quit.")
return
case <-statsTicker:
for _, receiver := range receivers {
statsClient.SendMetrics(receiver.Stats())
}
debug.ReadGCStats(&gcstats)
statsClient.SendSimple("gc.NumGC", gcstats.NumGC, 0)
statsClient.SendSimple("gc.TotalPause", gcstats.PauseTotal.Nanoseconds(), 0)
if gcstats.NumGC > 0 {
statsClient.SendSimple("gc.LastPause", gcstats.Pause[0].Nanoseconds(), 0)
}
case <-healthCheckTicker:
for _, receiver := range receivers {
if !receiver.Healthy() {
log.Error(receiver.GetName(), "claims to be unhealthy")
}
}
}
}
}