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="
{"clientip":"%h","ClientUser&q
uot;:"%l","authenticated":"%u&
quot;,"AccessTime":"%t","metho
d":"%r","status":"%s"
;,"SendBytes":"%b","Query?
string":"%q","partner":"%
{Referer}i","AgentVersion":"%{User?Agent}i"}" />
</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-*