forked from cloudradar-monitoring/cagent
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mem_windows.go
84 lines (69 loc) · 2.45 KB
/
mem_windows.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
// +build windows
package cagent
import (
"context"
"errors"
"strings"
"time"
"github.com/shirou/gopsutil/mem"
log "github.com/sirupsen/logrus"
"github.com/cloudradar-monitoring/cagent/pkg/common"
"github.com/cloudradar-monitoring/cagent/pkg/monitoring"
)
const memGetTimeout = time.Second * 10
func (ca *Cagent) MemResults() (common.MeasurementsMap, *mem.VirtualMemoryStat, error) {
var errs []string
ctx, cancel := context.WithTimeout(context.Background(), memGetTimeout)
defer cancel()
results := map[string]interface{}{
"total_B": nil,
"free_B": nil,
"free_percent": nil,
"cached_B": nil,
"cached_percent": nil,
"used_B": nil,
"used_percent": nil,
"available_B": nil,
"available_percent": nil,
}
memStat, err := mem.VirtualMemoryWithContext(ctx)
if err != nil {
errs = append(errs, err.Error())
log.Errorf("[MEM] Failed to get virtual memory stat: %s", err.Error())
} else {
results["total_B"] = memStat.Total
results["available_B"] = memStat.Available
available := float64(memStat.Available)
if available <= 0.0 {
available = 0.0
}
results["available_percent"] = floatToIntPercentRoundUP(available / float64(memStat.Total))
}
free, err := monitoring.GetWatcher().Query(`\Memory\Free & Zero Page List Bytes`, "*")
if err != nil {
errs = append(errs, err.Error())
log.Errorf("[MEM] Failed to get free memory: %s", err.Error())
} else {
results["used_B"] = int(memStat.Total) - int(free)
results["used_percent"] = floatToIntPercentRoundUP((float64(memStat.Total) - free) / float64(memStat.Total))
results["free_B"] = int(free)
results["free_percent"] = floatToIntPercentRoundUP(free / float64(memStat.Total))
}
cachedMemoryMetric := []string{`\Memory\Standby Cache Normal Priority Bytes`, `\Memory\Standby Cache Reserve Bytes`, `\Memory\Cache Bytes`, `\Memory\Modified Page List Bytes`}
cachedBytes := 0
for _, metric := range cachedMemoryMetric {
cached, err := monitoring.GetWatcher().Query(metric, "*")
if err != nil {
errs = append(errs, err.Error())
log.Errorf("[MEM] Failed to get cached memory(%s): %s", metric, err.Error())
continue
}
cachedBytes += int(cached)
}
results["cached_B"] = cachedBytes
results["cached_percent"] = floatToIntPercentRoundUP(float64(cachedBytes) / float64(memStat.Total))
if len(errs) == 0 {
return results, memStat, nil
}
return results, memStat, errors.New("MEM: " + strings.Join(errs, "; "))
}