安装
filebeat-<version>-windows 的文件夹。filebeat.yml 配置文件。可以使用文本编辑器(如 Notepad++ 或 Visual Studio Code)打开它。在 Windows 上,您可以将 Filebeat 作为服务运行。请按照以下步骤进行:
cd 命令导航到解压后的 Filebeat 文件夹。例如:
powershellcd "C:\Program Files\Filebeat\filebeat-<version>-windows"
powershell.\install-service-filebeat.ps1
powershellStart-Service filebeat
powershellGet-Service filebeat
powershellStart-Service filebeat
powershellGet-Service filebeat
C:\Program Files\Filebeat\logs 目录下。如果需要停止 Filebeat 服务,可以运行:
powershellStop-Service filebeat
如果需要卸载 Filebeat 服务,可以运行:
powershell.\uninstall-service-filebeat.bat
通过以上步骤,您应该能够在 Windows 上成功安装并运行 Filebeat。
filebeat.yml 示例yaml###################### Filebeat Configuration Example #########################
#=========================== Filebeat inputs =============================
filebeat.inputs:
# Apache 日志输入
- type: log
enabled: true
paths:
# 将以下路径改为对应的日志目录,可以使用 * 通配符匹配多个日志文件
- E:/RequirementsDocument/ELK-LOG/install/httpd-2.4.62-240904-win64-VS17/Apache24/logs/access*.log
fields:
log_type: apache
# IIS 日志输入
- type: log
enabled: true
paths:
- C:/inetpub/logs/LogFiles/W3SVC*/u_ex*.log
fields:
log_type: iis
# Tomcat 日志输入
- type: log
enabled: true
paths:
- D:/tomcat8.5.73/logs/catalina*.log
fields:
log_type: tomcat
# nginx 日志输入
- type: log
enabled: true
paths:
- E:/RequirementsDocument/ELK-LOG/logs/nginx/access*.log
fields:
log_type: nginx
#============================= Filebeat modules ===============================
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
#==================== Elasticsearch template setting ==========================
setup.template.name: "its-log"
setup.template.pattern: "its-log-*"
setup.template.settings:
index.number_of_shards: 1
index.number_of_replicas: 0
_source.enabled: true
setup.template:
# Apache 模板
- name: "its-log-apache"
pattern: "its-log-apache*"
settings:
index.number_of_shards: 1
index.number_of_replicas: 0
_source.enabled: true
# IIS 模板
- name: "its-log-iis"
pattern: "its-log-iis*"
settings:
index.number_of_shards: 1
index.number_of_replicas: 0
_source.enabled: true
# Tomcat 模板
- name: "its-log-tomcat"
pattern: "its-log-tomcat*"
settings:
index.number_of_shards: 1
index.number_of_replicas: 0
_source.enabled: true
# nginx模板
- name: "its-log-nginx"
pattern: "its-log-nginx*"
settings:
index.number_of_shards: 1
index.number_of_replicas: 0
_source.enabled: true
#============================== Kibana =====================================
setup.kibana:
#================================ Outputs =====================================
#-------------------------- Elasticsearch output ------------------------------
output.elasticsearch:
# Array of hosts to connect to.
hosts: ["10.180.27.36:9200"]
index: "its-log-%{[fields.log_type]}-cd-%{+YYYY.MM.dd}"
username: "xxxx"
password: "xxxxxxxxx"
#================================ Procesors =====================================
processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
#============================== Xpack Monitoring ===============================
number_of_files: 3
close_removed: true
clean_removed: true
#============================== Logging =====================================
logging:
level: info
to_files: true
files:
path: C:\Program Files\Filebeat\logs
name: filebeat
keepfiles: 7
permissions: 0644
###10 Windows filebeat.yml 示例解释
以下是对 Filebeat 配置文件的逐行解释:
yamlfilebeat.inputs:
# Apache 日志输入
- type: log
enabled: true
paths:
- E:/RequirementsDocument/ELK-LOG/install/httpd-2.4.62-240904-win64-VS17/Apache24/logs/access*.log
fields:
log_type: apache
filebeat.inputs:定义 Filebeat 的输入源。type: log:指定输入类型为日志文件。enabled: true:启用此输入。paths:定义要读取的日志文件路径,支持通配符(*)。fields:自定义字段,这里定义了 log_type 为 apache,可以用于后续的索引或处理。接下来的输入部分(IIS、Tomcat、nginx)与 Apache 输入类似,定义了不同类型的日志源,分别为:
C:/inetpub/logs/LogFiles/W3SVC*/u_ex*.logD:/tomcat8.5.73/logs/catalina*.logE:/RequirementsDocument/ELK-LOG/logs/nginx/access*.logyamlfilebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
filebeat.config.modules:配置 Filebeat 模块。path:指定模块文件的位置。reload.enabled:是否启用模块的动态重载,这里设置为 false。yamlsetup.template.name: "its-log"
setup.template.pattern: "its-log-*"
setup.template.settings:
index.number_of_shards: 1
index.number_of_replicas: 0
_source.enabled: true
setup.template.name:设置索引模板的名称。setup.template.pattern:指定匹配的索引模式。setup.template.settings:定义索引的设置:
index.number_of_shards:将索引分为 1 个分片。index.number_of_replicas:不创建副本(0)。_source.enabled:启用源字段。接下来的部分定义了针对不同日志类型(Apache、IIS、Tomcat、nginx)的模板设置,基本上与上述设置相同,都是为不同类型的日志指定索引模板。
yamlsetup.kibana:
setup.kibana:用于配置 Kibana 的地址和设置。虽然没有具体配置,但这表示 Filebeat 将连接到 Kibana。yamloutput.elasticsearch:
hosts: ["10.180.27.36:9200"]
index: "its-log-%{[fields.log_type]}-cd-%{+YYYY.MM.dd}"
username: "xxxx"
password: "xxxxxxxx"
output.elasticsearch:配置 Filebeat 将数据发送到 Elasticsearch。hosts:指定 Elasticsearch 的主机和端口。index:定义索引名称的模式,使用 fields.log_type 来区分不同日志类型,并按日期格式化。username 和 password:用于连接 Elasticsearch 的凭据。yamlprocessors:
- add_host_metadata: ~
- add_cloud_metadata: ~
processors:用于在发送数据之前处理数据。add_host_metadata:添加主机元数据(如主机名、IP 地址等)。add_cloud_metadata:添加云元数据(如果适用)。yamlnumber_of_files: 3
close_removed: true
clean_removed: true
number_of_files 指定最大文件数,close_removed 和 clean_removed 控制日志文件的关闭和清理行为。yamllogging:
level: info
to_files: true
files:
path: C:\Program Files\Filebeat\logs
name: filebeat
keepfiles: 7
permissions: 0644
logging:配置 Filebeat 的日志设置。level:设置日志级别为 info。to_files:将日志输出到文件。files:定义日志文件的相关设置:
path:日志文件的存储路径。name:日志文件的名称。keepfiles:保留的日志文件数量。permissions:设置日志文件的权限。这个配置文件定义了 Filebeat 如何收集不同类型的日志(Apache、IIS、Tomcat、nginx),如何将这些日志发送到 Elasticsearch,并配置了相关的索引模板和日志记录设置。每个部分都可以根据实际需求进行调整,以确保 Filebeat 能够高效地收集和发送日志数据。
首先,确保您已经下载并解压了 Filebeat 的 tar.gz 包。如果还没有,可以使用以下命令下载并解压:
bash# 下载 Filebeat tar.gz 包(请根据需要替换版本号)
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.8.23-linux-x86_64.tar.gz
# 解压缩
tar xzvf filebeat-6.8.23-linux-x86_64.tar.gz
# 进入解压后的目录
cd filebeat-6.8.23-linux-x86_64
在解压后的目录中,您会找到 filebeat.yml 配置文件。您需要根据您的需求编辑此文件(filebeat.yml示例请参考上面windows版本):
bashvim filebeat.yml
在安装 6.8.23 版本时,可能会出现权限问题,请在 filebeat.yml 文件中添加以下配置:
yamlseccomp:
default_action: allow
syscalls:
- action: allow
names:
- rseq
要在后台运行 Filebeat,您可以使用 nohup 命令或将其作为系统服务运行。以下是两种方法:
nohup 命令允许您在退出终端后继续运行程序。您可以使用以下命令在后台运行 Filebeat:
bashnohup ./filebeat -e -c filebeat.yml > filebeat.log 2>&1 &
-e:表示将日志输出到标准错误。-c filebeat.yml:指定配置文件。> filebeat.log 2>&1:将输出和错误重定向到 filebeat.log 文件。&:将命令放入后台运行。您可以使用以下命令查看 Filebeat 的日志输出:
bashtail -f filebeat.log
如果您希望更好地管理 Filebeat,建议将其作为系统服务运行。您可以创建一个系统服务单元文件,使 Filebeat 在系统启动时自动运行。
创建一个服务文件:
bashsudo nano /etc/systemd/system/filebeat.service
在文件中添加以下内容:
ini[Unit]
Description=Filebeat
Documentation=https://www.elastic.co/guide/en/beats/filebeat/current/index.html
After=network.target
[Service]
User=root
Group=root
ExecStart=/path/to/filebeat/filebeat -e -c /path/to/filebeat/filebeat.yml
Restart=always
[Install]
WantedBy=multi-user.target
请确保将 /path/to/filebeat/filebeat 和 /path/to/filebeat/filebeat.yml 替换为您实际的 Filebeat 可执行文件和配置文件的路径。
重新加载 systemd,使其识别新的服务:
bashsudo systemctl daemon-reload
启动 Filebeat 服务:
bashsudo systemctl start filebeat
设置 Filebeat 服务在系统启动时自动启动:
bashsudo systemctl enable filebeat
检查 Filebeat 服务状态:
bashsudo systemctl status filebeat
以上是在使用 tar.gz 包安装 Filebeat 后,在后台运行 Filebeat 的两种方法。使用系统服务的方式更为推荐,因为它提供了更好的管理和监控功能。
systemctl 启动的 Filebeat 服务的日志可以通过 journalctl 命令来查看。以下是查看 Filebeat 日志的步骤:您可以使用以下命令查看 Filebeat 的日志:
bashsudo journalctl -u filebeat.service -f
-u filebeat.service:指定要查看的服务。-f:实时跟踪日志输出,类似于 tail -f。如果您只想查看特定时间范围内的日志,可以使用 --since 和 --until 选项。例如,要查看今天的日志,可以这样做:
bashsudo journalctl -u filebeat.service --since "today"
如果您只想查看最近的几条日志,可以使用 -n 选项,例如查看最近的 50 条日志:
bashsudo journalctl -u filebeat.service -n 50
您还可以根据关键字过滤日志。例如,如果您想查找包含 "error" 的日志条目,可以使用:
bashsudo journalctl -u filebeat.service | grep "error"
如果您想查看所有日志而不限制于 Filebeat 服务,可以直接运行:
bashsudo journalctl -f
这将显示所有服务的实时日志输出。
本文作者:Allen Tang
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!