在我当前的设置中,我在 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 配置和索引是正确的,但我不知道如何编写正确的查询来获取该信息。我应该body
在elasticsearch.search
通话中使用什么