Logstash利用GeoIP库显示地图以及通过useragent显示浏览器(

 http://www.nibayuan.com/articles/2016/02/23/elk-logstash-geoip-kibana-tilemap.html

                           

  我们通过Logstash收集的Nginx Access log中已经包含了客户端IP的数据(remote_addr),但是只有这个IP还不够,要在Kibana的显示请求来源的地理位置还需要借助GeoIP数据库来实现。GeoIP 是最常见的免费 IP 地址归类查询库,同时也有收费版可以采购。GeoIP 库可以根据 IP 地址提供对应的地域信息,包括国别,省市,经纬度等,对于可视化地图和区域统计非常有用。

  另外GeoIP数据文件的准确性和geoip插件的性能还是比较头疼,。

一、下载GeoIP数据库

# cd /etc/logstash/
# wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
# gzip -d GeoLiteCity.dat.gz

   Linux系统MaxMind提供了,可以自动更新数据库。CentOS可以通过epel源来安装 geoipupdate 。

  修改配置文件 /etc/GeoIP.conf 中 ProductIds GeoLite2-City ,然后直接执行 geoipupdate ,便会自动下载并校验数据库文件。默认数据库文件目录为: /usr/local/share/GeoIP ,可以通过配置项 DatabaseDirectory /etc/logstash/ 更改数据库文件目录。

二、配置logstash,在filter中加入geoip配置即可

geoip {
  source => "remote_addr"    #设置解析IP地址的字段
  target => "geoip"    #将geoip数据保存到一个字段内
  database => "/etc/logstash/GeoLiteCity.dat"    #IP地址数据库
}

 

  得到的结果如下:

"geoip" => {
                      "ip" => "112.90.16.4",
           "country_code2" => "CN",
           "country_code3" => "CHN",
            "country_name" => "China",
          "continent_code" => "AS",
             "region_name" => "30",
               "city_name" => "Guangzhou",
                "latitude" => 23.11670000000001,
               "longitude" => 113.25,
                "timezone" => "Asia/Chongqing",
        "real_region_name" => "Guangdong",
                "location" => [
            [0] 113.25,
            [1] 23.11670000000001
        ]
    }

 

  GeoIP 库数据较多,如果你不需要这么多内容,可以通过 fields 选项指定自己所需要的。下例为全部可选内容:

geoip {
  fields => ["city_name", "continent_code", "country_code2", "country_code3", "country_name", "dma_code", "ip", "latitude", "longitude", "postal_code", "region_name", "timezone"]
}

 

  需要注意的是:geoip.location 是 logstash 通过 latitude 和 longitude 额外生成的数据。所以,如果你是想要经纬度又不想重复数据的话,需要在geoip中配置: remove_field => ["[geoip][latitude]", "[geoip][longitude]"] 。

posted on 2017-03-14 13:57  szllq2000  阅读(255)  评论(0编辑  收藏  举报