Skip to content

Commit 18db42a

Browse files
committed
CP-308856 host.get_ntp_synchronized
Signed-off-by: Ming Lu <ming.lu@cloud.com>
1 parent 4a67e13 commit 18db42a

File tree

6 files changed

+40
-0
lines changed

6 files changed

+40
-0
lines changed

ocaml/idl/datamodel_host.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2646,6 +2646,19 @@ let list_timezones =
26462646
~result:(Set String, "The set of available timezones on the host")
26472647
~allowed_roles:_R_READ_ONLY ()
26482648

2649+
let get_ntp_synchronized =
2650+
call ~name:"get_ntp_synchronized" ~lifecycle:[]
2651+
~doc:
2652+
"Returns true if the system clock on the host is synchronized with the \
2653+
NTP servers."
2654+
~params:[(Ref _host, "self", "The host")]
2655+
~result:
2656+
( Bool
2657+
, "true if the system clock on the host is synchronized with the NTP \
2658+
servers."
2659+
)
2660+
~allowed_roles:_R_READ_ONLY ()
2661+
26492662
(** Hosts *)
26502663
let t =
26512664
create_obj ~in_db:true
@@ -2800,6 +2813,7 @@ let t =
28002813
; get_ntp_servers_status
28012814
; set_timezone
28022815
; list_timezones
2816+
; get_ntp_synchronized
28032817
]
28042818
~contents:
28052819
([

ocaml/xapi/message_forwarding.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4184,6 +4184,12 @@ functor
41844184
let local_fn = Local.Host.list_timezones ~self in
41854185
let remote_fn = Client.Host.list_timezones ~self in
41864186
do_op_on ~local_fn ~__context ~host:self ~remote_fn
4187+
4188+
let get_ntp_synchronized ~__context ~self =
4189+
info "Host.get_ntp_synchronized: host = '%s'" (host_uuid ~__context self) ;
4190+
let local_fn = Local.Host.get_ntp_synchronized ~self in
4191+
let remote_fn = Client.Host.get_ntp_synchronized ~self in
4192+
do_op_on ~local_fn ~__context ~host:self ~remote_fn
41874193
end
41884194

41894195
module Host_crashdump = struct

ocaml/xapi/xapi_host.ml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3581,3 +3581,10 @@ let list_timezones ~__context ~self:_ =
35813581
Helpers.call_script !Xapi_globs.timedatectl ["list-timezones"]
35823582
|> Astring.String.cuts ~empty:false ~sep:"\n"
35833583
with e -> Helpers.internal_error "%s" (ExnHelper.string_of_exn e)
3584+
3585+
let get_ntp_synchronized ~__context ~self:_ =
3586+
match Xapi_host_ntp.is_synchronized () with
3587+
| Ok r ->
3588+
r
3589+
| Error msg ->
3590+
Helpers.internal_error "%s" msg

ocaml/xapi/xapi_host.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,3 +632,5 @@ val set_timezone :
632632
__context:Context.t -> self:API.ref_host -> value:string -> unit
633633

634634
val list_timezones : __context:Context.t -> self:API.ref_host -> string list
635+
636+
val get_ntp_synchronized : __context:Context.t -> self:API.ref_host -> bool

ocaml/xapi/xapi_host_ntp.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,12 @@ let promote_legacy_default_servers () =
175175
set_servers_in_conf defaults ;
176176
restart_ntp_service ()
177177
)
178+
179+
let is_synchronized () =
180+
let patterns = ["System clock synchronized: yes"; "NTP synchronized: yes"] in
181+
try
182+
Helpers.call_script !Xapi_globs.timedatectl ["status"]
183+
|> String.split_on_char '\n'
184+
|> List.exists ((Fun.flip List.mem) patterns)
185+
|> Result.ok
186+
with e -> Error (ExnHelper.string_of_exn e)

ocaml/xapi/xapi_host_ntp.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ val get_servers_from_conf : unit -> string list
3535
val is_ntp_dhcp_enabled : unit -> bool
3636

3737
val get_servers_status : unit -> (string * string) list
38+
39+
val is_synchronized : unit -> (bool, string) Result.t

0 commit comments

Comments
 (0)