forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
system.go
104 lines (82 loc) · 1.95 KB
/
system.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
package common
import (
"github.com/elastic/beats/libbeat/common"
sigar "github.com/elastic/gosigar"
)
type SwapStat struct {
sigar.Swap
UsedPercent float64 `json:"used_p"`
}
type SystemLoad struct {
Load1 float64 `json:"load1"`
Load5 float64 `json:"load5"`
Load15 float64 `json:"load15"`
}
type MemStat struct {
sigar.Mem
UsedPercent float64 `json:"used_p"`
ActualUsedPercent float64 `json:"actual_used_p"`
}
func GetSystemLoad() (*SystemLoad, error) {
concreteSigar := sigar.ConcreteSigar{}
avg, err := concreteSigar.GetLoadAverage()
if err != nil {
return nil, err
}
return &SystemLoad{
Load1: avg.One,
Load5: avg.Five,
Load15: avg.Fifteen,
}, nil
}
func GetMemory() (*MemStat, error) {
mem := sigar.Mem{}
err := mem.Get()
if err != nil {
return nil, err
}
return &MemStat{Mem: mem}, nil
}
func GetMemoryEvent(memStat *MemStat) common.MapStr {
return common.MapStr{
"total": memStat.Total,
"used": memStat.Used,
"free": memStat.Free,
"actual_used": memStat.ActualUsed,
"actual_free": memStat.ActualFree,
"used_p": memStat.UsedPercent,
"actual_used_p": memStat.ActualUsedPercent,
}
}
func GetSwap() (*SwapStat, error) {
swap := sigar.Swap{}
err := swap.Get()
if err != nil {
return nil, err
}
return &SwapStat{Swap: swap}, nil
}
func GetSwapEvent(swapStat *SwapStat) common.MapStr {
return common.MapStr{
"total": swapStat.Total,
"used": swapStat.Used,
"free": swapStat.Free,
"used_p": swapStat.UsedPercent,
}
}
func AddMemPercentage(m *MemStat) {
if m.Mem.Total == 0 {
return
}
perc := float64(m.Mem.Used) / float64(m.Mem.Total)
m.UsedPercent = Round(perc, .5, 4)
actual_perc := float64(m.Mem.ActualUsed) / float64(m.Mem.Total)
m.ActualUsedPercent = Round(actual_perc, .5, 4)
}
func AddSwapPercentage(s *SwapStat) {
if s.Swap.Total == 0 {
return
}
perc := float64(s.Swap.Used) / float64(s.Swap.Total)
s.UsedPercent = Round(perc, .5, 4)
}