/
process_posix.go
203 lines (170 loc) · 5.02 KB
/
process_posix.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
//go:build linux || darwin
// +build linux darwin
package checks
import (
"context"
"fmt"
"path"
"runtime"
"strconv"
"strings"
"time"
"github.com/it-novum/openitcockpit-agent-go/utils"
)
type ps struct {
Pid uint64
Ppid uint64
Cpup float64
Memp float64
User string
Stat []string
Nice int64
Rss uint64
VSZ uint64
Pagein uint64
Command string
}
// Run the actual check
// if error != nil the check result will be nil
// ctx can be canceled and runs the timeout
// CheckResult will be serialized after the return and should not change until the next call to Run
func (c *CheckProcess) Run(ctx context.Context) (interface{}, error) {
/*PID PPID %CPU %MEM USER STAT NI RSS VSZ PAGEIN COMMAND
* 1 0 0,1 0,1 root Ss 0 21340 4313476 0 /sbin/launchd
*109 1 0,0 0,0 root Ss 0 1136 4306048 0 /usr/sbin/syslogd
*110 1 0,0 0,1 root Ss 0 11456 4336292 0 /usr/libexec/UserEventAgent (System)
*112 1 0,0 0,0 root Ss 0 2252 4296900 0 /System/Library/PrivateFrameworks/Uninstall.framework/Resources/uninstalld
*113 1 0,0 0,1 root Ss 0 20908 4857808 0 /usr/libexec/kextd
*114 1 0,0 0,0 root Ss 0 8288 4324792 0 /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/FSEvents.framework/Versions/A/Support/fseventsd
*116 1 0,0 0,1 root Ss 0 12508 4336452 0 /System/Library/PrivateFrameworks/MediaRemote.framework/Support/mediaremoted
*119 1 0,0 0,1 root Ss 0 13780 4344220 0 /usr/sbin/systemstats --daemon
*120 1 0,0 0,1 root Ss 0 8992 4339428 0 /usr/libexec/configd
*/
var err error
timeout := 10 * time.Second
command := "ps -ax -o pid,ppid,%cpu,%mem,user,state,nice,rss,vsize,pagein,command"
if runtime.GOOS == "linux" {
command = command + " --columns 10000"
}
result, err := utils.RunCommand(ctx, utils.CommandArgs{
Command: command,
Timeout: timeout,
})
if err != nil || result.RC > 0 {
return nil, fmt.Errorf("Error while executing '%v'", command)
}
lines := strings.Split(result.Stdout, "\n")
if len(lines) > 0 {
//Remove first line (ps header)
lines = lines[1:]
}
var processes []*ps
fields := []string{"PID", "PPID", "%CPU", "%MEM", "USER", "STAT", "NI", "RSS", "VSZ", "PAGEIN", "COMMAND"}
for _, line := range lines {
line = strings.TrimSpace(line)
index := 0
ps := &ps{}
var piece string
delimiter := " "
for i, c := range line {
char := string(c)
if fields[index] == "COMMAND" {
// Command could have spaces so we use the complete string...
ps.Command = line[i:]
processes = append(processes, ps)
// Go to the next line of the ps output
index = 0
break
}
if char != delimiter {
piece = piece + char
} else {
// We hit a space
if piece != "" {
//fmt.Println(fields[index] + ": " + piece)
if fields[index] == "PID" {
ps.Pid, _ = strconv.ParseUint(piece, 10, 64)
}
if fields[index] == "PPID" {
ps.Ppid, _ = strconv.ParseUint(piece, 10, 64)
}
if fields[index] == "%CPU" {
ps.Cpup, _ = strconv.ParseFloat(piece, 64)
}
if fields[index] == "%MEM" {
ps.Memp, _ = strconv.ParseFloat(piece, 64)
}
if fields[index] == "USER" {
ps.User = piece
}
if fields[index] == "STAT" {
s := getStatusName(piece[0:1])
var status []string
status = append(status, s)
ps.Stat = status
}
if fields[index] == "NI" {
ps.Nice, _ = strconv.ParseInt(piece, 10, 64)
}
if fields[index] == "RSS" {
ps.Rss, _ = strconv.ParseUint(piece, 10, 64)
}
if fields[index] == "VSZ" {
ps.VSZ, _ = strconv.ParseUint(piece, 10, 64)
}
if fields[index] == "PAGEIN" {
ps.Pagein, _ = strconv.ParseUint(piece, 10, 64)
}
index++
}
piece = ""
}
}
}
processResults := make([]*resultProcess, 0, len(processes))
for _, process := range processes {
cmd := strings.Split(process.Command, " ")
binaryWithPath := cmd[0]
binary := path.Base(cmd[0])
result := &resultProcess{
Pid: process.Pid,
Ppid: process.Ppid,
Username: process.User,
Name: binary,
CPUPercent: process.Cpup,
MemoryPercent: process.Memp,
Cmdline: process.Command,
Status: process.Stat,
Exe: binaryWithPath,
Nice: process.Nice,
NumFds: 0,
Memory: &resultMemoryPosix{
RSS: process.Rss * 1024,
VMS: process.VSZ * 1024,
Swap: process.Pagein * 1024,
},
}
processResults = append(processResults, result)
}
return processResults, nil
}
func getStatusName(s string) string {
switch s {
case "R":
return "Running"
case "S":
return "Sleep"
case "T":
return "Stop"
case "I":
return "Idle"
case "Z":
return "Zombie"
case "W":
return "Wait"
case "L":
return "Lock"
default:
return ""
}
}