NTP
c.NTP calls the API's NTP group. The NTP servers of chrony, and its sync
status.
Methods
| Method | Returns | Permission | Description |
|---|---|---|---|
Get(ctx, hostname) | *Response[Collection[NtpStatus]] | ntp:read | Get the sync status of chrony and its sources. |
Create(ctx, hostname, input) | *Response[Collection[NtpResult]] | ntp:write | Set the NTP servers that chrony uses. |
Update(ctx, hostname, input) | *Response[Collection[NtpResult]] | ntp:write | Replace the NTP servers that osapi set. |
Delete(ctx, hostname) | *Response[Collection[NtpResult]] | ntp:write | Remove the NTP servers that osapi set. |
Get
func (s *NTPService) Get(
ctx context.Context,
hostname string,
) (*Response[Collection[NtpStatus]], error)
Get the sync status of chrony and its sources.
Reads chronyc tracking, for whether chrony is synchronized, its stratum, its offset from NTP time and the source that it follows, and chronyc sources, for each source that it has.
It sends GET /api/node/{hostname}/ntp, and needs the permission ntp:read.
The examples of the method's definition:
A host synchronized to time.cloudflare.com, go run ntp.go get:
resp, err := c.NTP.Get(ctx, target)
Create
func (s *NTPService) Create(
ctx context.Context,
hostname string,
input NTPCreateInput,
) (*Response[Collection[NtpResult]], error)
Set the NTP servers that chrony uses.
Writes a server line for each server to /etc/chrony/sources.d/osapi-ntp.sources, and reloads the sources with chronyc reload sources. A file that has the same servers does not change, and reports changed: false.
It sends POST /api/node/{hostname}/ntp, and needs the permission ntp:write.
The examples of the method's definition:
Set two servers, go run ntp.go create:
resp, err := c.NTP.Create(ctx, target, client.NTPCreateInput{Servers: []string{"0.pool.ntp.org", "1.pool.ntp.org"}})
The servers that the host has, go run ntp.go create-2:
resp, err := c.NTP.Create(ctx, target, client.NTPCreateInput{Servers: []string{"0.pool.ntp.org"}})
Update
func (s *NTPService) Update(
ctx context.Context,
hostname string,
input NTPUpdateInput,
) (*Response[Collection[NtpResult]], error)
Replace the NTP servers that osapi set.
The same as create, for servers that osapi set already. It fails with not found when osapi set none. The servers that the file has already report changed: false.
It sends PUT /api/node/{hostname}/ntp, and needs the permission ntp:write.
The examples of the method's definition:
Replace the servers, go run ntp.go update:
resp, err := c.NTP.Update(ctx, target, client.NTPUpdateInput{Servers: []string{"time.cloudflare.com"}})
The servers that the file has, go run ntp.go update-2:
resp, err := c.NTP.Update(ctx, target, client.NTPUpdateInput{Servers: []string{"0.pool.ntp.org"}})
Delete
func (s *NTPService) Delete(
ctx context.Context,
hostname string,
) (*Response[Collection[NtpResult]], error)
Remove the NTP servers that osapi set.
Removes /etc/chrony/sources.d/osapi-ntp.sources and reloads the sources, so chrony uses its other sources. When osapi set no servers, it reports changed: false.
It sends DELETE /api/node/{hostname}/ntp, and needs the permission
ntp:write.
The examples of the method's definition:
Remove the servers, go run ntp.go delete:
resp, err := c.NTP.Delete(ctx, target)
Types
The fields of each type are the properties of its schema in the API's spec.
NTPCreateInput
| Field | Type | Description |
|---|---|---|
Servers | []string | The NTP servers, hostnames or IP addresses. |
NTPUpdateInput
| Field | Type | Description |
|---|---|---|
Servers | []string | The NTP servers, hostnames or IP addresses. |
NtpResult
| Field | Type | Description |
|---|---|---|
Changed | bool | Whether the host changed. |
Error | string | Error message if the agent failed. |
Hostname | string | Hostname of the agent that produced this result. |
Status | RowStatus | What happened on the host: ok, the operation ran and succeeded; failed, it ran and did not succeed; skipped, the host's OS family does not support it; timeout, the host did not answer, so nobody knows whether it ran. |
NtpStatus
| Field | Type | Description |
|---|---|---|
CurrentSource | string | The source that chrony follows now. |
Error | string | Error message if the agent failed. |
Hostname | string | Hostname of the agent that produced this result. |
Offset | string | The offset of the system clock from NTP time, such as +0.000003422s. |
Servers | []string | The sources that chrony has. |
Status | RowStatus | What happened on the host: ok, the operation ran and succeeded; failed, it ran and did not succeed; skipped, the host's OS family does not support it; timeout, the host did not answer, so nobody knows whether it ran. |
Stratum | int | The NTP stratum of the host. |
Synchronized | bool | Whether chrony is synchronized to a source. |
RowStatus
What happened on the host: ok, the operation ran and succeeded; failed, it ran and did not succeed; skipped, the host's OS family does not support it; timeout, the host did not answer, so nobody knows whether it ran.
A string, one of ok, failed, skipped, timeout.
Example
examples/sdk/client/ntp.go
runs each example of this page. With no argument, it lists them.