elk收集log日志——logstash配置

继续上面的步骤来,已经用filebeat将log日志写入到了redis,现在需要从redis中取出log日志写入logstash

logstash配置文件目录

/etc/logstash/conf.d

写一个名为nginx_access_log.conf配置文件,内容如下,redis的配置对应的是filebeat写入redis的配置

input {
  redis {
    type => "nginx_access_log" 
    host => "10.x.53.x"//这里对应的是redis的地址,可以是互通的内网地址
    port => "8417"
    data_type => "list"
    key => "nginx_access_log" //这里要对应的redis的key
  }
}

filter {
     json {
       source => "message"
       remove_field => "message"
     }
}

output {
if [type] == "nginx_access_log"{
  elasticsearch {
    hosts => ["http://127.0.0.1:9200"]//这里是将日志输入到es  
    index => "nginx_access_log" //对应的索引
    #index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"
    user => "elastic" //这里是对应的用户名和密码 没有设置的话 这两项可以先注释  如何设置请查看后面文档
    password => "elasticSh51699890"
  }
}
}

重启logstash服务

这时候可以进入redis,可以看到对应的key正在被消费

kibana创建索引规则

进入到kibana中,创建对应的索引规则,并选择时间区分

app/management/kibana/indexPatterns

创建成功之后,在创建的索引里可以看到对应的内容

多个配置文件的问题

/etc/logstash/conf.d,配置目录里可以写多个.conf结尾的配置文件

举个例子,如果还有一个配置文件nginx_access_log2.conf

input {
  redis {
    type => "nginx_access_log2" 
    host => "10.x.53.x"//这里对应的是redis的地址,可以是互通的内网地址
    port => "8417"
    data_type => "list"
    key => "nginx_access_log2" //这里要对应的redis的key
  }
}

filter {
     json {
       source => "message"
       remove_field => "message"
     }
}

output {
if [type] == "nginx_access_log2"{
  elasticsearch {
    hosts => ["http://127.0.0.1:9200"]//这里是将日志输入到es  
    index => "nginx_access_log2" //对应的索引
    #index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"
    user => "elastic" //这里是对应的用户名和密码 没有设置的话 这两项可以先注释  如何设置请查看后面文档
    password => "elasticSh51699890"
  }
}
}

如果没有output的type类型判断,那么在索引nginx_access_log2和nginx_access_log里面会出现两个input的内容,索引这里要加个type来加以区分。

在别处看到的粘贴一下

logstash如何读取多个配置文件

我们知道在启动logstash的时候,只要加上-f /you_path_to_config_file就可以加载配置文件了,如果我们需要加载多个配置文件,只需要-f /you_path_to_config_directory就可以了。简单说,就是在-f后面加上目录就可以。 注意:目录后面不能加 * 号,否则只会读取一个文件,但是在读取日志文件时,可以匹配所有,比如sys.log可以匹配所有以sys.log开头的日志文件,如sys.log1,sys.log2等。

示例如下:

//比如 /home/husen/config/目录下有
//in1.conf、in2.conf、filter1.conf、filter2.conf、out.conf这5个文件

//我们使用 /logstash-5.5.1/bin/logstash -f /home/husen/config启动logtstash
//logstash会自动加载这个5个配置文件,并合并成1个整体的配置文件

logstash多个配置文件里的input、filter、output是否相互独立

比如:

## in1.conf内容如下:
input{
    file{
        path=>[
            "/home/husen/log/sys.log"
        ]
    }
}

## in2.conf内容如下:
input{
    file{
        path=>[
            "/home/husen/log/error.log"
        ]
    }
}

## out1.conf如下
elasticsearch {
        action => "index"          
        hosts  => "localhost:9200" 
        index  => "from_sys_log"
        codec => "json"
    }

## out2.conf如下
elasticsearch {
        action => "index"          
        hosts  => "localhost:9200" 
        index  => "from_error_log"
        codec => "json"
    }
//这几个配置文件的目的是:
//想把in1.conf读进来的sys.log的索引建立为from_sys_log
//把in.conf读进来的error.log的索引建立为femo_error_log

//logstash-5.5.1/bin/logstash -f /home/husen/config

//启动之后,会发现in1.conf的日志被输出了两次,in2.conf读进来的日志也被输出了两次

//结论:logstash读取多个配置文件只是简单的将所有配置文件整合到了一起!
//如果要彼此独立,需要自己加字段,然后判断一下
//比如读取来不同不同服务器的同样格式的日志,那么filter是可以共用的
//但是输出的索引需要分别建立,以提高辨识度

logstash读取多个配置文件建议的配置方法

如果要在配置文件中,独立一些部分,又要共用一些部分,比如我上门提高同样的日志来自不同的服务器,需要用同样的filter,但是建立不同的索引的问题,该怎么办? 建议使用tags或者type这两个特殊字段,即在读取文件的时候,添加标识符在tags中或者定义type变量。

示例如下:

## in1.conf内容如下:
input{
    file{
        path=>[
            "/home/husen/log/sys.log"
        ]
        type => "from_sys"
        #tags => ["from_sys"]
    }
}

## in2.conf内容如下:
input{
    file{
        path=>[
            "/home/husen/log/error.log"
        ]
        type => "from_error"
        #tags => ["from_sys"]
    }
}

## out1.conf如下
if [type] == "from_sys"{
#if "from_sys" in [tags]
    elasticsearch {
        action => "index"          
        hosts  => "localhost:9200" 
        index  => "from_sys_log"
        codec => "json"
    }
}

## out2.conf如下
if [type] == "from_error"{
#if "from_error" in [tags]
    elasticsearch {
        action => "index"          
        hosts  => "localhost:9200" 
        index  => "from_error_log"
        codec => "json"
    }
}

#特别地,如果要针对不同的类型日志用不同filter来grok解析,
#也可以通过类似的方法判断
原文地址:https://www.cnblogs.com/webclz/p/13548224.html