1 Star 0 Fork 0

sqos/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
process.go 2.70 KB
一键复制 编辑 原始数据 按行查看 历史
// +build darwin freebsd linux windows
package process
import (
"fmt"
"runtime"
"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"
"github.com/pkg/errors"
)
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
}
// New creates and returns a new MetricSet.
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
config := struct {
Procs []string `config:"processes"`
Cgroups *bool `config:"process.cgroups.enabled"`
EnvWhitelist []string `config:"process.env.whitelist"`
CPUTicks bool `config:"cpu_ticks"`
}{
Procs: []string{".*"}, // collect all processes by default
}
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.CPUTicks,
},
}
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
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sqos/beats.git
git@gitee.com:sqos/beats.git
sqos
beats
beats
v5.6.12

搜索帮助

0d507c66 1850385 C8b1a773 1850385