Filebeat是一个开源的文件收集器,主要用于获取日志文件,并把它们发送到logstash或elasticsearch。
Filebeat安装
filebeat的安装步骤见: https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-getting-started.html
概要如下:
- yum install filebeat
- 查看配置文件路径 whereis filebeat
Filebeat的配置文件是./filebeat/filebeat.yml,遵循YAML语法。
- 修改配置filebeat.yml:
filebeat.inputs:
- type: log
enabled: true
paths: (扫描输入路径可以配置多个)
- /var/log/b.log
- /var/log/c.log
output.logstash:
# The Logstash hosts
hosts: ["127.0.0.1:5044”]
- 启动
cd /usr/share/filebeat/bin/
./filebeat
-e -c filebeat.yml
Logstash安装
logstash详细说明,请参见官网 : https://www.elastic.co/guide/en/logstash/current/index.html
1、配置yum安装logstash
vi /etc/yum.repos.d/logstash.repo
[logstash-6.x]
name=Elastic repository for 6.x packages
baseurl=https://artifacts.elastic.co/packages/6.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
yum install logstash
2、查看配置文件程序路径
whereis logstash
3、Logstash配置文件内容
input {
beats {
port => "5044"
}
}
filter {
grok {
match => ["message", '%{DATA:time} %{DATA:duration} %{DATA:timestamp}']
}
}
output {
influxdb {
db => "mydb"
host => "localhost"
port => "8086"
user => "*****"
password => "******"
measurement => "mytable"
allow_time_override => true
retention_policy => default
data_points => {
"time" => "%{time}"
"duration" => "%{duration}"
"timestamp"=>"%{timestamp}"
# stdout { codec => rubydebug }
}
}
}
4、logstash filter
Logstash提供了一系列filter过滤plugin来处理收集到的log event,根据log event的特征去切分所需要的字段,方便kibana做visualize和dashboard的data analysis。所有logstash支持的event切分插件查看 https://www.elastic.co/guide/en/logstash/current/filter-plugins.html,
grok详情见:https://www.elastic.co/guide/en/logstash/current/plugins-filters-grok.html
如:
%{SYNTAX:SEMANTIC}
* `SYNTAX`代表匹配值的类型,例如,`0.11`可以`NUMBER`类型所匹配,`10.222.22.25`可以使用`IP`匹配。
* `SEMANTIC`表示存储该值的一个变量声明,它会存储在`elasticsearch`当中方便`kibana`做字段搜索和统计,你可以将一个`IP`定义为客户端IP地址`client_ip_address`,eg:`%{IP:client_ip_address}`,所匹配到的值就会存储到`client_ip_address`这个字段里边,类似数据库的列名,也可以把event log中的数字当成数字类型存储在一个指定的变量当中,比如响应时间`http_response_time`,假设event log record如下:
55.3.244.1 GET /index.html 15824 0.043
%{IP:client_id_address} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes} %{NUMBER:http_response_time}
在logstash的配置文件中配置如下:
filter { grok { match => { "message" => "%{IP:client_id_address} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes} %{NUMBER:http_response_time}" } } }
其中,grok内置的默认类型有很多种,查看所有默认类型,详情见https://github.com/logstash-plugins/logstash-patterns-core/blob/master/patterns/grok-patterns。
推荐使用grokdebugger http://grokdebug.herokuapp.com/来写匹配模式,输入event log record,再逐步使用pattern微调切分,下方会根据你所写的模式将输入切分字段。
安装插件Influxdb output plugin
参见官网 https://www.elastic.co/guide/en/logstash/current/output-plugins.html ,点击页面的influxdb,进入如下页面 https://www.elastic.co/guide/en/logstash/current/plugins-outputs-influxdb.html
概要说明如下:
cd /usr/share/logstash/
bin/logstash-plugin install logstash-output-influxdb
检查配置文件是否正常
bin/logstash -f /etc/logstash/logstash.conf --config.test_and_exit
启动
bin/logstash -f /etc/logstash/logstash.conf --config.reload.automatic
Influxdb
安装
1、下载rpm安装包
wget https://dl.influxdata.com/influxdb/releases/influxdb-0.10.3-1.x86_64.rpm
2、安装
yum localinstall influxdb-0.10.3-1.x86_64.rpm
3、修改配置文件
vim /etc/influxdb/influxdb.conf
hostname = "localhost" //配置主机名
启用8083和8086端口
[admin]
enabled = true
bind-address = ":8083"
https-enabled = false
https-certificate = "/etc/ssl/influxdb.pem"
[http]
enabled = true
bind-address = ":8086"
auth-enabled = false
log-enabled = true
write-tracing = false
pprof-enabled = false
https-enabled = false
4、创建数据库
本地连接执行 influx 进入数据库。
创建mydb数据库 CREATE DATABASE mydb
创建用户设置密码 create user root with password 'root'
授权 grant all privileges to root
设置默认保留策略 alter retention policy "default" on mydb duration 30d default;
参考文档:
https://www.elastic.co/guide/en/logstash/current/index.html
https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-getting-started.html
https://www.elastic.co/guide/en/logstash/current/configuration-file-structure.html
https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-configuration.html
https://www.elastic.co/guide/en/beats/filebeat/current/config-filebeat-logstash.html
https://www.elastic.co/guide/en/logstash/current/plugins-outputs-influxdb.html#
https://www.jianshu.com/p/d46b911fb83e
http://www.cnblogs.com/yincheng/p/logstash.html
https://www.elastic.co/guide/en/logstash/current/event-dependent-configuration.html (if 表达式)