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 pathsource-loader.go
393 lines (367 loc) · 13.7 KB
/
source-loader.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
// Load configuration file.
package main
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"launchpad.net/goyaml"
"net/http"
"regexp"
"strings"
"sync"
"time"
)
// Exported config structure
type Config struct {
StreamsHLS []Stream
StreamsHDS []Stream
StreamsHTTP []Stream
StreamsWV []Stream
GroupsHLS map[string]string // map[group]group
GroupsHDS map[string]string // map[group]group
GroupsHTTP map[string]string // map[group]group
GroupsWV map[string]string // map[group]group
Groups map[Group][]string
Samples []string
Params Params
GroupParams map[string]Params
m sync.Mutex
}
// Internal config structure parsed from YAML
type configYAML struct {
StreamsHLS map[string][]string `yaml:"hls-streams,omitempty"` // HLS parsing
StreamsHDS map[string][]string `yaml:"hds-streams,omitempty"` // HDS parsing
StreamsHTTP map[string][]string `yaml:"http-streams,omitempty"` // plain HTTP checks
StreamsWV map[string][]string `yaml:"wv-streams,omitempty"` // HTTP checks with additional WV VOD checks
GetStreamsHLS []string `yaml:"get-hls-streams,omitempty"` // load remote HLS checks configuration
GetStreamsHDS []string `yaml:"get-hds-streams,omitempty"` // load remote HLS checks configuration
GetStreamsHTTP []string `yaml:"get-http-streams,omitempty"` // load remote HTTP checks configuration
GetStreamsWV []string `yaml:"get-wv-streams,omitempty"` // load remote WV VOD checks configuration
Samples []string `yaml:"samples"`
Params Params `yaml:"params"`
GroupParams map[string]Params `yaml:"group-params,omitempty"` // parameters per group
Stubs StubValues `yaml:"stubs"`
}
type Params struct {
ProbersHTTP uint `yaml:"http-probers,omitempty"` // num of
ProbersHLS uint `yaml:"hls-probers,omitempty"` // num of
ProbersHDS uint `yaml:"hds-probers,omitempty"` // num of
ProbersWV uint `yaml:"wv-probers,omitempty"` // num of
MediaProbers uint `yaml:"media-probers,omitempty"` // num of
CheckBrokenTime uint `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
ListenHTTP string `yaml:"http-api-listen,omitempty"`
ErrorLog string `yaml:"error-log,omitempty"`
Zabbix Zabbix `yaml:"zabbix,omitempty"`
ParseName string `yaml:"parse-name,omitempty"` // regexp for alternative method of title/name parsing from the URL
User string `yaml:"user,omitempty"`
Pass string `yaml:"pass,omitempty"`
}
type Zabbix struct {
DiscoveryPath string `yaml:"discovery-path,omitempty"`
DiscoveryGroups []string `yaml:"discovery-groups,omitempty"`
StreamTemplate string `yaml:"stream-template,omitempty"`
}
// custom values for HTML-templates and reports
type StubValues struct {
Name string `yaml:"name,omitempty"`
}
// глобальный конфиг
var cfg *Config
// XXX выпилить
//var NameParseMode string // regexp for parse name/title from the stream URL, by default string splitted by space and http:// part becomes URL other part becomes stream name
func ReadConfig(confile string) *Config {
var cfg = &configYAML{}
// Hardcoded defaults:
cfg.Stubs = StubValues{Name: "Stream Surfer"}
// Final config:
Cfg := new(Config)
if confile == "" {
confile = "/etc/streamsurfer/default.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")
}
Cfg.Groups = make(map[Group][]string) // TODO свести использование к Cfg.Groups, убрать GroupsHLS, GroupsHDS, GroupsHTTP
Cfg.GroupsHLS = make(map[string]string)
Cfg.GroupsHDS = make(map[string]string)
Cfg.GroupsHTTP = make(map[string]string)
Cfg.GroupsWV = make(map[string]string)
Cfg.Params = cfg.Params
Cfg.Samples = cfg.Samples
Cfg.Params.MethodHTTP = strings.ToUpper(cfg.Params.MethodHTTP)
Stubs = &cfg.Stubs
Cfg.GroupParams = make(map[string]Params)
Cfg.GroupParams = cfg.GroupParams
// for group, params := range cfg.GroupParams {
// if
// Cfg.GroupParams[group] = cfg.Params
// fmt.Printf("%s %v\n", group, params)
// // if params.ProbersHTTP != 0 {
// // Cfg.GroupParams[group].ProbersHTTP = params.ProbersHTTP
// // }
// // if params.ProbersHLS != 0 {
// // Cfg.GroupParams[group].ProbersHLS = params.ProbersHLS
// // }
// // if cfg.GroupParams[group].ProbersHDS != 0 {
// // Cfg.GroupParams[group].ProbersHDS = cfg.GroupParams[group].ProbersHDS
// // }
// // if cfg.GroupParams[group].ProbersWV != 0 {
// // Cfg.GroupParams[group].ProbersWV = cfg.GroupParams[group].ProbersWV
// // }
// if params.ParseName != "" {
// Cfg.GroupParams[group].ParseName = params.ParseName
// }
// // if cfg.GroupParams[group].User != "" {
// // Cfg.GroupParams[group].User = cfg.GroupParams[group].User
// // Cfg.GroupParams[group].Pass = cfg.GroupParams[group].Pass
// // }
// }
for groupName, streamList := range cfg.StreamsHLS {
nameList := addLocalConfig(&Cfg.StreamsHLS, HLS, groupName, streamList)
Cfg.GroupsHLS[groupName] = groupName
Cfg.Groups[Group{HLS, groupName}] = nameList
}
for groupName, streamList := range cfg.StreamsHDS {
nameList := addLocalConfig(&Cfg.StreamsHDS, HDS, groupName, streamList)
Cfg.GroupsHDS[groupName] = groupName
Cfg.Groups[Group{HDS, groupName}] = nameList
}
for groupName, streamList := range cfg.StreamsHTTP {
nameList := addLocalConfig(&Cfg.StreamsHTTP, HTTP, groupName, streamList)
Cfg.GroupsHTTP[groupName] = groupName
Cfg.Groups[Group{HTTP, groupName}] = nameList
}
for groupName, streamList := range cfg.StreamsWV {
nameList := addLocalConfig(&Cfg.StreamsWV, WV, groupName, streamList)
Cfg.GroupsWV[groupName] = groupName
Cfg.Groups[Group{WV, groupName}] = nameList
}
if cfg.GetStreamsHLS != nil {
for _, source := range cfg.GetStreamsHLS {
groupURI, groupName := splitName("", source)
remoteUser := ""
remotePass := ""
if _, exists := cfg.GroupParams[groupName]; exists {
remoteUser = cfg.GroupParams[groupName].User
remotePass = cfg.GroupParams[groupName].Pass
}
nameList, err := addRemoteConfig(&Cfg.StreamsHLS, HLS, groupName, groupURI, remoteUser, remotePass)
if err != nil {
fmt.Printf("Load remote config for group \"%s\" (HLS) failed.\n", groupName)
} else {
Cfg.GroupsHLS[groupName] = groupName
Cfg.Groups[Group{HLS, groupName}] = nameList
}
}
}
fmt.Printf("%+v\n", Cfg.GroupParams)
if cfg.GetStreamsHDS != nil {
for _, source := range cfg.GetStreamsHDS {
groupURI, groupName := splitName("", source)
remoteUser := ""
remotePass := ""
if _, exists := cfg.GroupParams[groupName]; exists {
remoteUser = cfg.GroupParams[groupName].User
remotePass = cfg.GroupParams[groupName].Pass
}
nameList, err := addRemoteConfig(&Cfg.StreamsHDS, HDS, groupName, groupURI, remoteUser, remotePass)
if err != nil {
fmt.Printf("Load remote config for group \"%s\" (HDS) failed.\n", groupName)
} else {
Cfg.GroupsHDS[groupName] = groupName
Cfg.Groups[Group{HDS, groupName}] = nameList
}
}
}
if cfg.GetStreamsHTTP != nil {
for _, source := range cfg.GetStreamsHTTP {
groupURI, groupName := splitName("", source)
remoteUser := ""
remotePass := ""
if _, exists := cfg.GroupParams[groupName]; exists {
remoteUser = cfg.GroupParams[groupName].User
remotePass = cfg.GroupParams[groupName].Pass
}
nameList, err := addRemoteConfig(&Cfg.StreamsHTTP, HTTP, groupName, groupURI, remoteUser, remotePass)
if err != nil {
fmt.Printf("Load remote config for group \"%s\" (HTTP) failed.\n", groupName)
} else {
Cfg.GroupsHTTP[groupName] = groupName
Cfg.Groups[Group{HTTP, groupName}] = nameList
}
}
}
if cfg.GetStreamsWV != nil {
for _, source := range cfg.GetStreamsWV {
groupURI, groupName := splitName("", source)
remoteUser := ""
remotePass := ""
if _, exists := cfg.GroupParams[groupName]; exists {
remoteUser = cfg.GroupParams[groupName].User
remotePass = cfg.GroupParams[groupName].Pass
}
nameList, err := addRemoteConfig(&Cfg.StreamsWV, WV, groupName, groupURI, remoteUser, remotePass)
if err != nil {
fmt.Printf("Load remote config for group \"%s\" (WV) failed.\n", groupName)
} else {
Cfg.GroupsWV[groupName] = groupName
Cfg.Groups[Group{WV, groupName}] = nameList
}
}
}
for group, groupParams := range cfg.GroupParams {
if groupParams.ProbersHLS == 0 {
groupParams.ProbersHLS = cfg.Params.ProbersHLS
}
if groupParams.ProbersHDS == 0 {
groupParams.ProbersHDS = cfg.Params.ProbersHDS
}
if groupParams.ProbersHTTP == 0 {
groupParams.ProbersHTTP = cfg.Params.ProbersHTTP
}
if groupParams.ProbersWV == 0 {
groupParams.ProbersWV = cfg.Params.ProbersWV
}
if groupParams.MediaProbers == 0 {
groupParams.MediaProbers = cfg.Params.MediaProbers
}
if groupParams.CheckBrokenTime == 0 {
groupParams.CheckBrokenTime = cfg.Params.CheckBrokenTime
}
if groupParams.ConnectTimeout == 0 {
groupParams.ConnectTimeout = cfg.Params.ConnectTimeout
}
if groupParams.RWTimeout == 0 {
groupParams.RWTimeout = cfg.Params.RWTimeout
}
if groupParams.SlowWarningTimeout == 0 {
groupParams.SlowWarningTimeout = cfg.Params.SlowWarningTimeout
}
if groupParams.VerySlowWarningTimeout == 0 {
groupParams.VerySlowWarningTimeout = cfg.Params.VerySlowWarningTimeout
}
if groupParams.TimeBetweenTasks == 0 {
groupParams.TimeBetweenTasks = cfg.Params.TimeBetweenTasks
}
if groupParams.TaskTTL == 0 {
groupParams.TaskTTL = cfg.Params.TaskTTL
}
if groupParams.TryOneSegment {
groupParams.TryOneSegment = cfg.Params.TryOneSegment
}
if groupParams.ListenHTTP == "" {
groupParams.ListenHTTP = cfg.Params.ListenHTTP
}
if groupParams.ErrorLog == "" {
groupParams.ErrorLog = cfg.Params.ErrorLog
}
Cfg.GroupParams[group] = groupParams
}
} else {
print("Config file not found. Hardcoded defaults used.\n")
}
// fmt.Printf("HLS: %+v\n\n", Cfg.StreamsHLS)
//fmt.Printf("HTTP: %+v\n\n", Cfg.GroupsHTTP)
return Cfg
}
// Helper. Split stream link to URI and Name parts.
// Supported both cases: title<space>uri and uri<space>title
// URI must be prepended by http:// or https://
func splitName(group, source string) (uri string, name 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 {
name = strings.TrimSpace(splitted[1])
}
uri = strings.TrimSpace(splitted[0])
} else { // title uri
name = strings.TrimSpace(source[0:loc[0]])
uri = source[loc[0]:]
}
if name == "" {
name = uri
}
}
if group == "" {
return // для парсинга имён групп в YAML-конфиге не применяется парсинг по регвырам
}
if params, err := cfg.Params4(group); err == nil && params.ParseName != "" {
re := regexp.MustCompile(params.ParseName)
vals := re.FindStringSubmatch(uri)
if len(vals) > 1 {
name = vals[1]
}
}
return
}
// Helper. Parse config of
func addLocalConfig(dest *[]Stream, streamType StreamType, group string, sources []string) []string {
var nameList []string
for _, source := range sources {
uri, name := splitName(group, source)
*dest = append(*dest, Stream{URI: uri, Type: streamType, Name: name, Group: group})
nameList = append(nameList, name)
}
return nameList
}
// Helper. Get remote list of streams.
func addRemoteConfig(dest *[]Stream, streamType StreamType, group string, uri, remoteUser, remotePass string) ([]string, error) {
var nameList []string
defer func() error {
if r := recover(); r != nil {
return errors.New(fmt.Sprintf("Can't get remote config for (%s) %s %s", streamType, group, uri))
}
return nil
}()
client := NewTimeoutClient(10*time.Second, 10*time.Second)
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return nil, err
}
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 := splitName(group, line)
nameList = append(nameList, name)
*dest = append(*dest, Stream{URI: uri, Type: streamType, Name: name, Group: group})
}
}
return nameList, err
}
// TODO Dynamic configuration without program restart.
// Elder.
func ConfigKeeper(confile string) {
cfg = new(Config)
cfg = ReadConfig(confile)
select {} // TODO reload config by query
}
func (cfg *Config) Params4(group string) (*Params, error) {
cfg.m.Lock()
defer cfg.m.Unlock()
if val, ok := cfg.GroupParams[group]; !ok {
return &cfg.Params, nil
} else {
return &val, nil
}
}