process

package
v0.3.16 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package process provides functionality used to evaluate processes for problematic states.

Index

Constants

View Source
const (

	// ProcRootDir is the default mount point for the proc virtual filesystem.
	ProcRootDir string = "/proc"

	// ProcStatusFilename is the name of the human-readable process status
	// information present for each process directory in the proc filesystem.
	ProcStatusFilename string = "status"

	// ProcDirRegex is the regex pattern used to match process directory names
	// within the proc virtual filesystem.
	ProcDirRegex string = "^[0-9]+$"
)
View Source
const (
	ProcessNameField    string = "name"
	ProcessStateField   string = "state"
	ProcessPidField     string = "pid"
	ProcessPPidField    string = "ppid"
	ProcessThreadsField string = "threads"
	ProcessVMSwapField  string = "vmswap"
)

Process status field names.

View Source
const (
	KernelAnyProcessStateRunning   string = "R (running)"
	KernelAnyProcessStateSleeping  string = "S (sleeping)"
	KernelAnyProcessStateDiskSleep string = "D (disk sleep)"
	KernelAnyProcessStateStopped   string = "T (stopped)"
	KernelAnyProcessStateZombie    string = "Z (zombie)"
	KernelAnyProcessStateDead      string = "X (dead)"
)

Process state values observed for 2.6.32, 3.10, 4.18 & 5.14 kernels and presumed to be stable enough to be used by future kernels.

View Source
const (
	KernelLegacyProcessStateTracingStop string = "T (tracing stop)" // kernel 2.6.32
	KernelLegacyProcessStateDead        string = "x (dead)"         // kernel 3.10
	KernelLegacyProcessStateWakeKill    string = "K (wakekill)"     // kernel 3.10
	KernelLegacyProcessStateWaking      string = "W (waking)"       // kernel 3.10
)

Process state values observed for legacy kernels that are not present on current/modern kernel versions.

View Source
const (
	KernelCurrentProcessStateTracingStop string = "t (tracing stop)" // kernel 3.10, 4.18, 5.14
	KernelCurrentProcessStateIdle        string = "I (idle)"         // kernel 4.18, 5.14
	KernelCurrentProcessStateParked      string = "P (parked)"       // kernel 3.10, 4.18, 5.14
)

Process state values observed for newer kernel versions and presumed to be stable enough to be used by future kernels.

Variables

View Source
var (
	// ErrProblemProcessesFound indicates that one or more "problematic"
	// processes were found (e.g., uninterruptible disk sleep) which warrant
	// surfacing the issue.
	ErrProblemProcessesFound = errors.New("problematic processes found")

	// ErrInvalidProcStatusLineFormat indicates that a status file within the
	// /proc filesystem has an invalid key/value format.
	ErrInvalidProcStatusLineFormat = errors.New("invalid format for proc status line")

	// ErrInvalidProcStatusLineValue indicates that a status file within the
	// /proc filesystem has an invalid value (wrong type) for a process
	// property.
	ErrInvalidProcStatusLineValue = errors.New("invalid value for proc status line")

	// ErrMissingProcessPropertiesIndexEntry indicates that a requested
	// process property entry is missing from the index.
	ErrMissingProcessPropertiesIndexEntry = errors.New("entry missing from process properties index")

	// ErrInvalidProcessPropertiesIndexCount indicates that a
	// ProcessProperties index (map) is missing the minimum required entries
	// used to populate specific fields of a Process.
	ErrInvalidProcessPropertiesIndexCount = errors.New("minimum entries not present in process properties index")

	// ErrMissingProcessEntry indicates that an expected process was not found
	// in a given collection.
	ErrMissingProcessEntry = errors.New("process missing from collection")
)

Functions

func GetProcDirs

func GetProcDirs(path string) ([]string, error)

GetProcDirs evaluates the given base path (usually "/proc") for known process directories within the proc filesystem and returns an unqualified list of all valid matches.

func KnownCriticalProcessStates

func KnownCriticalProcessStates() []string

KnownCriticalProcessStates returns a list of the process states known to be problematic. These states are considered CRITICAL.

func KnownProblemProcessStates

func KnownProblemProcessStates() []string

KnownProblemProcessStates provides a list of the process states known to be problematic.

func KnownWarningProcessStates

func KnownWarningProcessStates() []string

KnownWarningProcessStates provides a list of the process states known to be problematic and warrant a WARNING, but not so severe to be considered CRITICAL.

func SupportedProcessStates

func SupportedProcessStates() []string

SupportedProcessStates provides a list of the process states supported by this package. Not all process states are supported by all kernel versions.

Types

type Process

type Process struct {
	Name string

	// State is the current state of the process.
	//
	// Valid values for a 2.6.32 (RHEL 6) kernel:
	//
	// "R (running)"
	// "S (sleeping)"
	// "D (disk sleep)"
	// "T (stopped)"
	// "T (tracing stop)"
	// "Z (zombie)"
	// "X (dead)"
	//
	// Valid values for a 3.10 (RHEL 7) kernel:
	//
	// "R (running)"
	// "S (sleeping)"
	// "D (disk sleep)"
	// "T (stopped)"
	// "t (tracing stop)"
	// "Z (zombie)"
	// "X (dead)"
	// "x (dead)"
	// "K (wakekill)"
	// "W (waking)"
	// "P (parked)"
	//
	//
	// Valid values for a 4.18 (RHEL 8) and 5.14 (RHEL 9) kernel:
	//
	// "R (running)"
	// "S (sleeping)"
	// "D (disk sleep)"
	// "T (stopped)"
	// "t (tracing stop)"
	// "X (dead)"
	// "Z (zombie)"
	// "P (parked)"
	// "I (idle)"
	//
	// https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/proc/array.c?h=v2.6.32#n136
	// https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/proc/array.c?h=v3.10#n135
	// https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/proc/array.c?h=v4.18#n130
	// https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/proc/array.c?h=v5.14#n130
	State         string
	Pid           int
	PPid          int
	Threads       int
	VMSwap        string
	AllProperties Properties
}

Process provides select type-safe values related to a running process (per /proc) and a map of property/values in string format.

https://man7.org/linux/man-pages/man5/proc.5.html https://linux.die.net/man/5/proc

func ParseProcStatusFile

func ParseProcStatusFile(filename string) (Process, error)

ParseProcStatusFile parses a given /proc/[pid]/status file and returns a Process value representing the status of a process.

func (Process) IsCriticalState

func (p Process) IsCriticalState() bool

IsCriticalState indicates whether the state matches known CRITICAL severity process states.

func (Process) IsOKState

func (p Process) IsOKState() bool

IsOKState indicates whether the process state is not in a list of known problematic process states.

func (Process) IsWarningState

func (p Process) IsWarningState() bool

IsWarningState indicates whether the state matches known WARNING severity process states.

func (Process) ParentProcess

func (p Process) ParentProcess(processes Processes) (Process, error)

ParentProcess returns the parent Process for the current Process value from the specified collection or an error if one occurs.

type Processes

type Processes []Process

Processes is a collection of Process values.

func FromProcDirs

func FromProcDirs(procDirs []string) (Processes, error)

FromProcDirs evaluates the status file within a given list of /proc/[pid] directories and returns either a collection of Process values or an error if one occurs.

func (Processes) Count

func (ps Processes) Count() int

Count returns the number of Process entries in the collection.

func (Processes) Exclude

func (ps Processes) Exclude(exclude ...Process) Processes

Exclude returns Process values from the collection which are not in the specified set. If the specified set is empty all Process values in the collection are returned. If the

func (Processes) ExcludeMyPID added in v0.1.1

func (ps Processes) ExcludeMyPID() Processes

ExcludeMyPID returns Process values from the collection which do not match the current process ID value of the tool executing this code.

func (Processes) HasCriticalState

func (ps Processes) HasCriticalState() bool

HasCriticalState indicates whether any items in the collection were evaluated to a CRITICAL state.

func (Processes) HasWarningState

func (ps Processes) HasWarningState() bool

HasWarningState indicates whether any items in the collection were evaluated to a WARNING state.

func (Processes) IsOKState

func (ps Processes) IsOKState() bool

IsOKState indicates whether all items in the collection were evaluated to an OK state.

func (Processes) MyProcess added in v0.1.1

func (ps Processes) MyProcess() Process

MyProcess returns the Process value from the collection which matches the current process ID value of the tool executing this code. A zero value Process is returned if a match is not found for the current process ID value.

func (Processes) NumCriticalState

func (ps Processes) NumCriticalState() int

NumCriticalState indicates how many items in the collection were evaluated to a CRITICAL state.

func (Processes) NumOKState

func (ps Processes) NumOKState() int

NumOKState indicates how many items in the collection were evaluated to an OK state.

func (Processes) NumWarningState

func (ps Processes) NumWarningState() int

NumWarningState indicates how many items in the collection were evaluated to a WARNING state.

func (Processes) ParentProcess

func (ps Processes) ParentProcess(p Process) (Process, error)

ParentProcess returns the parent Process for the specified Process value from the collection or an error if one occurs.

func (Processes) ServiceState

func (ps Processes) ServiceState() nagios.ServiceState

ServiceState returns the appropriate Service Check Status label and exit code for the collection's evaluation results.

func (Processes) State

func (ps Processes) State(state string) Processes

State returns each Process from the collection that are in the specified state. The caller is encouraged to specify state values using applicable state value constants for best results. The returned collection may be empty if no processes are in the requested state.

func (Processes) StateCount

func (ps Processes) StateCount(state string) int

StateCount is a helper method used to indicate how many processes in the collection are in the specified state. The caller is encouraged to specify state values using applicable state value constants for best results.

func (Processes) StateDead

func (ps Processes) StateDead() Processes

StateDead returns each Process from the collection currently in a dead state. The returned collection may be empty if no processes are in the requested state.

func (Processes) StateDeadCount

func (ps Processes) StateDeadCount() int

StateDeadCount returns the number of processes from the collection currently in a dead state.

func (Processes) StateDiskSleep

func (ps Processes) StateDiskSleep() Processes

StateDiskSleep returns each Process from the collection currently in a disk sleep state. The returned collection may be empty if no processes are in the requested state.

func (Processes) StateDiskSleepCount

func (ps Processes) StateDiskSleepCount() int

StateDiskSleepCount returns the number of processes from the collection currently in a disk sleep state.

func (Processes) StateIdle

func (ps Processes) StateIdle() Processes

StateIdle returns each Process from the collection currently in an idle state. The returned collection may be empty if no processes are in the requested state.

func (Processes) StateIdleCount

func (ps Processes) StateIdleCount() int

StateIdleCount returns the number of processes from the collection currently in a idle state.

func (Processes) StateParked

func (ps Processes) StateParked() Processes

StateParked returns each Process from the collection currently in a parked state. The returned collection may be empty if no processes are in the requested state.

func (Processes) StateParkedCount

func (ps Processes) StateParkedCount() int

StateParkedCount returns the number of processes from the collection currently in a parked state.

func (Processes) StateRunning

func (ps Processes) StateRunning() Processes

StateRunning returns each Process from the collection currently in a running state. The returned collection may be empty if no processes are in the requested state.

func (Processes) StateRunningCount

func (ps Processes) StateRunningCount() int

StateRunningCount returns the number of processes from the collection currently in a running state.

func (Processes) StateSleeping

func (ps Processes) StateSleeping() Processes

StateSleeping returns each Process from the collection currently in a sleeping state. The returned collection may be empty if no processes are in the requested state.

func (Processes) StateSleepingCount

func (ps Processes) StateSleepingCount() int

StateSleepingCount returns the number of processes from the collection currently in a sleeping state.

func (Processes) StateStopped

func (ps Processes) StateStopped() Processes

StateStopped returns each Process from the collection currently in a stopped state. Processes in a "tracing stop" state are not evaluated.

The returned collection may be empty if no processes are in the requested state.

func (Processes) StateStoppedCount

func (ps Processes) StateStoppedCount() int

StateStoppedCount returns the number of processes from the collection currently in a stopped state.

func (Processes) StateTracingStop

func (ps Processes) StateTracingStop() Processes

StateTracingStop returns each Process from the collection currently in a tracing stop state. The returned collection may be empty if no processes are in the requested state.

func (Processes) StateTracingStopCount

func (ps Processes) StateTracingStopCount() int

StateTracingStopCount returns the number of processes from the collection currently in a tracing stop state.

func (Processes) StateWakeKill

func (ps Processes) StateWakeKill() Processes

StateWakeKill returns each Process from the collection currently in a wakekill state. The returned collection may be empty if no processes are in the requested state.

func (Processes) StateWakeKillCount

func (ps Processes) StateWakeKillCount() int

StateWakeKillCount returns the number of processes from the collection currently in a wakekill state.

func (Processes) StateWaking

func (ps Processes) StateWaking() Processes

StateWaking returns each Process from the collection currently in a waking state. The returned collection may be empty if no processes are in the requested state.

func (Processes) StateWakingCount

func (ps Processes) StateWakingCount() int

StateWakingCount returns the number of processes from the collection currently in a waking state.

func (Processes) StateZombie

func (ps Processes) StateZombie() Processes

StateZombie returns each Process from the collection currently in a zombie state. The returned collection may be empty if no processes are in the requested state.

func (Processes) StateZombieCount

func (ps Processes) StateZombieCount() int

StateZombieCount returns the number of processes from the collection currently in a zombie state.

func (Processes) States

func (ps Processes) States(states []string) Processes

States returns each Process from the collection that are in the specified states. The caller is encouraged to specify state values using applicable state value constants for best results. The returned collection is empty if no processes are in the requested states.

func (Processes) SummaryList

func (ps Processes) SummaryList() []string

SummaryList returns a slice of process summary items.

func (Processes) SummaryOneLine

func (ps Processes) SummaryOneLine() string

SummaryOneLine returns a one line summary of all processes.

type Properties

type Properties map[string]string

Properties is a collection of key/value string pairs representing the various properties for a running process. Each key is lowercased with the original value retained as-is.

Jump to

Keyboard shortcuts

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