【问题标题】:How to Query Elasticsearch to get specific metricbeatdata?如何查询 Elasticsearch 以获取特定的 metricbeatdata?
【发布时间】:2017-05-26 07:01:08
【问题描述】:

在我当前的设置中,我在 192.168.1.35:9200 的网络中运行了 elasticsearch,并且 192.168.1.40 上的 metricbeat 正在从该机器 (192.168.1.40) 收集指标。我的metricbeat.yml 是这样的

metricbeat.modules:
- module: system
  metricsets:
    - core
    - cpu
    - filesystem
    - memory
    - network
    - process
  cgroups: true
  enabled: true
  period: 10
  procs: [".*"]
  cpu_ticks: false


output.elasticsearch:
  hosts: ['192.68.1.35:9200']

在我的 python 代码中,我使用 Elasticsearch API 来查询我的 Elasticsearch:

def es_match_all():
    elasticsearch = Elasticsearch(
    ['192.168.1.35'],
    port=9200,
    )
    result = elasticsearch.search(index='metricbeat-*', body={"query":{"match_all": {}}})
    print(result)

当这个方法被调用时,我得到了我想要的结果。我在下面打印了一个较短的版本:

{"hits": {"total": 2375277, "hits": [{"_id": "AVmI873zkPW_EI4mzGOc", "_source": {"beat": {"hostname": "ip-172-31-53-117", "name": "ip-172-31-53-117", "version": "5.1.1"}, "system": {"memory": {"total": 3945406464, "free": 2389319680, "swap": {"total": 0, "free": 0, "used": {"bytes": 0, "pct": 0.0}}, "used": {"bytes": 1556086784, "pct": 0.3944}, "actual": {"free": 3663335424, "used": {"bytes": 282071040, "pct": 0.0715}}}}, "type": "metricsets", "@timestamp": "2017-01-10T15:16:32.108Z", "metricset": {"module": "system", "name": "memory", "rtt": 97}}, "_score": 1.0, "_type": "metricsets", "_index": "metricbeat-2017.01.10"} ...

现在我想查询 ES 以获取特定主机名 (ip-172-31-53-117) 的一些信息:

def es_match_hostname():
    elasticsearch = Elasticsearch(
    ['192.168.1.35'],
    port=9200,
    )
    result = elasticsearch.search(
        index='metricbeat-*',
        body={"query": {"match": {'hostname': "ip-172-31-53-117"}}}
        )

但是当我调用该方法时得到以下结果:

{"hits": {"total": 0, "hits": [], "max_score": null}, "_shards": {"total": 10, "failed": 0, "successful": 10}, "took": 11, "timed_out": false}

所以我的 ES 配置和索引是正确的,但我不知道如何编写正确的查询来获取该信息。我应该在我的elasticsearch.search 通话中使用什么作为我的body

【问题讨论】:

    标签: python elasticsearch metricbeat


    【解决方案1】:

    我找到了一个解决方案,您可以使用 querystring 而不是 match。我在 Kibana 中可视化了我的 metricbeat,并注意到如果我使用 beat.namebeat.hostname,我可以在那里查询。我得到了这个代码:

    def es_match_hostname():
        elasticsearch = Elasticsearch(
        ['192.168.1.35'],
        port=9200,
        )
        match = "beat.name: ip-172-31-53-117*"
        result = elasticsearch.search(
            index='metricbeat-*',
            body={"query":{"query_string":{"query": match, "analyze_wildcard":True}}}
            )
    

    通过这样做,我只得到了那个特定节拍的结果。

    【讨论】:

      猜你喜欢
      • 2022-12-16
      • 1970-01-01
      • 1970-01-01
      • 2022-10-23
      • 1970-01-01
      • 1970-01-01
      • 2014-02-13
      • 1970-01-01
      • 2015-01-10
      相关资源
      最近更新 更多