Kibana的图形化——Tile Map

来源:互联网 发布:火影忍者手游pk网络卡 编辑:程序博客网 时间:2024/06/10 00:46

参考:https://www.digitalocean.com/community/tutorials/how-to-map-user-location-with-geoip-and-elk-elasticsearch-logstash-and-kibana


简介

  当我们查看访问网站的流量的来源时,往往通过awk+sed或其他工具分析日志文件,有没有一种方式可以实时查看并且在地图上直观的表现出来?当然,我们的Kibana就可以做到,下面我们来看看如何配置吧。

配置

本文是在http://http://blog.csdn.net/yanggd1987/article/details/50460246博文基础上进行配置,所有的配置文件都可在博文中找到。

1.安装GeoIP数据库

cd /usr/local/logstash/etccurl -O "http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz"gunzip GeoLiteCity.dat.gz
  • 1
  • 2
  • 3

2.配置logstash使用GeoIP

只需要在原来的logstash.conf中添加filter即可

vim /usr/local/logstash/etc/logstash.confinput {        file {                path => "/data/nginx/logs/access_java.log"                type => "nginx-access"                start_position => "beginning"                sincedb_path => "/usr/local/logstash/sincedb"                codec => "json"        }}filter {        if [type] == "nginx-access" {                geoip {                        source => "clientip"                        target => "geoip"                        database => "/usr/local/logstash/etc/GeoLiteCity.dat"                        add_field => [ "[geoip][coordinates]", "%{[geoip][longitude]}" ]                        add_field => [ "[geoip][coordinates]", "%{[geoip][latitude]}"  ]                }                mutate {                        convert => [ "[geoip][coordinates]", "float"]                }        }}output {        if [type] == "nginx-access" {                elasticsearch {                        hosts => ["10.10.20.16:9200"]                        manage_template => true                        index => "nginx-access-%{+YYYY-MM}"                }        }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

3.重启logstash即可。

Kibana出图及trouble shooting

访问10.10.10.16:5601,登录kibana

1.“Settings”刷新“Index Patterns” 

刷新后,我们就可以看到geoip的相关字段了。

2.“Visualize”新建Tile map 

3.通过聚合器来创建map 

如图:报错“No Compatible Fields: The “[nginx-access-]YYYY-MM” index pattern does not contain any of the following field types: geo_point”

原因:索引格式为[nginx-access-]YYYY-MM的日志文件由logstash输出到Elasticsearch;在 elasticsearch 中,所有的数据都有一个类型,什么样的类型,就可以在其上做一些对应类型的特殊操作。geo信息中的location字段是经纬度,我们需要使用经纬度来定位地理位置;在 elasticsearch 中,对于经纬度来说,要想使用 elasticsearch 提供的地理位置查询相关的功能,就需要构造一个结构,并且将其类型属性设置为geo_point,此错误明显是由于我们的geo的location字段类型不是geo_point。

我们可以通过以下方式验证一下:

curl -XGET http://127.0.0.1:9200/nginx-access-2016-01/_mapping/{"nginx-access-2016-01":{"mappings":{"nginx-access":{"properties":{"@timestamp":{"type":"date","format":"dateOptionalTime"},"@version":{"type":"string"},"cache_status":{"type":"string"},"clientip":{"type":"string"},"geoip":{"properties":{"area_code":{"type":"long"},"city_name":{"type":"string"},"continent_code":{"type":"string"},"coordinates":{"type":"double"},"country_code2":{"type":"string"},"country_code3":{"type":"string"},"country_name":{"type":"string"},"dma_code":{"type":"long"},"ip":{"type":"string"},"latitude":{"type":"double"},"location":{"type":"double"},"longitude":{"type":"double"},"postal_code":{"type":"string"},"real_region_name":{"type":"string"},"region_name":{"type":"string"},"timezone":{"type":"string"}}},"host":{"type":"string"},"http_host":{"type":"string"},"message":{"type":"string"},"method":{"type":"string"},"path":{"type":"string"},"protocol":{"type":"string"},"referer":{"type":"string"},"status":{"type":"string"},"tags":{"type":"string"},"type":{"type":"string"},"url":{"type":"string"},"useragent":{"type":"string"}}}}}}
  • 1
  • 2

其中”location”:{“type”:”double”},字段类型是double,而不是geo_point,因此会报图中的错误。

**解决方法:**Elasticsearch 支持给索引预定义设置和 mapping(前提是你用的 elasticsearch 版本支持这个 API,不过估计应该都支持)。其实ES中已经有一个默认预定义的模板,我们只要使用预定的模板即可,我们在ES中看下模板。

那为什么还会报错呢?因为默认预定义的模板必须只有匹配 logstash-* 的索引才会应用这个模板,由于我们在logstash中使用的是[nginx-access-]YYYY-MM索引方式,因此不会匹配到默认模板,我们只需要改一下索引方式即可,如下:

vim /usr/local/logstash/etc/logstash.confinput {        file {                path => "/data/nginx/logs/access_java.log"                type => "nginx-access"                start_position => "beginning"                sincedb_path => "/usr/local/logstash/sincedb"                codec => "json"        }}filter {        if [type] == "nginx-access" {                geoip {                        source => "clientip"                        target => "geoip"                        database => "/usr/local/logstash/etc/GeoLiteCity.dat"                        add_field => [ "[geoip][coordinates]", "%{[geoip][longitude]}" ]                        add_field => [ "[geoip][coordinates]", "%{[geoip][latitude]}"  ]                }                mutate {                        convert => [ "[geoip][coordinates]", "float"]                }        }}output {        if [type] == "nginx-access" {                elasticsearch {                        hosts => ["10.10.20.16:9200"]                        manage_template => true                        index => "logstash-nginx-access-%{+YYYY-MM}"                }        }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

将索引改为index => “logstash-nginx-access-%{+YYYY-MM}”即可,重启logstash,登录Kibana刷新后即可。

4.生成图片并保存

ok,每个城市的访问量就在map上显示了。

5.“Dashboard”载入Visual保存的map即可进行图形化展示了。

总结

  由于我们的nginx日志格式使用的是json,因此在logstash中的filter中配置比较简单;若nginx日志不是使用的是json,在logstash中的filter中就需要配合grok进行过滤,相对麻烦一些,性能也比json格式的差。

0 0
原创粉丝点击