代码拉取完成,页面将自动刷新
// +build darwin freebsd linux openbsd windows
package cpu
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", "cpu", New, parse.EmptyHostParser); err != nil {
panic(err)
}
}
// MetricSet for fetching system CPU metrics.
type MetricSet struct {
mb.BaseMetricSet
cpu *CPU
}
// New is a mb.MetricSetFactory that returns a cpu.MetricSet.
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
config := struct {
CpuTicks bool `config:"cpu_ticks"` // export CPU usage in ticks
}{
CpuTicks: false,
}
if err := base.Module().UnpackConfig(&config); err != nil {
return nil, err
}
return &MetricSet{
BaseMetricSet: base,
cpu: &CPU{
CpuTicks: config.CpuTicks,
},
}, nil
}
// Fetch fetches CPU metrics from the OS.
func (m *MetricSet) Fetch() (common.MapStr, error) {
stat, err := GetCpuTimes()
if err != nil {
return nil, errors.Wrap(err, "cpu times")
}
m.cpu.AddCpuPercentage(stat)
cpuCores := GetCores()
cpuStat := common.MapStr{
"cores": cpuCores,
"user": common.MapStr{
"pct": stat.UserPercent,
},
"system": common.MapStr{
"pct": stat.SystemPercent,
},
"idle": common.MapStr{
"pct": stat.IdlePercent,
},
"iowait": common.MapStr{
"pct": stat.IOwaitPercent,
},
"irq": common.MapStr{
"pct": stat.IrqPercent,
},
"nice": common.MapStr{
"pct": stat.NicePercent,
},
"softirq": common.MapStr{
"pct": stat.SoftIrqPercent,
},
"steal": common.MapStr{
"pct": stat.StealPercent,
},
}
if m.cpu.CpuTicks {
cpuStat["user"].(common.MapStr)["ticks"] = stat.User
cpuStat["system"].(common.MapStr)["ticks"] = stat.Sys
cpuStat["nice"].(common.MapStr)["ticks"] = stat.Nice
cpuStat["idle"].(common.MapStr)["ticks"] = stat.Idle
cpuStat["iowait"].(common.MapStr)["ticks"] = stat.Wait
cpuStat["irq"].(common.MapStr)["ticks"] = stat.Irq
cpuStat["softirq"].(common.MapStr)["ticks"] = stat.SoftIrq
cpuStat["steal"].(common.MapStr)["ticks"] = stat.Stolen
}
return cpuStat, nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。