Kibana的图形化——Tile Map

简介

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

配置

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

1.安装GeoIP数据库

cd /usr/local/logstash/etc
curl -O "http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz"
gunzip GeoLiteCity.dat.gz

2.配置logstash使用GeoIP

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

vim /usr/local/logstash/etc/logstash.conf
input {
        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}"
                }
        }

}

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"}}}}}}

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

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

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

vim /usr/local/logstash/etc/logstash.conf
input {
        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}"
                }
        }

}

将索引改为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格式的差。

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值