一、需求

使用logstash收集系统上的日志,并使用 grok解析日志,使用mutate修改解析出来的字段类型、删除字段、重命名字段,最后将解析好的日主输出到 elasticsearch中。

二、实现步骤

1、编写pipeline文件

vim output-es.yml

input {
file {
id => "mutate-id"
path => ["/Users/huan/soft/elastic-stack/logstash/logstash/pipeline.conf/output-es/*.log"]
start_position => "beginning"
sincedb_path => "/Users/huan/soft/elastic-stack/logstash/logstash/pipeline.conf/output-es/sincedb.db"
codec => multiline {
pattern => "^\[+"
negate => "true"
what => "previous"
charset => "UTF-8"
auto_flush_interval => 2
}
}
} filter {
grok {
match => {
"message" => "(?m)^\[%{INT:pid}\]%{SPACE}%{TIMESTAMP_ISO8601:createTime}%{SPACE}\[%{DATA:threadName}\]%{SPACE}%{LOGLEVEL:LEVEL}%{SPACE}%{JAVACLASS:javaClass}#(?<methodName>[a-zA-Z_]+):%{INT:linenumber}%{SPACE}-%{GREEDYDATA:msg}"
remove_field => ["message"]
}
} mutate {
convert => {
"pid" => "integer"
} rename => {
"msg" => "message"
}
} # 格式化 createTime 将 源格式 转换成 目标格式
date {
match => ["createTime","yyyy-MM-dd HH:mm:ss.SSS","yyyy-MM-dd HH:mm:ss.SSS"]
target => "@timestamp"
remove_field => ["createTime"]
}
} output {
# 可以通过 template 或 template_name 指定es模板的名字
elasticsearch {
hosts => ["http://localhost:9200","http://localhost:9201","http://localhost:9202"]
user => "springboot_logstash"
password => "123456"
index => "springboot-%{+YYYY.MM.dd}"
template_overwrite => "false"
}
}

1、elasticsearch配置参数解析:

  1. hosts: es的访问地址,建议使用非master节点。
  2. user: 访问es的用户名。
  3. password:访问es的密码。
  4. index:在es中的索引名称。
  5. template:设置自己的es模板路径。
  6. template_name:使用es中的索引模板名称。
  7. 上方的es的密码是明文的,可能存在泄漏,可以使用 logstash keystore来解决。
    1. 参考链接 https://www.elastic.co/guide/en/logstash/current/keystore.html

2、可能会报的一个异常

{
"error": {
"root_cause": [
{
"type": "security_exception",
"reason": "action [indices:data/ write/bulk] is unauthorized for user [logstash_system] on indices [], this action is granted by the index privileges [create_doc,create,delete,index,write,all]"
}
],
"type": "secu rity_exception",
"reason": "action [indices:data/write/bulk] is unauthorized for user [logstash_system] on indices [], this action is granted by the index privileges [create_doc ,create,delete,index,write,all]"
},
"status": 403
}

当我们使用系统自带的logstash_system用户时,可能会报indices:data/write/bulk这个操作没有权限,解决方法如下(自己新建一个用户和角色)。

2、准备测试数据

[9708] 2021-05-13 11:14:51.873 [http-nio-8080-exec-1] INFO  org.springframework.web.servlet.DispatcherServlet#initServletBean:547 -Completed initialization in 1 ms
[9708] 2021-05-13 11:14:51.910 [http-nio-8080-exec-1] ERROR com.huan.study.LogController#showLog:32 -请求:[/showLog]发生了异常
java.lang.ArithmeticException: / by zero
at com.huan.study.LogController.showLog(LogController.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

3、启动logstash

bin/logstash -f output-es.yml

4、在es上创建索引模式

5、进行日志搜索

三、参考文档

1、https://www.elastic.co/guide/en/logstash/current/keystore.html

2、https://www.elastic.co/guide/en/logstash/current/plugins-outputs-elasticsearch.html

logstash收集的日志输出到elasticsearch中的更多相关文章

  1. logstash收集rsyslog日志

    (1)rsyslog配置 在192.168.1.31配置 #vim /etc/rsyslog.conf *.* @@192.168.1.32:514 //所有设备名,所有日志级别都发送到192.168 ...

  2. logstash收集springboot日志

    logstash收集springboot日志 maven依赖 <dependency> <groupId>net.logstash.logback</groupId> ...

  3. 使用 Elastic Agents 把定制的日志摄入到 Elasticsearch 中

    转载自:https://mp.weixin.qq.com/s/QQxwYh1uLCkKn1LK72ojJA 在以前的系统中,我们可以使用如下的几种方式来采集日志: 1.我们可以直接使用 Beats 把 ...

  4. logstash收集syslog日志

    logstash收集syslog日志注意:生产用syslog收集日志!!! 编写logstash配置文件 #首先我用rubydebug测试数据 [root@elk-node1 conf.d]# cat ...

  5. ELK-logstash案例实战之读取日志输出到elasticsearch

    简介:从日志文件中读取日志,输出到elasticsearch集群中 $ cd /home/es/logstash-/config $ vim test3_es.conf $ cd /home/es/l ...

  6. 使用Logstash把MySQL数据导入到Elasticsearch中

    总结:这种适合把已有的MySQL数据导入到Elasticsearch中 有一个csv文件,把里面的数据通过Navicat Premium 软件导入到数据表中,共有998条数据 文件下载地址:https ...

  7. logstash 6.6.0 读取nginx日志 插入到elasticsearch中

    logstash.conf input { # For detail config for log4j as input, # See: https://www.elastic.co/guide/en ...

  8. 构建Logstash+tomcat镜像(让logstash收集tomcat日志)

    1.首先pull logstash镜像作为父镜像(logstash的Dockerfile在最下面): 2.构建my-logstash镜像,使其在docker镜像实例化时,可以使用自定义的logstas ...

  9. Logstash收集nginx日志之使用grok过滤插件解析日志

    grok作为一个logstash的过滤插件,支持根据模式解析文本日志行,拆成字段. nginx日志的配置: log_format main '$remote_addr - $remote_user [ ...

随机推荐

  1. Java数学函数的使用

    Java的Math类中提供了一系列关于数学运算的静态方法,常见的运算整理如下[1] 算数运算 Math.sqrt() // 平方根 Math.cbrt() // 立方根 Math.pow(a, b) ...

  2. POJ1426——Find The Multiple

    POJ1426--Find The Multiple Description Given a positive integer n, write a program to find out a non ...

  3. 安卓使用讯飞sdk报错

    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.iflytek.cloud.SpeechSy ...

  4. python函数模块

    python函数模块 学习完本篇,你将会深入掌握 函数相关知识 1.函数定义 2.函数如何调用 3.递归函数以及匿名函数使用 模块相关知识 1.模块如何导入调用 2.函数调用顺序 函数定义 函数代码块 ...

  5. JDBC-1(概述&建立)

    基于宋红康老师所讲JDBC所作笔记 1.JDBC概述 1.1 数据持久化 持久化:将数据保持到可掉电式存储设备中以供之后使用. 数据持久化意味着将内存中的数据保存到硬盘上加以固化,实现过程大多通过各种 ...

  6. 微信小程序 创建自己的第一个小程序

    * 成为微信公众平台的开发者 注册 https://mp.weixin.qq.com * 登录 https://open.weixin.qq.com/ * 开发者工具下载 https://develo ...

  7. shell 脚本获取数组字符串长度

    #!/bin/sh source /etc/init.d/functions funOne() { array=(I am dfh kjlhfjksdf sdfj jdkfhaskl mjjoldfu ...

  8. P4859-已经没有什么好害怕的了【容斥,dp】

    正题 题目链接:https://www.luogu.com.cn/problem/P4859 题目大意 两个长度为\(n\)的序列\(a,b\)两两匹配,求\(a_i>b_i\)的组数比\(a_ ...

  9. NOIP 模拟七 考试总结

    T1匹配 签到大水题,这里有hash,kmp,ac自动机,还有后缀数组,后缀自动机任您挑选. 不过这个数据范围有些坑啊,re就很不爽.做法我还是比较倾向hash的,毕竟不论神魔字符算法,hash大都能 ...

  10. HTML对溢出的控制:overflow

    用于对超出模块的内容进行控制 overflow的三种参数 1.visible(默认):溢出部分可见 2.hidden:溢出部分隐藏 3.auto:适当加入滚动条 overflow使用方法 1.常用 o ...