/
yaml.go
58 lines (48 loc) · 1.52 KB
/
yaml.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
package services
import (
"github.com/pkg/errors"
"github.com/statping/statping/utils"
"gopkg.in/yaml.v2"
)
type yamlFile struct {
Services []*Service `yaml:"services,flow"`
}
// LoadServicesYaml will attempt to load the 'services.yml' file for Service Auto Creation on startup.
func LoadServicesYaml() (*yamlFile, error) {
if !utils.FileExists(utils.Directory + "/services.yml") {
return nil, nil
}
f, err := utils.OpenFile(utils.Directory + "/services.yml")
if err != nil {
return nil, err
}
var svrs *yamlFile
if err := yaml.Unmarshal([]byte(f), &svrs); err != nil {
log.Errorln("Unable to parse the services.yml file", err)
return nil, err
}
log.Infof("Found %d services inside services.yml file", len(svrs.Services))
for _, svr := range svrs.Services {
serviceByHash := findServiceByHash(svr.Hash())
if serviceByHash == nil {
if err := svr.Create(); err != nil {
return nil, errors.Wrapf(err, "could not create service %s", svr.Name)
}
log.Infof("Automatically creating service '%s' checking %s", svr.Name, svr.Domain)
go ServiceCheckQueue(svr, true)
} else {
log.Infof("Service %s #%d, already inserted", svr.Name, serviceByHash.Id)
}
}
return svrs, nil
}
// findServiceByHas will return a service that matches the SHA256 hash of a service
// Service hash example: sha256(name:EXAMPLEdomain:HTTP://DOMAIN.COMport:8080type:HTTPmethod:GET)
func findServiceByHash(hash string) *Service {
for _, service := range All() {
if service.Hash() == hash {
return service
}
}
return nil
}