forked from papertrail/remote_syslog2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
405 lines (325 loc) · 10.2 KB
/
config.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package main
import (
"crypto/x509"
"errors"
"fmt"
"os"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"github.com/mitchellh/mapstructure"
"github.com/papertrail/remote_syslog2/papertrail"
"github.com/papertrail/remote_syslog2/syslog"
"github.com/papertrail/remote_syslog2/utils"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
var (
config *viper.Viper
flags *pflag.FlagSet
Version string
ErrUsage = errors.New("usage")
)
const (
envPrefix = "remote_syslog"
defaultConfigFile = "/etc/log_files.yml"
)
// The global Config object for remote_syslog2 server. "mapstructure" tags
// signify the config file key names.
type Config struct {
ConnectTimeout time.Duration `mapstructure:"connect_timeout"`
WriteTimeout time.Duration `mapstructure:"write_timeout"`
NewFileCheckInterval time.Duration `mapstructure:"new_file_check_interval"`
ExcludeFiles []*regexp.Regexp `mapstructure:"exclude_files"`
ExcludePatterns []*regexp.Regexp `mapstructure:"exclude_patterns"`
LogLevels string `mapstructure:"log_levels"`
DebugLogFile string `mapstructure:"debug_log_file"`
PidFile string `mapstructure:"pid_file"`
TcpMaxLineLength int `mapstructure:"tcp_max_line_length"`
NoDetach bool `mapstructure:"no_detach"`
TCP bool `mapstructure:"tcp"`
TLS bool `mapstructure:"tls"`
Files []LogFile
Hostname string
Severity syslog.Priority
Facility syslog.Priority
Poll bool
Destination struct {
Host string
Port int
Protocol string
}
RootCAs *x509.CertPool
}
type LogFile struct {
Path string
Tag string
}
func init() {
initConfigAndFlags()
}
func initConfigAndFlags() {
flags = pflag.NewFlagSet(envPrefix, pflag.ExitOnError)
config = viper.New()
config.SetEnvPrefix(envPrefix)
// set defaults for configuration values that aren't provided by flags here:
config.SetDefault("destination.protocol", "udp")
config.SetDefault("tcp_max_line_length", 99990)
config.SetDefault("debug_log_file", "/dev/null")
config.SetDefault("connect_timeout", 30*time.Second)
config.SetDefault("write_timeout", 30*time.Second)
// flag-only "configuration" values (help and version)
flags.BoolP("help", "h", false, "Display this help message")
flags.BoolP("version", "V", false, "Display version and exit")
// set available commandline flags here:
flags.StringP("configfile", "c", defaultConfigFile, "Path to config")
config.BindPFlag("config_file", flags.Lookup("configfile"))
flags.StringP("dest-host", "d", "", "Destination syslog hostname or IP")
config.BindPFlag("destination.host", flags.Lookup("dest-host"))
flags.IntP("dest-port", "p", 514, "Destination syslog port")
config.BindPFlag("destination.port", flags.Lookup("dest-port"))
flags.StringP("facility", "f", "user", "Facility")
config.BindPFlag("facility", flags.Lookup("facility"))
hostname, _ := os.Hostname()
flags.String("hostname", hostname, "Local hostname to send from")
config.BindPFlag("hostname", flags.Lookup("hostname"))
flags.String("pid-file", "", "Location of the PID file")
config.BindPFlag("pid_file", flags.Lookup("pid-file"))
flags.StringP("severity", "s", "notice", "Severity")
config.BindPFlag("severity", flags.Lookup("severity"))
flags.Bool("tcp", false, "Connect via TCP (no TLS)")
config.BindPFlag("tcp", flags.Lookup("tcp"))
flags.Bool("tls", false, "Connect via TCP with TLS")
config.BindPFlag("tls", flags.Lookup("tls"))
flags.Bool("poll", false, "Detect changes by polling instead of inotify")
config.BindPFlag("poll", flags.Lookup("poll"))
flags.Int("new-file-check-interval", 10, "How often to check for new files (seconds)")
config.BindPFlag("new_file_check_interval", flags.Lookup("new-file-check-interval"))
flags.String("debug-log-cfg", "", "The debug log file; overridden by -D/--no-detach")
config.BindPFlag("debug_log_file", flags.Lookup("debug-log-cfg"))
flags.String("log", "<root>=INFO", "Set loggo config, like: --log=\"<root>=DEBUG\"")
config.BindPFlag("log_levels", flags.Lookup("log"))
// only present this flag to systems that can daemonize
if utils.CanDaemonize {
flags.BoolP("no-detach", "D", false, "Don't daemonize and detach from the terminal; overrides --debug-log-cfg")
config.BindPFlag("no_detach", flags.Lookup("no-detach"))
}
// deprecated flags
flags.Bool("no-eventmachine-tail", false, "No action, provided for backwards compatibility")
flags.Bool("eventmachine-tail", false, "No action, provided for backwards compatibility")
// bind env vars to config automatically
config.AutomaticEnv()
}
// Read in configuration from environment, flags, and specified or default config file.
func NewConfigFromEnv() (*Config, error) {
if err := flags.Parse(os.Args[1:]); err != nil {
return nil, err
}
if h, _ := flags.GetBool("help"); h {
usage()
return nil, ErrUsage
}
if v, _ := flags.GetBool("version"); v {
version()
return nil, ErrUsage
}
c := &Config{}
// read in config file if it's there
configFile := config.GetString("config_file")
config.SetConfigFile(configFile)
if err := config.ReadInConfig(); err != nil && configFile != defaultConfigFile {
return nil, err
}
// override daemonize setting for platforms that don't support it
if !utils.CanDaemonize {
config.Set("no_daemonize", true)
}
// unmarshal environment config into our Config object here
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Result: c,
WeaklyTypedInput: true,
DecodeHook: decodeHook,
})
if err != nil {
return nil, err
}
if err = decoder.Decode(config.AllSettings()); err != nil {
return nil, err
}
// explicitly set destination fields since they are nested
c.Destination.Host = config.GetString("destination.host")
c.Destination.Port = config.GetInt("destination.port")
c.Destination.Protocol = config.GetString("destination.protocol")
// explicitly set destination protocol if we've asked for tcp or tls
if c.TLS {
c.Destination.Protocol = "tls"
}
if c.TCP {
c.Destination.Protocol = "tcp"
}
// add the papertrail root CA if necessary
if c.Destination.Protocol == "tls" && c.Destination.Host == "logs.papertrailapp.com" {
c.RootCAs = papertrail.RootCA()
}
// figure out where to create a pidfile if none was configured
if c.PidFile == "" {
c.PidFile = getPidFile()
}
// collect any extra args passed on the command line and add them to our file list
for _, file := range flags.Args() {
files, err := decodeLogFiles([]interface{}{file})
if err != nil {
return nil, err
}
c.Files = append(c.Files, files...)
}
return c, nil
}
func (c *Config) Validate() error {
if c.Destination.Host == "" {
return fmt.Errorf("No destination hostname specified")
}
if c.NewFileCheckInterval < 1*time.Second {
return fmt.Errorf("new_file_check_interval is too small, try setting >= 1")
}
return nil
}
func decodeDuration(f interface{}) (time.Duration, error) {
var (
i int
err error
)
switch val := f.(type) {
case string:
i, err = strconv.Atoi(val)
if err != nil {
return 0, err
}
case int:
i = val
case time.Duration:
return val, nil
default:
return 0, fmt.Errorf("Invalid duration: %#v", val)
}
return time.Duration(i) * time.Second, nil
}
func decodeRegexps(f interface{}) ([]*regexp.Regexp, error) {
rs, ok := f.([]interface{})
if !ok {
return nil, fmt.Errorf("Invalid input type for regular expression %#v", f)
}
exps := make([]*regexp.Regexp, len(rs))
for i, r := range rs {
str, ok := r.(string)
if !ok {
return nil, fmt.Errorf("Invalid input type for regular expression %#v", r)
}
exp, err := regexp.Compile(str)
if err != nil {
return nil, err
}
exps[i] = exp
}
return exps, nil
}
func decodeLogFiles(f interface{}) ([]LogFile, error) {
var (
files []LogFile
)
vals, ok := f.([]interface{})
if !ok {
return files, fmt.Errorf("Invalid input type for files: %#v", f)
}
for _, v := range vals {
switch val := v.(type) {
case string:
lf := strings.Split(val, "=")
switch len(lf) {
case 2:
files = append(files, LogFile{Tag: lf[0], Path: lf[1]})
case 1:
files = append(files, LogFile{Path: val})
default:
return files, fmt.Errorf("Invalid log file name %s", val)
}
case map[interface{}]interface{}:
var (
tag string
path string
)
tag, _ = val["tag"].(string)
path, _ = val["path"].(string)
if path == "" {
return files, fmt.Errorf("Invalid log file %#v", val)
}
files = append(files, LogFile{Tag: tag, Path: path})
default:
panic(vals)
}
}
return files, nil
}
func decodePriority(p interface{}) (interface{}, error) {
ps, ok := p.(string)
if !ok {
return nil, fmt.Errorf("Invalid priority: %#v", p)
}
pri, err := syslog.Severity(ps)
if err == nil {
return pri, nil
}
// if it's not a severity, try facility
pri, err = syslog.Facility(ps)
if err == nil {
return pri, nil
}
return nil, fmt.Errorf("%s: %s", err.Error(), ps)
}
func decodeHook(from reflect.Type, to reflect.Type, data interface{}) (interface{}, error) {
switch to {
case reflect.TypeOf([]LogFile{}):
return decodeLogFiles(data)
case reflect.TypeOf([]*regexp.Regexp{}):
return decodeRegexps(data)
case reflect.TypeOf(syslog.Priority(0)):
return decodePriority(data)
case reflect.TypeOf(time.Duration(0)):
return decodeDuration(data)
}
return data, nil
}
func getPidFile() 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 usage() {
fmt.Fprintf(os.Stderr, "Usage of %s %s:\n", envPrefix, Version)
flags.PrintDefaults()
}
func version() {
fmt.Fprintf(os.Stderr, "%s %s\n", envPrefix, Version)
}