-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathmain.go
154 lines (124 loc) · 3.44 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
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"github.com/cloudinsight/cloudinsight-agent/agent"
"github.com/cloudinsight/cloudinsight-agent/collector"
_ "github.com/cloudinsight/cloudinsight-agent/collector/plugins"
"github.com/cloudinsight/cloudinsight-agent/common/config"
"github.com/cloudinsight/cloudinsight-agent/common/log"
"github.com/cloudinsight/cloudinsight-agent/forwarder"
"github.com/cloudinsight/cloudinsight-agent/statsd"
)
var fConfig = flag.String("config", "", "configuration file to load")
var fTest = flag.Bool("test", false, "collect metrics, print them out, and exit")
var fPluginFilters = flag.String("plugin-filter", "",
"filter the plugins to enable, separator is :")
const usage = `Cloudinsight Agent, a system tool that monitors system processes and services.
Usage:
cloudinsight-agent [commands|flags]
The commands & flags are:
--config <file> configuration file to load
--test collect metrics once, print them to stdout, and exit
--plugin-filter filter the plugins to enable, separator is :
Examples:
# run cloudinsight-agent with all plugins defined in config file
cloudinsight-agent --config cloudinsight-agent.conf
# run a single collection, outputing metrics to stdout
cloudinsight-agent --config cloudinsight-agent.conf -test
# run cloudinsight-agent, enabling the system & disk plugins
cloudinsight-agent --config cloudinsight-agent.conf --plugin-filter system:disk
`
func startAgent(shutdown chan struct{}, conf *config.Config, test bool) {
ag := agent.NewAgent(conf)
if test {
log.SetLevel("error")
log.SetOutput(os.Stderr)
if err := ag.Test(); err != nil {
log.Fatal(err)
}
os.Exit(0)
}
if err := ag.Run(shutdown); err != nil {
log.Fatal(err)
}
}
func startForwarder(shutdown chan struct{}, conf *config.Config) {
f := forwarder.NewForwarder(conf)
if err := f.Run(shutdown); err != nil {
log.Fatal(err)
}
}
func startStatsd(shutdown chan struct{}, conf *config.Config) {
s := statsd.NewStatsd(conf)
if err := s.Run(shutdown); err != nil {
log.Fatal(err)
}
}
func usageExit(rc int) {
fmt.Println(usage)
os.Exit(rc)
}
func main() {
reload := make(chan bool, 1)
reload <- true
for <-reload {
reload <- false
flag.Usage = func() { usageExit(0) }
flag.Parse()
shutdown := make(chan struct{})
signals := make(chan os.Signal)
signal.Notify(signals, os.Interrupt, syscall.SIGHUP)
go func() {
select {
case sig := <-signals:
if sig == os.Interrupt {
close(shutdown)
}
if sig == syscall.SIGHUP {
log.Infof("Reloading config...")
<-reload
reload <- true
close(shutdown)
}
}
}()
pluginFilters := []string{}
if *fPluginFilters != "" {
pluginFilters = strings.Split(":"+strings.TrimSpace(*fPluginFilters)+":", ":")
}
conf, err := config.NewConfig(*fConfig, pluginFilters)
if err != nil {
log.Fatalf("failed to load config: %s", err)
}
err = conf.InitializeLogging()
if err != nil {
log.Fatal(err)
}
fmt.Println("Available Plugins:")
for k := range collector.Plugins {
fmt.Printf(" %s\n", k)
}
log.Infof("Loaded plugins: %s", strings.Join(conf.PluginNames(), " "))
var wg sync.WaitGroup
wg.Add(3)
go func() {
defer wg.Done()
startAgent(shutdown, conf, *fTest)
}()
go func() {
defer wg.Done()
startForwarder(shutdown, conf)
}()
go func() {
defer wg.Done()
startStatsd(shutdown, conf)
}()
wg.Wait()
}
}