如何查询Elasticsearch获取特定的metricbeatdata?

2024-10-02 04:34:26 发布

您现在位置:Python中文网/ 问答频道 /正文

在我当前的设置中,我在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:

^{pr2}$

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

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


Tags: ipfreebyteselasticsearchsystemhostnameusedtotal
1条回答
网友
1楼 · 发布于 2024-10-02 04:34:26

我找到了一个解决方案,您可以使用querystring而不是{}。我在Kibana可视化了我的metricbate,注意到如果我使用beat.name或{},我可以在那里查询。我得到了这个密码:

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

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

相关问题 更多 >

    热门问题