forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
61 lines (50 loc) · 1.44 KB
/
service.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
package ntp
import (
"path"
"regexp"
"strings"
boshdir "github.com/cloudfoundry/bosh-agent/settings/directories"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
var (
offsetRegex = regexp.MustCompile(`^(.+)\s+ntpdate.+offset\s+(-*\d+\.\d+)`)
badServerRegex = regexp.MustCompile(`no server suitable for synchronization found`)
)
type Info struct {
Offset string `json:"offset,omitempty"`
Timestamp string `json:"timestamp,omitempty"`
Message string `json:"message,omitempty"`
}
type Service interface {
GetInfo() (ntpInfo Info)
}
type concreteService struct {
fs boshsys.FileSystem
dirProvider boshdir.Provider
}
func NewConcreteService(fs boshsys.FileSystem, dirProvider boshdir.Provider) Service {
return concreteService{
fs: fs,
dirProvider: dirProvider,
}
}
func (oc concreteService) GetInfo() Info {
ntpPath := path.Join(oc.dirProvider.BaseDir(), "/bosh/log/ntpdate.out")
content, err := oc.fs.ReadFileString(ntpPath)
if err != nil {
return Info{Message: "file missing"}
}
lines := strings.Split(strings.Trim(content, "\n"), "\n")
lastLine := lines[len(lines)-1]
matches := offsetRegex.FindAllStringSubmatch(lastLine, -1)
if len(matches) > 0 && len(matches[0]) == 3 {
return Info{
Timestamp: matches[0][1],
Offset: matches[0][2],
}
} else if badServerRegex.MatchString(lastLine) {
return Info{Message: "bad ntp server"}
} else {
return Info{Message: "bad file contents"}
}
}