1 Star 0 Fork 0

zhangjungang/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
.github
auditbeat
deploy/kubernetes
dev-tools
docs/devguide
filebeat
generator
heartbeat
libbeat
metricbeat
_meta
beater
cmd
docs
helper
include
mb
module
aerospike
apache
ceph
couchbase
docker
dropwizard
elasticsearch
etcd
golang
graphite
haproxy
http
jolokia
kafka
kibana
kubernetes
logstash
memcached
mongodb
mysql
nginx
php_fpm
postgresql
prometheus
rabbitmq
redis
system
_meta
core
cpu
diskio
filesystem
fsstat
load
memory
network
_meta
doc.go
network.go
network_test.go
process
process_summary
socket
uptime
doc.go
module.yml
system.go
system_linux.go
system_other.go
system_windows.go
util.go
util_test.go
vsphere
windows
zookeeper
doc.go
plugin.go
modules.d
processor/add_kubernetes_metadata
scripts
tests/system
vendor
.gitignore
Dockerfile
Makefile
README.md
docker-compose.yml
main.go
main_test.go
make.bat
metricbeat.reference.yml
metricbeat.yml
packetbeat
script
testing/environments
vendor
winlogbeat
.appveyor.yml
.editorconfig
.gitattributes
.gitignore
.go-version
.pylintrc
.travis.yml
CHANGELOG.asciidoc
CONTRIBUTING.md
LICENSE.txt
Makefile
NOTICE.txt
README.md
Vagrantfile
codecov.yml
reviewdog.yml
setup.yml
克隆/下载
network.go 2.39 KB
一键复制 编辑 原始数据 按行查看 历史
// +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,
},
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangjungang/beats.git
git@gitee.com:zhangjungang/beats.git
zhangjungang
beats
beats
v6.1.2

搜索帮助