This repository has been archived by the owner on Apr 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathconfig.go
283 lines (258 loc) · 8.92 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
// Load configuration file.
package main
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"launchpad.net/goyaml"
"net/http"
"regexp"
"strings"
"time"
)
// parsed and validated config data
type Config struct {
GroupParams map[string]*configGroup
GroupStreams map[string]*[]Stream // map[groupname]stream
Stubs configStub
Zabbix configZabbix
Samples []string
ListenHTTP string
User string
Pass string
UserAgents []string
ErrorLog string
ExpireDurationDB time.Duration // measured in hours
IsReady chan bool // config parsed and ready to use
}
// parsed grup config
type configGroup struct {
Type StreamType
Probers int
MediaProbers int
CheckBrokenTime int
ConnectTimeout time.Duration
RWTimeout time.Duration
SlowWarningTimeout time.Duration
VerySlowWarningTimeout time.Duration
TimeBetweenTasks time.Duration
TaskTTL time.Duration
TryOneSegment bool
MethodHTTP string
ParseMethod string
User string
Pass string
}
type configZabbix struct {
DiscoveryPath string `yaml:"discovery-path,omitempty"`
DiscoveryGroups []string `yaml:"discovery-groups,omitempty"`
NameTemplate string `yaml:"name-template,omitempty"`
TitleTemplate string `yaml:"title-template,omitempty"`
}
// custom values for HTML-templates and reports
type configStub struct {
Name string `yaml:"name,omitempty"`
}
// raw config data
type configYAML struct {
ListenHTTP string `yaml:"http-api-listen,omitempty"`
User string `yaml:"http-api-user,omitempty"`
Pass string `yaml:"http-api-pass,omitempty"`
Stubs configStub `yaml:"stubs,omitempty"`
Zabbix configZabbix `yaml:"zabbix,omitempty"`
Samples []string `yaml:"unmortal,omitempty"`
UserAgents []string `yaml:"user-agents,omitempty"`
Defaults configGroupYAML `yaml:"defaults,omitempty"`
Groups map[string]configGroupYAML `yaml:"groups,omitempty"`
ExpireDurationDB time.Duration `yaml:"db-expired"` // measured in hours
}
// rawconfig group data
type configGroupYAML struct {
Type string `yaml:"type,omitempty"`
URI string `yaml:"streams-uri,omitempty"` // external link list
Streams []string `yaml:"streams,omitempty"` // link list
Probers int `yaml:"probers,omitempty"` // num of
MediaProbers int `yaml:"media-probers,omitempty"` // num of
CheckBrokenTime int `yaml:"check-broken-time"` // ms
ConnectTimeout time.Duration `yaml:"connect-timeout,omitempty"` // sec
RWTimeout time.Duration `yaml:"rw-timeout,omitempty"` // sec
SlowWarningTimeout time.Duration `yaml:"slow-warning-timeout,omitempty"` // sec
VerySlowWarningTimeout time.Duration `yaml:"very-slow-warning-timeout,omitempty"` // sec
TimeBetweenTasks time.Duration `yaml:"time-between-tasks,omitempty"` // sec
TaskTTL time.Duration `yaml:"task-ttl,omitempty"` // sec
TryOneSegment bool `yaml:"one-segment,omitempty"`
MethodHTTP string `yaml:"http-method,omitempty"` // GET, HEAD
ErrorLog string `yaml:"error-log,omitempty"`
ParseMethod string `yaml:"parse-method,omitempty"` // regexp for alternative method of title/name parsing from the URL
User string `yaml:"user,omitempty"`
Pass string `yaml:"pass,omitempty"`
}
var cfg *Config
// TODO Dynamic configuration without program restart.
// Elder.
func InitConfig(confile string) {
rawcfg := rawConfig(confile)
cfg = new(Config)
cfg.IsReady = make(chan bool, 1)
parseOptionsConfig(rawcfg)
parseGroupsConfig(rawcfg)
cfg.IsReady <- true
}
// XXX for future
func ConfigKeeper() {
<-cfg.IsReady
select {} // TODO reload config by query
}
//
func (cfg *Config) Params(gname string) configGroup {
if data, ok := cfg.GroupParams[gname]; ok {
return *data
} else {
return configGroup{}
}
}
//
func (cfg *Config) Streams() {
}
// Read raw config with YAML validation
func rawConfig(confile string) *configYAML {
cfg := new(configYAML)
// Hardcoded defaults:
cfg.Stubs = configStub{Name: "Stream Surfer"}
if confile == "" {
confile = "/etc/streamsurfer.yaml"
}
data, e := ioutil.ReadFile(FullPath(confile))
if e == nil {
e = goyaml.Unmarshal(data, &cfg)
if e != nil {
print("Config file parsing failed. Hardcoded defaults used.\n")
}
}
return cfg
}
//
func parseOptionsConfig(rawcfg *configYAML) {
cfg.ListenHTTP = rawcfg.ListenHTTP
cfg.User = rawcfg.User
cfg.Pass = rawcfg.Pass
cfg.Stubs = rawcfg.Stubs
cfg.Zabbix = rawcfg.Zabbix
cfg.Samples = rawcfg.Samples
cfg.UserAgents = rawcfg.UserAgents
cfg.ExpireDurationDB = rawcfg.ExpireDurationDB * time.Hour
}
//
func parseGroupsConfig(rawcfg *configYAML) {
cfg.GroupParams = make(map[string]*configGroup)
cfg.GroupStreams = make(map[string]*[]Stream)
for gname, gdata := range rawcfg.Groups {
stype := String2StreamType(gdata.Type)
cfg.GroupParams[gname] = &configGroup{
Type: stype,
Probers: gdata.Probers,
MediaProbers: gdata.MediaProbers,
CheckBrokenTime: gdata.CheckBrokenTime,
ParseMethod: gdata.ParseMethod,
TimeBetweenTasks: gdata.TimeBetweenTasks,
ConnectTimeout: gdata.ConnectTimeout,
RWTimeout: gdata.RWTimeout,
SlowWarningTimeout: gdata.SlowWarningTimeout,
VerySlowWarningTimeout: gdata.VerySlowWarningTimeout,
TaskTTL: gdata.TaskTTL,
TryOneSegment: gdata.TryOneSegment,
MethodHTTP: strings.ToUpper(gdata.MethodHTTP),
User: gdata.User,
Pass: gdata.Pass,
}
if gdata.URI != "" {
cfg.GroupStreams[gname] = new([]Stream)
addRemoteConfig(cfg.GroupStreams[gname], cfg.GroupParams[gname], gname, gdata.URI, gdata.User, gdata.Pass)
} else {
cfg.GroupStreams[gname] = new([]Stream)
addLocalConfig(cfg.GroupStreams[gname], cfg.GroupParams[gname], gname, gdata.Streams)
}
}
// println("----------------------------------------")
// for key, data := range cfg.GroupParams {
// fmt.Printf("%s %#v\n", key, data)
// }
// println("----------------------------------------")
// for key, data := range cfg.GroupStreams {
// fmt.Printf("%s %#v\n", key, data)
// }
}
// Helper. Get remote list of streams.
func addRemoteConfig(dest *[]Stream, params *configGroup, group string, uri, remoteUser, remotePass string) error {
defer func() error {
if r := recover(); r != nil {
return errors.New(fmt.Sprintf("Can't get remote config for (%s) %s %s", params.Type, group, uri))
}
return nil
}()
client := NewTimeoutClient(10*time.Second, 10*time.Second)
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return err
}
if remoteUser != "" {
req.SetBasicAuth(remoteUser, remotePass)
}
result, err := client.Do(req)
if err == nil {
body := bufio.NewReader(result.Body)
for {
line, err := body.ReadString('\n')
if err != nil {
break
}
uri, name, title := splitName(params.ParseMethod, line)
*dest = append(*dest, Stream{URI: uri, Type: params.Type, Name: name, Title: title, Group: group})
}
}
return err
}
// Helper. Parse config of
func addLocalConfig(dest *[]Stream, params *configGroup, group string, sources []string) {
for _, source := range sources {
uri, name, title := splitName(params.ParseMethod, source)
*dest = append(*dest, Stream{URI: uri, Type: params.Type, Name: name, Title: title, Group: group})
}
}
// Helper. Split stream link to URI and Name parts.
// Supported both cases: title<space>uri and uri<space>title
// If `re` presents then name parsed from uri by regular expression.
// URI must be prepended by http:// or https://
func splitName(re, source string) (uri, name, title string) {
source = strings.TrimSpace(source)
sep := regexp.MustCompile("htt(p|ps)://")
loc := sep.FindStringIndex(source)
if loc != nil {
if loc[0] == 0 { // uri title
splitted := strings.SplitN(source, " ", 2)
if len(splitted) > 1 {
title = strings.TrimSpace(splitted[1])
}
uri = strings.TrimSpace(splitted[0])
} else { // title uri
title = strings.TrimSpace(source[0:loc[0]])
uri = source[loc[0]:]
}
if title == "" {
title = uri
}
}
if re != "" { // get name by regexp
compiledRe := regexp.MustCompile(re)
vals := compiledRe.FindStringSubmatch(uri)
if len(vals) > 1 {
name = vals[1]
} else {
name = title
}
} else {
name = title
}
return
}