filebeat记录nginx每个请求所消耗的时间并在kibana内显示

  • filebeat 版本 : 7.4.2
  • elasticsearch 版本: 7.4.2
  • Kibana 7.4.2
  • nginx 日志新增request_timeupstream_response_time两个字段

1. nginx配置

http {
	## main 为log 日志格式的标志名
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" $request_time $upstream_response_time';
    access_log  logs/access.log  main;
}

2. 自定义grok正则

  • 这里假设你打开了filebeats的 nginx module的话,就可以直接配置$(filebeats的路径)/module/nginx/access/default.json这个文件。
  • 如果你是在filetbeat.yml文件里面配置的input的话,则可以使用kibana自带的dev工具http://xxxx/app/kibana#/dev_tools/console?_g=()将pipeline的配置信息PUT到elastcisearch,如下图所示,配置信息可以使用上面所说的default.json里面的内容,记得input要指定pipeline该属性
    在这里插入图片描述
  • grok的正则配置可以参考:grok自带的的pattern(这里用logstash在github上的源文件)
  • 下面除了正则的processor以外,也用了convert的processor,主要目的是为了将request_time和response_time的数据类型转化为float,由于nginx的日志输出这两个值的时候,如果没有request_time的话,将会直接打印-这个字符,如果在grok里面使用NUMBER匹配的话会匹配失败,所以这里使用NOTSPACE匹配,并且在convert进行数据类型转化。
{
    "processors": [
        {
            "grok": {
                "field": "message",
                "patterns": [
                    "\"?(?:%{IP_LIST:nginx.access.remote_ip_list}|%{DATA:source.address}) - %{DATA:user.name} \\[%{HTTPDATE:nginx.access.time}\\] \"%{DATA:nginx.access.info}\" %{NUMBER:http.response.status_code:long} %{NUMBER:http.response.body.bytes:long} \"%{DATA:http.request.referrer}\" \"%{DATA:user_agent.original}\" %{NOTSPACE:http.request_time} %{NOTSPACE:http.upstream.response_time}"
                ],
                "pattern_definitions": {
                    "IP_LIST": "%{IP}(\"?,?\\s*%{IP})*"
                },
                "ignore_missing": true
            }
        },
        {
            "convert": {
                "field": "http.request_time",
				"type": "float",
				"ignore_missing": true,
				"on_failure": [
					{
						"set": {
							"field": "http.request_time",
							"value": -1.0
						}
					}
				]
            }
        },
        {
            "convert": {
                "field": "http.upstream.response_time",
				"type": "float",
				"ignore_missing": true,
				"on_failure": [
					{
						"set": {
							"field": "http.upstream.response_time",
							"value": -1.0
						}
					}
				]
            }
        }   
}   

4. 删除elasticsearch的pipeline缓存

这里可以使用kibana自带的dev工具http://xxxx/app/kibana#/dev_tools/console?_g=()

DELETE _ingest/pipeline/xxxxxxx

在这里插入图片描述

5.重启filebeats

如果在filebeats的主配置文件中有配置重新加载配置的话,就无须关掉filebeats进程,等待重新加载配置即可。

  • filebeats.yml自动加载配置文件变化的配置(注意这里自动加载仅仅是modules.d/文件夹里面的配置文件)
filebeat.config.modules:
  # Glob pattern for configuration loading
  path: ${path.config}/modules.d/*.yml

  # Set to true to enable config reloading
  reload.enabled: true

  # Period on which files under path should be checked for changes
  #reload.period: 10s
  • 为了实时看出配置信息的变化,在启动filebeats的时候可以指定参数输出debug信息到控制台filebeats.exe -e -d "publish"

可能遇到的问题

  1. nginx的日志改变之后,kibana界面显示日志格式不匹配。
    进行第4步的删除pipeline,或者使用PUT修改pipeline内容,如果是使用nginx module的话,pipeline的名字应该是filebeat-{filebeat.version}-nginx-access-default
  2. 日志解析正常,pipeline内容已经改变(可以在dev工具里面使用GET _ingest/pipeline/查看)
    进入kibana的managment-index pattern(http://xxxx/kibana#/management/kibana/index_patterns?_g=()),选择对应的index,刷新。
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值