ELK logstash 过滤插件:GeoIP

阅读 201

2022-09-12


过滤插件:GeoIP



Description

The GeoIP filter adds information about the geographical location of IP addresses, based on data from the Maxmind GeoLite2 databases.

 GeoIP过滤器根据来自Maxmind GeoLite2数据库的数据添加有关IP地址地理位置的信息。

过滤插件:GeoIP(能够将日志当中的IP解析为具体的地理位置,这个必须要有数据库去校验这个IP属于哪个国家地区的)

GeoIP插件:根据Maxmind GeoLite2数据库中的数据添加有关IP地址位置信息。使用多模式匹配,写多个正则表达式,只要满足其中一条就能匹配成功。(商业版和免费版,对地理位置不是很严谨可以使用免费版。商业更加准确)

常用字段:

• source 指定要解析的IP字段,结果保存到geoip字段

• database GeoLite2数据库文件的路径

• fields 保留解析的指定字段(解析出IP字段比较多,经纬度等,这里可以只选择自己感兴趣的)

下载地址: ​​https://www.maxmind.com/en/accounts/436070/geoip/downloads(需要登录)​​

ELK logstash 过滤插件:GeoIP_elk

 

Supported Databases

This plugin is bundled with ​​GeoLite2​​ City database out of the box. From Maxmind’s description — "GeoLite2 databases are free IP geolocation databases comparable to, but less accurate than, MaxMind’s GeoIP2 databases". Please see GeoIP Lite2 license for more details.

此插件​​开箱​​​即用,与​​GeoLite2​​ City数据库捆绑在一起。根据Maxmind的描述-“ GeoLite2数据库是免费的IP地理位置数据库,可与MaxMind的GeoIP2数据库相比,但准确性较差”。有关更多详细信息,请参阅GeoIP Lite2许可证。

​​Commercial databases​​ from Maxmind are also supported in this plugin.

此插件还支持Maxmind的​​商业数据库​​。

If you need to use databases other than the bundled GeoLite2 City, you can download them directly from Maxmind’s website and use the ​​database​​​ option to specify their location. The GeoLite2 databases can be ​​downloaded from here​​.

如果您需要使用捆绑的GeoLite2 City以外的数据库,则可以直接从Maxmind的网站下载它们,并使用该​​database​​​选项指定它们的位置。可以​​从此处下载​​GeoLite2数据库。

 

Geoip过滤器配置选项

database


  • 值类型是​​路径​​
  • 此设置没有默认值。


Logstash应该使用的Maxmind数据库文件的路径。默认数据库是GeoLite2-City。GeoLite2-City,GeoLite2-Country,GeoLite2-ASN是Maxmind支持的免费数据库。GeoIP2-City,GeoIP2-ISP,GeoIP2-Country是Maxmind支持的商业数据库。

如果未指定,则默认为Logstash随附的GeoLite2 City数据库。

source


  • 这是必需的设置。
  • 值类型为​​字符串​​
  • 此设置没有默认值。


包含要通过geoip映射的IP地址或主机名的字段。如果此字段是数组,则仅使用第一个值。


fields


  • 值类型为​​数组​​
  • 此设置没有默认值。


事件中包含的一系列geoip字段。

可能的字段取决于数据库类型。默认情况下,所有geoip字段都包含在事件中。

对于内置GeoLite2市数据库,以下是可供选择: ​​city_name​​​,​​continent_code​​​,​​country_code2​​​,​​country_code3​​​,​​country_name​​​, ​​dma_code​​​,​​ip​​​,​​latitude​​​,​​longitude​​​,​​postal_code​​​,​​region_code​​​, ​​region_name​​​和​​timezone​​。

 

使用GeoIP插件做IP解析

下载数据库解压到指定的目录下

[root@localhost ~]# tar xf GeoLite2-City_20201103.tar.gz 
[root@localhost ~]# mv GeoLite2-City_20201103 /usr/local/GeoLite2-City
[root@localhost ~]# cd /usr/local/GeoLite2-City/
[root@localhost GeoLite2-City]# ls
COPYRIGHT.txt GeoLite2-City.mmdb LICENSE.txt README.txt

  • 这个是数据库文件,会去读取该文件查询IP对应的地理位置 GeoLite2-City.mmdb
  • Source指定了对哪个字段做IP地理位置解析

配置如下: 

[root@localhost ~]# cat /usr/local/logstash/conf.d/test.conf 
input {
file {
path => "/var/log/test.log"
}
}

filter {
grok {
match => {
"message" => "%{IPV4:client_ip} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes} %{NUMBER:duration}"
}
}

geoip{
source => "client_ip"
database => "/usr/local/GeoLite2-City/GeoLite2-City.mmdb"
}
}

output {
elasticsearch {
hosts => ["192.168.179.102:9200"]
index => "test-%{+YYYY.MM.dd}"
}
}


[root@localhost conf.d]# kill -HUP 1989
[root@localhost ~]# journalctl -u logstash -f

 测试一下,注意内网IP是解析不了的,需要公网IP,下面两个公网IP和一个内网IP

[root@localhost ~]# echo "192.168.1.10 GET /login.html 12345 0.666" >> /var/log/test.log
[root@localhost ~]# echo "8.8.8.8 GET /login.html 12345 0.666" >> /var/log/test.log
[root@localhost ~]# echo "106.11.248.144 GET /login.html 12345 0.666" >> /var/log/test.log

可以看到基于IP进行了解析,这里主要是使用经纬度来进行地图定位来获取地理位置,来获取用户所在的区域

ELK logstash 过滤插件:GeoIP_elk_02

 保留解析的指定字段

geoip {
source => "client"
database => "/opt/GeoLite2-City.mmdb"
target => "geoip"
fields => ["city_name", "country_code2", "country_name","region_name"]
}

 

精彩评论(0)

0 0 举报