-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathprocess_info.go
101 lines (83 loc) · 2.31 KB
/
process_info.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
// This file is licensed under the MIT License.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright © 2015 Kentaro Kuribayashi <kentarok@gmail.com>
// Copyright 2014-present Datadog, Inc.
//go:build linux || darwin
// Package gops extracts the information on running processes from gopsutil
package gops
import (
"fmt"
"runtime"
// 3p
log "github.com/cihub/seelog"
// project
"github.com/shirou/gopsutil/v3/mem"
"github.com/shirou/gopsutil/v3/process"
)
// ProcessInfo contains information about a single process
type ProcessInfo struct {
PID int32
PPID int32
Name string
RSS uint64
PctMem float64
VMS uint64
Username string
}
// GetProcesses returns a slice of all the processes that are running
func GetProcesses() ([]*ProcessInfo, error) {
processInfos := make([]*ProcessInfo, 0, 10)
virtMemStat, err := mem.VirtualMemory()
if err != nil {
err = fmt.Errorf("error fetching system memory stats: %w", err)
return nil, err
}
totalMem := float64(virtMemStat.Total)
pids, err := process.Pids()
if err != nil {
err = fmt.Errorf("error fetching PIDs: %w", err)
return nil, err
}
for _, pid := range pids {
p, err := process.NewProcess(pid)
if err != nil {
// an error can occur here only if the process has disappeared,
log.Debugf("Process with pid %d disappeared while scanning: %w", pid, err)
continue
}
processInfo, err := newProcessInfo(p, totalMem)
if err != nil {
log.Debugf("Error fetching info for pid %d: %w", pid, err)
continue
}
processInfos = append(processInfos, processInfo)
}
// platform-specific post-processing on the collected info
postProcess(processInfos)
return processInfos, nil
}
// Make a new ProcessInfo from a Process from gopsutil
func newProcessInfo(p *process.Process, totalMem float64) (*ProcessInfo, error) {
memInfo, err := p.MemoryInfo()
if err != nil {
return nil, err
}
pid := p.Pid
ppid, err := p.Ppid()
if err != nil {
return nil, err
}
name, err := p.Name()
if err != nil {
return nil, err
}
pctMem := 100. * float64(memInfo.RSS) / totalMem
var username string
if runtime.GOOS != "android" {
username, err = p.Username()
if err != nil {
return nil, err
}
}
return &ProcessInfo{pid, ppid, name, memInfo.RSS, pctMem, memInfo.VMS, username}, nil
}