ELK stack 是实时日志处理领域开源的一套解决方案,最近这两年越来越火,大有一统江湖之势。ELK 是一套解决方案而不是一款软件,三个字母分别是三个软件产品的缩写。 E 代表 Elasticsearch , 负责日志的存储和检索; L 代表 Logstash , 负责日志的收集,过滤和格式化; K 代表 Kibana ,负责日志的展示统计和数据可视化 先来看下 elk 主要的应用场景:
使用 logstash 能够解析不同服务器上的不同类型的日志,将这些信息格式化然后通过接口传递给 elasticsearch 集群,elasticsearch 集群负责存储这些日志信息,并为这些日志信息建立索引提供查询接口给 kibana。我们通过 kibana 页面就可以很方便的实时查询线上日志信息,还可以做各种统计,生成实时报表。
- brew cash install java
- https://www.elastic.co/downloads/elasticsearch
- https://www.elastic.co/downloads/logstash
- https://www.elastic.co/downloads/kibana
- http://supervisord.org/installing.html
本章节将演示如何用 ELK 来对 Nginx 访问日志进行解析,存储以及可视化。
默认的 nginx 日志输出格式为
- log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- '$status $body_bytes_sent "$http_referer" '
- '"$http_user_agent" "$http_x_forwarded_for"';
- //一条具体的日志信息
- 127.0.0.1 - - [21/May/2017:17:10:03 +0800] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) ApplewebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36" "-"
nginx_access_logstash.conf 文件的内容如下
- input {
- file {
- path => ["/usr/local/logs/nginx/*.log"]
- type => "system"
- start_position => "beginning"
- }
- }
- filter {
- grok {
- match => {
- "message" => '%{IPORHOST:remote_ip} - %{DATA:user_name} \[%{HTTPDATE:time}\] "%{WORD:request_action} %{DATA:request} HTTP/%{NUMBER:http_version}" %{NUMBER:response} %{NUMBER:bytes} "%{DATA:referrer}" "%{DATA:agent}"'
- }
- }
- date {
- match => [ "time", "dd/MMM/YYYY:HH:mm:ss Z" ]
- locale => en
- }
- geoip {
- source => "remote_ip"
- target => "geoip"
- }
- useragent {
- source => "agent"
- target => "user_agent"
- }
- }
- output {
- elasticsearch {
- hosts => ["127.0.0.1:9200"]
- index => "logstash-nginx-access-log"
- document_type => "logs"
- flush_size => 20000
- idle_flush_time => 10
- sniffing => true
- template_overwrite => true
- }
- }
在浏览器中运行
- http://127.0.0.1:9200/logstash-nginx-access-log/logs/_search?q=*
- http://localhost:5601/
- logstash-nginx-access-log
Elastic Stack 入门教程 ELKstack 中文指南
来源: http://www.tuicool.com/articles/JrEjMjv