Filebeat Nginx Module 自定义字段

Filebeat Nginx Module 自定义字段

一、修改/usr/local/nginx/conf/nginx.conf中

log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" ' '"$http_x_real_ip" "$server_addr" "$host" ' '$request_time $upstream_response_time "$upstream_addr" ' '"$time_iso8601"'; access_log /var/log/nginx/access.log access;

二、启用filebeat的nginx module

filebeat modules enable nginx

三、filebeat nginx module 增加字段

文件 /usr/share/filebeat/module/nginx/access/ingest/default.json 中
原始:

"grok": { "field": "message", "patterns":[ "\"?%{IP_LIST:nginx.access.remote_ip_list} - %{DATA:nginx.access.user_name} \\[%{HTTPDATE:nginx.access.time}\\] \"%{GREEDYDATA:nginx.access.info}\" %{NUMBER:nginx.access.response_code} %{NUMBER:nginx.access.body_sent.bytes} \"%{DATA:nginx.access.referrer}\" \"%{DATA:nginx.access.agent}\"" ], "pattern_definitions": { "IP_LIST": "%{IP}(\"?,?\\s*%{IP})*" }

修改后:

"grok": { "field": "message", "patterns":[ "\"?%{IP_LIST:nginx.access.remote_ip_list} - %{DATA:nginx.access.user_name} \\[%{HTTPDATE:nginx.access.time}\\] \"%{GREEDYDATA:nginx.access.info}\" %{NUMBER:nginx.access.response_code} %{NUMBER:nginx.access.body_sent.bytes} \"%{DATA:nginx.access.referrer}\" \"%{DATA:nginx.access.agent}\" \"%{DATA:nginx.access.xff}\" \"%{DATA:nginx.access.x_real_ip}\" \"%{DATA:nginx.access.server_addr}\" \"%{DATA:nginx.access.host}\" %{DATA:nginx.access.request_time} %{DATA:nginx.access.upstream_response_time} \"%{DATA:nginx.access.upstream_addr}\" \"%{DATA:nginx.access.time_iso8601}\"" ], "pattern_definitions": { "IP_LIST": "%{IP}(\"?,?\\s*%{IP})*" }

四、文件 /etc/filebeat/fields.yml 中

找到nginx字段配置

- name: agent type: text description: > Contains the un-parsed user agent string. Only present if the user agent Elasticsearch plugin is not available or not used.

后面加入

- name: xff type: group description: > http_x_forwarded_for. - name: x_real_ip type: group description: > http_x_real_ip. - name: server_addr type: group description: > server_addr 服务器地址. - name: host type: group description: > host http_host http主机头. - name: request_time type: group description: > request_time 请求时间. - name: upstream_response_time type: group description: > upstream_response_time 后端响应时间. - name: upstream_addr type: group description: > upstream_addr 后端地址. - name: time_iso8601 type: group description: > time_iso8601 iso8601格式时间.

五、Filebeta中 nginx 日志路径定义

在文件 /etc/filebeat/modules.d/nginx.yml 中修改日志路径

- module: nginx # Access logs access: enabled: true # Set custom paths for the log files. If left empty, # Filebeat will choose the paths depending on your OS. var.paths: ["/data/wwwlogs/*.log*"] # Error logs error: enabled: true # Set custom paths for the log files. If left empty, # Filebeat will choose the paths depending on your OS. var.paths: ["/usr/local/nginx/logs/error.log*"]

__EOF__

本文作者带着泥土
本文链接https://www.cnblogs.com/obitoma/p/14871444.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   带着泥土  阅读(768)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

了解更多