Elasticsearch:如何解决 Elasticsearch Geoip 处理器故障

2023年3月5日   |   by mebius

%title插图%num

什么是 geoip 处理器?

简而言之,一种将 IP 地址转换为地理位置数据的处理器。请参阅我之前的文章 “Elasticsearch:运用 geoip 处理器来丰富数据”。 举个例子,你有一个 IP 地址“8.8.8.8”,应该按如下方式解析:

PUT _ingest/pipeline/geoip
{
  "description" : "Add geoip info",
  "processors" : [
    {
      "geoip" : {
        "field" : "ip"
      }
    }
  ]
}
 
PUT my_index/_doc/my_id?pipeline=geoip
{
  "ip": "8.8.8.8"
}
 
GET my_index/_doc/my_id

上面最后一个命令返回的结果是:

{
  "_index" : "my_index",
  "_type" : "_doc",
  "_id" : "my_id",
  "_version" : 4,
  "_tgcodeseq_no" : 5,
  "_primary_term" : 9,
  "found" : true,
  "_source" : {
    "geoip" : {
      "continent_name" : "North America",
      "country_iso_code" : "US",
      "location" : {
        "lon" : -97.822,
        "lat" : 37.751
      }
    },
    "ip" : "8.8.8.8"
  }
}

当你期望用于图表绘制的经纬度对(Kibana 地图可视化)或者你只是想知道此请求的来源(例如本例中的美国)时,地理位置将很有用。

一个奇怪的例外

对于大多数情况,Elasticsearch 发行版应该有可用的支持 geolite2 数据库文件。 然而,有时你可能会发现你的发行版未能使用这些文件。 例外情况是这样的句子“_geoip_database_unavailable_GeoLite2-City.mmdb”。 显然缺少一个 geolite2 数据库文件。

要进一步证明 geolite2 文件是否可用,请运行以下命令:

POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "processors": [
      {
        "geoip": {
          "field": "location"
        }
      }
    ]
  },
  "docs": [
    {
      "_source": {
        "location": "8.8.8.8"
      }
    }
  ]
}

在我的电脑上显示如下的响应:

%title插图%num

如果你遇到 “_geoip_database_unavailable_GeoLite2-City.mmdb” 异常,恭喜……你是幸运儿。你找对地方了 [Smile]。

解决方案

运行以下测试并检查数据库文件是否存在:

# check the database file availability
GET _ingest/geoip/stats

在我的电脑上tgcode显示:

{
  "stats": {
    "successful_downloads": 0,
    "failed_downloads": 1,
    "total_download_time": 0,
    "databases_count": 0,
    "skipped_updates": 0,
    "expired_databases": 3
  },
  "nodes": {}
}

如果你看到结果显示一个空节点(nodes)……那么可能由于某些原因预期的文件不可用。

接下来运行以下命令以启用 Elasticsearch 以再次下载和管理数据库文件

PUT _cluster/settings
{
  "persistent": {
    "ingest": {
      "geoip": {
        "downloader": {
          "enabled": "true"
        }
      }
    }
  }
}

上述命令显示相应:

{
  "acknowledged": true,
  "persistent": {
    "ingest": {
      "geoip": {
        "downloader": {
          "enabled": "true"
        }
      }
    }
  },
  "transient": {}
}

等过一会儿,我执行如下的命令:

POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "processors": [
      {
        "geoip": {
          "field": "location"
        }
      }
    ]
  },
  "docs": [
    {
      "_source": {
        "location": "8.8.8.8"
      }
    }
  ]
}

上面命令显示的结果是:

{
  "docs": [
    {
      "doc": {
        "_index": "_index",
        "_id": "_id",
        "_version": "-3",
        "_source": {
          "location": "8.8.8.8",
          "geoip": {
            "continent_name": "North America",
            "region_iso_code": "US-CA",
            "city_name": "Los Angeles",
            "country_iso_code": "US",
            "country_name": "United States",
            "region_name": "California",
            "location": {
              "lon": -118.2441,
              "lat": 34.0544
            }
          }
        },
        "_ingest": {
          "timestamp": "2023-03-01T23:22:39.676904Z"
        }
      }
    }
  ]
}

如果我们再次执行如下的命令:

GET _ingest/geoip/stats

我们会发现:

{
  "stats": {
    "successful_downloads": 0,
    "failed_downloads": 1,
    "total_download_time": 0,
    "databases_count": 0,
    "skipped_updates": 0,
    "expired_databases": 0
  },
  "nodes": {
    "o75KS8A4ReKg36ILS1BK1A": {
      "databases": [
        {
          "name": "GeoLite2-ASN.mmdb"
        },
        {
          "name": "GeoLite2-Country.mmdb"
        },
        {
          "name": "GeoLite2-City.mmdb"
        }
      ],
      "files_in_temp": [
        "GeoLite2-ASN.mmdb_elastic-geoip-database-service-agreement-LICENSE.txt",
        "GeoLite2-ASN.mmdb_LICENSE.txt",
        "GeoLite2-City.mmdb_LICENSE.txt",
        "GeoLite2-Country.mmdb_elastic-geoip-database-service-agreement-LICENSE.txt",
        "GeoLite2-ASN.mmdb",
        "GeoLite2-City.mmdb_COPYRIGHT.txt",
        "GeoLite2-City.mmdb",
        "GeoLite2tgcode-City.mmdb_elastic-geoip-database-service-agreement-LICENSE.txt",
        "GeoLite2-Country.mmdb_LICENSE.txt",
        "GeoLite2-Country.mmdb",
        "GeoLite2-ASN.mmdb_COPYRIGHT.txt",
        "GeoLite2-Country.mmdb_COPYRIGHT.txt",
        "GeoLite2-City.mmdb_README.txt"
      ]
    }
  }
}

这次,我们可以看到 nodes 里的数据是有数据的。

文章来源于互联网:Elasticsearch:如何解决 Elasticsearch Geoip 处理器故障

相关推荐: Elasticsearch:Text vs. Keyword – 它们之间的差异以及它们的行为方式

很多刚开始学习 Elasticsearch 的人经常会混淆 text 和 keyword 字段数据类型。 它们之间的区别很简单,但非常关键。 在本文中,我将讨论两者之间的区别、如何使用它们、它们的行为方式以及使用哪一种。 区别 它们之间的关键区别在于,Elas…

Tags: