-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmem.go
74 lines (71 loc) · 1.9 KB
/
mem.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
package main
import (
"fmt"
"github.com/shirou/gopsutil/v3/mem"
)
func getMemMetrics(timestamp int64) ([]PromMetric, error) {
vmStat, err := mem.VirtualMemory()
if err != nil {
return nil, fmt.Errorf("Error: obtaining virtual memory info: %v", err)
}
swapStat, err := mem.SwapMemory()
if err != nil {
return nil, fmt.Errorf("Error: obtaining swap memory info: %v", err)
}
memUsed := float64(0)
if vmStat.Total > 0 {
memUsed = 100.0 * float64(vmStat.Used) / float64(vmStat.Total)
}
swapUsed := float64(0)
if swapStat.Total > 0 {
swapUsed = 100.0 * float64(swapStat.Used) / float64(swapStat.Total)
}
metrics := []PromMetric{
PromMetric{
Label: "system_mem_used",
Value: memUsed,
Type: "gauge",
HelpComment: "Percent of memory used",
IncludeComments: true,
},
PromMetric{
Label: "system_mem_used_bytes",
Value: float64(vmStat.Used),
Type: "gauge",
HelpComment: "Used memory in bytes",
IncludeComments: true,
},
PromMetric{
Label: "system_mem_total_bytes",
Value: float64(vmStat.Total),
Type: "gauge",
HelpComment: "Total memory in bytes",
IncludeComments: true,
},
PromMetric{
Label: "system_swap_used",
Value: swapUsed,
Type: "gauge",
HelpComment: "Percent of swap used",
IncludeComments: true,
},
PromMetric{
Label: "system_swap_used_bytes",
Value: float64(swapStat.Used),
Type: "gauge",
HelpComment: "Used swap in bytes",
IncludeComments: true,
},
PromMetric{
Label: "system_swap_total_bytes",
Value: float64(swapStat.Total),
Type: "gauge",
HelpComment: "Total swap in bytes",
IncludeComments: true,
},
}
for i := range metrics {
metrics[i].Timestamp = timestamp
}
return metrics, nil
}