一、Filebeat介绍
Filebeat是用于采集和转发日志数据的轻量级采集器,Filebeat作为代理安装在服务器上,它监视您指定的日志文件或位置,收集日志事件,并将它们转发到Elasticsearch或Logstash进行索引。
当启动Filebeat时,它会启动一个或多个输入,这些输入查找指定的日志位置进行日志采集。Filebeat对定位的每个日志都会启动一个Harvester(收割机,收割日志),每个Harvester读取新内容的单个日志,并将新日志数据发送到libbeat,libbeat聚合事件(Spooler,就像传送带不断运送的物资)并将聚合数据发送到Filebeat配置的输出。
二、filebeat下载
官方下载地址:Download Filebeat • Lightweight Log Analysis | Elastic
这里安装在x86_64架构的银河麒麟V10服务器版上,可以下载Linux x86_64或rpm x86_64的包,正式环境使用推荐用rpm包,安装完成后直接注册了系统服务,可以使用systemctl管理服务。如果使用tar.gz安装包,还需要手动创建服务文件,相对繁琐一些。
三、Filebeat运行
此处用于测试,因此使用tar.gz安装包,并且直接在前台运行服务,便于观察测试效果。
# 查看安装包
[root@kylin ~]# ll
总用量 42360
-rw-r--r-- 1 root root 43375758 7月 3 20:00 filebeat-8.8.2-linux-x86_64.tar.gz
# 解压
[root@kylin ~]# tar xvf filebeat-8.8.2-linux-x86_64.tar.gz
[root@kylin ~]# ll
总用量 42360
drwxr-xr-x 5 root root 212 7月 3 20:01 filebeat-8.8.2-linux-x86_64
-rw-r--r-- 1 root root 43375758 7月 3 20:00 filebeat-8.8.2-linux-x86_64.tar.gz
# 进入解压后目录,查看目录包含文件
[root@kylin ~]# cd filebeat-8.8.2-linux-x86_64
[root@kylin filebeat-8.8.2-linux-x86_64]# ll
总用量 140536
-rw-r--r-- 1 root root 3745648 6月 24 03:04 fields.yml
-rwxr-xr-x 1 root root 137290072 6月 24 03:11 filebeat
-rw-r--r-- 1 root root 177676 6月 24 03:04 filebeat.reference.yml
-rw------- 1 root root 8622 6月 24 03:04 filebeat.yml
drwxr-xr-x 4 root root 24 6月 24 03:04 kibana
-rw-r--r-- 1 root root 13675 6月 24 02:42 LICENSE.txt
drwxr-xr-x 71 root root 4096 6月 24 03:04 module
drwxr-xr-x 2 root root 4096 6月 24 03:04 modules.d
-rw-r--r-- 1 root root 2644331 6月 24 02:42 NOTICE.txt
-rw-r--r-- 1 root root 809 6月 24 03:11 README.md
3.1 input标准输入、output标准输出
初步接触filebeat,推荐先练习标准输入和标准输出,有助于理解filebeat功能。
只有知道采集了什么日志,输出时以什么样的格式输出,进一步处理日志时才能得心应手。
filebeat.inputs:
- type: stdin 标准输入,从控制台输入
output.console: 控制台输出
pretty: true 美化显示效果
# 备份filebeat配置文件
[root@kylin filebeat-8.8.2-linux-x86_64]# cp filebeat.yml filebeat.yml.bak
# 创建新的input和output
[root@kylin filebeat-8.8.2-linux-x86_64]# cat > filebeat.yml <<EOF
filebeat.inputs:
- type: stdin
output.console:
pretty: true
EOF
#查看配置文件
[root@kylin filebeat-8.8.2-linux-x86_64]# cat filebeat.yml
filebeat.inputs:
- type: stdin
output.console:
pretty: true
运行filebeat,指定配置文件filebeat.yml(Ctrl+C停止filebeat)
[root@kylin filebeat-8.8.2-linux-x86_64]# ./filebeat -e -c filebeat.yml
在控制台输入内容后,filebeat读取到控制台(stdin)的输入,从console输出
红色框为手动输入内容:hello
绿色框为filebeat采集到数据后输出信息,其中message部分为读取到的数据
3.2 processors、ignore_older、fields、include_lines、exclude_lines对日志做一些处理
如果想filebeat输出日志时,添加上所在服务器的一些信息,可以用processors
processors:
- add_host_metadata: ~ 添加主机信息
停止filebeat,修改配置文件如下
[root@kylin filebeat-8.8.2-linux-x86_64]# cat filebeat.yml
filebeat.inputs:
- type: stdin
processors:
- add_host_metadata: ~
output.console:
pretty: true
再次运行filebeat,在控制台输入hello,可以看到输出的数据多了host字段,其中包含IP、MAC、主机名、系统架构、系统版本、内核等信息
{
"@timestamp": "2023-07-03T12:54:37.958Z",
"@metadata": {
"beat": "filebeat",
"type": "_doc",
"version": "8.8.2"
},
"log": {
"file": {
"path": ""
},
"offset": 0
},
"message": "hello",
"input": {
"type": "stdin"
},
"ecs": {
"version": "8.0.0"
},
"host": {
"id": "0232e19ae2664217b302a4ed6b3e0e7e",
"containerized": false,
"ip": [
"192.168.80.50",
"fe80::6897:ba60:d239:961a",
"192.168.10.50",
"fe80::ed9a:2384:e2b6:c96f",
"172.18.0.1",
"172.17.0.1"
],
"mac": [
"00-0C-29-4A-D2-0F",
"00-0C-29-4A-D2-19",
"02-42-34-C4-07-BF",
"02-42-A0-80-71-19"
],
"name": "kylin.toper",
"hostname": "kylin.toper",
"architecture": "x86_64",
"os": {
"type": "linux",
"platform": "kylin",
"version": "V10 (Sword)",
"family": "",
"name": "Kylin Linux Advanced Server",
"kernel": "4.19.90-24.4.v2101.ky10.x86_64",
"codename": "Sword"
}
},
"agent": {
"id": "988f408f-f6fb-4e7e-a985-39e58c09ef57",
"name": "kylin.toper",
"type": "filebeat",
"version": "8.8.2",
"ephemeral_id": "869ebbab-0b1b-4be0-92c6-af24807dfa22"
}
}
我们再做一些修改:
添加自定义字段,标识输入的日志为console-log,有助于后续logstash使用filter过滤数据
fields_under_root: true (如果此选项设置为true,则自定义字段将作为顶级字段输出,否则将在顶级字段fields的子字典下输出。如果自定义字段名称与Filebeat添加的其他字段名称冲突,则自定义字段将覆盖其他字段。)
fields:
logtype: "console-log"
ignore_older: 72h 不采集72小时前的数据,采集日志时,通常比较久时间之前的日志不采集,一是采集了可能没有什么意义,二是如果日志文件比较大,批量部署一批filebeat时,突然读取并输出大量日志,会给filebeat输出的目标端造成很大压力
include_lines和exclude_lines根据实际需要使用一种即可
include_lines: ["^LOG"] 只采集LOG开头的日志,区分大小写
exclude_lines: ["^DEBUG", "^INFO"] 不采集DEBUG、INFO开头的日志,区分大小写
丢弃某些字段,如果是顶级字段,如input,直接写字段名称;如果是顶级字段子集,则是dingji.ziji,如host.os
- drop_fields:
fields: ["host.hostname", "host.os", "host.architecture", "host.id", "host.mac", "host.containerized", "agent", "ecs", "input"]
上述修改配置文件如下
[root@kylin filebeat-8.8.2-linux-x86_64]# cat filebeat.yml
filebeat.inputs:
- type: stdin
fields_under_root: true
fields:
logtype: "console-log"
ignore_older: 72h
include_lines: ["^LOG", "^ERROR"]
exclude_lines: ["^DEBUG", "^INFO"]
processors:
- add_host_metadata:
- drop_fields:
fields: ["host.hostname", "host.os", "host.architecture", "host.id", "host.mac", "host.containerized", "agent", "ecs", "input"]
output.console:
pretty: true
重新运行Filebeat,在控制台输入LOG test后输出信息如下:
输入LOG开头的数据才可以被filebeat采集到
input等字段,host的一些子字典信息被丢弃了,输出的数据精简了很多
{
"@timestamp": "2023-07-03T13:55:36.451Z",
"@metadata": {
"beat": "filebeat",
"type": "_doc",
"version": "8.8.2"
},
"host": {
"name": "kylin.toper",
"ip": [
"192.168.80.50",
"fe80::6897:ba60:d239:961a",
"192.168.10.50",
"fe80::ed9a:2384:e2b6:c96f",
"172.18.0.1",
"172.17.0.1"
]
},
"log": {
"file": {
"path": ""
},
"offset": 0
},
"message": "LOG test",
"logtype": "console-log"
}
3.3 input-从文件读取日志
验证从文件中读取日志,例如Linux系统文件messages日志(实际中Linux系统日志使用syslog发送,此处仅用于演示文件读取功能)
- type: filestream 输入类型文件流(之前版本为log,现在被废弃了,测试了还可以用,但不推荐)
paths: 日志路径
- /var/log/messages* message日志按照日期生成,会有多个日志文件,因此结尾用通配符匹配
配置文件修改如下
[root@kylin filebeat-8.8.2-linux-x86_64]# ll /var/log/messages*
-rw------- 1 root root 1641427 7月 3 22:01 /var/log/messages
-rw------- 1 root root 4761666 6月 29 11:49 /var/log/messages-20230629
[root@kylin filebeat-8.8.2-linux-x86_64]# cat filebeat.yml
filebeat.inputs:
- type: filestream
paths:
- /var/log/messages*
fields_under_root: true
fields:
logtype: "Linux-systemlog"
ignore_older: 1h
processors:
- add_host_metadata: ~
- drop_fields:
fields: ["host.hostname", "host.os", "host.architecture", "host.id", "host.mac", "host.containerized", "agent", "ecs", "input"]
output.console:
pretty: true
查看最新messages日志文件中的最后几条日志
[root@kylin filebeat-8.8.2-linux-x86_64]# tail -f /var/log/messages
Jul 3 22:10:27 kylin dnf[2269]: 元数据缓存近期已刷新。
Jul 3 22:10:27 kylin systemd[1]: dnf-makecache.service: Succeeded.
Jul 3 22:10:27 kylin audit[1]: SERVICE_START pid=1 uid=0 auid=4294967295 ses=4294967295 msg='unit=dnf-makecache comm="systemd" exe="/usr/lib/systemd/systemd" hostname=? addr=? terminal=? res=success'
Jul 3 22:10:27 kylin audit[1]: SERVICE_STOP pid=1 uid=0 auid=4294967295 ses=4294967295 msg='unit=dnf-makecache comm="systemd" exe="/usr/lib/systemd/systemd" hostname=? addr=? terminal=? res=success'
Jul 3 22:10:27 kylin systemd[1]: Started dnf makecache.
Jul 3 22:10:27 kylin auditd[877]: find kylin-root
Jul 3 22:10:27 kylin auditd[877]: AUDIT:bfree=442086,threshold_size=75,fs_space_warning=0
Jul 3 22:10:27 kylin auditd[877]: find kylin-root
Jul 3 22:10:27 kylin auditd[877]: AUDIT:bfree=442086,threshold_size=75,fs_space_warning=0
Jul 3 22:10:27 kylin rsyslogd[1267]: cannot connect to 192.168.80.50:514: Connection refused [v8.2006.0 try https://www.rsyslog.com/e/2027 ]
重新运行Filebeat后,可以看到采集到了messages文件中的日志,和tail查看日志文件中的信息一致
"message": "Jul 3 22:01:01 kylin rsyslogd[1267]: cannot connect to 192.168.80.50:514: Connection refused [v8.2006.0 try https://www.rsyslog.com/e/2027 ]",
"logtype": "Linux-systemlog"
{
"@timestamp": "2023-07-03T14:06:04.104Z",
"@metadata": {
"beat": "filebeat",
"type": "_doc",
"version": "8.8.2"
},
"host": {
"name": "kylin.toper",
"ip": [
"192.168.80.50",
"fe80::6897:ba60:d239:961a",
"192.168.10.50",
"fe80::ed9a:2384:e2b6:c96f",
"172.18.0.1",
"172.17.0.1"
]
},
"log": {
"offset": 1641285,
"file": {
"path": "/var/log/messages"
}
},
"message": "Jul 3 22:01:01 kylin rsyslogd[1267]: cannot connect to 192.168.80.50:514: Connection refused [v8.2006.0 try https://www.rsyslog.com/e/2027 ]",
"logtype": "Linux-systemlog"
}
3.4 input-modules使用
也可以直接使用filebeat内置的modules来读取日志,modules除了读取日志之外,最大的作用是将读取的日志直接做了解析,并赋予字段名。无需用logstash再做处理,直接可以在kibana上查询、展示对应字段。
[root@kylin filebeat]# vi filebeat.yml
在filebeat配置文件中指定modules所在目录
[root@kylin filebeat]# cat filebeat.yml
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
output.console:
pretty: true
启用nginx module
[root@kylin filebeat]# ./filebeat modules enable nginx
Enabled nginx
也可以禁用nginx module
[root@kylin filebeat]# ./filebeat modules disable nginx
Disabled nginx
我们再次启用nginx module
[root@kylin filebeat]# ./filebeat modules enable nginx
Enabled nginx
查看nginx modules列表,可以看到Enable的modules有nginx
[root@kylin filebeat]# ./filebeat modules list
查看modules所在目录,可以看到除了启用的nginx,其他modules配置文件末尾都是.disabled,所以我们配置文件中指定读取*.yml的module,只会读取到nginx
[root@kylin filebeat]# ll modules.d/
修改nginx module配置,启用access日志,并指定日志路径
[root@kylin filebeat]# vi modules.d/nginx.yml
[root@kylin filebeat]# cat modules.d/nginx.yml
- module: nginx
# Access logs
access:
enabled: true
var.paths: ["/usr/local/nginx/log/access.log*"]
运行filebeat后可以看到采集的日志输出如下。如果从Kibana查看输出的日志,可以看到module最大的作用其实是将message中的数据做了解析并赋予了对应字段名,查看更直观。
例如:message开头的192.168.80.1,会解析为源IP、客户端IP、相关IP等字段。