如何获取集群中正在使用的碎片数量-Python



我正在创建一个lambda函数,它将包含一个python脚本。lambda函数的目的是在集群中的碎片使用率超过90%时发出警报,即使用4500/5000个碎片。我使用的是python的ElasticSearch客户端,所以我想知道是否有任何方法可以让你计算开放碎片的容量。提前感谢

您可以使用这个Elasticsearch库并运行以下内容:

es = Elasticsearch()
stats = es.cluster.stats()
shards = stats['indices']['shards']['total']

它使用这种统计方法来获取集群的统计数据,从那里你可以获得关于所有索引中碎片数量的所有信息。

如果你想计算碎片的最大阈值,并根据使用率的百分比进行通知,请找到你的最大碎片数/节点,从stats中获得节点数,计算最大阈值,然后检查你是否需要用类似notify = shards/max > 0.9的东西进行通知。

编辑:

下面是一个更完整的代码示例:

threshold = 0.9
cluster_stats = es.cluster.stats()
cluster_settings = es.cluster.get_settings()
total_shards = cluster_stats['indices']['shards']['total']
data_nodes = cluster_stats['nodes']['count']['data']
max_shards_node = int(cluster_settings['persistent']['cluster']['max_shards_per_node'])
# Calculate if the current amount of shards is approaching the threshold
notify = total_shards/(data_nodes * max_shards_node) > threshold
if notify:
# you choose how to handle notification

最新更新