代码拉取完成,页面将自动刷新
// +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 disk 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,
},
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。