forked from cloudradar-monitoring/cagent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swap.go
40 lines (30 loc) · 880 Bytes
/
swap.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
package cagent
import (
"context"
"errors"
"strings"
"time"
"github.com/shirou/gopsutil/mem"
log "github.com/sirupsen/logrus"
"github.com/cloudradar-monitoring/cagent/pkg/common"
)
const swapGetTimeout = time.Second * 10
func (ca *Cagent) SwapResults() (common.MeasurementsMap, error) {
results := common.MeasurementsMap{}
var errs []string
ctx, cancel := context.WithTimeout(context.Background(), swapGetTimeout)
defer cancel()
swapStat, err := mem.SwapMemoryWithContext(ctx)
if err != nil {
log.Errorf("[SWAP] Failed to get swap memory stat: %s", err.Error())
errs = append(errs, err.Error())
} else if swapStat.Total > 0 {
results["total_B"] = swapStat.Total
results["used_B"] = swapStat.Used
results["free_B"] = swapStat.Free
}
if len(errs) == 0 {
return results, nil
}
return results, errors.New("SWAP: " + strings.Join(errs, "; "))
}