-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
128 lines (115 loc) · 3.13 KB
/
process.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2022 Adam Chalkley
//
// https://github.com/atc0005/check-process
//
// Licensed under the MIT License. See LICENSE file in the project root for
// full license information.
package process
import (
"fmt"
)
// 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
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
}
// 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.
type Properties map[string]string
// ParentProcess returns the parent Process for the current Process value from
// the specified collection or an error if one occurs.
func (p Process) ParentProcess(processes Processes) (Process, error) {
parentID := p.PPid
for _, process := range processes {
if process.Pid == parentID {
return process, nil
}
}
return Process{}, fmt.Errorf(
"failed to resolve parent process: %w",
ErrMissingProcessEntry,
)
}
// IsOKState indicates whether the process state is not in a list of known
// problematic process states.
func (p Process) IsOKState() bool {
for _, probState := range KnownProblemProcessStates() {
if p.State == probState {
return false
}
}
return true
}
// IsWarningState indicates whether the state matches known WARNING severity
// process states.
func (p Process) IsWarningState() bool {
for _, probState := range KnownWarningProcessStates() {
if p.State == probState {
return true
}
}
return false
}
// IsCriticalState indicates whether the state matches known CRITICAL severity
// process states.
func (p Process) IsCriticalState() bool {
for _, probState := range KnownCriticalProcessStates() {
if p.State == probState {
return true
}
}
return false
}