zoukankan      html  css  js  c++  java
  • logstash 收集日志到redis

    一、Logstash与Redis那点事

    在企业中,日志规模的量级远远超出我们的想象,这就是为什么会有一家公司 日志易 专门做日志收集,给大型金融公司收集日志,比如银行,因为你有可能看到,1秒钟好几千万的日志量,往服务器写入,那么企业中的集群,架构都不是单台的,而是多台的,一台如果是1千万,那么5台的量级,10台的量级,我们要对他们进行收集,进行分析,难免会在网络传输过程中,丢数据。

    日志是什么?
    日志对于企业来说,有什么作用?
    用户使用我们的产品,体验如何?
    用户的客诉,我们能拿出什么样的数据来说话?
    ...

    一系列的问题,都和日志相关,如果至关重要的那个数据丢失了,那么公司的损失可不仅仅是一条日志那么简单。如果我们不知道,用户对我们产品最感兴趣的地方在哪,那么产品的寿命也就越来越短。如果被攻击了,恶意攻击的IP源我们都找不到,那么或许就不是产品的寿命越来越短,而是这个企业存在的寿命,越来越短。

    好吧,一顿排比句,说的那么浮夸,说白了,我就是想要告诉你们,一个大规模日志量级的企业想要做到数据的安全性,数据的一致性,我们需要消息队列:Redis , Kafka,在ELK5版本中,建议使用Redis来做消息队列,Kafka能不能用?也能,只不过会有一些不必要的坑,需要我们去爬。在ELK6版本中,开始使用Kafka来做消息队列。

    话不多说,我们接下来就开始将Logstash收集到的日志,输出到Redis中。

    二,配置logstash收集单个日志到redis

    0.环境准备
    主机 IP 服务
    web01 10.0.0.7 nginx,tomcat,logstash
    redis01 10.0.0.81 redis
    redis02 10.0.0.82 logstash
    es01 10.0.0.91 ES,kibana
    1.收集日志到redis
    1)安装redis
    [root@redis01 ~]# yum install -y redis
    
    2)配置redis
    [root@redis01 ~]# vim /etc/redis.conf
    bind 172.16.1.81 127.0.0.1
    
    3)启动redis
    [root@redis01 ~]# systemctl start redis
    
    4)配置logstash收集日志写入redis
    [root@web01 ~]# vim /etc/logstash/conf.d/file_redis.conf
    input {
      file {
        path => "/var/log/nginx/access.log"
        start_positon => "end"
        codec => "json"
      }
    }
    
    output {
      redis {
        host => "172.16.1.81"
        port => "6379"
        key => "nginx_log"
        data_type => "list"
      }
    }
    

    2.将redis数据取出写入ES

    1)安装Java环境
    [root@redis02 ~]# yum localinstall -y jdk-8u181-linux-x64.rpm
    
    2)安装logstash
    [root@redis02 ~]# yum localinstall -y logstash-6.6.0.rpm
    
    3)配置logstash取出redis数据写入ES
    [root@redis02 ~]# vim /etc/logstash/conf.d/redis_es.conf
    input {
      redis {
        host => "172.16.1.81"
        port => "6379"
        data_type => "list"
        key => "nginx_log"
      }
    }
    
    output {
      elasticsearch {
        hosts => ["10.0.0.71:9200"]
        index => "nginx_redis_es_%{+YYYY-MM-dd}"
      }
    }
    
    4)启动
    [root@redis02 ~]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/redis_es.conf
    

    三,配置logstash收集多个日志到redis

    1)配置
    [root@web01 ~]# cat /etc/logstash/conf.d/file_redis.conf
    input {
      file {
        type => "nginx_log"
        path => "/var/log/nginx/access.log"
        start_position => "end"
        codec => "json"
      }
      file {
        type => "tomcat_log"
        path => "/usr/local/tomcat/logs/tomcat_access_json.*.log"
        start_position => "end"
        codec => "json"
      }
    }
    
    output {
      if [type] == "nginx_log" {
        redis {
          host => "172.16.1.81"
          port => "6379"
          key => "nginx_log"
          data_type => "list"
          db => "0"
        }
      }
      if [type] == "tomcat_log" {
        redis {
          host => "172.16.1.81"
          port => "6379"
          key => "tomcat_log"
          data_type => "list"
          db => "1"
        }
      }
    }
    
    2)启动
    [root@web01 ~]# logstash -f /etc/logstash/conf.d/file_redis.conf
    
    3)访问测试查看redis数据
    [root@redis01 ~]# redis-cli 
    127.0.0.1:6379> KEYS *
    1) "nginx_log"
    127.0.0.1:6379> LLEN nginx_log
    (integer) 12
    
    127.0.0.1:6379> SELECT 1
    OK
    127.0.0.1:6379[1]> KEYS *
    1) "tomcat_log"
    127.0.0.1:6379[1]> LLEN tomcat_log
    (integer) 18
    
    3.配置logstash取出redis数据写入ES
    1)配置
    [root@redis02 ~]# vim /etc/logstash/conf.d/redis_es.conf
    input {
      redis {
        host => "172.16.1.81"
        port => "6379"
        data_type => "list"
        key => "nginx_log"
        db => "0"
      }
      redis {
        host => "172.16.1.81"
        port => "6379"
        data_type => "list"
        key => "tomcat_log"
        db => "1"
      }
    }
    
    output {
      if [type] == "nginx_log" {
        elasticsearch {
          hosts => ["10.0.0.71:9200"]
          index => "nginx_redis_es_%{+YYYY-MM-dd}"
        }
      }
      if [type] == "tomcat_log" {
        elasticsearch {
          hosts => ["10.0.0.71:9200"]
          index => "tomcat_redis_es_%{+YYYY-MM-dd}"
        }
      }
    }
    
    2)启动
    [root@redis02 ~]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/redis_es.conf
    
    3)查看redis数据
    127.0.0.1:6379[1]> LLEN tomcat_log
    (integer) 55
    127.0.0.1:6379[1]> LLEN tomcat_log
    (integer) 0
    
    127.0.0.1:6379[1]> SELECT 0
    OK
    127.0.0.1:6379> LLEN nginx_log
    (integer) 0
    
  • 相关阅读:
    R语言:提取路径中的文件名字符串(basename函数)
    课程一(Neural Networks and Deep Learning),第三周(Shallow neural networks)—— 0、学习目标
    numpy.squeeze()的用法
    课程一(Neural Networks and Deep Learning),第二周(Basics of Neural Network programming)—— 4、Logistic Regression with a Neural Network mindset
    Python numpy 中 keepdims 的含义
    课程一(Neural Networks and Deep Learning),第二周(Basics of Neural Network programming)—— 3、Python Basics with numpy (optional)
    课程一(Neural Networks and Deep Learning),第二周(Basics of Neural Network programming)—— 2、编程作业常见问题与答案(Programming Assignment FAQ)
    课程一(Neural Networks and Deep Learning),第二周(Basics of Neural Network programming)—— 0、学习目标
    课程一(Neural Networks and Deep Learning),第一周(Introduction to Deep Learning)—— 0、学习目标
    windows系统numpy的下载与安装教程
  • 原文地址:https://www.cnblogs.com/xiaolang666/p/14107754.html
Copyright © 2011-2022 走看看