Filebeat +Redis+ELK处理Nginx日志系统

(一)简述:

 filebeat:具有日志收集功能,是下一代的Logstash收集器,但是filebeat更轻量,占用资源更少,适合客户端使用。

  redis:Redis 服务器通常都是用作 NoSQL 数据库,不过 logstash 只是用来做消息队列。

  logstash:主要是用来日志的搜集、分析、过滤日志的工具,支持大量的数据获取方式。一般工作方式为c/s架构,client端安装在需要收集日志的主机上,server端负责将收到的各节点日志进行过滤、修改等操作在一并发往elasticsearch上去。

  elasticsearch:Elasticsearch是个开源分布式搜索引擎,提供搜集、分析、存储数据三大功能。它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。

  kibana:Kibana可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助汇总、分析和搜索重要数据日志。

下图展示的基本的架构图。

Filebeat +Redis+ELK处理Nginx日志系统

(二)具体步骤

1、filebeat的简介和具体配置
Filebeat是一个日志文件托运工具,在你的服务器上安装客户端后,filebeat会监控日志目录或者指定的日志文件,追踪读取这些文件(追踪文件的变化,不停的读),并且转发这些信息到elasticsearch或者logstarsh、redis中存放

1.1、工作原理
filebeat由2个主要组件构成:prospector和harvesters。这两类组件一起协同完成Filebeat的工作,从指定文件中把数据读取出来,然后发送事件数据到配置的output中。

harvesters:主要负责进行单个文件的内容收集;在运行过程中,每一个Harvester会对一个文件逐行进行内容读取,并且把读写到的内容发送到配置的output中。

Prospector负责管理Harvsters,并且找到所有需要进行读取的数据源。如果input type配置的是log类型,Prospector将会去配置度路径下查找所有能匹配上的文件,然后为每一个文件创建一个Harvster。每个Prospector都运行在自己的Go routine里

1.2工作流程
当你开启filebeat程序的时候,它会启动一个或多个探测器(prospectors)去检测你指定的日志目录或文件,对于探测器找出的每一个日志文件,filebeat启动收割进程(harvester),每一个收割进程读取一个日志文件的新内容,并发送这些新的日志数据到处理程序(spooler),处理程序会集合这些事件,最后filebeat会发送集合的数据到你指定的地点
Filebeat +Redis+ELK处理Nginx日志系统

1.3、具体的相关配置

[root@localhost ~]# vim /etc/filebeat/filebeat.yml
#=========================== Filebeat inputs =============================

filebeat.inputs:

# Each - is an input. Most options can be set at the input level, so
# you can use different inputs for various configurations.
# Below are the input specific configurations.

- type: log

  # Change to true to enable this input configuration.
  enabled: true

  # Paths that should be crawled and fetched. Glob based paths.
  paths:
    - /opt/access.log
#================================ Outputs =====================================
# Configure what output to use when sending the data collected by the beat.
output.redis:
  hosts: ["172.20.67.50:6379"]
  #port: 6379
  #password: "123456"  
  db: 2
  timeout: 10
  key: "nginx-log"

    #####备注:目前使用filebeat向redis写日志的时候不能向redis集群里写,会提示报错,所有redis只能写到单台里,
root@localhost ~]# systemctl start filebeat
[root@localhost ~]# systemctl status filebeat
● filebeat.service - Filebeat sends log files to Logstash or directly to Elasticsearch.
   Loaded: loaded (/usr/lib/systemd/system/filebeat.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2018-09-20 19:48:36 CST; 3s ago
     Docs: https://www.elastic.co/products/beats/filebeat
 Main PID: 11947 (filebeat)
   CGroup: /system.slice/filebeat.service
           └─11947 /usr/share/filebeat/bin/filebeat -c /etc/filebeat/filebeat.yml -path.home /usr/share/filebeat -path.config /etc/filebeat -path.data /var/lib/filebeat -path.logs /var/log/filebeat

Sep 20 19:48:36 localhost.localdomain systemd[1]: Started Filebeat sends log files to Logstash or directly to Elasticsearch..
Sep 20 19:48:36 localhost.localdomain systemd[1]: Starting Filebeat sends log files to Logstash or directly to Elasticsearch....
[root@localhost ~]# 

2、Redis具体的操作
在redis上查看具体导入的数据
Filebeat +Redis+ELK处理Nginx日志系统

3、logstash 具体的配置

[root@localhost ~]# vim /usr/local/logstash/data/redis.conf 
input {
        redis {
                host => "172.20.67.50"
                port => "6379"
                data_type => "list"
                db => 2
                batch_count => 1     ###这个值是指从队列中读取数据时,一次性取出多少条。不写会报错,解决办法就是,不使用这个功能,将batch_size设置为1
                #type => "log"
                key => "nginx-log"
        }
}

filter {
          grok {
      match => { "message" => "%{IPORHOST:remote_addr} - - \[%{HTTPDATE:time_local}\] \"%{WORD:method} %{URIPATHPARAM:request} HTTP/%{NUMBER:httpversion}\" %{INT:status} %{INT:body_bytes_sent} %{QS:http_referer} %{QS:http_user_agent}"
                                }
                     }
 }

output {  
        elasticsearch {  
                hosts => "172.20.67.50:9200"
                index => "nginx-access-%{+YYYY.MM.dd}"
                }   
} 

4、查看ES集群是否有数据
可t通过head进行查看是否有数据。

5、在kibana中创建索引,即可在主页查看相关的数据了。

错误集锦:
错误一:

“2018-09-20T17:58:05.029+0800   ERROR   redis/client.go:231 Failed to RPUSH to redis list with: MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands 
that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for det
ails about the RDB error.”
解决方案:去redis上查看日志,出现了“
18180:M 20 Sep 18:10:35.061 * 10 changes in 300 seconds. Saving...
18180:M 20 Sep 18:10:35.061 # Can't save in background: fork: Cannot al”
彻底解决的办法是在redis服务器上依次执行:
1、vim /etc/sysctl.conf
2、添加 vm.overcommit_memory=1
3、sysctl -p

错误二、

[WARN ][logstash.inputs.redis    ] Redis connection problem {:exception=>#<Redis::CommandError: ERR unknown command 'script'>}
Redis connection problem {:exception=>#<Redis::CommandError: ERR Error runni”

解决方法:
Filebeat +Redis+ELK处理Nginx日志系统

解决的办法:不使用这个功能,将batch_size设置为1batch_count => 1

转载于:https://blog.51cto.com/liqingbiao/2177873

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值