forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
135 lines (103 loc) · 3.61 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
// +build darwin freebsd linux openbsd windows
package cpu
import (
"runtime"
"github.com/elastic/beats/metricbeat/module/system"
sigar "github.com/elastic/gosigar"
)
// NumCPU is the number of CPU cores the system has.
var NumCPU = runtime.NumCPU()
type CPU struct {
CpuPerCore bool
LastCpuTimes *CpuTimes
LastCpuTimesList []CpuTimes
CpuTicks bool
Cores int
}
type CpuTimes struct {
sigar.Cpu
UserPercent float64 `json:"user_p"`
SystemPercent float64 `json:"system_p"`
IdlePercent float64 `json:"idle_p"`
IOwaitPercent float64 `json:"iowait_p"`
IrqPercent float64 `json:"irq_p"`
NicePercent float64 `json:"nice_p"`
SoftIrqPercent float64 `json:"softirq_p"`
StealPercent float64 `json:"steal_p"`
}
func GetCpuTimes() (*CpuTimes, error) {
cpu := sigar.Cpu{}
err := cpu.Get()
if err != nil {
return nil, err
}
return &CpuTimes{Cpu: cpu}, nil
}
func GetCpuTimesList() ([]CpuTimes, error) {
cpuList := sigar.CpuList{}
err := cpuList.Get()
if err != nil {
return nil, err
}
cpuTimes := make([]CpuTimes, len(cpuList.List))
for i, cpu := range cpuList.List {
cpuTimes[i] = CpuTimes{Cpu: cpu}
}
return cpuTimes, nil
}
func GetCpuPercentage(last *CpuTimes, current *CpuTimes) *CpuTimes {
if last != nil && current != nil {
allDelta := current.Cpu.Total() - last.Cpu.Total()
if allDelta == 0 {
// first inquiry
return current
}
calculate := func(field2 uint64, field1 uint64) float64 {
perc := 0.0
delta := int64(field2 - field1)
perc = float64(delta) / float64(allDelta)
return system.Round(perc*float64(NumCPU), .5, 4)
}
current.UserPercent = calculate(current.Cpu.User, last.Cpu.User)
current.SystemPercent = calculate(current.Cpu.Sys, last.Cpu.Sys)
current.IdlePercent = calculate(current.Cpu.Idle, last.Cpu.Idle)
current.IOwaitPercent = calculate(current.Cpu.Wait, last.Cpu.Wait)
current.IrqPercent = calculate(current.Cpu.Irq, last.Cpu.Irq)
current.NicePercent = calculate(current.Cpu.Nice, last.Cpu.Nice)
current.SoftIrqPercent = calculate(current.Cpu.SoftIrq, last.Cpu.SoftIrq)
current.StealPercent = calculate(current.Cpu.Stolen, last.Cpu.Stolen)
}
return current
}
func GetCpuPercentageList(last, current []CpuTimes) []CpuTimes {
if last != nil && current != nil && len(last) == len(current) {
calculate := func(field2 uint64, field1 uint64, all_delta uint64) float64 {
perc := 0.0
delta := int64(field2 - field1)
perc = float64(delta) / float64(all_delta)
return system.Round(perc, .5, 4)
}
for i := 0; i < len(last); i++ {
allDelta := current[i].Cpu.Total() - last[i].Cpu.Total()
current[i].UserPercent = calculate(current[i].Cpu.User, last[i].Cpu.User, allDelta)
current[i].SystemPercent = calculate(current[i].Cpu.Sys, last[i].Cpu.Sys, allDelta)
current[i].IdlePercent = calculate(current[i].Cpu.Idle, last[i].Cpu.Idle, allDelta)
current[i].IOwaitPercent = calculate(current[i].Cpu.Wait, last[i].Cpu.Wait, allDelta)
current[i].IrqPercent = calculate(current[i].Cpu.Irq, last[i].Cpu.Irq, allDelta)
current[i].NicePercent = calculate(current[i].Cpu.Nice, last[i].Cpu.Nice, allDelta)
current[i].SoftIrqPercent = calculate(current[i].Cpu.SoftIrq, last[i].Cpu.SoftIrq, allDelta)
current[i].StealPercent = calculate(current[i].Cpu.Stolen, last[i].Cpu.Stolen, allDelta)
}
}
return current
}
func GetCores() int {
cores := runtime.NumCPU()
return cores
}
func (cpu *CPU) AddCpuPercentage(t2 *CpuTimes) {
cpu.LastCpuTimes = GetCpuPercentage(cpu.LastCpuTimes, t2)
}
func (cpu *CPU) AddCpuPercentageList(t2 []CpuTimes) {
cpu.LastCpuTimesList = GetCpuPercentageList(cpu.LastCpuTimesList, t2)
}