1 Star 0 Fork 0

zhangjungang/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
memory.go 1.52 KB
一键复制 编辑 原始数据 按行查看 历史
// +build darwin freebsd linux openbsd windows
// +build darwin freebsd linux openbsd windows
package memory
import (
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/mb/parse"
"github.com/pkg/errors"
)
func init() {
if err := mb.Registry.AddMetricSet("system", "memory", New, parse.EmptyHostParser); err != nil {
panic(err)
}
}
// MetricSet for fetching system memory metrics.
type MetricSet struct {
mb.BaseMetricSet
}
// New is a mb.MetricSetFactory that returns a memory.MetricSet.
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
return &MetricSet{base}, nil
}
// Fetch fetches memory metrics from the OS.
func (m *MetricSet) Fetch() (event common.MapStr, err error) {
memStat, err := GetMemory()
if err != nil {
return nil, errors.Wrap(err, "memory")
}
AddMemPercentage(memStat)
swapStat, err := GetSwap()
if err != nil {
return nil, errors.Wrap(err, "swap")
}
AddSwapPercentage(swapStat)
memory := common.MapStr{
"total": memStat.Total,
"used": common.MapStr{
"bytes": memStat.Used,
"pct": memStat.UsedPercent,
},
"free": memStat.Free,
"actual": common.MapStr{
"free": memStat.ActualFree,
"used": common.MapStr{
"pct": memStat.ActualUsedPercent,
"bytes": memStat.ActualUsed,
},
},
}
swap := common.MapStr{
"total": swapStat.Total,
"used": common.MapStr{
"bytes": swapStat.Used,
"pct": swapStat.UsedPercent,
},
"free": swapStat.Free,
}
memory["swap"] = swap
return memory, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangjungang/beats.git
git@gitee.com:zhangjungang/beats.git
zhangjungang
beats
beats
v5.6.9

搜索帮助