-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
check_proc.go
225 lines (192 loc) · 6.03 KB
/
check_proc.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package services
import (
"context"
"fmt"
"regexp"
"strings"
"time"
"github.com/Notifiarr/notifiarr/pkg/mnd"
"github.com/hako/durafmt"
"github.com/shirou/gopsutil/v3/process"
)
// ProcInfo is derived from a pid.
type ProcInfo struct {
CmdLine string
Created time.Time
PID int32
/* // these are possibly available..
cmdlineslice []string
cwd string
exe string
meminfo *process.MemoryInfoStat
memperc float32
name string
/**/
}
// procExpect is setup for each 'process' service from input data on initialization.
type procExpect struct {
checkRE *regexp.Regexp
countMin int
countMax int
restarts bool
running bool
}
const epochOffset = 1000
// GetAllProcesses returns all running process on the host.
func GetAllProcesses(ctx context.Context) ([]*ProcInfo, error) {
processes, err := process.ProcessesWithContext(ctx)
if err != nil {
return nil, fmt.Errorf("getting process list: %w", err)
}
procs := []*ProcInfo{}
for _, proc := range processes {
procinfo, errs := getProcInfo(ctx, proc)
if len(errs) != 0 {
if procinfo == nil || procinfo.CmdLine == "" {
// log.Printf("DEBUG: [pid: %d] proc errs: %v", proc.Pid, errs)
continue
}
// log.Printf("DEBUG: [pid: %d] proc errs: %v, cmd: %s", proc.Pid, errs, procinfo.CmdLine)
} //nolint:wsl
procinfo.PID = proc.Pid
procs = append(procs, procinfo)
}
return procs, nil
}
// start a loop through processes to find the one we care about.
func (s *Service) checkProccess(ctx context.Context) *result {
ctx, cancel := context.WithTimeout(ctx, s.Timeout.Duration)
defer cancel()
processes, err := process.ProcessesWithContext(ctx)
if err != nil {
return &result{
state: StateUnknown,
output: "process list error: " + err.Error(),
}
}
return s.getProcessResults(ctx, processes)
}
func (s *Service) getProcessResults(ctx context.Context, processes []*process.Process) *result {
var (
found []int32
ages []time.Time
)
// Loop each process/pid, get the command line name, and check for a match.
for _, proc := range processes {
procinfo, _ := getProcInfo(ctx, proc)
if procinfo.CmdLine == "" {
continue
}
// Look for a match for our process.
if strings.Contains(procinfo.CmdLine, s.Value) ||
(s.svc.proc.checkRE != nil && s.svc.proc.checkRE.FindString(procinfo.CmdLine) != "") {
found = append(found, proc.Pid)
ages = append(ages, procinfo.Created)
// log.Printf("pid: %d, age: %v, cmd: %v",
// proc.Pid, time.Since(procinfo.Created).Round(time.Second), procinfo.CmdLine)
if !procinfo.Created.IsZero() && s.svc.proc.restarts && time.Since(procinfo.Created) < s.Interval.Duration {
return &result{
state: StateCritical,
output: fmt.Sprintf("%s: process restarted since last check, age: %v, pid: %d, proc: %s",
s.Value, time.Since(procinfo.Created), proc.Pid, procinfo.CmdLine),
}
}
}
}
return s.checkProcessCounts(found, ages)
}
// checkProcessCounts validates process check thresholds.
func (s *Service) checkProcessCounts(pids []int32, ages []time.Time) *result {
min, max, age, pid := s.getProcessStrings(pids, ages)
switch count := len(pids); {
case !s.svc.proc.running && count == 0: // not running!
fallthrough
case s.svc.proc.countMax != 0 && count > s.svc.proc.countMax: // too many running!
fallthrough
case count < s.svc.proc.countMin: // not enough running!
return &result{
state: StateCritical,
output: fmt.Sprintf("%s: found %d processes; %s%s%s%s", s.Value, count, min, max, age, pid),
}
case s.svc.proc.running && count > 0: // running but should not be!
return &result{
state: StateCritical,
output: fmt.Sprintf("%s: found %d processes; expected: 0%s%s", s.Value, count, age, pid),
}
default: // running within thresholds!
return &result{
state: StateOK,
output: fmt.Sprintf("%s: found %d processes; %s%s%s%s", s.Value, count, min, max, age, pid),
}
}
}
// getProcessStrings compiles output strings for a process service check.
func (s *Service) getProcessStrings(pids []int32, ages []time.Time) (min, max, age, pid string) {
if min = "min: 1"; s.svc.proc.countMin > 0 { // min always exists.
min = fmt.Sprintf("min: %d", s.svc.proc.countMin)
}
if s.svc.proc.countMax > 0 {
max = fmt.Sprintf(", max: %d", s.svc.proc.countMax)
}
if len(ages) == 1 && !ages[0].IsZero() {
age = fmt.Sprintf(", age: %v", durafmt.ParseShort(time.Since(ages[0]).Round(time.Second)))
}
for _, activePid := range pids {
if pid == "" {
pid = ", pids: "
} else {
pid += ";"
}
pid += fmt.Sprintf("%v", activePid)
}
return
}
// getProcInfo returns age and cli args for a process.
func getProcInfo(ctx context.Context, proc *process.Process) (*ProcInfo, []error) {
var (
err error
errs []error
procinfo ProcInfo
)
procinfo.CmdLine, err = proc.CmdlineWithContext(ctx)
if err != nil {
errs = append(errs, fmt.Errorf("CmdlineWithContext: %w", err))
}
if procinfo.CmdLine == "" {
if procinfo.CmdLine, err = proc.NameWithContext(ctx); err != nil {
errs = append(errs, fmt.Errorf("NameWithContext: %w", err))
}
}
// FreeBSD doesn't have create time.
if !mnd.IsFreeBSD {
created, err := proc.CreateTimeWithContext(ctx)
if err != nil {
errs = append(errs, fmt.Errorf("CreateTimeWithContext: %w", err))
} else {
procinfo.Created = time.Unix(created/epochOffset, 0).Round(time.Millisecond)
}
}
/*
procinfo.cmdlineslice, err = p.CmdlineSliceWithContext(ctx)
if err != nil {
return nil, fmt.Errorf("CmdlineSliceWithContext: %w", err)
}
procinfo.name, err = p.NameWithContext(ctx)
if err != nil {
return nil, fmt.Errorf("NameWithContext: %w", err)
}
procinfo.exe, err = p.ExeWithContext(ctx)
if err != nil {
return nil, fmt.Errorf("ExeWithContext: %w", err)
}
procinfo.meminfo, err = p.MemoryInfoWithContext(ctx)
if err != nil {
return nil, fmt.Errorf("MemoryInfoWithContext: %w", err)
}
procinfo.memperc, err = p.MemoryPercentWithContext(ctx)
if err != nil {
return nil, fmt.Errorf("MemoryPercentWithContext: %w", err)
}
*/
return &procinfo, errs
}