Sysctl
c.Sysctl calls the API's Sysctl group. Kernel parameters, written to sysctl.d
files and applied.
Methods
| Method | Returns | Permission | Description |
|---|---|---|---|
List(ctx, hostname, params) | *Response[Collection[SysctlEntry]] | sysctl:read | List the kernel's parameters and which osapi manages. |
Create(ctx, hostname, input) | *Response[Collection[SysctlMutationResult]] | sysctl:write | Manage a kernel parameter and apply it. |
Get(ctx, hostname, key) | *Response[Collection[SysctlEntry]] | sysctl:read | Get a kernel parameter's current value, and whether osapi manages it. |
Update(ctx, hostname, input) | *Response[Collection[SysctlMutationResult]] | sysctl:write | Set a kernel parameter, manage it, and apply it. |
Delete(ctx, hostname, key) | *Response[Collection[SysctlMutationResult]] | sysctl:write | Stop the management of a kernel parameter. |
List
func (s *SysctlService) List(
ctx context.Context,
hostname string,
params SysctlListParams,
) (*Response[Collection[SysctlEntry]], error)
List the kernel's parameters and which osapi manages.
Lists every parameter the kernel shows under /proc/sys, with its current value, as sysctl -a does, and marks the ones osapi manages. A prefix, such as net.ipv4, limits the list to that part of the tree. The kernel can lack a parameter that osapi manages, for example when its module is not loaded. Then the list shows the value that osapi wrote.
It sends GET /api/node/{hostname}/sysctl, and needs the permission
sysctl:read.
The examples of the method's definition:
Every parameter, and which osapi manages, go run sysctl.go list:
resp, err := c.Sysctl.List(ctx, target, client.SysctlListParams{})
One part of the tree, go run sysctl.go list-2:
resp, err := c.Sysctl.List(ctx, target, client.SysctlListParams{Prefix: "net.ipv4"})
A managed parameter the kernel does not have right now,
go run sysctl.go list-3:
resp, err := c.Sysctl.List(ctx, target, client.SysctlListParams{Prefix: "net.bridge"})
Create
func (s *SysctlService) Create(
ctx context.Context,
hostname string,
input SysctlCreateInput,
) (*Response[Collection[SysctlMutationResult]], error)
Manage a kernel parameter and apply it.
Writes osapi-net.ipv4.ip_forward.conf for net.ipv4.ip_forward to behaviors.sysctl.dir, /etc/sysctl.d by default, and applies it with sysctl -p. It fails when sysctl -p fails, because the value is not set. A second run with the same value changes nothing and reports changed: false. If the kernel has a different value, for example after a failed apply, it applies the value again.
It sends POST /api/node/{hostname}/sysctl, and needs the permission
sysctl:write.
The examples of the method's definition:
Turn on IP forwarding, go run sysctl.go create:
resp, err := c.Sysctl.Create(ctx, target, client.SysctlCreateInput{
Key: "net.ipv4.ip_forward",
Value: "1",
})
Get
func (s *SysctlService) Get(
ctx context.Context,
hostname string,
key string,
) (*Response[Collection[SysctlEntry]], error)
Get a kernel parameter's current value, and whether osapi manages it.
It sends GET /api/node/{hostname}/sysctl/{key}, and needs the permission
sysctl:read.
The examples of the method's definition:
A parameter osapi manages, go run sysctl.go get:
resp, err := c.Sysctl.Get(ctx, target, "vm.swappiness")
A value of several fields, go run sysctl.go get-2:
resp, err := c.Sysctl.Get(ctx, target, "net.ipv4.ping_group_range")
A managed parameter the kernel does not have right now,
go run sysctl.go get-3:
resp, err := c.Sysctl.Get(ctx, target, "net.bridge.bridge-nf-call-iptables")
Update
func (s *SysctlService) Update(
ctx context.Context,
hostname string,
input SysctlUpdateInput,
) (*Response[Collection[SysctlMutationResult]], error)
Set a kernel parameter, manage it, and apply it.
The same as create, addressed by the parameter: osapi manages the parameter from then on whether it did before or not. Setting the value it already has reports changed: false.
It sends PUT /api/node/{hostname}/sysctl/{key}, and needs the permission
sysctl:write.
The examples of the method's definition:
Lower swappiness, go run sysctl.go update:
resp, err := c.Sysctl.Update(ctx, target, client.SysctlUpdateInput{
Key: "vm.swappiness",
Value: "10",
})
Delete
func (s *SysctlService) Delete(
ctx context.Context,
hostname string,
key string,
) (*Response[Collection[SysctlMutationResult]], error)
Stop the management of a kernel parameter.
Removes the file that osapi wrote for it and reloads with sysctl --system. Then a setting for the key in a different file takes effect. If there is no such setting, the parameter keeps its current value until the next reboot. A delete of a parameter that osapi does not manage reports changed: false.
It sends DELETE /api/node/{hostname}/sysctl/{key}, and needs the permission
sysctl:write.
The examples of the method's definition:
Remove a parameter, go run sysctl.go delete:
resp, err := c.Sysctl.Delete(ctx, target, "vm.swappiness")
Types
The fields of each type are the properties of its schema in the API's spec.
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.
SysctlCreateInput
| Field | Type | Description |
|---|---|---|
Key | string | Parameter name, such as net.ipv4.ip_forward. |
Value | string | Value to set. |
SysctlEntry
| Field | Type | Description |
|---|---|---|
Error | string | Error message if the agent failed. |
Hostname | string | Hostname of the agent that produced this result. |
Key | string | Parameter name. |
Managed | bool | Whether osapi manages it: a file of its own sets it at boot. |
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. |
Value | string | Current runtime value. |
SysctlMutationResult
| 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. |
Key | string | Parameter name. |
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. |
SysctlUpdateInput
| Field | Type | Description |
|---|---|---|
Key | string | The key of the path. Parameter name, such as net.ipv4.ip_forward. |
Value | string | Value to set. |
Example
examples/sdk/client/sysctl.go
runs each example of this page. With no argument, it lists them.