Logstash同步日志文件到Elasticsearch

Logstash

logstash下载

logstash下载地址
在这里插入图片描述

logstash版本与elasticsearch版本一致

多文件形式解决方案

C:\Users\admin\Desktop\a 目录下

1.创建生成日志的测试脚本

createlog.sh

#!/bin/bash
icnt=1;
while (true)
do
  echo '{"id":'${icnt}',"name":"test","loginTime":1620269208}' >> login_log_20210506.log
  echo '{"id":'${icnt}',"name":"test","logoutTime":1620269208}' >> logout_log_20210506.log
  icnt=$((icnt+1));
done

生成日志文件

login_log_20210506.log

{"id":1,"name":"test","loginTime":1620269208}
{"id":2,"name":"test","loginTime":1620269208}
{"id":3,"name":"test","loginTime":1620269208}

logout_log_20210506.log

{"id":1,"name":"test","logoutTime":1620269208}
{"id":2,"name":"test","logoutTime":1620269208}
{"id":3,"name":"test","logoutTime":1620269208}
2.编写logstash配置文件a.conf
# 输入
input {

  # 文件形式输入
  file {
    # 指定输入文件 login*.log为所有日志文件
    path => [ "C:/Users/admin/Desktop/a/login*.log" ]
    # 监听文件的起始位置
    start_position => "beginning"
    type => "loginlog"
    codec => json {
      charset => "UTF-8"
    }   
  }

  # 文件形式输入
  file {
    # 指定输入文件 logout*.log为所有日志文件
    path => [ "C:/Users/admin/Desktop/a/logout*.log" ]
    start_position => "beginning"
    type => "logoutlog"
    codec => json {
      charset => "UTF-8"
    }   
  }

}

filter {
  grok {
    # 截取文件名filename作为临时变量,不会输出到es中
    match => ["path","%{GREEDYDATA}/%{GREEDYDATA:[@metadata][filename]}\.log"]
  }

  # 一行日志为一个message,将message转为json数据
  json {
    source => "message" 
  } 

  # 类型为loginlog
  if[type] == "loginlog"{
    mutate {
      # 默认字段类型为字符串,转换字段类型
      convert => { "id" => "integer" }
      convert => { "loginTime" => "integer" }
    }
  }

  # 类型为bulletlog
  if[type] == "logoutlog"{
    mutate {
      # 默认字段类型为字符串,转换字段类型
      convert => { "id" => "integer" }
      convert => { "logoutTime" => "integer" }
    }
  }


  mutate {
    # 移除指定字段,不让其输出到es
    remove_field => ["@version"]
    remove_field => ["@timestamp"]
    remove_field => ["host"]
    remove_field => ["path"]
  }
}

# 输出到es
output {
  elasticsearch {
    # 指定es
    hosts => "127.0.0.1:9200"
    # 将filename作为索引名称
    index => "%{[@metadata][filename]}"
  }
  stdout { codec => json_lines }
}
3.运行logstash 并指定使用a.conf配置文件
logstash.bat -f a.conf
4.连接elasticsearch查看生成的索引信息
http://localhost:9200/login_log_20210506/_search
http://localhost:9200/logout_log_20210506/_search
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值