zabbix+filebeat+logstash日志收集


一、前言

  公司有一項目出錯,過了兩個小時才發現它,為了第一時間發現錯誤,我們運維團隊選擇filebeat+logstash采集錯誤日志,zabbix實現報警。

二、配置流程

1.配置zabbix

#創建應用集

#在應用集里創建監控項

#創建觸發器

2、配置filebeat

編輯filebeat配置文件
vi /etc/filebeat/filebeat.yml
filebeat.prospectors:
- input_type: log
  paths:
    - /opt/ops/logs/easyopen.log
#匹配時間格式開頭的日志合並多行為一行(2019-06-14)
  multiline.pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2}' 
  multiline.negate: true
  multiline.match: after
#過濾包含‘ERROR’字符串的行
  include_lines: ['ERROR']
#添加logtype字段,在logstash 做條件判斷
  fields:
    logtype: easyopen_log
#輸出到logstah
output.logstash:
  # The Logstash hosts   Logstash 采用tcp采集模式
  hosts: ["172.17.0.15:5044"]
2019-06-14 17:19:13.317 ERROR 13864 --- [ntLoopGroup-3-2] c.g.a.netty.ApiConfigServerHandler       : 服務端出錯

java.io.IOException: Connection reset by peer
        at sun.nio.ch.FileDispatcherImpl.read0(Native Method) ~[na:1.8.0_131]
        at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39) ~[na:1.8.0_131]
        at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223) ~[na:1.8.0_131]
        at sun.nio.ch.IOUtil.read(IOUtil.java:192) ~[na:1.8.0_131]
        at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380) ~[na:1.8.0_131]
        at io.netty.buffer.PooledUnsafeDirectByteBuf.setBytes(PooledUnsafeDirectByteBuf.java:288) ~[netty-all-4.1.25.Final.jar!/:4.1.25.Final]
        at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1108) ~[netty-all-4.1.25.Final.jar!/:4.1.25.Final]
        at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:345) ~[netty-all-4.1.25.Final.jar!/:4.1.25.Final]
        at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:148) ~[netty-all-4.1.25.Final.jar!/:4.1.25.Final]
        at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:647) [netty-all-4.1.25.Final.jar!/:4.1.25.Final]
        at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:582) [netty-all-4.1.25.Final.jar!/:4.1.25.Final]
        at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:499) [netty-all-4.1.25.Final.jar!/:4.1.25.Final]
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:461) [netty-all-4.1.25.Final.jar!/:4.1.25.Final]
        at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884) [netty-all-4.1.25.Final.jar!/:4.1.25.Final]
        at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) [netty-all-4.1.25.Final.jar!/:4.1.25.Final]
        at java.lang.Thread.run(Thread.java:748) [na:1.8.0_131]
錯誤日志示例
#重啟filebeat服務
systemctl restart filebeat.service

3.配置logstash

 [root@Ops-system conf.d]#  cat FK-middleware.conf

input {
  beats {port => 5044}
}
filter {
#logtype 這個字段用於做條件判斷是哪個服務的日志
   if [fields][logtype] == "easyopen_log" {
        mutate {
                    add_field => [ "[@metadata][zabbix_key]" , "easyopen" ]      #zabbix服務端定義的鍵名
                    add_field => [ "[@metadata][zabbix_host]" , "FK-tomcat" ]    #zabbix客戶端定義的主機名
                }
        }
}
output {
    stdout{
        codec=>rubydebug
     }
        zabbix {
         zabbix_host => "[@metadata][zabbix_host]"
        zabbix_key => "[@metadata][zabbix_key]"
        zabbix_server_host => "127.0.0.1" #zabbix服務端地址
        zabbix_server_port => "10051"       #zabbix服務端端口
        zabbix_value => "message"
                }
     }
#重啟logstash
/etc/init.d/logstash  restart 

4.展示效果

 

郵件報警顯示

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM