上一节讲到elasticsearch和kibana的安装,这一节就来讲logstash和fielbeat安装。
一、filebeat的安装和配置
filebeat的作用就是日志采集,将采集到的日志发送到中间件,我这边以redis为例,也可以用其他的(kafka)等等。
1 | bash# wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.7.0-linux-x86_64.tar.gz |
编辑/usr/local/filebeat/filebeat.yml进行配置
1 | #vim /usr/local/filebeat/filebeat.yml |
将filebeat做成系统服务,filebeat.service文件内容如下:
1 | bash# vim /etc/systemd/system/filebeat.yml |
二、安装logstash
logstash服务负责将redis的数据读取出来,经过处理写到elasticsearch。
1 | bash# wget https://artifacts.elastic.co/downloads/logstash/logstash-6.7.0.tar.gz |
编辑/usr/local/logstash/config/client-redis.conf进行配置1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66bash# vim /usr/local/logstash/config/client-redis.conf
input {
redis {
host => "192.168.1.3"
port => "6379"
key => "filebeat"
data_type => "list"
password => ""
threads => 10
}
}
filter{
####将nginx日志和自主应用日志分开处理
if "/data/logs/nginx" in [source] {
grok {
patterns_dir => "/usr/local/logstash/patterns"
match => { "message" => "%{NGINXACCESS}" }
}
geoip {
source => "remote_addr"
}
date {
match => [ "log_timestamp" , "dd/MMM/YYYY:HH:mm:ss Z" ]
target => "@timestamp"
}
}else{
grok {
match => {"message" => "(?<datetime>\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3})"}
}
date {
match => ["datetime", "yyyy-MM-dd HH:mm:ss.SSS"]
target => "@timestamp"
}
if [source] =~ "api" or [source] =~ "admin" {
ruby {
code => "event.set('timestamp', event.get('@timestamp').time.localtime - 8*60*60)"
}
ruby {
code => "event.set('@timestamp', event.get('timestamp'))"
}
}
}
}
###nginx和自主应用分别使用不同的模版和index
output {
if "/data/logs/nginx" in [source] {
elasticsearch {
hosts => ["127.0.0.1:10086"]
index => "logstash-nginx-%{+YYYY.MM.dd}"
template => "/usr/local/logstash/config/nginx-template.json"
template_name => "wb-nginx"
template_overwrite => true
}
}else{
elasticsearch {
hosts => ["127.0.0.1:10086"]
index => "logstash-%{+YYYY.MM.dd}"
template => "/usr/local/logstash/config/app-template.json"
template_name => "wb_index"
template_overwrite => true
}
}
stdout {codec => rubydebug}
}
编辑/usr/local/logstash/config/nginx-template.json
1 | bash# vim /usr/local/logstash/config/nginx-template.json |
编辑/usr/local/logstash/config/app-template.json
1 | bash# vim /usr/local/logstash/config/app-template.json |
编辑nginx日志的匹配模版文件
1 | bash# mkdir /usr/local/logstash/patterns |
nginx的日志格式为
1 | log_format main '$remote_addr - $remote_user [$time_local] "$request" ' |
使用supervisor进行管理logstash进程
1 | bash# vim /etc/supervisor.d/logstash.ini |
三、启动logstash和filebeat
1 | bash# systemctl start filebeat |