-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
187 lines (161 loc) · 3.72 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
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
package main
import (
"flag"
"log"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"github.com/BurntSushi/toml"
"github.com/gabrielperezs/loup/lib"
"github.com/gabrielperezs/loup/plugins/aggregator/file"
"github.com/gabrielperezs/loup/plugins/input/cmd"
"github.com/gabrielperezs/loup/plugins/output/awsS3"
)
const (
defaultConfigFile = "config.toml"
)
var (
loaded lib.Loaded
conf lib.Config
configFile string
debug bool
done chan struct{}
)
func main() {
flag.StringVar(&configFile, "config", defaultConfigFile, "Path to the config file")
flag.BoolVar(&debug, "debug", false, "Debug mode")
flag.Parse()
if !debug {
log.SetFlags(0)
}
done = make(chan struct{})
signals()
reload()
<-done
}
func signals() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGHUP, syscall.SIGTERM, os.Interrupt)
go func() {
for {
switch <-sigs {
case syscall.SIGHUP:
log.Printf("Reload")
reload()
default:
finishLoaded()
log.Printf("Exit")
done <- struct{}{}
return
}
}
}()
}
func finishLoaded() {
wg := &sync.WaitGroup{}
// Finish running Inputs
loaded.Input.Range(func(k, v interface{}) bool {
wg.Add(1)
go func(in lib.Input) {
defer wg.Done()
in.Exit()
}(v.(lib.Input))
loaded.Input.Delete(k)
return true
})
wg.Wait()
loaded.Aggregator.Range(func(k, v interface{}) bool {
loaded.Input.Delete(k)
return true
})
// Finish running Outputs
loaded.Output.Range(func(k, v interface{}) bool {
wg.Add(1)
go func(o lib.Output) {
defer wg.Done()
o.Exit()
}(v.(lib.Output))
loaded.Input.Delete(k)
return true
})
wg.Wait()
}
func reload() {
log.Printf("Loading %s", configFile)
if _, err := os.Stat(configFile); err != nil {
log.Fatal("Config file is missing: ", configFile)
}
finishLoaded()
var localConf lib.Config
if _, err := toml.DecodeFile(configFile, &localConf); err != nil {
log.Fatalf("Invalid configuration file: %s", err)
}
conf = localConf
wg := &sync.WaitGroup{}
log.Printf("Loading output plugins")
for name, c := range conf.Output {
wg.Add(1)
go func(name string, c interface{}) {
defer wg.Done()
switch strings.ToLower(c.(map[string]interface{})["plugin"].(string)) {
case "awss3":
loaded.Output.Store(name, awsS3.New(name, c))
}
}(name, c)
}
wg.Wait()
log.Printf("Loading aggregators plugins")
for name, in := range conf.Aggregator {
loaded.Aggregator.Store(name, in)
}
log.Printf("Loading input plugins")
for name, in := range conf.Input {
wg.Add(1)
go func(name string, in interface{}) {
defer wg.Done()
var plugin string
var aggregatorName string
var aggregatorCfg map[string]interface{}
var o lib.Output
for k, v := range in.(map[string]interface{}) {
k = strings.ToLower(k)
switch k {
case "output":
var oi interface{}
var ok bool
if oi, ok = loaded.Output.Load(v.(string)); !ok {
log.Printf("[I:%s] ERROR: Output don't exits %s", name, k)
return
}
o = oi.(lib.Output)
case "aggregator":
aggregatorName = v.(string)
var oi interface{}
var ok bool
if oi, ok = loaded.Aggregator.Load(aggregatorName); !ok {
log.Printf("[I:%s] ERROR: Aggregator don't exits %s", name, k)
return
}
aggregatorCfg = oi.(map[string]interface{})
case "plugin":
plugin = v.(string)
}
}
switch plugin {
case "cmd":
p := cmd.New(name, in.(map[string]interface{}), newAggregator(aggregatorName, aggregatorCfg, o))
loaded.Input.Store(name, p)
}
}(name, in)
}
wg.Wait()
}
func newAggregator(k string, cfg map[string]interface{}, o lib.Output) lib.Aggregator {
switch k {
case strings.ToLower(cfg["plugin"].(string)):
return file.New(k, cfg, o)
}
return nil
}