-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocdiff.go
More file actions
144 lines (123 loc) · 2.69 KB
/
procdiff.go
File metadata and controls
144 lines (123 loc) · 2.69 KB
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
package main
import (
"log"
"math"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
"github.com/c9s/goprocinfo/linux"
)
type ProcDiff []*Proc
func (pd *ProcDiff) Contains(cmd ...string) {
maxp, e := linux.ReadMaxPID("/proc/sys/kernel/pid_max")
if e != nil {
log.Println(e)
return
}
for p := uint64(1); p < maxp; p++ {
// Do not check it's own PID
if int(p) == selfPID {
continue
}
sp := strconv.FormatUint(p, 10)
status, e := linux.ReadProcessStatus(filepath.Join("/proc", sp, "status"))
if e != nil {
continue
}
cmdline, e := linux.ReadProcessCmdline(filepath.Join("/proc", sp, "cmdline"))
if e != nil {
continue
}
// Tgid != Pid for a thread
if status.Tgid != status.Pid {
continue
}
for _, c := range cmd {
if strings.Contains(cmdline, c) {
*pd = append(*pd, &Proc{p, cmdline, nil, nil, 0})
break
}
}
}
}
func cpu() uint64 {
stat, e := linux.ReadStat("/proc/stat")
if e != nil {
log.Println(e)
return 0
}
s := stat.CPUStatAll
return s.User + s.Nice + s.System + s.Idle
}
func (pd *ProcDiff) Percentage() {
for _, p := range *pd {
sp := strconv.FormatUint(p.Pid, 10)
r, e := linux.ReadProcessStat(filepath.Join("/proc", sp, "stat"))
if e != nil {
log.Println(e)
continue
}
p.Fir = r
}
cpu1 := cpu()
time.Sleep(time.Second)
for _, p := range *pd {
sp := strconv.FormatUint(p.Pid, 10)
r, e := linux.ReadProcessStat(filepath.Join("/proc", sp, "stat"))
if e != nil {
log.Println(e)
continue
}
p.Sec = r
}
cpu2 := cpu()
for _, p := range *pd {
p1 := p.Fir
p2 := p.Sec
if p1 == nil || p2 == nil {
continue
}
user := int64(p2.Utime-p1.Utime) + (p2.Cutime - p1.Cutime)
system := int64(p2.Stime-p1.Stime) + (p2.Cstime - p1.Cstime)
p.Per = (float64(user+system) / float64((cpu2-cpu1)/uint64(runtime.NumCPU()))) * 100
}
}
func checkProcs(ss ...string) []uint64 {
pd := new(ProcDiff)
pd.Contains(ss...)
pd.Percentage()
pdf := []uint64{}
for _, p := range *pd {
if p.Per > *fper {
log.Printf("[WARNING]: %v: %v%% (%v)", p.Pid, math.Trunc(p.Per), p.Cmd)
pdf = append(pdf, p.Pid)
} else if *fver == true {
log.Printf("%v: %v%% (%v)", p.Pid, math.Trunc(p.Per), p.Cmd)
}
}
if *fver == true {
log.Printf("Found \033[1m%v\033[0m corresponding processes, with \033[1m%v\033[0m > %v%%.\n", len(*pd), len(pdf), *fper)
}
return pdf
}
func killProcs(pd, npd []uint64) {
for _, np := range npd {
for _, p := range pd {
if p != np {
continue
}
proc, e := os.FindProcess(int(p))
if e != nil {
log.Println(e)
}
log.Printf("[KILL] sent signal %s to %v\n", *fsys, proc.Pid)
e = proc.Signal(sysc[*fsys])
if e != nil {
log.Println(e)
}
}
}
}