Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions ocaml/idl/datamodel_host.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2627,6 +2627,19 @@ let get_ntp_servers_status =
)
~allowed_roles:_R_READ_ONLY ()

let get_ntp_synchronized =
call ~name:"get_ntp_synchronized" ~lifecycle:[]
~doc:
"Returns true if the system clock on the host is synchronized with the \
NTP servers."
~params:[(Ref _host, "self", "The host")]
~result:
( Bool
, "true if the system clock on the host is synchronized with the NTP \
servers."
)
~allowed_roles:_R_READ_ONLY ()

(** Hosts *)
let t =
create_obj ~in_db:true
Expand Down Expand Up @@ -2779,6 +2792,7 @@ let t =
; disable_ntp
; enable_ntp
; get_ntp_servers_status
; get_ntp_synchronized
]
~contents:
([
Expand Down
6 changes: 6 additions & 0 deletions ocaml/xapi/message_forwarding.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4170,6 +4170,12 @@ functor
let local_fn = Local.Host.get_ntp_servers_status ~self in
let remote_fn = Client.Host.get_ntp_servers_status ~self in
do_op_on ~local_fn ~__context ~host:self ~remote_fn

let get_ntp_synchronized ~__context ~self =
info "Host.get_ntp_synchronized: host = '%s'" (host_uuid ~__context self) ;
let local_fn = Local.Host.get_ntp_synchronized ~self in
let remote_fn = Client.Host.get_ntp_synchronized ~self in
do_op_on ~local_fn ~__context ~host:self ~remote_fn
end

module Host_crashdump = struct
Expand Down
7 changes: 7 additions & 0 deletions ocaml/xapi/xapi_globs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,8 @@ let ntp_dhcp_dir = ref "/run/chrony-dhcp"

let ntp_client_path = ref "/usr/bin/chronyc"

let timedatectl = ref "/usr/bin/timedatectl"

let udhcpd_skel = ref (Filename.concat "/etc/xensource" "udhcpd.skel")

let udhcpd_leases_db = ref "/var/lib/xcp/dhcp-leases.db"
Expand Down Expand Up @@ -1904,6 +1906,11 @@ let other_options =
, (fun () -> !ntp_client_path)
, "Path to the ntp client binary"
)
; ( "timedatectl"
, Arg.Set_string timedatectl
, (fun () -> !timedatectl)
, "Path to the timedatectl executable"
)
; gen_list_option "legacy-default-ntp-servers"
"space-separated list of legacy default NTP servers"
(fun s -> s)
Expand Down
7 changes: 7 additions & 0 deletions ocaml/xapi/xapi_host.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3555,3 +3555,10 @@ let get_ntp_servers_status ~__context ~self:_ =
Xapi_host_ntp.get_servers_status ()
else
[]

let get_ntp_synchronized ~__context ~self:_ =
match Xapi_host_ntp.is_synchronized () with
| Ok r ->
r
| Error msg ->
Helpers.internal_error "%s" msg
2 changes: 2 additions & 0 deletions ocaml/xapi/xapi_host.mli
Original file line number Diff line number Diff line change
Expand Up @@ -627,3 +627,5 @@ val sync_ntp_config : __context:Context.t -> host:API.ref_host -> unit

val get_ntp_servers_status :
__context:Context.t -> self:API.ref_host -> (string * string) list

val get_ntp_synchronized : __context:Context.t -> self:API.ref_host -> bool
9 changes: 9 additions & 0 deletions ocaml/xapi/xapi_host_ntp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,12 @@ let promote_legacy_default_servers () =
set_servers_in_conf defaults ;
restart_ntp_service ()
)

let is_synchronized () =
let patterns = ["System clock synchronized: yes"; "NTP synchronized: yes"] in
try
Helpers.call_script !Xapi_globs.timedatectl ["status"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option is to use c function ntp_gettime to get the maxerror of the system. That can avoid the timedatectl output changes in different versions. Although the current method is easy enough too. I'm OK for both.

|> String.split_on_char '\n'
|> List.exists ((Fun.flip List.mem) patterns)
|> Result.ok
with e -> Error (ExnHelper.string_of_exn e)
2 changes: 2 additions & 0 deletions ocaml/xapi/xapi_host_ntp.mli
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ val get_servers_from_conf : unit -> string list
val is_ntp_dhcp_enabled : unit -> bool

val get_servers_status : unit -> (string * string) list

val is_synchronized : unit -> (bool, string) Result.t