Skip to content

Commit 409244f

Browse files
committed
CP-308854 Add host.get_ntp_servers_status
Signed-off-by: Changlei Li <changlei.li@cloud.com>
1 parent 680f30d commit 409244f

File tree

7 files changed

+75
-0
lines changed

7 files changed

+75
-0
lines changed

ocaml/idl/datamodel_host.ml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2585,6 +2585,17 @@ let enable_ntp =
25852585
~params:[(Ref _host, "self", "The host")]
25862586
~allowed_roles:_R_POOL_OP ()
25872587

2588+
let get_ntp_servers_status =
2589+
call ~name:"get_ntp_servers_status" ~lifecycle:[]
2590+
~doc:"Get the NTP servers status on the host"
2591+
~params:[(Ref _host, "self", "The host")]
2592+
~result:
2593+
( Map (String, String)
2594+
, "The map of NTP server to its status, status may be \
2595+
synced/combined/uncombined/error/variable/unreachable/unknown"
2596+
)
2597+
~allowed_roles:_R_READ_ONLY ()
2598+
25882599
(** Hosts *)
25892600
let t =
25902601
create_obj ~in_db:true
@@ -2734,6 +2745,7 @@ let t =
27342745
; set_ntp_custom_servers
27352746
; disable_ntp
27362747
; enable_ntp
2748+
; get_ntp_servers_status
27372749
]
27382750
~contents:
27392751
([

ocaml/xapi/message_forwarding.ml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4150,6 +4150,13 @@ functor
41504150
let local_fn = Local.Host.disable_ntp ~self in
41514151
let remote_fn = Client.Host.disable_ntp ~self in
41524152
do_op_on ~local_fn ~__context ~host:self ~remote_fn
4153+
4154+
let get_ntp_servers_status ~__context ~self =
4155+
info "Host.get_ntp_servers_status: host = '%s'"
4156+
(host_uuid ~__context self) ;
4157+
let local_fn = Local.Host.get_ntp_servers_status ~self in
4158+
let remote_fn = Client.Host.get_ntp_servers_status ~self in
4159+
do_op_on ~local_fn ~__context ~host:self ~remote_fn
41534160
end
41544161

41554162
module Host_crashdump = struct

ocaml/xapi/xapi_globs.ml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,8 @@ let ntp_dhcp_script = ref (Filename.concat "/etc/dhcp/dhclient.d" "chrony.sh")
805805

806806
let ntp_dhcp_dir = ref "/run/chrony-dhcp"
807807

808+
let ntp_client_path = ref "/usr/bin/chronyc"
809+
808810
let udhcpd_skel = ref (Filename.concat "/etc/xensource" "udhcpd.skel")
809811

810812
let udhcpd_leases_db = ref "/var/lib/xcp/dhcp-leases.db"
@@ -1848,6 +1850,11 @@ let other_options =
18481850
, (fun () -> !ntp_dhcp_dir)
18491851
, "Path to the ntp dhcp directory"
18501852
)
1853+
; ( "ntp-client-path"
1854+
, Arg.Set_string ntp_client_path
1855+
, (fun () -> !ntp_client_path)
1856+
, "Path to the ntp client binary"
1857+
)
18511858
; gen_list_option "default-ntp-servers"
18521859
"space-separated list of default NTP servers"
18531860
(fun s -> s)

ocaml/xapi/xapi_host.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3461,3 +3461,9 @@ let sync_ntp_config ~__context ~host =
34613461
Db.Host.set_ntp_custom_servers ~__context ~self:host ~value:servers ;
34623462
let ntp_enabled = Xapi_host_ntp.is_ntp_service_active () in
34633463
Db.Host.set_ntp_enabled ~__context ~self:host ~value:ntp_enabled
3464+
3465+
let get_ntp_servers_status ~__context ~self:_ =
3466+
if Xapi_host_ntp.is_ntp_service_active () then
3467+
Xapi_host_ntp.get_servers_status ()
3468+
else
3469+
[]

ocaml/xapi/xapi_host.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,3 +607,6 @@ val enable_ntp : __context:Context.t -> self:API.ref_host -> unit
607607
val disable_ntp : __context:Context.t -> self:API.ref_host -> unit
608608

609609
val sync_ntp_config : __context:Context.t -> host:API.ref_host -> unit
610+
611+
val get_ntp_servers_status :
612+
__context:Context.t -> self:API.ref_host -> (string * string) list

ocaml/xapi/xapi_host_ntp.ml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,41 @@ let is_ntp_dhcp_enabled () =
117117
let stat = Unix.stat !Xapi_globs.ntp_dhcp_script in
118118
stat.Unix.st_perm land 0o100 <> 0
119119
with _ -> false
120+
121+
(* chronyc -c sources output the ntp servers status in csv format. Example:
122+
^,-,10.62.16.11,5,6,377,54,0.000496471,0.000496471,0.071449950
123+
^,?,17.253.14.123,0,8,0,4294967295,0.000000000,0.000000000,0.000000000
124+
^,?,104.40.149.189,0,8,0,4294967295,0.000000000,0.000000000,0.000000000
125+
^,*,10.71.56.11,5,6,377,57,-0.000006851,-0.000118707,0.082518920
126+
Source mode: '^' = server, '=' = peer, '#' = local clock.
127+
Source state: '*' = current synced, '+' = combined, '-' = not combined,
128+
'?' = unreachable, 'x' = time may be in error, '~' = time too variable
129+
*)
130+
let get_servers_status () =
131+
let convert = function
132+
| "*" ->
133+
"synced"
134+
| "+" ->
135+
"combined"
136+
| "-" ->
137+
"uncombined"
138+
| "x" ->
139+
"error"
140+
| "~" ->
141+
"variable"
142+
| "?" ->
143+
"unreachable"
144+
| _ ->
145+
"unknown"
146+
in
147+
let r = Helpers.call_script !Xapi_globs.ntp_client_path ["-c"; "sources"] in
148+
let lines = String.split_on_char '\n' r in
149+
List.filter_map
150+
(fun line ->
151+
line |> String.trim |> String.split_on_char ',' |> function
152+
| "^" :: status :: server :: _ ->
153+
Some (server, convert status)
154+
| _ ->
155+
None
156+
)
157+
lines

ocaml/xapi/xapi_host_ntp.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@ val is_ntp_service_active : unit -> bool
3131
val get_servers_from_conf : unit -> string list
3232

3333
val is_ntp_dhcp_enabled : unit -> bool
34+
35+
val get_servers_status : unit -> (string * string) list

0 commit comments

Comments
 (0)