目录html
本文中全部的版本都是基于5.2.0,由于公司es(Elasticsearch)的环境是5.2.0。linux
关于Elasticsearch的安装在以前的文章中已经写过了,这里再也不赘述。nginx
[elasticsearch及head插件安装与配置](https://www.cnblogs.com/chaos-x/p/9446250.html)shell
Kibana是一个能把你es中数据进行可视化显示的工具,包括实时统计和分析,基本上是零配置。vim
Kibana 5.2.0 linux 64-bit tar.gz浏览器
wget https://artifacts.elastic.co/downloads/kibana/kibana-5.2.0-linux-x86_64.tar.gz
ruby
tar -zxvf kibana-5.2.0-linux-x86_64.tar.gz
服务器
vim kibana-5.2.0-linux-x86_64/config/kibana.yml
app
# Kibana is served by a back end server. This setting specifies the port to use. server.port: 30000 # Specifies the address to which the Kibana server will bind. IP addresses and host names are both valid values. # The default is 'localhost', which usually means remote machines will not be able to connect. # To allow connections from remote users, set this parameter to a non-loopback address. server.host: "0.0.0.0" # The URL of the Elasticsearch instance to use for all your queries. # es的访问地址和商品号 elasticsearch.url: "http://localhost:19200"
sh kibana-5.2.0-linux-x86_64/bin/kibana
elasticsearch
在浏览器中访问Kibana的服务器加端口号就能够看到Kibana的页面了。
Logstash是日志的收集工具,能够对日志进行收集,分析,解码,过滤,输出。通常使用Filebeat收集日志到Logstash,由Logstash处理后保存到es。关于Filebeat后面再说。
https://artifacts.elastic.co/downloads/logstash/logstash-5.2.0.tar.gz
wget https://artifacts.elastic.co/downloads/logstash/logstash-5.2.0.tar.gz
tar -zxvf logstash-5.2.0.tar.gz
sh logstash-5.2.0/bin/logstash -e 'input{stdin{}}output{stdout{codec=>rubydebug}}'
启动后输入'hello word',回车。会输出以下结果
{ "message" => "Hello World", "@version" => "1", "@timestamp" => "2014-08-07T10:30:59.937Z", "host" => "raochenlindeMacBook-Air.local", }
Logstash 会给事件添加一些额外信息。最重要的就是 @timestamp,用来标记事件的发生时间。由于这个字段涉及到 Logstash 的内部流转,因此必须是一个 joda 对象,若是你尝试本身给一个字符串字段重命名为 @timestamp 的话,Logstash 会直接报错。因此,请使用 filters/date 插件 来管理这个特殊字段。
能够把把配置写到一个文件中,来启动Logstash。
test.yml
文件cd logstash-5.2.0/config/ vim test.yml
input{ stdin{} } ouput{ stdout{ codec=>rubydebug } }
启动
sh logstash-5.2.0/bin/logstash -f logstash-5.2.0/config/test.yml
能够达步骤3的效果。
nginx-log.yml
文件cd logstash-5.2.0/config/ vim nginx-log.yml
input { file { # 指定一个文件做为输入源 path => "/usr/local/nginx/logs/access.log" # 指定文件的路径 start_position => "beginning" # 指定什么时候开始收集,这时设置从文件的最开头收集 type => "nginx" # 定义日志类型,可自定义 } } filter { # 配置过滤器 grok { match => { "message" => "%{IPORHOST:http_host} %{IPORHOST:clientip} - %{USERNAME:remote_user} \[%{HTTPDATE:timestamp}\] \"(?:%{WORD:http_verb} %{NOTSPACE:http_request}(?: HTTP/%{NUMBER:http_version})?|%{DATA:raw_http_request})\" %{NUMBER:response} (?:%{NUMBER:bytes_read}|-) %{QS:referrer} %{QS:agent} %{QS:xforwardedfor} %{NUMBER:request_time:float}"} # 定义日志的输出格式 } geoip { source => "clientip" } } output { # 标准输出,是输出到终端 stdout { codec => rubydebug } # 输出到es elasticsearch { hosts => ["127.0.0.1:19200"] index => "nginx-log-%{+YYYY.MM.dd}" } }
sh logstash --path.settings /etc/logstash/ -f logstash-5.2.0/config/nginx-log.yml --config.test_and_exit
vim /usr/local/nginx/conf/nginx.conf
http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; log_format main2 '$http_host $remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$upstream_addr" $request_time'; }
#access_log logs/host.access.log main; # 增长以下内容, 日志格式化main2要在上面定义,否则这里没法引用 access_log logs/elk_access.log main2; location / { root html; index index.html index.htm; # 增长以下三行内容,分别是携带访问host,远程地址和各层代理地址 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
sh /usr/local/nginx/sbin/nginx -s reload
检查
sh logstash-5.2.0/bin/logstash -f logstash-5.2.0/config/nginx-log.yml --config.test_and_exit
启动
sh logstash-5.2.0/bin/logstash -f logstash-5.2.0/config/nginx-log.yml
当终端输出以下内容就成功了
{ "@timestamp" => 2018-12-18T08:44:56.361Z, "plugin_instance" => "vda", "read" => 467266, "plugin" => "disk", "host" => "172.24.121.18", "@version" => "1", "collectd_type" => "disk_ops", "type" => "collectd", "write" => 12204609 } { "longterm" => 0.08, "@timestamp" => 2018-12-18T08:44:46.362Z, "plugin" => "load", "shortterm" => 0.06, "host" => "172.24.121.18", "@version" => "1", "collectd_type" => "load", "type" => "collectd", "midterm" => 0.04 }
进入Kibana页面以下
在Kibana中,要分析展现数据时,要先建立Index Patterns
选择index的时候能够用通配符 ‘*’ 来把全部的nginx-log的访问日志分红一个组。
Time-field name 是要指定一个日期格式的字段,以便于后面统计使用。
而后就能够在这里选择配置好的Index patterns了。
其中x轴的时间就是建立Index Patterns的时候选择的那个日期字段。
参考:
---恢复内容结束---
本文中全部的版本都是基于5.2.0,由于公司es(Elasticsearch)的环境是5.2.0。
关于Elasticsearch的安装在以前的文章中已经写过了,这里再也不赘述。
[elasticsearch及head插件安装与配置](https://www.cnblogs.com/chaos-x/p/9446250.html)
Kibana是一个能把你es中数据进行可视化显示的工具,包括实时统计和分析,基本上是零配置。
Kibana 5.2.0 linux 64-bit tar.gz
wget https://artifacts.elastic.co/downloads/kibana/kibana-5.2.0-linux-x86_64.tar.gz
tar -zxvf kibana-5.2.0-linux-x86_64.tar.gz
vim kibana-5.2.0-linux-x86_64/config/kibana.yml
# Kibana is served by a back end server. This setting specifies the port to use. server.port: 30000 # Specifies the address to which the Kibana server will bind. IP addresses and host names are both valid values. # The default is 'localhost', which usually means remote machines will not be able to connect. # To allow connections from remote users, set this parameter to a non-loopback address. server.host: "0.0.0.0" # The URL of the Elasticsearch instance to use for all your queries. # es的访问地址和商品号 elasticsearch.url: "http://localhost:19200"
sh kibana-5.2.0-linux-x86_64/bin/kibana
在浏览器中访问Kibana的服务器加端口号就能够看到Kibana的页面了。
Logstash是日志的收集工具,能够对日志进行收集,分析,解码,过滤,输出。通常使用Filebeat收集日志到Logstash,由Logstash处理后保存到es。关于Filebeat后面再说。
https://artifacts.elastic.co/downloads/logstash/logstash-5.2.0.tar.gz
wget https://artifacts.elastic.co/downloads/logstash/logstash-5.2.0.tar.gz
tar -zxvf logstash-5.2.0.tar.gz
sh logstash-5.2.0/bin/logstash -e 'input{stdin{}}output{stdout{codec=>rubydebug}}'
启动后输入'hello word',回车。会输出以下结果
{ "message" => "Hello World", "@version" => "1", "@timestamp" => "2014-08-07T10:30:59.937Z", "host" => "raochenlindeMacBook-Air.local", }
Logstash 会给事件添加一些额外信息。最重要的就是 @timestamp,用来标记事件的发生时间。由于这个字段涉及到 Logstash 的内部流转,因此必须是一个 joda 对象,若是你尝试本身给一个字符串字段重命名为 @timestamp 的话,Logstash 会直接报错。因此,请使用 filters/date 插件 来管理这个特殊字段。
能够把把配置写到一个文件中,来启动Logstash。
test.yml
文件cd logstash-5.2.0/config/ vim test.yml
input{ stdin{} } ouput{ stdout{ codec=>rubydebug } }
启动
sh logstash-5.2.0/bin/logstash -f logstash-5.2.0/config/test.yml
能够达步骤3的效果。
nginx-log.yml
文件cd logstash-5.2.0/config/ vim nginx-log.yml
input { file { # 指定一个文件做为输入源 path => "/usr/local/nginx/logs/access.log" # 指定文件的路径 start_position => "beginning" # 指定什么时候开始收集,这时设置从文件的最开头收集 type => "nginx" # 定义日志类型,可自定义 } } filter { # 配置过滤器 grok { match => { "message" => "%{IPORHOST:http_host} %{IPORHOST:clientip} - %{USERNAME:remote_user} \[%{HTTPDATE:timestamp}\] \"(?:%{WORD:http_verb} %{NOTSPACE:http_request}(?: HTTP/%{NUMBER:http_version})?|%{DATA:raw_http_request})\" %{NUMBER:response} (?:%{NUMBER:bytes_read}|-) %{QS:referrer} %{QS:agent} %{QS:xforwardedfor} %{NUMBER:request_time:float}"} # 定义日志的输出格式 } geoip { source => "clientip" } } output { # 标准输出,是输出到终端 stdout { codec => rubydebug } # 输出到es elasticsearch { hosts => ["127.0.0.1:19200"] index => "nginx-log-%{+YYYY.MM.dd}" } }
sh logstash --path.settings /etc/logstash/ -f logstash-5.2.0/config/nginx-log.yml --config.test_and_exit
vim /usr/local/nginx/conf/nginx.conf
http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; log_format main2 '$http_host $remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$upstream_addr" $request_time'; }
#access_log logs/host.access.log main; # 增长以下内容, 日志格式化main2要在上面定义,否则这里没法引用 access_log logs/elk_access.log main2; location / { root html; index index.html index.htm; # 增长以下三行内容,分别是携带访问host,远程地址和各层代理地址 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
sh /usr/local/nginx/sbin/nginx -s reload
检查
sh logstash-5.2.0/bin/logstash -f logstash-5.2.0/config/nginx-log.yml --config.test_and_exit
启动
sh logstash-5.2.0/bin/logstash -f logstash-5.2.0/config/nginx-log.yml
当终端输出以下内容就成功了
{ "@timestamp" => 2018-12-18T08:44:56.361Z, "plugin_instance" => "vda", "read" => 467266, "plugin" => "disk", "host" => "172.24.121.18", "@version" => "1", "collectd_type" => "disk_ops", "type" => "collectd", "write" => 12204609 } { "longterm" => 0.08, "@timestamp" => 2018-12-18T08:44:46.362Z, "plugin" => "load", "shortterm" => 0.06, "host" => "172.24.121.18", "@version" => "1", "collectd_type" => "load", "type" => "collectd", "midterm" => 0.04 }
进入Kibana页面以下
在Kibana中,要分析展现数据时,要先建立Index Patterns
选择index的时候能够用通配符 ‘*’ 来把全部的nginx-log的访问日志分红一个组。
Time-field name 是要指定一个日期格式的字段,以便于后面统计使用。
而后就能够在这里选择配置好的Index patterns了。
其中x轴的时间就是建立Index Patterns的时候选择的那个日期字段。
参考: