如何查询 Elasticsearch 以获取特定的 metricbeatdata?
How to Query Elasticsearch to get specific metricbeatdata?
在我当前的设置中,我的网络中有 elasticsearch 运行 192.168.1.35:9200 和 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
我找到了一个使用 querystring
而不是 match
的解决方案。我在 Kibana 中可视化了我的 metricbeat,并注意到如果我使用 beat.name
或 beat.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}}}
)
通过这样做,我只得到了那个特定节拍的结果。
在我当前的设置中,我的网络中有 elasticsearch 运行 192.168.1.35:9200 和 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
我找到了一个使用 querystring
而不是 match
的解决方案。我在 Kibana 中可视化了我的 metricbeat,并注意到如果我使用 beat.name
或 beat.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}}}
)
通过这样做,我只得到了那个特定节拍的结果。