zoukankan      html  css  js  c++  java
  • elk系列8之logstash+redis+es的架构来收集apache的日志

    preface

    logstash--> redis --> logstash --> es这套架构在讲究松耦合关系里面是最简单的,
    架构图如下:
    image

    解释下这个架构图的流程

    1. 首先前端logstash读取apache的日志(input读取)。然后放到redis的db里面(output存入)。存储形式为列表形式存放
    2. 后端logstash从redis读取日志内容(input读取),从前端logstash指定的库里面读取指定的key。读取之后filter(filter-grok)过滤。然后传送到es上(output推送)。
    3. es接受日志,处理。

    我们可以参考官网的建议:https://www.elastic.co/guide/en/logstash/2.3/deploying-and-scaling.html#deploying-message-queueing

    废话不多说,我们开始着手配置吧,。

    安装redis

    在linux-node2节点上操作
    安装redis,并且启动,

    [root@linux-node2 conf.d]# yum -y install redis    # 确保一定是2.4版本以上的,不然logstash input的时候会报错的
    [root@linux-node2 conf.d]# grep bind /etc/redis.conf
    bind 0.0.0.0   # 这里记得修改监听地址
    

    确认无误后启动redis

    [root@linux-node2 conf.d]# /etc/init.d/redis start
    

    配置logstash

    linux-node1操作
    logstash对应的模块是output里面的redis模块,当然,也可以支持rabbitMQ,选择redis是因为redis性能强,而且配置简单。那么为啥要在output上做呢,是因为output是logstash的输出,直接对接在redis上,所以是ouput。
    下面就看看官网对redis模块的讲解:https://www.elastic.co/guide/en/logstash/2.3/plugins-outputs-redis.html

    下面贴下logstash配置,这里把屏幕输入传送到redis上

    [root@linux-node1 conf.d]# cat redis.conf
    input {
        stdin {
        }
    }
    output {
        redis {
            host => "192.168.141.4"
            port => "6379"
            db => "6"      # 选择那个库
            data_type => "list"   # 存入数据的类型
            key => "demo"    # 数据的键
        }
    }
    

    随便回车敲入一些内容,使其redis库里面有东西

    查看redis结果

    我们到登陆redis后查看内容:

    redis 127.0.0.1:6379> select 6   # 切换到数据库6下面
    OK
    redis 127.0.0.1:6379[6]> keys *
    1) "demo"
    redis 127.0.0.1:6379[6]> type demo
    list
    redis 127.0.0.1:6379[6]> llen demo   # 查看列表长度
    (integer) 3  
    redis 127.0.0.1:6379[6]> lindex demo -1   # 从redis的左边取第一位
    "{"message":"man","@version":"1","@timestamp":"2016-12-11T07:18:44.751Z","host":"linux-node1"}"
    
    

    到此,可以说明logstash的output和redis已经能够正常的工作了

    收集apache的日志

    linux-node1操作
    我们此时更改下logstash的配置,配置如下:

    [root@linux-node1 conf.d]# cat /etc/logstash/conf.d/redis.conf
    input {
      file {
        path => "/var/log/httpd/access_log"
        start_position => "beginning"
      }
    }
    
    
    output {
        redis {
            host => "192.168.141.4"
            port => "6379"
            db => "6"
            data_type => "list"
            key => "apache"
        }
    }
    

    配置确认无误后,启动logstash

    [root@linux-node1 conf.d]# /opt/logstash/bin/logstash -f redis.conf
    

    此时切换到linux-node2的终端上查看

    redis 127.0.0.1:6379[6]> keys *
    1) "demo"
    2) "apache"      # 出现这个key了
    redis 127.0.0.1:6379[6]> llen apache
    (integer) 2002
    redis 127.0.0.1:6379[6]> lindex apache 0
    "{"message":"192.168.141.4 - - [11/Dec/2016:15:54:09 +0800] \"GET / HTTP/1.0\" 403 4961 \"-\" \"ApacheBench/2.3\"","@version":"1","@timestamp":"2016-12-11T07:54:09.745Z","path":"/var/log/httpd/access_log","host":"linux-node1"}"   # 有内容了
    

    下面我们在linux-node2上配置logstash,来读取redis的内容

    [root@linux-node2 conf.d]# cat getredis.conf
    input {
        redis {
            host => "192.168.141.4"
            db => "6"
            data_type => "list"
            key => "apache"
        }
    }
    
    output {
        stdout{
            codec => rubydebug
        }
    }
    
    

    确认没有问题,启动logstash

    [root@linux-node2 conf.d]# /opt/logstash/bin/logstash --verbose -f getredis.conf
    

    我擦,报错了,报错内容如下,该报错内容一直刷屏:

    Redis connection problem {:exception=>#<Redis::CommandError: ERR unknown command 'script'>, :level=>:warn}
    

    出现上面这个报错的问题是因为redis版本太低,yum安装的redis版本是2.4.10的,然后我自己源码包安装的是3.2.5,步骤如下:

    [root@linux-node2 tmp]# wget http://download.redis.io/releases/redis-3.2.5.tar.gz
    [root@linux-node2 tmp]# tar xzf redis-3.2.5.tar.gz
    [root@linux-node2 tmp]# cd redis-3.2.5
    [root@linux-node2 redis-3.2.5]# make
    [root@linux-node2 redis-3.2.5]# src/redis-server /etc/redis.conf  #配置文件稍作更改即可,该监听地址
    

    再次启动logstash,就可以了,完美启动

    [root@linux-node2 conf.d]# /opt/logstash/bin/logstash --verbose -f getredis.conf
    

    此时我们可以停止刚才启动的logstash,重新配置一下。

    使用grok模块处理apache日志

    我们再次配置下linuix-node2节点上的logstash。先找到分析apache日志的模块,然后添加filter-grok。如下所示:

    首先过滤出分析apache日志的模块,方便待会调用
    [root@linux-node2 conf.d]# grep APACHE    /opt/logstash/vendor/bundle/jruby/1.9/gems/logstash-patterns-core-2.0.5/patterns/grok-patterns --color     # 首先过滤出分析apache日志的模块,方便待会调用
    COMMONAPACHELOG %{IPORHOST:clientip} %{HTTPDUSER:ident} %{USER:auth} [%{HTTPDATE:timestamp}] "(?:%{WORD:verb} %{NOTSPACE:request}(?: HTTP/%{NUMBER:httpversion})?|%{DATA:rawrequest})" %{NUMBER:response} (?:%{NUMBER:bytes}|-)
    COMBINEDAPACHELOG %{COMMONAPACHELOG} %{QS:referrer} %{QS:agent}
    
    配置logstash

    添加filter-grok。

    [root@linux-node2 conf.d]# cat getredis.conf
    input {
        redis {
            host => "192.168.141.4"
            db => "6"
            data_type => "list"
            key => "apache"
        }
    }
    
    filter {
        grok {
            match => { "message" => "%{COMBINEDAPACHELOG}"}
        }
    }
    
    output {
        elasticsearch {
            hosts => ["192.168.141.3:9200"]
            index => "apache-log-%{+YYYY.MM}"
        }
    }
    
    

    确认无误后,再次启动logstash

    [root@linux-node2 conf.d]# /opt/logstash/bin/logstash --verbose -f getredis.conf
    

    访问下apache,然后我们在head上就可以看到apapche的日志。

  • 相关阅读:
    判断是否IPv6网络
    Makefile使用库
    Makefile编译
    Makefile编译库
    lua定义一个简单的类
    c++ 注册类到 lua
    redis的安装
    yield return的作用
    使用boost的asio,io_service无法初始化
    apache+php+mysql的配置(转载)
  • 原文地址:https://www.cnblogs.com/liaojiafa/p/6160215.html
Copyright © 2011-2022 走看看