-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocess.go
55 lines (48 loc) · 1.31 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
package main
import (
"strconv"
"github.com/shirou/gopsutil/process"
)
// procStat represent the processes informations
type procStat struct {
total string
running string
//TODO add zombies threads ?
}
// getProcStat get informations about the processes
func getProcStat() (ret procStat, err error) {
pids, err := processPids() //TODO replace by something like psutil.process_iter() if available in gopsutils
if err == nil {
run := 0
for i := range pids {
proc, err1 := processNewProcess(pids[i])
if err1 == nil { //TODO
status, err2 := procStatus(proc)
if err2 == nil {
if status == "R" { // "R" for running process
run++
}
} else {
err = appendError(err, err2)
}
} else {
err = appendError(err, err1)
}
}
ret.total = strconv.Itoa(len(pids))
ret.running = strconv.Itoa(run)
}
return ret, err
}
// wrap `process.Pids()` in an unexported variable for testability
var processPids = func() ([]int32, error) {
return process.Pids()
}
// wrap `process.NewProcess()` in an unexported variable for testability
var processNewProcess = func(pid int32) (*process.Process, error) {
return process.NewProcess(pid)
}
// wrap `Process.Status()` in an unexported variable for testability
var procStatus = func(proc *process.Process) (string, error) {
return proc.Status()
}