Help us shape the future of Go! Take the 2026 Go Developer Survey

sysctl

package module
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 30, 2022 License: BSD-3-Clause Imports: 7 Imported by: 61

README

Go Sysctl

GoDoc Build Go Report Card License

Go wrapper around the sysctl interface.

Documentation

See Go doc.

Usage

import sysctl "github.com/lorenzosaino/go-sysctl"

var (
    val string
    vals map[string]string
    err error
)

// Get value of a single sysctl
// This is equivalent to running "sysctl <key>"
val, err = sysctl.Get("net.ipv4.ip_forward")

// Get the values of all sysctls matching a given pattern
// This is equivalent to running "sysctl -a -r <pattern>"
vals, err = sysctl.GetPattern("net.ipv4.ipfrag")

// Get the values of all sysctls
// This is equivalent to running "sysctl -a"
vals, err = sysctl.GetAll()

// Set the value of a sysctl
// This is equivalent to running "sysctl -w <key>=<value>"
err = sysctl.Set("net.ipv4.ip_forward", "1")

// Set sysctl values from configuration file
// This is equivalent to running "sysctl -p <config-file>"
err = sysctl.LoadConfigAndApply("/etc/sysctl.conf")

License

BSD 3-clause

Documentation

Overview

Package sysctl provides functions wrapping the sysctl interface.

Index

Constants

View Source
const DefaultPath = "/proc/sys/"

DefaultPath is the default path to the sysctl virtual files.

Variables

This section is empty.

Functions

func Get

func Get(key string) (string, error)

Get returns a sysctl from a given key.

func GetAll

func GetAll() (map[string]string, error)

GetAll returns all sysctls. This is equivalent to running the command sysctl -a.

func GetPattern

func GetPattern(pattern string) (map[string]string, error)

GetPattern returns a map of sysctls matching a given pattern The pattern uses a POSIX extended regular expression syntax. This function matches the same sysctls that the command sysctl -a -r <pattern> would return.

func LoadConfig

func LoadConfig(files ...string) (map[string]string, error)

LoadConfig gets sysctl values from a list of sysctl configuration files. The values in the rightmost files take priority. If no file is specified, values are read from /etc/sysctl.conf.

func LoadConfigAndApply

func LoadConfigAndApply(files ...string) error

LoadConfigAndApply sets sysctl values from a list of sysctl configuration files. The values in the rightmost files take priority. If no file is specified, values are read from /etc/sysctl.conf.

func Set

func Set(key, value string) error

Set updates the value of a sysctl.

Types

type Client added in v0.2.0

type Client struct {
	// contains filtered or unexported fields
}

Client is a client for reading and writing sysctls

func NewClient added in v0.2.0

func NewClient(path string) (*Client, error)

NewClient returns a new Client. The path argument is the base path containing all sysctl virtual files. By default this is DefaultPath, but there may be cases where you may want to use a different path, e.g. for tests or if procfs path is mounted to a different path.

func (*Client) Get added in v0.2.0

func (c *Client) Get(key string) (string, error)

Get returns a sysctl from a given key.

func (*Client) GetAll added in v0.2.0

func (c *Client) GetAll() (map[string]string, error)

GetAll returns all sysctls. This is equivalent to running the command sysctl -a.

func (*Client) GetPattern added in v0.2.0

func (c *Client) GetPattern(pattern string) (map[string]string, error)

GetPattern returns a map of sysctls matching a given pattern The pattern uses a POSIX extended regular expression syntax. This function matches the same sysctls that the command sysctl -a -r <pattern> would return.

func (*Client) LoadConfigAndApply added in v0.2.0

func (c *Client) LoadConfigAndApply(files ...string) error

LoadConfigAndApply sets sysctl values from a list of sysctl configuration files. The values in the rightmost files take priority. If no file is specified, values are read from /etc/sysctl.conf.

func (*Client) Set added in v0.2.0

func (c *Client) Set(key, value string) error

Set updates the value of a sysctl.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL