一、下载源码:
https://github.com/elastic/beats
二、创建module
1、执行 make create-metricset
执行命令后过程如下:
依次输入module和metricset,输入完成后可以在metricbeat/module目录下可以看到新增加一个domain目录,并且domain目录下有个flow目录。
如果在执行create-metricbeat命令时,提示mage命令找不到,可参考: https://magefile.org/
2、注册新增加的module
进入metricbeat/include/list_common.go 添加新注册的module
_ "github.com/elastic/beats/v7/metricbeat/module/vsphere"
_ "github.com/elastic/beats/v7/metricbeat/module/vsphere/datastore"
_ "github.com/elastic/beats/v7/metricbeat/module/vsphere/host"
_ "github.com/elastic/beats/v7/metricbeat/module/vsphere/virtualmachine"
_ "github.com/elastic/beats/v7/metricbeat/module/windows"
_ "github.com/elastic/beats/v7/metricbeat/module/windows/perfmon"
_ "github.com/elastic/beats/v7/metricbeat/module/windows/service"
_ "github.com/elastic/beats/v7/metricbeat/module/zookeeper"
_ "github.com/elastic/beats/v7/metricbeat/module/zookeeper/connection"
_ "github.com/elastic/beats/v7/metricbeat/module/zookeeper/mntr"
_ "github.com/elastic/beats/v7/metricbeat/module/zookeeper/server"
// 按照原有格式注册新添加的module
_ "github.com/elastic/beats/v7/metricbeat/module/domain"
_ "github.com/elastic/beats/v7/metricbeat/module/domain/flow"
3、为新增加的module增加配置文件
进入metricbeat/modules.d目录,按照已存在的配置文件格式为新增加的metricset增加配置,如示例:
# Module: domain
# Docs: https://www.elastic.co/guide/en/beats/metricbeat/master/metricbeat-module-system.html
# 新增加domain.yml文件,配置采集flow相关选项
- module: domain
period: 10s
metricsets:
- flow
# Configure the mount point of the host’s filesystem for use in monitoring a host from within a container
4、为新增加的metricset编写简单逻辑,进行测试
修改metricbeat/module/domain/flow/flow.go文件,修改fetch函数
func (m *MetricSet) Fetch(report mb.ReporterV2) error {
report.Event(mb.Event{
MetricSetFields: common.MapStr{
// 增加value,测试是否创建module成功
"VALUE": "test_create_module",
"counter": m.counter,
},
})
m.counter++
return nil
}
5、重新编译metricbeat,并运行
go build -o metricbeat
./metricbeat
三、结果
查看metricbeat发出的内容,可以看到刚才添加的内容已经成功发出,说明module注册成功。
附:
官方参考文档:Creating a Metricset | Beats Developer Guide [master] | Elastic