forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.go
315 lines (260 loc) · 7.27 KB
/
helper.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// +build darwin freebsd linux windows
package process
import (
"fmt"
"os"
"regexp"
"runtime"
"strings"
"time"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/module/system"
"github.com/elastic/beats/metricbeat/module/system/memory"
sigar "github.com/elastic/gosigar"
)
type ProcsMap map[int]*Process
type Process struct {
Pid int `json:"pid"`
Ppid int `json:"ppid"`
Pgid int `json:"pgid"`
Name string `json:"name"`
Username string `json:"username"`
State string `json:"state"`
CmdLine string `json:"cmdline"`
Mem sigar.ProcMem
Cpu sigar.ProcTime
Ctime time.Time
FD sigar.ProcFDUsage
}
type ProcStats struct {
Procs []string
regexps []*regexp.Regexp
ProcsMap ProcsMap
CpuTicks bool
}
// newProcess creates a new Process object based on the state information.
func newProcess(pid int) (*Process, error) {
state := sigar.ProcState{}
if err := state.Get(pid); err != nil {
return nil, fmt.Errorf("error getting process state for pid=%d: %v", pid, err)
}
proc := Process{
Pid: pid,
Ppid: state.Ppid,
Pgid: state.Pgid,
Name: state.Name,
State: getProcState(byte(state.State)),
Username: state.Username,
Ctime: time.Now(),
}
return &proc, nil
}
// getDetails fills in CPU, memory, FD usage, and command line details for the process.
func (proc *Process) getDetails(cmdline string) error {
proc.Mem = sigar.ProcMem{}
if err := proc.Mem.Get(proc.Pid); err != nil {
return fmt.Errorf("error getting process mem for pid=%d: %v", proc.Pid, err)
}
proc.Cpu = sigar.ProcTime{}
if err := proc.Cpu.Get(proc.Pid); err != nil {
return fmt.Errorf("error getting process cpu time for pid=%d: %v", proc.Pid, err)
}
if cmdline == "" {
args := sigar.ProcArgs{}
if err := args.Get(proc.Pid); err != nil && !sigar.IsNotImplemented(err) {
return fmt.Errorf("error getting process arguments for pid=%d: %v", proc.Pid, err)
}
proc.CmdLine = strings.Join(args.List, " ")
} else {
proc.CmdLine = cmdline
}
if fd, err := getProcFDUsage(proc.Pid); err != nil {
return fmt.Errorf("error getting process file descriptor usage for pid=%d: %v", proc.Pid, err)
} else if fd != nil {
proc.FD = *fd
}
return nil
}
// getProcFDUsage returns file descriptor usage information for the process
// identified by the given PID. If the feature is not implemented then nil
// is returned with no error. If there is a permission error while reading the
// data then nil is returned with no error (/proc/[pid]/fd requires root
// permissions). Any other errors that occur are returned.
func getProcFDUsage(pid int) (*sigar.ProcFDUsage, error) {
// It's not possible to collect FD usage from other processes on FreeBSD
// due to linprocfs not exposing the information.
if runtime.GOOS == "freebsd" && pid != os.Getpid() {
return nil, nil
}
fd := sigar.ProcFDUsage{}
if err := fd.Get(pid); err != nil {
switch {
case sigar.IsNotImplemented(err):
return nil, nil
case os.IsPermission(err):
return nil, nil
default:
return nil, err
}
}
return &fd, nil
}
func GetProcMemPercentage(proc *Process, totalPhyMem uint64) float64 {
// in unit tests, total_phymem is set to a value greater than zero
if totalPhyMem == 0 {
memStat, err := memory.GetMemory()
if err != nil {
logp.Warn("Getting memory details: %v", err)
return 0
}
totalPhyMem = memStat.Mem.Total
}
perc := (float64(proc.Mem.Resident) / float64(totalPhyMem))
return system.Round(perc, .5, 4)
}
func Pids() ([]int, error) {
pids := sigar.ProcList{}
err := pids.Get()
if err != nil {
return nil, err
}
return pids.List, nil
}
func getProcState(b byte) string {
switch b {
case 'S':
return "sleeping"
case 'R':
return "running"
case 'D':
return "idle"
case 'T':
return "stopped"
case 'Z':
return "zombie"
}
return "unknown"
}
func (procStats *ProcStats) GetProcessEvent(process *Process, last *Process) common.MapStr {
proc := common.MapStr{
"pid": process.Pid,
"ppid": process.Ppid,
"pgid": process.Pgid,
"name": process.Name,
"state": process.State,
"username": process.Username,
"memory": common.MapStr{
"size": process.Mem.Size,
"rss": common.MapStr{
"bytes": process.Mem.Resident,
"pct": GetProcMemPercentage(process, 0 /* read total mem usage */),
},
"share": process.Mem.Share,
},
}
if process.CmdLine != "" {
proc["cmdline"] = process.CmdLine
}
if procStats.CpuTicks {
proc["cpu"] = common.MapStr{
"user": process.Cpu.User,
"system": process.Cpu.Sys,
"total": common.MapStr{
"ticks": process.Cpu.Total,
"pct": GetProcCpuPercentage(last, process),
},
"start_time": unixTimeMsToTime(process.Cpu.StartTime),
}
} else {
proc["cpu"] = common.MapStr{
"total": common.MapStr{
"pct": GetProcCpuPercentage(last, process),
},
"start_time": unixTimeMsToTime(process.Cpu.StartTime),
}
}
if process.FD != (sigar.ProcFDUsage{}) {
proc["fd"] = common.MapStr{
"open": process.FD.Open,
"limit": common.MapStr{
"soft": process.FD.SoftLimit,
"hard": process.FD.HardLimit,
},
}
}
return proc
}
func GetProcCpuPercentage(last *Process, current *Process) float64 {
if last != nil && current != nil {
dCPU := int64(current.Cpu.Total - last.Cpu.Total)
dt := float64(current.Ctime.Sub(last.Ctime).Nanoseconds()) / float64(1e6) // in milliseconds
perc := float64(dCPU) / dt
return system.Round(perc, .5, 4)
}
return 0
}
func (procStats *ProcStats) MatchProcess(name string) bool {
for _, reg := range procStats.regexps {
if reg.MatchString(name) {
return true
}
}
return false
}
func (procStats *ProcStats) InitProcStats() error {
procStats.ProcsMap = make(ProcsMap)
if len(procStats.Procs) == 0 {
return nil
}
procStats.regexps = []*regexp.Regexp{}
for _, pattern := range procStats.Procs {
reg, err := regexp.Compile(pattern)
if err != nil {
return fmt.Errorf("Failed to compile regexp [%s]: %v", pattern, err)
}
procStats.regexps = append(procStats.regexps, reg)
}
return nil
}
func (procStats *ProcStats) GetProcStats() ([]common.MapStr, error) {
if len(procStats.Procs) == 0 {
return nil, nil
}
pids, err := Pids()
if err != nil {
logp.Warn("Getting the list of pids: %v", err)
return nil, err
}
processes := []common.MapStr{}
newProcs := make(ProcsMap, len(pids))
for _, pid := range pids {
var cmdline string
if previousProc := procStats.ProcsMap[pid]; previousProc != nil {
cmdline = previousProc.CmdLine
}
process, err := newProcess(pid)
if err != nil {
logp.Debug("metricbeat", "Skip process pid=%d: %v", pid, err)
continue
}
if procStats.MatchProcess(process.Name) {
err = process.getDetails(cmdline)
if err != nil {
logp.Err("Error getting process details. pid=%d: %v", process.Pid, err)
continue
}
newProcs[process.Pid] = process
last, _ := procStats.ProcsMap[process.Pid]
proc := procStats.GetProcessEvent(process, last)
processes = append(processes, proc)
}
}
procStats.ProcsMap = newProcs
return processes, nil
}
// unixTimeMsToTime converts a unix time given in milliseconds since Unix epoch
// to a common.Time value.
func unixTimeMsToTime(unixTimeMs uint64) common.Time {
return common.Time(time.Unix(0, int64(unixTimeMs*1000000)))
}