Skip to content

Commit 0b03a4c

Browse files
authoredMay 31, 2018
Merge pull request #7 from oscp/feature/chrony
Feature/chrony
2 parents 925672f + 236d857 commit 0b03a4c

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed
 

‎daemon/client/checks/common.go

+50
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,56 @@ func CheckExternalSystem(url string) error {
3434
return nil
3535
}
3636

37+
func CheckChrony() error {
38+
log.Println("Checking output of 'chronyc tracking'")
39+
40+
out, err := exec.Command("bash", "-c", "chronyc tracking").Output()
41+
if err != nil {
42+
msg := "Could not check chrony status: " + err.Error()
43+
log.Println(msg)
44+
return errors.New(msg)
45+
}
46+
47+
offset, err := parseChronyOffset(string(out))
48+
49+
if offset < -0.1 || offset > 0.1 { // 100 Millisekunden
50+
return errors.New("Time is not correct on the server or chrony is not running")
51+
} else {
52+
return nil
53+
}
54+
}
55+
56+
func parseChronyOffset(out string) (float64, error) {
57+
for _, line := range strings.Split(string(out), "\n") {
58+
if strings.Contains(line, "Last offset") {
59+
// Example output
60+
// Reference ID : 0A7CD814 (bn-ntp-01.sbb.ch)
61+
// Stratum : 2
62+
// Ref time (UTC) : Thu May 31 13:41:40 2018
63+
// System time : 0.000037743 seconds fast of NTP time
64+
// Last offset : +0.000061081 seconds <--- SECONDS
65+
// RMS offset : 0.000333012 seconds
66+
// Frequency : 6.629 ppm fast
67+
// Residual freq : +0.004 ppm
68+
// Skew : 0.140 ppm
69+
// Root delay : 0.002649408 seconds
70+
// Root dispersion : 0.000559144 seconds
71+
// Update interval : 517.4 seconds
72+
// Leap status : Normal
73+
rgx := regexp.MustCompile("(.*offset\\s+:\\s+)(.*?)\\s+seconds")
74+
offset := rgx.FindStringSubmatch(line)
75+
76+
log.Println("Found chrony offset:", offset[2])
77+
out, err := strconv.ParseFloat(offset[2], 64)
78+
if err != nil {
79+
return -1000, fmt.Errorf("couldn't parse chrony offset. Value was %v", offset[2])
80+
}
81+
return out, nil
82+
}
83+
}
84+
return -1000, fmt.Errorf("couldn't parse chrony offset. Offset line was not found.")
85+
}
86+
3787
func CheckNtpd() error {
3888
log.Println("Checking output of 'ntpq -c rv 0 offset'")
3989

‎daemon/client/handlers/minor.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func HandleMinorChecks(daemonType string, w http.ResponseWriter, r *http.Request
105105
}
106106
}
107107

108-
if err := checks.CheckNtpd(); err != nil {
108+
if err := checks.CheckChrony(); err != nil {
109109
errors = append(errors, err.Error())
110110
}
111111

0 commit comments

Comments
 (0)
Please sign in to comment.