-
Notifications
You must be signed in to change notification settings - Fork 13
/
memory.go
109 lines (97 loc) · 2.81 KB
/
memory.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
package mem
import (
"context"
"fmt"
"runtime"
"github.com/circonus-labs/circonus-unified-agent/cua"
"github.com/circonus-labs/circonus-unified-agent/plugins/inputs"
"github.com/circonus-labs/circonus-unified-agent/plugins/inputs/system"
)
type Stats struct {
ps system.PS
platform string
}
func (*Stats) Description() string {
return "Read metrics about memory usage"
}
func (*Stats) SampleConfig() string {
return `
instance_id = "" # unique instance identifier (REQUIRED)
`
}
func (s *Stats) Init() error {
s.platform = runtime.GOOS
return nil
}
func (s *Stats) Gather(ctx context.Context, acc cua.Accumulator) error {
vm, err := s.ps.VMStat()
if err != nil {
return fmt.Errorf("error getting virtual memory info: %w", err)
}
fields := map[string]interface{}{
"total": vm.Total,
"available": vm.Available,
"used": vm.Used,
"used_percent": 100 * float64(vm.Used) / float64(vm.Total),
"available_percent": 100 * float64(vm.Available) / float64(vm.Total),
}
switch s.platform {
case "darwin":
fields["active"] = vm.Active
fields["free"] = vm.Free
fields["inactive"] = vm.Inactive
fields["wired"] = vm.Wired
case "openbsd":
fields["active"] = vm.Active
fields["cached"] = vm.Cached
fields["free"] = vm.Free
fields["inactive"] = vm.Inactive
fields["wired"] = vm.Wired
case "freebsd":
fields["active"] = vm.Active
fields["buffered"] = vm.Buffers
fields["cached"] = vm.Cached
fields["free"] = vm.Free
fields["inactive"] = vm.Inactive
fields["laundry"] = vm.Laundry
fields["wired"] = vm.Wired
case "linux":
fields["active"] = vm.Active
fields["buffered"] = vm.Buffers
fields["cached"] = vm.Cached
fields["commit_limit"] = vm.CommitLimit
fields["committed_as"] = vm.CommittedAS
fields["dirty"] = vm.Dirty
fields["free"] = vm.Free
fields["high_free"] = vm.HighFree
fields["high_total"] = vm.HighTotal
fields["huge_pages_free"] = vm.HugePagesFree
fields["huge_page_size"] = vm.HugePageSize
fields["huge_pages_total"] = vm.HugePagesTotal
fields["inactive"] = vm.Inactive
fields["low_free"] = vm.LowFree
fields["low_total"] = vm.LowTotal
fields["mapped"] = vm.Mapped
fields["page_tables"] = vm.PageTables
fields["shared"] = vm.Shared
fields["slab"] = vm.Slab
fields["sreclaimable"] = vm.Sreclaimable
fields["sunreclaim"] = vm.Sunreclaim
fields["swap_cached"] = vm.SwapCached
fields["swap_free"] = vm.SwapFree
fields["swap_total"] = vm.SwapTotal
fields["vmalloc_chunk"] = vm.VmallocChunk
fields["vmalloc_total"] = vm.VmallocTotal
fields["vmalloc_used"] = vm.VmallocUsed
fields["write_back_tmp"] = vm.WriteBackTmp
fields["write_back"] = vm.WriteBack
}
acc.AddGauge("mem", fields, nil)
return nil
}
func init() {
ps := system.NewSystemPS()
inputs.Add("mem", func() cua.Input {
return &Stats{ps: ps}
})
}