forked from elastic/logstash-forwarder
-
Notifications
You must be signed in to change notification settings - Fork 5
/
config.go
204 lines (183 loc) · 5.05 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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"log"
"os"
"time"
)
const default_NetworkConfig_Timeout int64 = 15
const default_NetworkConfig_Reconnect int64 = 1
const default_FileConfig_DeadTime string = "24h"
type Config struct {
Network NetworkConfig `json:network`
Files []FileConfig `json:files`
}
type NetworkConfig struct {
Servers []string `json:servers`
SSLCertificate string `json:"ssl certificate"`
SSLKey string `json:"ssl key"`
SSLCA string `json:"ssl ca"`
Timeout int64 `json:timeout`
timeout time.Duration
Reconnect int64 `json:reconnect`
reconnect time.Duration
}
type FileConfig struct {
Paths []string `json:paths`
Fields map[string]*string `json:fields`
Codec map[string]interface{} `json:codec`
codec CodecFactory
DeadTime string `json:"dead time"`
deadtime time.Duration
}
func LoadConfig(path string) (config *Config, err error) {
config_file, err := os.Open(path)
if err != nil {
log.Printf("Failed to open config file '%s': %s\n", path, err)
return
}
defer config_file.Close()
fi, err := config_file.Stat()
if err != nil {
log.Printf("Stat failed for config file: %s\n", err)
return
}
if fi.Size() > (10 << 20) {
err = errors.New("Config file too large?")
log.Printf("Config file too large? Aborting, just in case. '%s' is %d bytes\n", path, fi)
return
}
// Strip comments and read config into stripped
var s, p, state int
var stripped bytes.Buffer
{
// Pull the config file into memory
buffer := make([]byte, fi.Size())
_, err = config_file.Read(buffer)
if err != nil {
log.Printf("Failed reading config file: %s\n", err)
return nil, err
}
for p < len(buffer) {
b := buffer[p]
if state == 0 {
// Main body
if b == '"' {
state = 1
} else if b == '\'' {
state = 2
} else if b == '#' {
state = 3
stripped.Write(buffer[s:p])
} else if b == '/' {
state = 4
}
} else if state == 1 {
// Double-quoted string
if b == '\\' {
state = 5
} else if b == '"' {
state = 0
}
} else if state == 2 {
// Single-quoted string
if b == '\\' {
state = 6
} else if b == '\'' {
state = 0
}
} else if state == 3 {
// End of line comment (#)
if b == '\r' || b == '\n' {
state = 0
s = p
}
} else if state == 4 {
// Potential start of multiline comment
if b == '*' {
state = 7
stripped.Write(buffer[s : p-1])
} else {
state = 0
}
} else if state == 5 {
// Escape within double quote
state = 1
} else if state == 6 {
// Escape within single quote
state = 2
} else if state == 7 {
// Multiline comment (/**/)
if b == '*' {
state = 8
}
} else { // state == 8
// Potential end of multiline comment
if b == '/' {
state = 0
s = p + 1
} else {
state = 7
}
}
p++
}
stripped.Write(buffer[s:p])
log.Printf("%s\n", stripped.String())
}
config = &Config{}
err = json.Unmarshal(stripped.Bytes(), config)
if err != nil {
log.Printf("Failed unmarshalling json: %s\n", err)
return
}
if config.Network.Timeout == 0 {
config.Network.Timeout = default_NetworkConfig_Timeout
}
config.Network.timeout = time.Duration(config.Network.Timeout) * time.Second
if config.Network.Reconnect == 0 {
config.Network.Reconnect = default_NetworkConfig_Reconnect
}
config.Network.reconnect = time.Duration(config.Network.Reconnect) * time.Second
for k := range config.Files {
var codec_name string
if config.Files[k].Codec != nil {
var ok bool
codec_name, ok = config.Files[k].Codec["name"].(string)
if !ok {
err = errors.New("The name of the codec must be specified.")
log.Printf(fmt.Sprint(err))
return
}
} else {
config.Files[k].Codec = make(map[string]interface{}, 1)
config.Files[k].Codec["name"] = "plain"
codec_name = "plain"
}
var factory CodecFactory;
if factory, err = NewCodecFactory(codec_name, config.Files[k].Codec); err == nil {
if factory != nil {
config.Files[k].codec = factory
} else {
err = errors.New(fmt.Sprintf("Unrecognised codec '%s'. Please check your configuration.\n", codec_name))
log.Printf("%s", err)
return
}
} else {
log.Printf("%s", err)
return
}
if config.Files[k].DeadTime == "" {
config.Files[k].DeadTime = default_FileConfig_DeadTime
}
config.Files[k].deadtime, err = time.ParseDuration(config.Files[k].DeadTime)
if err != nil {
log.Printf("Failed to parse dead time duration '%s'. Error was: %s\n", config.Files[k].DeadTime, err)
return
}
}
return
}