当前位置: 首页 > 后端技术 > Python

裸机使用filebeat收集裸机上的日志文件发送到elasticsearch

时间:2023-03-26 00:38:58 Python

部署elasticsearch和kibana因为是demo所以直接用docker-compose运行即可version:"3"services:elk-elasticsearch:container_name:elk-elasticsearchimage:elasticsearch:7.17.1ports:-"9200:9200"-"9300:9300"environment:-discovery.type=single-node-"ES_JAVA_OPTS=-Xms1g-Xmx1g"#限制内存es的大小,否则将消耗10GB以上的RAMelk-kibana:container_name:elk-kibanaimage:kibana:7.17.1ports:-"5601:5601"environment:-ELASTICSEARCH_HOSTS=http://192.168.31.245:9200volumes:-./kibana.yml:/usr/share/kibana/config/kibana.yml#kibana其实是支持中文的,只需要在/usr/share/kibana/config/中添加一行i18n.locale:"zh-CN"/kibanakibana.yml.yml的内容主要是为了显示中文##**THISISANAUTO-GENERATEDFILE**##DefaultKibanaconfigurationfordockertargetserver.host:"0.0.0.0"server.shutdownTimeout:"5s"elasticsearch.Hosts:["http://192.168.31.174:9200"]monitoring.ui.container.elasticsearch.enabled:truei18n.locale:"zh-CN"elasticsearch和kibana的版本要一致,下面的filebeat应该也有一致的版本!这次我们都用7.17.1。如果需要使用dbeaver等工具连接elasticsearch遇到currentlicenseisnon-compliantfor[jdbc],可以参考:currentlicenseisnon-compliantforjdbc解决!在创建索引之初,我参考了这个教程:EFK搭建一个简单的日志分析系统。不得不说,这篇教程写的很烂,分量很短,但即便如此,它已经部署在了谷歌搜索的前列。在elasticsearch和kibana之后,我们需要去elasticsearch创建索引。为什么我们需要创建索引(index)?elasticsearch中index的概念相当于mysql中的table(elasticsearch没有db的概念)。主要需要注意的是,在创建elasticsearch索引时,只需要一个索引名,不需要定义schema。我们需要索引作为日志的存储容器,不同的索引存储不同项目或者不同业务的日志。您不能将所有日志存储在同一个索引中!如何创建索引?方法有很多种,比如:通过dbeaver连接elasticsearch,然后在dbeaver中为elasticsearch创建索引使用kibana连接elasticsearch,然后在kibana中为elasticsearch创建索引使用编程语言提供的elasticsearch客户端sdk比如python和java直接连接elasticsearch并创建索引使用python创建elasticsearch的索引。我选择python,因为我最熟悉python的elasticsearchclientsdk的版本也需要和版本保持一致!pipinstallelasticsearch==7.17.1如何使用elasticsearchclientsdk操作elasticsearch,可以参考这篇文章:Elasticsearch基本介绍及其用Python实现(本文elasticsearchclientsdk比较老,所以一些api参数名等有变化)这里有几个参考代码:创建索引,我这里称之为ideaboomfromelasticsearchimportElasticsearches=Elasticsearch("http://192.168.31.245:9200")result=es.indices.create(index='ideaboom')print(result)在这里非常简单。其实还需要加上一些参数,比如指定索引的“副本数”和“分片数”。比如你的elasticsearch集群有3个节点,那么“分片数”应该是3的倍数,这样才能更好的利用集群的资源!使用Kibana创建elasticsearch索引好像是使用pythonsdk创建索引,不能指定“分片数”和“副本数”,所以加个版本的KibanaPUTcrawlab{"settings":{"index":{"number_of_shards":9,"number_of_replicas":1}}}参考:ElasticSearch设置shards和replicas个数裸机使用filebeat收集裸机上的日志文件发送到elasticsearch先安装filebeat参考:Filebeat快速入门:debian系统安装配置安装filebeat7.17.1下载:wgethttps://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.17.1-amd64.deb超快,无需安装代理:sudoaptinstall./filebeat-7.17.1-amd64.deb使用filebeat收集数据并将其发送到elasticsearch。filebeat的语法可以参考:filebeat输出结果到elasticsearch的多个索引我的filebeat.yml内容如下:filebeat.inputs:-type:log#改成true开启这个输入配置。enabled:true#应该抓取和获取的路径。基于全局的路径。paths:#日志的实际路径地址-/home/bot/Desktop/coder/ideaboom/test_ELK_EFK/logs/run.logfields:#日志标签,区分不同的日志,会使用下面的索引type:"ideaboom"fields_under_root:true#指定被监控的编码类型文件,plain和utf-8都是可以处理中文日志的编码:utf-8#setup.kibana:#host:"192.168.31.245:5601"setup.ilm.enabled:falseoutput.elasticsearch:hosts:["192.168.31.245:9200"]indices:#索引名,通常是'服务名+ip+--%{+yyyy.MM.dd}'-index:"ideaboom-%{+yyyy.MM.dd}"when.contains:#Labels,对应日志和索引,对应上面的type:"ideaboom"之前已经在elasticsearch中创建了一个名为ideaboom的索引??,收集的是run.log文件,我只是在这个文件中放了一些东西:2022-07-0802:39:39,746-13651-logger-INFO-total[1]urls,max_id[69013414]2022-07-0802:39:39,746-13651-记录器-信息-总[1]个网址,最大ID[69013415]2022-07-0802:39:39,746-13651-记录器-信息-总[1]个网址,最大ID[69013414]2022-07-0802:39:39,746-13651-记录器-信息-总[1]个网址,最大ID[69013415]2022-07-0802:39:39,746-13651-记录器-信息-总[1]个网址,最大ID[69013414]2022-07-0802:39:39,746-13651-logger-INFO-total[1]urls,max_id[69013415]和nginx类似,我们需要将自己的yml映射到filebeat的默认配置文件路径sudocp/home/bot/Desktop/coder/ideaboom/test_ELK_EFK/filebeat.yml/etc/filebeat/filebeat.yml关于默认配置文件path,可以参考:为什么使用了-c参数,filebeat还是加载了etc中的filebeat.yml?运行filebeatsudofilebeat注意运行filebeat有很多坑,filebe在为了数据安全,需要root才能运行filebeat。如果不是root运行,相关配置文件的owner必须和运行filebeat等的用户一致,否则会遇到ubuntudebianExiting:errorloadingconfigfile:open/etc/filebeat/filebeat.yml:permissiondenied报类似错误!还有一个超级要注意的是filebeat是没有输出的,也就是说它不会告诉你它是收集了日志还是发送到elasticsearch了!(可能有办法知道,但我不知道怎么做),这很棘手,为了知道filebeat是否正常工作,我什至打开了wireshark来抓包!查看elasticsearch中的日志假设此时,你的filebeat已经成功采集日志,并成功将日志发送到elasticsearch。我们可以在dbeaver中查看(没有安装dbeaver的可以跳过这一步)可以看到,名为ideaboom的索引??中已经有4条日志了。检查kibana中的日志。下一步是重头戏。我是在浏览器打开的:http://192.168.31.245:5601先点击左上角的三个横条,然后点击管理下的StackManagement然后选择“IndexMode”,再选择“CreateIndexMode”就可以了选择之前创建的ideaboom。因为之前做过,所以这里显示已经创建了!点击左上角的三个横条,选择“发现”选择索引模式!选择合适的时间范围,然后点击“更新”,我们就可以看到日志了!您也可以在搜索框中搜索指定的关键字。elasticsearch会帮我们分词,然后建立倒排索引