­

使用packetbeat 采集mysql数据

  • 2019 年 10 月 4 日
  • 筆記

实验环境: CentOS 6.7 X86_64

ELK版本:5.6.0

实验机器IP: 10.0.20.25

大部分操作及ELK语法这里我都省略掉了。

搭建ELK

这里实验方便起见,我只用一台主机演示,因此是单节点的ES。

直接到官方网站下载rpm包然后传到服务器上,yum安装即可

yum install elasticsearch-5.6.0.rpm kibana-5.6.0-x86_64.rpm logstash-5.6.0.rpm

vim /etc/elasticsearch/elasticsearch.yml修改后的如下(未修改的地方没有列出来):

cluster.name: my-application  network.host: 10.0.20.25  http.port: 9200  ## 在centos6系列上安装ELK5.6需要加上下面2行代码,不然ES启动会失败  bootstrap.system_call_filter: false  bootstrap.memory_lock: false

vim /etc/logstash/logstash.yml   这里实验环境,我没有做任何改动,使用的默认配置参数。

path.data: /var/lib/logstash  path.config: /etc/logstash/conf.d  path.logs: /var/log/logstash

vim/etc/kibana/kibana.yml修改后的如下(未修改的地方没有列出来):

server.port: 5601  server.host: "10.0.20.25"  elasticsearch.url: "http://10.0.20.25:9200"

另外,实际生产环境的ES及logstash 最好修改下JVM的参数配置。

安装packetbeat

rpm -ivh packetbeat-5.6.0-x86_64.rpm  vim /etc/packetbeat/packetbeat.yml  packetbeat.interfaces.device:eth0  # 注意: 我原先采集网卡写的是any,后来部署了prometheus通过127.0.0.1采集数据,  # 经常采集到大量的业务无关的监控数据,因此这里建议只采集需要的网卡。  packetbeat.protocols.mysql:    ports: [3306]    send_request: true    transaction_timeout: 30s  output.elasticsearch:    hosts: ["10.0.20.25:9200"]    work: 1    bulk_max_size: 50    timeout: 90    flush_interval: 1s    template.enabled: true    template.name: "packetbeat.template"    template.overwrite: false    index: test-mysql-%{+yyyy.MM.dd}

然后执行:

curl -XPUT 'http://10.0.20.25:9200/_template/packetbeat'  -d@/etc/packetbeat/packetbeat.template.json   将模板上传到ES里面。

补充:

查看模板是否提交:curl 'http://10.0.20.25:9200/_template/packetbeat'

删除模板的命令:curl -XDELETE 'http://10.0.20.25:9200/_template/packetbeat'

!!如果忘记这一步,先到es里把当天的index删除(curl -XDELETE'http://10.0.20.25:9200/test-mysql-2017.09.23'?pretty),再重启启动beat!!

!!如果自定义了index名称,需要修改/etc/packetbeat/packetbeat.template.json中最后一行的对应的名称再post启动:

然后可以使用命令 curl -XGET'http://10.0.20.25:9200/_template/packetbeat'?pretty  检查目前ES里存在哪些模板

/etc/init.d/packetbeat start  启动Packetbeat抓包工具

稍等片刻,可以使用 curl -XGET'http://10.0.20.25:9200/test-mysql-2017.09.23/_search?pretty' 查看ES里面是否有采集到数据。

到http://10.0.20.25:5601里面,添加索引,名称为 test-mysql-* 

可以看到已经采集到数据了,如下图: