system

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: AGPL-3.0 Imports: 30 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// HistoryRetention is how long historical system/mount/docker usage
	// samples are kept in memory before aging out.
	HistoryRetention = 24 * time.Hour

	// MaxHistoryPoints caps the number of points returned for a single
	// series in one SystemInfoHistory response; a finer step than this
	// would allow is coarsened to respect it.
	MaxHistoryPoints = 720

	// DefaultHistoryRange is the window used when a request supplies
	// neither range nor a from/to pair.
	DefaultHistoryRange = 1 * time.Hour
)
View Source
const MaxCPUSamples = 100 // Maximum number of CPU samples to keep in history

Variables

View Source
var SignalMap = map[string]syscall.Signal{
	"ABRT": syscall.SIGABRT,
	"ALRM": syscall.SIGALRM,
	"FPE":  syscall.SIGFPE,
	"HUP":  syscall.SIGHUP,
	"ILL":  syscall.SIGILL,
	"INT":  syscall.SIGINT,
	"KILL": syscall.SIGKILL,
	"PIPE": syscall.SIGPIPE,
	"QUIT": syscall.SIGQUIT,
	"SEGV": syscall.SIGSEGV,
	"TERM": syscall.SIGTERM,
	"USR1": syscall.SIGUSR1,
	"USR2": syscall.SIGUSR2,
	"CHLD": syscall.SIGCHLD,
	"CONT": syscall.SIGCONT,
	"STOP": syscall.SIGSTOP,
	"TSTP": syscall.SIGTSTP,
	"TTIN": syscall.SIGTTIN,
	"TTOU": syscall.SIGTTOU,
	"URG":  syscall.SIGURG,
	"XCPU": syscall.SIGXCPU,
	"XFSZ": syscall.SIGXFSZ,
}

Map of signal names to their corresponding syscall.Signal values

Functions

func AddUserToDockerGroup

func AddUserToDockerGroup(username string, dockerGID uint32) (bool, error)

AddUserToDockerGroup adds the given user to the group identified by dockerGID. If no group with that GID exists in /etc/group, a new group named "docker" is created first. This does not require the Docker socket to be present.

func ApplyShell

func ApplyShell(username, shell string) error

ApplyShell updates the login shell for the given user in /etc/passwd. It performs an atomic write so the file is never left in a partial state.

func ApplySudo

func ApplySudo(username string, enable bool) error

ApplySudo enables or revokes passwordless sudo for the given user. It is safe to call this function at any time, including when the sudo state has not changed.

func CreateEnvVars

func CreateEnvVars(envVars []string, homeDir string) []string

CreateEnvVars creates new environment variables with the provided environment variables and a home directory. It includes all existing environment variables and ensures that duplicate keys are not added and that the HOME variable is set correctly.

func CreateUser

func CreateUser(user *models.User) error

CreateUser creates the user in the system.

func GetMountUsages

func GetMountUsages() ([]k8shelld.MountUsage, error)

func GetPIDListeningOnPort

func GetPIDListeningOnPort(port int) (int, error)

GetPIDListeningOnPort tries to find a PID that is listening on the given TCP port.

func GetProcessRunningTime

func GetProcessRunningTime(pid int) (time.Duration, error)

GetProcessRunningTime returns how long the given PID has been running as a time.Duration.

func GetSignalValue

func GetSignalValue(name string) (syscall.Signal, error)

Function to convert signal name to syscall.Signal value

func GetStartTimeFromProcStat

func GetStartTimeFromProcStat() (time.Time, error)

getStartTimeFromProcStat retrieves the start time of the process from /proc/1/stat

func GetSupplementalGroups

func GetSupplementalGroups(username string) []uint32

func GetUserLoginShell

func GetUserLoginShell(username string) (string, error)

GetUserLoginShell returns the login shell for the given user.

func KillAllProcesses

func KillAllProcesses(logger *zerolog.Logger)

func NewAlpineProvider

func NewAlpineProvider() *alpineProvider

func NewStandardProvider

func NewStandardProvider() *standardProvider

func ParseSizeBytes

func ParseSizeBytes(s string) (uint64, error)

parseSizeBytes parses Kubernetes-style quantities (compatible with resource.Quantity), e.g.: - Binary SI: Ki, Mi, Gi, Ti, Pi, Ei (base 1024) e.g. "10Gi", "1.5Gi" - Decimal SI: n, u, m, k, M, G, T, P, E (base 10) e.g. "500M", "1G", "250m" - Scientific notation is accepted e.g. "1e3", "1.2e6"

func UserExists

func UserExists(nameOrUID string) *user.User

userExists checks if a user with the given name or UID exists.

Types

type CPUSample

type CPUSample struct {
	Timestamp time.Time
	Usage     float64
}

CPUSample represents a CPU usage sample

type PodmanDetails

type PodmanDetails struct {
	PodmanVersion     string `json:"podmanVersion"` // e.g. "5.8.1"
	GraphDriver       string `json:"graphDriver"`   // e.g. "overlay"
	RunRoot           string `json:"runRoot"`       // e.g. "/tmp/storage-run-1000/containers"
	ContainersTotal   int    `json:"containersTotal"`
	ContainersRunning int    `json:"containersRunning"`
	ContainersPaused  int    `json:"containersPaused"`
	ContainersStopped int    `json:"containersStopped"`
}

PodmanDetails carries Podman-specific metadata that has no equivalent in the Docker-compat DockerUsage proto. It is populated from /libpod/info.

func GetDockerUsage

func GetDockerUsage(ctx context.Context) (*k8shelld.DockerUsage, *PodmanDetails, error)

type ProcessInfo

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

type ProcessWatcher

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

func NewProcessWatcher

func NewProcessWatcher(terminateOrphans bool, reapZombies bool, checkInterval int, excludePatterns []string) *ProcessWatcher

func (*ProcessWatcher) AddPIDIgnoreTerminate

func (p *ProcessWatcher) AddPIDIgnoreTerminate(pid int)

func (*ProcessWatcher) LockForCreation

func (p *ProcessWatcher) LockForCreation() func()

LockForCreation must be called before starting a child process and the returned unlock function called immediately after AddPIDIgnoreTerminate. This prevents terminateOrphans from seeing a newly-started process before its PID has been added to the ignore list.

func (*ProcessWatcher) Run

func (p *ProcessWatcher) Run(ctx context.Context)

type SystemInfo

type SystemInfo struct {
	CollectedAt        time.Time // Time of collection
	CPUUsageUsec       int64     // CPU usage in microseconds
	CPUUsageMillicores float64   // CPU usage in mCPU
	MemoryUsageMiB     float64   // Memory usage in MiB
	CPULimitMillicores float64   // CPU limit in mCPU (if set)
	MemLimitMiB        float64   // Memory limit in MiB (if set)
	CPUUsageSeconds    float64   // CPU usage in seconds
	CPUAvg1Min         float64   // CPU usage average over 1 minute
	CPUAvg5Min         float64   // CPU usage average over 5 minutes
	CPUAvg15Min        float64   // CPU usage average over 15 minutes
	// contains filtered or unexported fields
}

Usage struct to hold CPU & Memory metrics The metrics are collected from cgroups v2 files

func NewSystemInfo

func NewSystemInfo(config *config.Config, blueprint *commonmodels.Blueprint) *SystemInfo

func (*SystemInfo) Collect

func (s *SystemInfo) Collect(ctx context.Context, refreshTimeSec int) error

func (*SystemInfo) GetDockerUsageSnapshot

func (s *SystemInfo) GetDockerUsageSnapshot(_ context.Context) (*k8shelld.DockerUsage, error)

GetDockerUsageSnapshot returns the cached docker usage. Refreshed in the background by Collect.

func (*SystemInfo) GetMountUsageSnapshot

func (s *SystemInfo) GetMountUsageSnapshot() ([]k8shelld.MountUsage, error)

GetMountUsageSnapshot returns the cached mount usage. Refreshed in the background by Collect.

func (*SystemInfo) GetPodmanDetailsSnapshot

func (s *SystemInfo) GetPodmanDetailsSnapshot() *PodmanDetails

GetPodmanDetailsSnapshot returns the cached extra Podman details. Refreshed in the background by Collect.

func (*SystemInfo) GetSystemInfoHistory added in v0.16.0

GetSystemInfoHistory resolves a SystemInfoHistory query against the recorded ring buffer and returns the downsampled series. The returned From/To/Step reflect what was actually used after clamping/coarsening.

func (*SystemInfo) GetSystemUsageSnapshot

func (s *SystemInfo) GetSystemUsageSnapshot() (*k8shelld.SystemUsage, error)

GetSystemUsageSnapshot returns a snapshot of system metrics.

type SystemStats

type SystemStats struct {
	CPUHistory []CPUSample
	MaxSamples int
}

SystemStats holds the history of CPU usage samples

func (*SystemStats) AddSample

func (s *SystemStats) AddSample(sample CPUSample)

NewSystemStats initializes a new SystemStats instance

func (*SystemStats) GetAverage

func (s *SystemStats) GetAverage(duration time.Duration) float64

GetAverage returns the average CPU usage in millicores over the specified duration

Jump to

Keyboard shortcuts

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