forked from papertrail/remote_syslog2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_manager.go
349 lines (304 loc) · 8.29 KB
/
config_manager.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
package main
import (
"crypto/x509"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"time"
"github.com/ogier/pflag"
"github.com/papertrail/remote_syslog2/papertrail"
"github.com/papertrail/remote_syslog2/syslog"
"github.com/papertrail/remote_syslog2/utils"
"launchpad.net/goyaml"
)
const (
MinimumRefreshInterval = (time.Duration(10) * time.Second)
DefaultConfigFile = "/etc/log_files.yml"
)
type ConfigFile struct {
Files []string
Destination struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
Protocol string `yaml:"protocol"`
}
Hostname string `yaml:"hostname"`
//SetYAML is only called on pointers
RefreshInterval *RefreshInterval `yaml:"new_file_check_interval"`
ExcludeFiles *RegexCollection `yaml:"exclude_files"`
ExcludePatterns *RegexCollection `yaml:"exclude_patterns"`
}
type ConfigManager struct {
Config ConfigFile
FlagFiles []string
Flags struct {
Hostname string
DestHost string
DestPort int
ConfigFile string
LogLevels string
DebugLogFile string
PidFile string
RefreshInterval RefreshInterval
UseTCP bool
UseTLS bool
NoDaemonize bool
Severity string
Facility string
}
}
type RefreshInterval struct {
Duration time.Duration
}
func (r *RefreshInterval) String() string {
return fmt.Sprint(*r)
}
func (r *RefreshInterval) Set(value string) error {
d, err := time.ParseDuration(value)
if err != nil {
return err
}
if d < MinimumRefreshInterval {
return fmt.Errorf("refresh interval must be greater than %s", MinimumRefreshInterval)
}
r.Duration = d
return nil
}
func (r *RefreshInterval) SetYAML(tag string, value interface{}) bool {
err := r.Set(value.(string))
if err != nil {
return false
}
return true
}
type RegexCollection []*regexp.Regexp
func (r *RegexCollection) Set(value string) error {
exp, err := regexp.Compile(value)
if err != nil {
return err
}
*r = append(*r, exp)
return nil
}
func (r *RegexCollection) String() string {
return fmt.Sprint(*r)
}
func (r *RegexCollection) SetYAML(tag string, value interface{}) bool {
items, ok := value.([]interface{})
if !ok {
return false
}
for _, item := range items {
s, ok := item.(string)
if !ok {
return false
}
err := r.Set(s)
if err != nil {
panic(fmt.Sprintf("Failed to compile regex expression \"%s\"", s))
}
}
return true
}
func NewConfigManager() ConfigManager {
cm := ConfigManager{}
err := cm.Initialize()
if err != nil {
log.Criticalf("Failed to configure the application: %s", err)
os.Exit(1)
}
return cm
}
func (cm *ConfigManager) Initialize() error {
cm.Config.ExcludeFiles = &RegexCollection{}
cm.Config.ExcludePatterns = &RegexCollection{}
cm.parseFlags()
err := cm.readConfig()
if err != nil {
return err
}
return nil
}
func (cm *ConfigManager) parseFlags() {
pflag.StringVarP(&cm.Flags.ConfigFile, "configfile", "c", DefaultConfigFile, "Path to config")
pflag.StringVarP(&cm.Flags.DestHost, "dest-host", "d", "", "Destination syslog hostname or IP")
pflag.IntVarP(&cm.Flags.DestPort, "dest-port", "p", 0, "Destination syslog port")
if utils.CanDaemonize {
pflag.BoolVarP(&cm.Flags.NoDaemonize, "no-detach", "D", false, "Don't daemonize and detach from the terminal")
} else {
cm.Flags.NoDaemonize = true
}
pflag.StringVarP(&cm.Flags.Facility, "facility", "f", "user", "Facility")
pflag.StringVar(&cm.Flags.Hostname, "hostname", "", "Local hostname to send from")
pflag.StringVar(&cm.Flags.PidFile, "pid-file", "", "Location of the PID file")
// --parse-syslog
pflag.StringVarP(&cm.Flags.Severity, "severity", "s", "notice", "Severity")
// --strip-color
pflag.BoolVar(&cm.Flags.UseTCP, "tcp", false, "Connect via TCP (no TLS)")
pflag.BoolVar(&cm.Flags.UseTLS, "tls", false, "Connect via TCP with TLS")
pflag.Var(&cm.Flags.RefreshInterval, "new-file-check-interval", "How often to check for new files")
_ = pflag.Bool("no-eventmachine-tail", false, "No action, provided for backwards compatibility")
_ = pflag.Bool("eventmachine-tail", false, "No action, provided for backwards compatibility")
pflag.StringVar(&cm.Flags.DebugLogFile, "debug-log-cfg", "", "the debug log file")
pflag.StringVar(&cm.Flags.LogLevels, "log", "<root>=INFO", "\"logging configuration <root>=INFO;first=TRACE\"")
pflag.Parse()
cm.FlagFiles = pflag.Args()
}
func (cm *ConfigManager) readConfig() error {
log.Infof("Reading configuration file %s", cm.Flags.ConfigFile)
err := cm.loadConfigFile()
if err != nil {
log.Errorf("%s", err)
return err
}
return nil
}
func (cm *ConfigManager) loadConfigFile() error {
file, err := ioutil.ReadFile(cm.Flags.ConfigFile)
// don't error if the default config file isn't found
if os.IsNotExist(err) && cm.Flags.ConfigFile == DefaultConfigFile {
return nil
}
if err != nil {
return fmt.Errorf("Could not read the config file: %s", err)
}
err = goyaml.Unmarshal(file, &cm.Config)
if err != nil {
return fmt.Errorf("Could not parse the config file: %s", err)
}
return nil
}
func (cm *ConfigManager) Daemonize() bool {
return !cm.Flags.NoDaemonize
}
func (cm *ConfigManager) Hostname() string {
switch {
case cm.Flags.Hostname != "":
return cm.Flags.Hostname
case cm.Config.Hostname != "":
return cm.Config.Hostname
default:
hostname, _ := os.Hostname()
return hostname
}
}
func (cm *ConfigManager) RootCAs() *x509.CertPool {
if cm.DestProtocol() == "tls" && cm.DestHost() == "logs.papertrailapp.com" {
return papertrail.RootCA()
} else {
return nil
}
}
func (cm *ConfigManager) DestHost() string {
switch {
case cm.Flags.DestHost != "":
return cm.Flags.DestHost
case cm.Config.Destination.Host == "":
log.Criticalf("No destination hostname specified")
os.Exit(1)
}
return cm.Config.Destination.Host
}
func (cm ConfigManager) DestPort() int {
switch {
case cm.Flags.DestPort != 0:
return cm.Flags.DestPort
case cm.Config.Destination.Port != 0:
return cm.Config.Destination.Port
default:
return 514
}
}
func (cm *ConfigManager) DestProtocol() string {
switch {
case cm.Flags.UseTLS:
return "tls"
case cm.Flags.UseTCP:
return "tcp"
case cm.Config.Destination.Protocol != "":
return cm.Config.Destination.Protocol
default:
return "udp"
}
}
func (cm *ConfigManager) Severity() syslog.Priority {
s, err := syslog.Severity(cm.Flags.Severity)
if err != nil {
log.Criticalf("%s is not a designated facility", cm.Flags.Severity)
os.Exit(1)
}
return s
}
func (cm *ConfigManager) Facility() syslog.Priority {
f, err := syslog.Facility(cm.Flags.Facility)
if err != nil {
log.Criticalf("%s is not a designated facility", cm.Flags.Facility)
os.Exit(1)
}
return f
}
func (cm *ConfigManager) Files() []string {
return append(cm.FlagFiles, cm.Config.Files...)
}
func (cm *ConfigManager) DebugLogFile() string {
switch {
case cm.Flags.DebugLogFile != "":
return cm.Flags.DebugLogFile
default:
return "/dev/null"
}
}
func (cm *ConfigManager) defaultPidFile() string {
pidFiles := []string{
"/var/run/remote_syslog.pid",
os.Getenv("HOME") + "/run/remote_syslog.pid",
os.Getenv("HOME") + "/tmp/remote_syslog.pid",
os.Getenv("HOME") + "/remote_syslog.pid",
os.TempDir() + "/remote_syslog.pid",
os.Getenv("TMPDIR") + "/remote_syslog.pid",
}
for _, f := range pidFiles {
dir := filepath.Dir(f)
dirStat, err := os.Stat(dir)
if err != nil || dirStat == nil || !dirStat.IsDir() {
continue
}
fd, err := os.OpenFile(f, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
continue
}
fd.Close()
return f
}
return "/tmp/remote_syslog.pid"
}
func (cm *ConfigManager) PidFile() string {
switch {
case cm.Flags.PidFile != "":
return cm.Flags.PidFile
default:
return cm.defaultPidFile()
}
}
func (cm *ConfigManager) LogLevels() string {
return cm.Flags.LogLevels
}
func (cm *ConfigManager) RefreshInterval() RefreshInterval {
switch {
case cm.Config.RefreshInterval != nil && cm.Flags.RefreshInterval.Duration != 0:
return cm.Flags.RefreshInterval
case cm.Config.RefreshInterval != nil:
return *cm.Config.RefreshInterval
case cm.Flags.RefreshInterval.Duration != 0:
return cm.Flags.RefreshInterval
}
return RefreshInterval{Duration: MinimumRefreshInterval}
}
func (cm *ConfigManager) ExcludeFiles() []*regexp.Regexp {
return *cm.Config.ExcludeFiles
}
func (cm *ConfigManager) ExcludePatterns() []*regexp.Regexp {
return *cm.Config.ExcludePatterns
}