1 Star 0 Fork 0

zhangjungang / beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
process.go 2.60 KB
一键复制 编辑 原始数据 按行查看 历史
// +build darwin freebsd linux windows
package process
import (
"fmt"
"runtime"
"github.com/pkg/errors"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/mb/parse"
"github.com/elastic/beats/metricbeat/module/system"
"github.com/elastic/gosigar/cgroup"
)
var debugf = logp.MakeDebug("system.process")
func init() {
if err := mb.Registry.AddMetricSet("system", "process", New, parse.EmptyHostParser); err != nil {
panic(err)
}
}
// MetricSet that fetches process metrics.
type MetricSet struct {
mb.BaseMetricSet
stats *ProcStats
cgroup *cgroup.Reader
cacheCmdLine bool
}
// New creates and returns a new MetricSet.
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
config := defaultConfig
if err := base.Module().UnpackConfig(&config); err != nil {
return nil, err
}
m := &MetricSet{
BaseMetricSet: base,
stats: &ProcStats{
Procs: config.Procs,
EnvWhitelist: config.EnvWhitelist,
CpuTicks: config.IncludeCPUTicks || (config.CPUTicks != nil && *config.CPUTicks),
CacheCmdLine: config.CacheCmdLine,
IncludeTop: config.IncludeTop,
},
}
err := m.stats.InitProcStats()
if err != nil {
return nil, err
}
if runtime.GOOS == "linux" {
systemModule, ok := base.Module().(*system.Module)
if !ok {
return nil, fmt.Errorf("unexpected module type")
}
if config.Cgroups == nil || *config.Cgroups {
debugf("process cgroup data collection is enabled, using hostfs='%v'", systemModule.HostFS)
m.cgroup, err = cgroup.NewReader(systemModule.HostFS, true)
if err != nil {
if err == cgroup.ErrCgroupsMissing {
logp.Warn("cgroup data collection will be disabled: %v", err)
} else {
return nil, errors.Wrap(err, "error initializing cgroup reader")
}
}
}
}
return m, nil
}
// Fetch fetches metrics for all processes. It iterates over each PID and
// collects process metadata, CPU metrics, and memory metrics.
func (m *MetricSet) Fetch() ([]common.MapStr, error) {
procs, err := m.stats.GetProcStats()
if err != nil {
return nil, errors.Wrap(err, "process stats")
}
if m.cgroup != nil {
for _, proc := range procs {
pid, ok := proc["pid"].(int)
if !ok {
debugf("error converting pid to int for proc %+v", proc)
continue
}
stats, err := m.cgroup.GetStatsForProcess(pid)
if err != nil {
debugf("error getting cgroups stats for pid=%d, %v", pid, err)
continue
}
if statsMap := cgroupStatsToMap(stats); statsMap != nil {
proc["cgroup"] = statsMap
}
}
}
return procs, err
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangjungang/beats.git
git@gitee.com:zhangjungang/beats.git
zhangjungang
beats
beats
v6.1.4

搜索帮助

344bd9b3 5694891 D2dac590 5694891