1 Star 0 Fork 0

zhangjungang/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
network.go 2.39 KB
一键复制 编辑 原始数据 按行查看 历史
Shota Ito 提交于 2017-04-11 20:02 . fix comment of disk IO metrics (#3986)
// +build darwin freebsd linux windows
package network
import (
"strings"
"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/pkg/errors"
"github.com/shirou/gopsutil/net"
)
var debugf = logp.MakeDebug("system-network")
func init() {
if err := mb.Registry.AddMetricSet("system", "network", New, parse.EmptyHostParser); err != nil {
panic(err)
}
}
// MetricSet for fetching system network IO metrics.
type MetricSet struct {
mb.BaseMetricSet
interfaces map[string]struct{}
}
// New is a mb.MetricSetFactory that returns a new MetricSet.
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
// Unpack additional configuration options.
config := struct {
Interfaces []string `config:"interfaces"`
}{}
err := base.Module().UnpackConfig(&config)
if err != nil {
return nil, err
}
var interfaceSet map[string]struct{}
if len(config.Interfaces) > 0 {
interfaceSet = make(map[string]struct{}, len(config.Interfaces))
for _, ifc := range config.Interfaces {
interfaceSet[strings.ToLower(ifc)] = struct{}{}
}
debugf("network io stats will be included for %v", interfaceSet)
}
return &MetricSet{
BaseMetricSet: base,
interfaces: interfaceSet,
}, nil
}
// Fetch fetches network IO metrics from the OS.
func (m *MetricSet) Fetch() ([]common.MapStr, error) {
stats, err := net.IOCounters(true)
if err != nil {
return nil, errors.Wrap(err, "network io counters")
}
var events []common.MapStr
if m.interfaces == nil {
// Include all stats.
for _, counters := range stats {
events = append(events, ioCountersToMapStr(counters))
}
} else {
// Select stats by interface name.
for _, counters := range stats {
name := strings.ToLower(counters.Name)
if _, include := m.interfaces[name]; include {
events = append(events, ioCountersToMapStr(counters))
continue
}
}
}
return events, nil
}
func ioCountersToMapStr(counters net.IOCountersStat) common.MapStr {
return common.MapStr{
"name": counters.Name,
"in": common.MapStr{
"errors": counters.Errin,
"dropped": counters.Dropin,
"bytes": counters.BytesRecv,
"packets": counters.PacketsRecv,
},
"out": common.MapStr{
"errors": counters.Errout,
"dropped": counters.Dropout,
"packets": counters.PacketsSent,
"bytes": counters.BytesSent,
},
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangjungang/beats.git
git@gitee.com:zhangjungang/beats.git
zhangjungang
beats
beats
v6.0.0

搜索帮助