@@ -34,6 +34,56 @@ func CheckExternalSystem(url string) error {
34
34
return nil
35
35
}
36
36
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
+
37
87
func CheckNtpd () error {
38
88
log .Println ("Checking output of 'ntpq -c rv 0 offset'" )
39
89
0 commit comments