前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >ElasticSearch实战:Linux日志对接Kibana

ElasticSearch实战:Linux日志对接Kibana

作者头像
java架构师
发布于 2019-02-27 08:37:29
发布于 2019-02-27 08:37:29
7320
举报
文章被收录于专栏:Java架构师进阶Java架构师进阶

ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTFul web接口。ElasticSearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。ElasticSearch常用于全文检索,结构化检索,数据分析等。

如果想学习Java工程化、高性能及分布式、深入浅出。微服务、Spring,MyBatis,Netty源码分析的朋友可以加我的Java高级交流:854630135,群里有阿里大牛直播讲解技术,以及Java大型互联网技术的视频免费分享给大家。

下面,我们以ElasticSearch接管Linux日志(/var/log/xxx.log)为例,详细介绍如何进行配置与部署。

总体架构图

一,准备工作

1,CVM及ElasticSearch

在腾讯云帐号下,申请一台CVM(Linux操作系统)、一个ElasticSearch集群(后面简称ES),使用最简配置即可;申请的CVM和ES,必须在同一个VPC的同一个子网下。

CVM详情信息

ElasticSearch详情信息

2,Filebeat工具

为了将Linux日志提取到ES中,我们需要使用Filebeat工具。Filebeat是一个日志文件托运工具,在你的服务器上安装客户端后,Filebeat会监控日志目录或者指定的日志文件,追踪读取这些文件(追踪文件的变化,不停的读),并且转发这些信息到ElasticSearch或者logstarsh中存放。当你开启Filebeat程序的时候,它会启动一个或多个探测器(prospectors)去检测你指定的日志目录或文件,对于探测器找出的每一个日志文件,Filebeat启动收割进程(harvester),每一个收割进程读取一个日志文件的新内容,并发送这些新的日志数据到处理程序(spooler),处理程序会集合这些事件,最后Filebeat会发送集合的数据到你指定的地点。

官网简介:https://www.elastic.co/products/beats/filebeat

二,操作步骤

1,Filebeat下载与安装

首先,登录待接管日志的CVM,在CVM上下载Filebeat工具:

[root@VM_3_7_centos ~]#cd/opt/[root@VM_3_7_centos opt]#lltotal4drwxr-xr-x.2rootroot4096Sep72017rh[root@VM_3_7_centos opt]#wgethttps://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.2.2-x86_64.rpm--2018-12-1020:24:26--https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.2.2-x86_64.rpmResolvingartifacts.elastic.co(artifacts.elastic.co)...107.21.202.15,107.21.127.184,54.225.214.74, ...Connectingtoartifacts.elastic.co(artifacts.elastic.co)|107.21.202.15|:443...connected.HTTPrequestsent,awaitingresponse...200OKLength:12697788(12M)[binary/octet-stream]Savingto: ‘filebeat-6.2.2-x86_64.rpm’100%[=================================================================================================>]12,697,788160KB/sin1m41s2018-12-1020:26:08(123KB/s)-‘filebeat-6.2.2-x86_64.rpm’saved[12697788/12697788]

然后,进行安装filebeat:

[root@VM_3_7_centosopt]# rpm -vi filebeat-6.2.2-x86_64.rpmwarning: filebeat-6.2.2-x86_64.rpm: Header V4RSA/SHA512Signature, key ID d88e42b4: NOKEYPreparing packages...filebeat-6.2.2-1.x86_64[root@VM_3_7_centosopt]#

至此,Filebeat安装完成。

2,Filebeat配置

如果想学习Java工程化、高性能及分布式、深入浅出。微服务、Spring,MyBatis,Netty源码分析的朋友可以加我的Java高级交流:854630135,群里有阿里大牛直播讲解技术,以及Java大型互联网技术的视频免费分享给大家。

进入Filebeat配置文件目录:/etc/filebeat/

[root@VM_3_7_centos opt]# cd /etc/filebeat/[root@VM_3_7_centos filebeat]# lltotal 108-rw-r--r-- 1 root root 44384 Feb 17 2018 fields.yml-rw-r----- 1 root root 52193 Feb 17 2018 filebeat.reference.yml-rw------- 1 root root 7264 Feb 17 2018 filebeat.ymldrwxr-xr-x 2 root root 4096 Dec 10 20:35 modules.d[root@VM_3_7_centos filebeat]#

其中,filebeat.yml就是我们需要修改的配置文件。建议修改配置前,先备份此文件。

然后,确认需要对接ElasticSearch的Linux的日志目录,我们以下图(/var/log/secure)为例。

/var/log/secure日志文件

使用vim打开/etc/filebeat/filebeat.yml文件,修改其中的:

1)Filebeat prospectors类目中,enable默认为false,我们要改为true

2)paths,默认为/var/log/*.log,我们要改为待接管的日志路径:/var/log/secure

3)Outputs类目中,有ElasticSearchoutput配置,其中hosts默认为"localhost:9200",需要我们手工修改为上面申请的ES子网地址和端口,即"10.0.3.8:9200"

修改好上述内容后,保存退出。

修改好的配置文件全文如下:

[root@VM_3_7_centos /]# vim /etc/filebeat/filebeat.yml[root@VM_3_7_centos /]# cat /etc/filebeat/filebeat.yml###################### Filebeat Configuration Example ########################## This file is an example configuration file highlighting only the most common# options. The filebeat.reference.yml file from the same directory contains all the# supported options with more comments. You can use it as a reference.## You can find the full configuration reference here:# https://www.elastic.co/guide/en/beats/filebeat/index.html# For more available modules and options, please see the filebeat.reference.yml sample# configuration file.#=========================== Filebeat prospectors =============================filebeat.prospectors:#Each - is a prospector. Most options can besetat the prospector level, so#you can use different prospectorsforvarious configurations.# Below are the prospector specific configurations.- type: log #Change totruetoenablethis prospector configuration.enabled: true # Paths that should be crawled and fetched. Glob based paths.paths: - /var/log/secure #- c:\programdata\elasticsearch\logs\* # Exclude lines. A list of regular expressions to match. It drops the lines that are # matching any regular expression from the list. #exclude_lines: ['^DBG'] # Include lines. A list of regular expressions to match. It exports the lines that are # matching any regular expression from the list. #include_lines: ['^ERR','^WARN'] # Exclude files. A list of regular expressions to match. Filebeat drops the files that # are matching any regular expression from the list. By default, no files are dropped. #exclude_files: ['.gz$'] # Optional additional fields. These fields can be freely picked #to add additional information to the crawledlogfilesforfiltering #fields: # level: debug # review: 1 ### Multiline options #Mutiline can be usedforlogmessages spanning multiple lines. This is common #forJava Stack Traces or C-Line Continuation # The regexp Pattern that has to be matched. The example pattern matches all lines starting with [ #multiline.pattern: ^\[ #Definesifthe patternsetunder pattern should be negated or not. Default isfalse. #multiline.negate:false #Match can besetto"after"or"before". It is used to defineiflines should be append to a pattern # that was (not) matched before or after or as long as a pattern is not matched based on negate. #Note: After is the equivalent to previous and before is the equivalent to to nextinLogstash #multiline.match: after#============================= Filebeat modules ===============================filebeat.config.modules: #Glob patternforconfiguration loadingpath: ${path.config}/modules.d/*.yml #Set totruetoenableconfig reloadingreload.enabled: false #Period onwhichfiles under path should be checkedforchanges #reload.period: 10s#==================== Elasticsearch template setting ==========================setup.template.settings: index.number_of_shards: 3 #index.codec: best_compression #_source.enabled:false#================================ General =====================================# The name of the shipper that publishes the network data. It can be used to group#all the transactions sent by a single shipperinthe web interface.#name:#The tags of the shipper are includedintheir own field with each# transaction published.#tags: ["service-X","web-tier"]# Optional fields that you can specify to add additional information to the# output.#fields:# env: staging#============================== Dashboards =====================================# These settings control loading the sample dashboards to the Kibana index. Loading# the dashboards is disabled by default and can be enabled either by setting the#options here, or by using the `-setup` CLI flag or the `setup`command.#setup.dashboards.enabled:false#The URL fromwhereto download the dashboards archive. By default this URL#has a valuewhichis computed based on the Beat name and version. For released# versions, this URL points to the dashboard archive on the artifacts.elastic.co# website.#setup.dashboards.url:#============================== Kibana =====================================# Starting with Beats version 6.0.0, the dashboards are loaded via the Kibana API.# This requires a Kibana endpoint configuration.setup.kibana: # Kibana Host #Scheme and port can be left out and will besetto the default (http and 5601) #Incaseyou specify and additional path, the scheme is required: http://localhost:5601/path # IPv6 addresses should always be defined as: https://[2001:db8::1]:5601 #host:"localhost:5601"#============================= Elastic Cloud ==================================# These settings simplify using filebeat with the Elastic Cloud (https://cloud.elastic.co/).# The cloud.id setting overwrites the `output.elasticsearch.hosts` and# `setup.kibana.host` options.#You can find the `cloud.id`inthe Elastic Cloud web UI.#cloud.id:# The cloud.auth setting overwrites the `output.elasticsearch.username` and# `output.elasticsearch.password` settings. The format is `<user>:<pass>`.#cloud.auth:#================================ Outputs =====================================# Configure what output to use when sending the data collected by the beat.#-------------------------- Elasticsearch output ------------------------------output.elasticsearch: # Array of hosts to connect to.hosts: ["10.0.3.8:9200"] # Optional protocol and basic auth credentials. #protocol:"https" #username:"elastic" #password:"changeme"#----------------------------- Logstash output --------------------------------#output.logstash: # The Logstash hosts #hosts: ["localhost:5044"] # Optional SSL. By default is off. #List of root certificatesforHTTPS server verifications #ssl.certificate_authorities: ["/etc/pki/root/ca.pem"] #CertificateforSSL client authentication #ssl.certificate:"/etc/pki/client/cert.pem" # Client Certificate Key #ssl.key:"/etc/pki/client/cert.key"#================================ Logging =====================================#Setsloglevel. The defaultloglevel is info.#Availableloglevels are: error, warning, info, debug#logging.level: debug#At debug level, you can selectivelyenablelogging onlyforsome components.#Toenableall selectors use ["*"]. Examples of other selectors are"beat",#"publish","service".#logging.selectors: ["*"]#============================== Xpack Monitoring ===============================#filebeat canexportinternal metrics to a central Elasticsearch monitoring#cluster. This requires xpack monitoring to be enabledinElasticsearch. The# reporting is disabled by default.#Set totruetoenablethe monitoring reporter.#xpack.monitoring.enabled:false# Uncomment to send the metrics to Elasticsearch. Most settings from the#Elasticsearch output are accepted here as well. Any setting that is notsetis#automatically inherited from the Elasticsearch output configuration, soifyou# have the Elasticsearch output configured, you can simply uncomment the# following line.#xpack.monitoring.elasticsearch:[root@VM_3_7_centos /]#

执行下列命令启动filebeat

[root@VM_3_7_centos/]# sudo /etc/init.d/filebeat startStarting filebeat (via systemctl): [ OK ][root@VM_3_7_centos/]#

3,Kibana配置

如果想学习Java工程化、高性能及分布式、深入浅出。微服务、Spring,MyBatis,Netty源码分析的朋友可以加我的Java高级交流:854630135,群里有阿里大牛直播讲解技术,以及Java大型互联网技术的视频免费分享给大家。

进入ElasticSearch对应的Kibana管理页,如下图。

首次访问Kibana默认会显示管理页

首次登陆,会默认进入Management页面,我们需要将Index pattern内容修改为:filebeat-*,然后页面会自动填充Time Filter field name,不需手动设置,直接点击Create即可。点击Create后,页面需要一定时间来加载配置和数据,请稍等。如下图:

将Index pattern内容修改为:filebeat-*,然后点击Create

至此,CVM上,/var/log/secure日志文件,已对接到ElasticSearch中,历史日志可以通过Kibana进行查询,最新产生的日志也会实时同步到Kibana中。

三,实战效果

日志接管已完成配置,如何使用呢?

如下图:

在Index Patterns中可以看到我们配置过的filebeat-*

点击Discover,即可看到secure中的所有日志,页面上方的搜索框中输入关键字,即可完成日志的检索。如下图(点击图片,可查看高清大图):

使用Kibana进行日志检索

如果想学习Java工程化、高性能及分布式、深入浅出。微服务、Spring,MyBatis,Netty源码分析的朋友可以加我的Java高级交流:854630135,群里有阿里大牛直播讲解技术,以及Java大型互联网技术的视频免费分享给大家。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.01.28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
python SMTP邮件发送
本例使用的时python2.7环境,python3的操作应该也是差不多的。 需要用到smtplib和email两个包。
py3study
2020/01/16
1.8K0
python邮件自动发送测试报告,看这篇文章就够了
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/100698.html原文链接:https://javaforall.cn
全栈程序员站长
2022/06/29
4900
python邮件自动发送测试报告,看这篇文章就够了
Python3实现自动发送邮件
首先了解SMTP(简单邮件传输协议),邮件传送代理程序使用SMTP协议来发送电邮到接收者的邮件服务器。SMTP协议只能用来发送邮件,不能用来接收邮件,而大多数的邮件发送服务器都是使用SMTP协议。SMTP协议的默认TCP端口号是25。
用户9925864
2022/07/27
3310
Python3实现自动发送邮件
Python发送邮件
Python发邮件需要有SMTP服务,可以在本地搭建SMTP服务,也可以使用第三方的SMTP服务(比如网易邮箱或QQ邮箱)。 这里我使用了网易126邮箱来发邮件。
海天一树
2018/07/25
1.5K0
Python发送邮件
用python发送邮件
SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件、HTML邮件以及带附件的邮件。Python对SMTP支持有smtplib和email两个模块,email负责构造邮件,smtplib负责发送邮件。
用户6021899
2021/03/11
3230
用python发送邮件
【测试开发】python系列教程:smtplib库
SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。
雷子
2023/08/21
2810
【测试开发】python系列教程:smtplib库
简单三步,用 Python 发邮件
0. 前言 发送电子邮件是个很常见的开发需求。比如你写了个监控天气的脚本,发现第二天要下雨,或者网站上关注的某个商品降价了,就可以发个邮件到邮箱来提醒自己。 使用 Python 脚本发送邮件并不复杂。不过在网页上搜了些教程,都亲测无效,发现或多或少有点问题,导致发送失败。所以今天我们就来讲讲如何使用 Python 发送邮件。 本文主要内容包括,了解发邮件的思路,发送邮件需要的一些设置,发送一封简单的邮件,发送一封包含附件的邮件,在写代码过程中我们需要注意哪些问题等等。(完整参考代码地址见文末) 1. 思
Crossin先生
2018/04/17
1.2K0
简单三步,用 Python 发邮件
python 使用stmp发送邮件
SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。
py3study
2020/01/06
1.1K0
Python发送文本邮件
在运行机器学习等需要大量计算的程序时,可以在报错或者程序运行完成时,发送邮件提醒。
Michael阿明
2021/02/19
5670
Python发送邮件的例子
import smtplib from email.mime.text import MIMEText from email.header import Header # 第三方 SMTP 服务 mail_host="smtp.qq.com" #设置服务器 mail_user="12121212@qq.com" #用户名 mail_pass="1223333" #口令 sender = '121212@qq.com' receivers = ['xi121@qq.com'] # 接收邮件
py3study
2020/01/15
4800
python发送邮件源码
这是之前一个公司的面试题中的一小部分。比较习惯把这中工具性的东西单独的写成一个函数,这样以后再用到,直接拿来就好。
the5fire
2019/02/28
6840
python发送、抄送邮件
python发送抄送邮件 sendemial.py #!/usr/bin/python # -*- coding: UTF-8 -*- import smtplib from email.mime.text import MIMEText from email.header import Header from email.mime.multipart import MIMEMultipart import time import os mail_host="smtp.miao.cn" mail_u
py3study
2020/01/08
1K0
python3 SMTP发送邮件
最近看了一个资料关于用python来实现SMTP发送邮件,觉得挺有意思就实践了一下。先简单介绍一下吧
行云博客
2022/05/10
6250
python发送邮件
一.获取邮箱授权(以QQ邮箱为例子) 点击设置>账号 开启服务:POP3/SMTP服务 选择开启 然后获取授权码xxx 二.发送文本 import smtplib from email.mime.text import MIMEText subject = "标题" # 邮件的主题 content = '测试' #内容 sender = "568972484@qq.com" # 发件人 password = 'xxx' # 刚才我们在QQ邮箱里设置的授权密码 receiver = "5689
小小咸鱼YwY
2020/06/19
5360
【Python实用工具】巧妙利用Python实现发送带附件的邮件
SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。
天道Vax的时间宝藏
2021/08/11
2.5K0
接口测试基础——第2篇smtplib发送文字邮件
王者荣耀真害人!这就是昨天没发的原因…… 我先给大家补充一个用QQ发送纯文本电子邮件的代码,用QQ的朋友可以参考一下: # coding=utf-8 import smtplib from email.mime.text import MIMEText mail_host = “smtp.qq.com” receivers = “123@qq.com” sender = “456@qq.com” passwd = ‘QQ邮箱的授权码’ contents = “python发送邮件” # 构造邮件正文 ms
孟船长
2018/05/18
7450
Python 发邮件
普通邮件 [root@localhost checksalt]# cat python_email.py  #!/usr/bin/python # -*- coding: utf-8 -*- import sys def smtp(title,file):     import smtplib     from email.mime.text import MIMEText     from email.header import Header           with open(file, 'r') 
py3study
2020/01/15
1.7K0
使用Python调用SMTP服务自动发送Email
假设我们想设计一个定时任务,比如每天定时的用python来测试服务是否在正常运行,但是又不希望每天登录到系统后台去查看服务状态。这里我们就可以采取python的smtp模块进行任务结果广播,申请一个公共邮箱,每次python执行完定时的测试任务后,调用smtp的接口将测试结果广播给需要接收的人的邮箱中。这就使得,我们可以在移动端就能按照我们的意愿实时监测系统的状态。
DechinPhy
2021/05/21
9010
python3实现邮件的发送
使用的email和smtplib模块,这里简单介绍下smtplib.SMTP()类
dogfei
2020/07/31
3770
[Python] Python发送邮件(文本邮件发送)
直接上代码吧 然后采用者直接写好自己的信息就可以运行啦 import smtplib from email.mime.text import MIMEText from email.header import Header mail_host = "smtp.qq.com" #邮箱服务器(这里采用的是QQ的) mail_user = "******@qq.com" #发送者的邮箱地址 mail_pass
Michael阿明
2021/02/19
2.5K0
相关推荐
python SMTP邮件发送
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档