day105 ELK-filbeate收集日志

filebeat作為代理安裝在服務(wù)器上,監(jiān)視指定的日志文件或位置,收集日志事件,并將他們轉(zhuǎn)發(fā)到logstash,elasticsearch,kafka等

input 我們要采集的日志文件路徑, 收割機(jī) harvester 監(jiān)聽文件的變化 -->
splooer程序 --> 轉(zhuǎn)發(fā) es | logstash | kafka | redis

image
filebeat.inputs:
 - type: stdin #標(biāo)準(zhǔn)輸入
   enabled: true #啟用
 output.console: #標(biāo)準(zhǔn)輸出
 pretty: true
 enable: true
將文件最新發(fā)生變化的內(nèi)容,存入ES
[root@web01 ~]# cat /etc/filebeat/file.yml
filebeat.inputs:
- type: log
  paths: /var/log/nginx/access.log
  enabled: true
  output.elasticsearch:
  hosts:["10.0.0.161:9200","10.0.0.162:9200","10.0.0.163:9200"]

收集系統(tǒng)日志

特別分散--> syslog --> file.txt
1.減少無(wú)用的數(shù)據(jù)
2.調(diào)整索引名稱
3.測(cè)試調(diào)整模板,設(shè)定分片

[root@web01 filebeat]# cat filebeat_system.yml 
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/oldxu.log
  include_lines: ['^ERR','^WARN','sshd'] #只看指定的日志

output.elasticsearch:
  hosts: ["10.0.0.161:9200","10.0.0.162:9200","10.0.0.163:9200"]
  index: "system-%{[agent.version]}-%{+yyyy.MM.dd}"

setup.ilm.enabled: false
setup.template.name: "system"
setup.template.pattern: "system-*"

setup.template.settings:            #定義索引分片數(shù)和副本
  index.number_of_shards: 3
  index.number_of_replicas: 1

1.修改system模板   ---> 添加 shards 分片數(shù)數(shù)量,replicas的數(shù)量
2.刪除模板關(guān)聯(lián)的索引
3.刪除filebeat自行指定的分片數(shù)和副本數(shù)
4.重啟filebeat
5.產(chǎn)生新的日志

收集Nginx日志

配置filebeat

[root@web01 filebeat]# cat filebeat_nginx.yml 
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/nginx/access.log
  json.keys_under_root: true    #默認(rèn)Flase,還會(huì)將json解析的日志存儲(chǔ)至messages字段
  json.overwrite_keys: true     #覆蓋默認(rèn)的key,使用自定義json格式的key

output.elasticsearch:
  hosts: ["10.0.0.161:9200","10.0.0.162:9200","10.0.0.163:9200"]
  index: "nginx-%{[agent.version]}-%{+yyyy.MM.dd}"

setup.ilm.enabled: false
setup.template.name: nginx   #索引關(guān)聯(lián)的模板名稱
setup.template.pattern: nginx-*

收集nginx訪問(wèn)日志和錯(cuò)誤日志

[root@web01 filebeat]# cat filebeat_access.yml 
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/nginx/access.log
  json.keys_under_root: true    #默認(rèn)Flase,還會(huì)將json解析的日志存儲(chǔ)至messages字段
  json.overwrite_keys: true     #覆蓋默認(rèn)的key,使用自定義json格式的key
  tags: ["access"]

- type: log
  enabled: true
  paths:
    - /var/log/nginx/error.log
  tags: ["error"]

output.elasticsearch:
  hosts: ["10.0.0.161:9200","10.0.0.162:9200","10.0.0.163:9200"]
  indices:
    - index: "nginx-access-%{[agent.version]}-%{+yyyy.MM.dd}"
      when.contains:
        tags: "access"

    - index: "nginx-error-%{[agent.version]}-%{+yyyy.MM.dd}"
      when.contains:
        tags: "error"

setup.ilm.enabled: false
setup.template.name: nginx   #索引關(guān)聯(lián)的模板名稱
setup.template.pattern: nginx-*

收集nginx多個(gè)虛擬主機(jī)的日志

image

1.虛擬主機(jī)

[root@web01 conf.d]# cat elk.oldxu.com.conf 
server {
    listen 80;
    server_name elk.oldxu.com;
    root /code/elk;
        access_log /var/log/nginx/elk.oldxu.com.log json;

    location / {
        index index.html;
    }
}
[root@web01 conf.d]# cat bk.oldxu.com.conf 
server {
    listen 80;
    server_name bk.oldxu.com;
    root /code/bk;
        access_log /var/log/nginx/bk.oldxu.com.log json;

    location / {
        index index.html;
    }
}
[root@web01 conf.d]# cat bs.oldxu.com.conf 
server {
    listen 80;
    server_name bs.oldxu.com;
    root /code/bs;
        access_log /var/log/nginx/bs.oldxu.com.log json;

    location / {
        index index.html;
    }
}

2.測(cè)試,模擬產(chǎn)生日志

[root@web01 conf.d]# curl -H Host:elk.oldxu.com http://10.0.0.7
elk.oldux.com
[root@web01 conf.d]# curl -H Host:bs.oldxu.com http://10.0.0.7
bs.oldux.com
[root@web01 conf.d]# curl -H Host:bk.oldxu.com http://10.0.0.7
bk.oldux.com

3.配置filebeat

[root@web01 filebeat]# cat filebeat-vhosts.yml 
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/nginx/elk.oldxu.com.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["nginx-elk-host"]

- type: log
  enabled: true
  paths:
    - /var/log/nginx/bs.oldxu.com.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["nginx-bs-host"]

- type: log
  enabled: true
  paths:
    - /var/log/nginx/bk.oldxu.com.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["nginx-bk-host"]

- type: log
  enabled: true
  paths:
    - /var/log/nginx/error.log
  tags: ["nginx-error"]

output.elasticsearch:
  hosts: ["10.0.0.161:9200","10.0.0.162:9200","10.0.0.163:9200"]
  indices:
    - index: "nginx-elk-access-%{[agent.version]}-%{+yyyy.MM.dd}"
      when.contains:
        tags: "nginx-elk-host"

    - index: "nginx-bs-access-%{[agent.version]}-%{+yyyy.MM.dd}"
      when.contains:
        tags: "nginx-bs-host"

    - index: "nginx-bk-access-%{[agent.version]}-%{+yyyy.MM.dd}"
      when.contains:
        tags: "nginx-bk-host"

    - index: "nginx-error-%{[agent.version]}-%{+yyyy.MM.dd}"
      when.contains:
        tags: "nginx-error"

setup.ilm.enabled: false
setup.template.name: nginx   #索引關(guān)聯(lián)的模板名稱
setup.template.pattern: nginx-*

Tomcat日志

訪問(wèn)日志 ---> json格式

1.修改tomcat日志格式
 [root@web02 soft]# yum install java -y
 [root@web02 soft]# vim tomcat/conf/server.xml
     <Host name="tomcat.oldxu.com" appBase="webapps"
           unpackWARs="true" autoDeploy="true">
       <Valve
className="org.apache.catalina.valves.AccessLogValve"
directory="logs"
               prefix="tomcat.oldxu.com.log"
suffix=".txt"
               pattern="
{&quot;clientip&quot;:&quot;%h&quot;,&quot;ClientUser&q
uot;:&quot;%l&quot;,&quot;authenticated&quot;:&quot;%u&
quot;,&quot;AccessTime&quot;:&quot;%t&quot;,&quot;metho
d&quot;:&quot;%r&quot;,&quot;status&quot;:&quot;%s&quot
;,&quot;SendBytes&quot;:&quot;%b&quot;,&quot;Query?
string&quot;:&quot;%q&quot;,&quot;partner&quot;:&quot;%
{Referer}i&quot;,&quot;AgentVersion&quot;:&quot;%{User?Agent}i&quot;}" />
     </Host>

配置filebeat
[root@web01 filebeat]# cat filebeat-tomcat-mutilline.yml 
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /soft/tomcat/logs/tomcat.oldxu.com.log.*.txt
  json.keys_under_root: true    #默認(rèn)Flase,還會(huì)將json解析的日志存儲(chǔ)至messages字段
  json.overwrite_keys: true     #覆蓋默認(rèn)的key,使用自定義json格式的key
  tags: ["tomcat-access"]

- type: log
  enabled: true
  paths:
    - /soft/tomcat/logs/catalina.out
  multiline.pattern: '^\d{2}'   #匹配以2個(gè)數(shù)字開頭的
  multiline.negate: true
  multiline.match: after
  multiline.max_lines: 10000    #默認(rèn)最大合并行為500,可根據(jù)實(shí)際情況調(diào)整。
  tags: ["tomcat-error"]

output.elasticsearch:
  hosts: ["10.0.0.161:9200","10.0.0.162:9200"]
  indices:
    - index: "tomcat-access-%{[agent.version]}-%{+yyyy.MM.dd}"
      when.contains:
        tags: "tomcat-access"

    - index: "tomcat-error-%{[agent.version]}-%{+yyyy.MM.dd}"
      when.contains:
        tags: "tomcat-error"

setup.ilm.enabled: false
setup.template.name: tomcat   #索引關(guān)聯(lián)的模板名稱
setup.template.pattern: tomcat-*
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Filebeat是本地文件的日志數(shù)據(jù)采集器,可監(jiān)控日志目錄或特定日志文件(tail file),并將它們轉(zhuǎn)發(fā)給El...
    康康6840閱讀 63,201評(píng)論 2 39
  • 工欲善其事,必先利其器。 最近討論的比較多的就是如何把我們的log有效的管理起來(lái),收集了一些ELK相關(guān)的知識(shí),做個(gè)...
    jaymz明閱讀 374評(píng)論 0 2
  • 為什么用到ELK: 一般我們需要進(jìn)行日志分析場(chǎng)景:直接在日志文件中 grep、awk 就可以獲得自己想要的信息。但...
    李紹俊閱讀 418評(píng)論 0 2
  • 本人陸陸續(xù)續(xù)接觸了ELK的1.4,2.0,2.4,5.0,5.2版本,可以說(shuō)前面使用當(dāng)中一直沒有太多感觸,最近使用...
    三杯水Plus閱讀 4,277評(píng)論 0 12
  • 當(dāng)時(shí)媒體上宣布金融危機(jī)已經(jīng)過(guò)去,但是熟悉華爾街的人都知道,事情顯然還沒有結(jié)束,用他們的話說(shuō),壞消息還沒出盡。果然,...
    林珊頤閱讀 176評(píng)論 0 1

友情鏈接更多精彩內(nèi)容