ELK学习实验014:Nginx日志JSON格式收集

1 Kibana的显示配置

https://demo.elastic.co/app/kibana#/dashboard/welcome_dashboard

环境先处理干净

安装nginx和httpd-tools

2 使用压测工具产生日志

[[email protected] ~]# ab -n 100 -c 100 http://192.168.132.134/

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.132.134 (be patient).....done

Server Software:        nginx/1.16.1
Server Hostname:        192.168.132.134
Server Port:            80

Document Path:          /
Document Length:        612 bytes

Concurrency Level:      100
Time taken for tests:   0.011 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      84500 bytes
HTML transferred:       61200 bytes
Requests per second:    9071.12 [#/sec] (mean)
Time per request:       11.024 [ms] (mean)
Time per request:       0.110 [ms] (mean, across all concurrent requests)
Transfer rate:          7485.44 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    3   0.5      3       3
Processing:     2    3   1.1      2       5
Waiting:        0    3   1.2      2       5
Total:          4    6   0.7      5       7
WARNING: The median and mean for the total time are not within a normal deviation
        These results are probably not that reliable.

Percentage of the requests served within a certain time (ms)
  50%      5
  66%      6
  75%      6
  80%      7
  90%      7
  95%      7
  98%      7
  99%      7
 100%      7 (longest request)

[[email protected] ~]# tail -f /usr/local/nginx/logs/access.log

192.168.132.181 - - [18/Jan/2020:21:47:23 -0500] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3"
192.168.132.181 - - [18/Jan/2020:21:47:23 -0500] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3"
192.168.132.181 - - [18/Jan/2020:21:47:23 -0500] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3"

3 安装filebeat

[[email protected] src]# wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.5.1-x86_64.rpm

[[email protected] src]# rpm -ivh filebeat-7.5.1-x86_64.rpm

[[email protected] src]# rpm -qc filebeat

[[email protected] src]# cd /etc/filebeat/

[[email protected] filebeat]# cp filebeat.yml /opt/

[[email protected] filebeat]# grep -Ev "^$|[#;]" filebeat.yml

filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /usr/local/nginx/logs/access.log
filebeat.config.modules:
  path: ${path.config}/modules.d/*.yml
  reload.enabled: false
setup.template.settings:
  index.number_of_shards: 1
setup.kibana:
output.elasticsearch:
  hosts: ["192.168.132.131:9200","192.168.132.132:9200","192.168.132.133:9200"]
processors:
  - add_host_metadata: ~
  - add_cloud_metadata: ~
  - add_docker_metadata: ~
  - add_kubernetes_metadata: ~

[[email protected] filebeat]# systemctl restart filebeat

使用kibana查看

访问:http://192.168.132.131:5601/app/kibana

对于一些基本的操作,再前面的学习中已经演示,参看前面的学习实验

4 配置nginx的日志改为json格式

log_format access_json ‘{"@timestamp":"$time_iso8601",‘
                           ‘"host":"$server_addr",‘
                           ‘"clientip":"$remote_addr",‘
                           ‘"size":$body_bytes_sent,‘
                           ‘"responsetime":$request_time,‘
                           ‘"upstreamtime":"$upstream_response_time",‘
                           ‘"upstreamhost":"$upstream_addr",‘
                           ‘"http_host":"$host",‘
                           ‘"url":"$uri",‘
                           ‘"domain":"$host",‘
                           ‘"xff":"$http_x_forwarded_for",‘
                           ‘"referer":"$http_referer",‘
                           ‘"status":"$status"}‘;

添加日志格式

访问后

{"@timestamp":"2020-01-18T23:33:57-05:00","host":"192.168.132.134","clientip":"192.168.132.1","size":0,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"192.168.132.134","url":"/index.html","domain":"192.168.132.134","xff":"-","referer":"-","status":"304"}

安装jq工具

[[email protected] filebeat]# yum -y install jq

[[email protected] ~]# tail -f /usr/local/nginx/logs/access.log |jq .

但是读取的依然是整个的message日志

5 filebeat解析成json格式

[[email protected] ~]# grep -Ev "^$|[#;]" /etc/filebeat/filebeat.yml

filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /usr/local/nginx/logs/access.log
  json.key_under_root: true
  json.overwrite_keys: true
filebeat.config.modules:
  path: ${path.config}/modules.d/*.yml
  reload.enabled: false
setup.template.settings:
  index.number_of_shards: 1
setup.kibana:
output.elasticsearch:
  hosts: ["192.168.132.131:9200","192.168.132.132:9200","192.168.132.133:9200"]
processors:
  - add_host_metadata: ~
  - add_cloud_metadata: ~
  - add_docker_metadata: ~
  - add_kubernetes_metadata: ~

[[email protected] ~]# systemctl restart filebeat

得到格式的日志

ES原始数据

{
"_index": "filebeat-7.5.1",
"_type": "_doc",
"_id": "oq03vG8BcnOPLK2r_2wa",
"_version": 1,
"_score": 1,
"_source": {
"@timestamp": "2020-01-19T05:13:02.912Z",
"json": {
"upstreamtime": "-",
"host": "192.168.132.134",
"@timestamp": "2020-01-19T00:13:01-05:00",
"Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36",
"clientip": "192.168.132.1",
"domain": "192.168.132.134",
"http_host": "192.168.132.134",
"status": "304",
"referer": "-",
"xff": "-",
"responsetime": 0,
"url": "/index.html",
"size": 0,
"upstreamhost": "-"
},
"input": {
"type": "log"
},
"ecs": {
"version": "1.1.0"
},
"host": {
"name": "node4",
"hostname": "node4",
"architecture": "x86_64",
"os": {
"platform": "centos",
"version": "7 (Core)",
"family": "redhat",
"name": "CentOS Linux",
"kernel": "3.10.0-957.27.2.el7.x86_64",
"codename": "Core"
},
"id": "a833bbe7e3634c75ab1b427c4049d056",
"containerized": false
},
"agent": {
"ephemeral_id": "bab448ec-726f-48bf-aa62-0b8fe39c2684",
"hostname": "node4",
"id": "3d856c84-4859-4b20-a25f-4b6c1a8a8108",
"version": "7.5.1",
"type": "filebeat"
},
"log": {
"file": {
"path": "/usr/local/nginx/logs/access.log"
},
"offset": 15601
}
}
}

Kibana显示

使用json格式收集nginx的日志介绍到这里

原文地址:https://www.cnblogs.com/zyxnhr/p/12210938.html

时间: 2024-10-30 16:36:41

ELK学习实验014:Nginx日志JSON格式收集的相关文章

ELK系列一:ELK安装配置及nginx日志分析

本文分三个部分介绍了elk.elk安装配置及基于filebeat分析nginx日志的配置. 第一部分:elk介绍 一.什么是elk ELK 其实并不是一款软件,而是一整套解决方案,是三个软件产品的首字母缩写,Elasticsearch,Logstash 和 Kibana.这三款软件都是开源软件,通常是配合使用. 二.Elasticsearch 1.Elasticsearch介绍 Elasticsearch 是一个实时的分布式搜索和分析引擎,它可以用于全文搜索,结构化搜索以及分析.它是一个建立在全

ELK学习实验016:filebeat收集tomcat日志

filebeat收集tomcat日志 1 安装tomcat [[email protected] ~]# yum -y install tomcat tomcat-webapps tomcat-admin-webapps tomcat-docs-webapp tomcat-javadoc [[email protected] ~]# systemctl start tomcat [[email protected] ~]# systemctl status tomcat [[email prot

ELK学习实验018:filebeat收集docker日志

Filebeat收集Docker日志 1 安装docker [[email protected] ~]# yum install -y yum-utils device-mapper-persistent-data lvm2 [[email protected] ~]# yum update [[email protected] ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/d

ELK安装配置及nginx日志分析

一.ELK简介1.组成ELK是Elasticsearch.Logstash.Kibana三个开源软件的组合.在实时数据检索和分析场合,三者通常是配合使用,而且又都先后归于 Elastic.co 公司名下,故有此简称.Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等.Logstash是一个完全开源的工具,它可以对你的日志进行收集.分析,并将其存储供以后使用.kibana 是一个开源和

nginx 日志log_format格式

官方文档: http://nginx.org/en/docs/http/ngx_http_log_module.html The ngx_http_log_module module writes request logs in the specified format. Requests are logged in the context of a location where processing ends. It may be different from the original loc

ELK整合Filebeat监控nginx日志

ELK 日志分析 1. 为什么用到 ELK 一般我们需要进行日志分析场景:直接在日志文件中 grep. awk 就可以获得自己想要的信息.但在规模较大的场景中,此方法效率低下,面临问题包括日志量太大如何归档.文本搜索太慢怎么办.如何多维度查询.需要集中化的日志管理,所有服务器上的日志收集汇总.常见解决思路是建立集中式日志收集系统,将所有节点上的日志统一收集,管理,访问. 一般大型系统是一个分布式部署的架构,不同的服务模块部署在不同的服务器上,问题出现时,大部分情况需要根据问题暴露的关键信息,定位

android学习二十三(解析json格式数据)

前面我们已经掌握了xml格式数据的解析方式,那么接下来我们学习下如何解析json格式的数据.比起xml,json的主要优势在于它体积更小,在网络上传输的时候可以更省流量.但缺点在于,它的语义性差,看起来不如xml直观. 开始之前,先在自己的电脑apache-tomcat-6.0.39\webapps\ROOT路径目录下新建一个get_data.json的文件,然后编辑这个文件,并加入如下json格式的内容: [{"id":"5","version"

Logstash动态模板映射收集Nginx的Json格式的日志

Logstash传输给ES的数据会自动映射为5索引,5备份,字段都为text的的索引.这样基本上无法进行数据分析.所以必须将Logstash的数据按照既定的格式存储在ES中,这时候就要使用到ES模板技术了.在ES中可以定义自定义模板和动态模板,之后es会自动将相关索引映射为模板规定的格式 编译动态映射模板文件bigdata.template: 在Json日志文件中的KEY的位置不固定.或字段数不明确时使用动态映射模板 { "template": "bigdata-templa

ELK学习实验019:ELK使用redis缓存

1 安装一个redis服务 [[email protected] ~]# yum -y install redis 直接启动 [[email protected] ~]# systemctl restart redis [[email protected] ~]# systemctl status redis [[email protected] ~]# redis-cli  -h 127.0.0.1 2 配置filebeat,把数据传给redis [[email protected] ~]#