热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

metricbeat监控mysql_metricbeat

开发方向直接拓展metricbeat直接开发beat让metricbeat作为库依赖使用Metricbeat构成MetricbeatmoduleMetricbeat包含一系列的mo

2ff34e647e2e3cdfd8dca593e17d9b0a.png

开发方向直接拓展metricbeat

直接开发beat让metricbeat作为库依赖使用

Metricbeat构成

Metricbeat module

Metricbeat包含一系列的module和metricsets,module的命名有他所采集的服务来决定比如mysqlmodule,每个module包含了许多metricsets,metricsets可以用一个指令返回多条数据,比如mysqlmodule的status metricsets就是来返回mysql状态信息。

module和metricsets开发要求必须有fields.yml用来生成module文档和Elasticsearch模板

有文档文件

有集成测试

涵盖80%的测试

创建一个metricset

metricset是module的一部分,metricset用来采集并结构化服务的数据

执行create-metricset指令

首先安装Virtualenv,该工具用来在同一个系统中对不同的pyhton环境进行隔离,metricset脚本需要用到1

2yum -y install python-setuptools

easy_install virtualenv

或者1

2

3

4

5

6curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

python get-pip.py

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple virtualenv

#pip install pip -U

#pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

在 go/src/github.com/elastic/beats/metricbeat下面执行1make create-metricset

该命令会调用当前目录下的Makefile文件的指令create-metricset,由Makefile可知调用的python脚本1

2

3

4# Creates a new metricset. Requires the params MODULE and METRICSET

create-metricset: python-env

@${PYTHON_ENV}/bin/python ${ES_BEATS}/metricbeat/scripts/create_metricset.py --path=$(PWD) --es_beats=$(ES_BEATS) --module=$(MODULE) --metricset=$(METRICSET)

该脚本使用交互模式要求输入module名称和metricset名称,如果输入的module没有就会自动创建并且准备好相应的基础文件。基础文件来自于go/src/github.com/elastic/beats/metricbeat/scripts下面

编译

编译需要执行两条命令1

2make collect

make

make collect

第一条命令也可见Makefile,用来收集并更新相关的文件,在每次更改配置文件后都要进行make collect,各个module和metricsets配置文件在go/src/github.com/elastic/beats/metricbeat/modules.d,metricsets的配置文件在其所属module里面1

2

3# Runs all collection steps and updates afterwards

.PHONY: collect

collect: fields collect-docs configs kibana imports

执行的命令1/usr/local/soft/go/src/github.com/elastic/beats/metricbeat/build/python-env/bin/python ../metricbeat/scripts/modules_collector.py --docs_branch=master

make

make用来编译二进制文件,1go build -i -ldflags "-X github.com/elastic/beats/libbeat/version.buildTime=2018-11-07T09:33:26Z -X github.com/elastic/beats/libbeat/version.commit=b9afa344e937b086d95cea725c84a7755b384ab0"

生成文件解析

创建后会在module/{module}/{metricset}生成下面文件{metricset}.go

_meta/docs.asciidoc

_meta/data.json

_meta/fields.yml

{metricset}.go

该文件定义了如何采集数据和输出

下面是个模板文件,该文件可以在go/src/github.com/elastic/beats/metricbeat/scripts/module/metricset找到,可知1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52package {metricset}

import (

"github.com/elastic/beats/libbeat/common"

"github.com/elastic/beats/libbeat/common/cfgwarn"

"github.com/elastic/beats/metricbeat/mb"

)

// init registers the MetricSet with the central registry as soon as the program

// starts. The New function will be called later to instantiate an instance of

// the MetricSet for each host defined in the module's configuration. After the

// MetricSet has been created then Fetch will begin to be called periodically.

func () {

mb.Registry.MustAddMetricSet("{module}", "{metricset}", New)

}

// MetricSet holds any configuration or state information. It must implement

// the mb.MetricSet interface. And this is best achieved by embedding

// mb.BaseMetricSet because it implements all of the required mb.MetricSet

// interface methods except for Fetch.

type MetricSet struct {

mb.BaseMetricSet

counter int

}

// New creates a new instance of the MetricSet. New is responsible for unpacking

// any MetricSet specific configuration options if there are any.

func New(base mb.BaseMetricSet) (mb.MetricSet, error) {

cfgwarn.Experimental("The {module} {metricset} metricset is experimental.")

config := struct{}{}

if err := base.Module().UnpackConfig(&config); err != nil {

return nil, err

}

return &MetricSet{

BaseMetricSet: base,

counter: 1,

}, nil

}

// Fetch methods implements the data gathering and data conversion to the right

// format. It publishes the event which is then forwarded to the output. In case

// of an error set the Error field of mb.Event or simply call report.Error().

func (m *MetricSet) Fetch(report mb.ReporterV2) {

report.Event(mb.Event{

MetricSetFields: common.MapStr{

"counter": m.counter,

},

})

m.counter++

}

模板代码执行了下面几个流程来采集数据并持久化然后返回到beat平台

初始化

init()方法调用了函数mb.Registry.AddMetricSet,该函数首先把module和metricset进行中心注册,然后传入New函数变量,该New函数在导入模块之后和采集数据之前执行1

2

3

4

5func () {

if err := mb.Registry.AddMetricSet("{module}", "{metricset}", New); err != nil {

panic(err)

}

}

定义返回字段

该struct来定义变量,这些变量用来持久化采集数据和在多个采集之间配置,此struct必须要继承mb.BaseMetricSet字段,然后再添加其他的字段1

2

3

4type MetricSet struct {

mb.BaseMetricSet

counter int

}

创建实例

该New会创建一个MetricSet实例,在进行采集之前执行1

2

3

4

5

6

7

8

9

10

11

12func New(base mb.BaseMetricSet) (mb.MetricSet, error) {

config := struct{}{}

if err := base.Module().UnpackConfig(&config); err != nil {

return nil, err

}

return &MetricSet{

BaseMetricSet: base,

}, nil

}

数据采集

Fetch()在period的事件间隔内去采集数据,该采集的返回是common.MapStr对象,这个对象要返回到Elasticsearch,当发生错误时也要返回,无论成功与否都要返回事件,由代码可知event就是common.MapStr对象,最终这个对象会解析成json格式数据1

2

3

4

5

6

7

8

9func (m *MetricSet) Fetch() (common.MapStr, error) {

event := common.MapStr{

"counter": m.counter,

}

m.counter++

return event, nil

}

多次采集

相对于单次采集,多次采集Fetch()函数将会返回common.MapStr数组,对于多个事件Metricbeat会自动加上时间戳1(m *MetricSet) Fetch() ([]common.MapStr, error)

MetricSet相关配置

添加MetricSet配置选项

比如增加密码配置选项,在配置文件加入过后需要拓展New方法

首先增加配置字段1

2

3

4metricbeat.modules:

- module: {module}

metricsets: ["{metricset}"]

password: "test1234"

拓展New方法

首先需要在struct定义出该变量然后把struct传入方法UnpackConfig1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23type MetricSet struct {

mb.BaseMetricSet

password string

}

func New(base mb.BaseMetricSet) (mb.MetricSet, error) {

//解析出配置

config := struct {

Password string `config:"password"`

}{

Password: "",

}

err := base.Module().UnpackConfig(&config)

if err != nil {

return nil, err

}

return &MetricSet{

BaseMetricSet: base,

password: config.Password,

}, nil

}

data.go

采集后的数据需要做数据转换创建data.go文件,该文件内主要包含eventMapping(…)方法来进行数据转换

fields.yml

该文件这些作用创建Elasticsearch模板

创建Kibana索引配置

创建field文档为metricset

docs.asciidoc

metricset需要存在文档,文档位置module/{module}/{metricset}/_meta/docs.asciidoc

开发项目

根据所给目录来得出所在磁盘的分区情况,项目源码



推荐阅读
  • Python中的PyInputPlus模块原文:https ... [详细]
  • 安装mysqlclient失败解决办法
    本文介绍了在MAC系统中,使用django使用mysql数据库报错的解决办法。通过源码安装mysqlclient或将mysql_config添加到系统环境变量中,可以解决安装mysqlclient失败的问题。同时,还介绍了查看mysql安装路径和使配置文件生效的方法。 ... [详细]
  • Centos7.6安装Gitlab教程及注意事项
    本文介绍了在Centos7.6系统下安装Gitlab的详细教程,并提供了一些注意事项。教程包括查看系统版本、安装必要的软件包、配置防火墙等步骤。同时,还强调了使用阿里云服务器时的特殊配置需求,以及建议至少4GB的可用RAM来运行GitLab。 ... [详细]
  • 篇首语:本文由编程笔记#小编为大家整理,主要介绍了软件测试知识点之数据库压力测试方法小结相关的知识,希望对你有一定的参考价值。 ... [详细]
  • Python操作MySQL(pymysql模块)详解及示例代码
    本文介绍了使用Python操作MySQL数据库的方法,详细讲解了pymysql模块的安装和连接MySQL数据库的步骤,并提供了示例代码。内容涵盖了创建表、插入数据、查询数据等操作,帮助读者快速掌握Python操作MySQL的技巧。 ... [详细]
  • 开源Keras Faster RCNN模型介绍及代码结构解析
    本文介绍了开源Keras Faster RCNN模型的环境需求和代码结构,包括FasterRCNN源码解析、RPN与classifier定义、data_generators.py文件的功能以及损失计算。同时提供了该模型的开源地址和安装所需的库。 ... [详细]
  • 本文总结了使用不同方式生成 Dataframe 的方法,包括通过CSV文件、Excel文件、python dictionary、List of tuples和List of dictionary。同时介绍了一些注意事项,如使用绝对路径引入文件和安装xlrd包来读取Excel文件。 ... [详细]
  • 通过Anaconda安装tensorflow,并安装运行spyder编译器的完整教程
    本文提供了一个完整的教程,介绍了如何通过Anaconda安装tensorflow,并安装运行spyder编译器。文章详细介绍了安装Anaconda、创建tensorflow环境、安装GPU版本tensorflow、安装和运行Spyder编译器以及安装OpenCV等步骤。该教程适用于Windows 8操作系统,并提供了相关的网址供参考。通过本教程,读者可以轻松地安装和配置tensorflow环境,以及运行spyder编译器进行开发。 ... [详细]
  • 搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的详细步骤
    本文详细介绍了搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的步骤,包括环境说明、相关软件下载的地址以及所需的插件下载地址。 ... [详细]
  • Python实现变声器功能(萝莉音御姐音)的方法及步骤
    本文介绍了使用Python实现变声器功能(萝莉音御姐音)的方法及步骤。首先登录百度AL开发平台,选择语音合成,创建应用并填写应用信息,获取Appid、API Key和Secret Key。然后安装pythonsdk,可以通过pip install baidu-aip或python setup.py install进行安装。最后,书写代码实现变声器功能,使用AipSpeech库进行语音合成,可以设置音量等参数。 ... [详细]
  • 31.项目部署
    目录1一些概念1.1项目部署1.2WSGI1.3uWSGI1.4Nginx2安装环境与迁移项目2.1项目内容2.2项目配置2.2.1DEBUG2.2.2STAT ... [详细]
  • 本文介绍了Composer依赖管理的重要性及使用方法。对于现代语言而言,包管理器是标配,而Composer作为PHP的包管理器,解决了PEAR的问题,并且使用简单,方便提交自己的包。文章还提到了使用Composer能够避免各种include的问题,避免命名空间冲突,并且能够方便地安装升级扩展包。 ... [详细]
  • 1.直接在cmd窗口运行pipinstalljieba2.使用conda自带的安装工具condainstalljieba3.有一些模块是无法使用以上两种方式安装上ÿ ... [详细]
  • EzPP 0.2发布,新增YAML布局渲染功能
    EzPP发布了0.2.1版本,新增了YAML布局渲染功能,可以将YAML文件渲染为图片,并且可以复用YAML作为模版,通过传递不同参数生成不同的图片。这个功能可以用于绘制Logo、封面或其他图片,让用户不需要安装或卸载Photoshop。文章还提供了一个入门例子,介绍了使用ezpp的基本渲染方法,以及如何使用canvas、text类元素、自定义字体等。 ... [详细]
  • Node.js学习笔记(一)package.json及cnpm
    本文介绍了Node.js中包的概念,以及如何使用包来统一管理具有相互依赖关系的模块。同时还介绍了NPM(Node Package Manager)的基本介绍和使用方法,以及如何通过NPM下载第三方模块。 ... [详细]
author-avatar
Aovte
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有